Spaces:
Build error
Build error
Update app.py
Browse files# β
Training Script Summary:
# - Use torchvision.datasets.ImageFolder with folders "Edible/" and "Poisonous/"
# - Apply data augmentation (flip, jitter, rotate, etc.)
# - Split into train/val and load with DataLoader
# - Use pretrained ResNet50 with replaced final FC layer
# - Train using CrossEntropyLoss and Adam optimizer
# - Save model using torch.save(...)
app.py
CHANGED
@@ -5,8 +5,6 @@ import torchvision.transforms as transforms
|
|
5 |
from torchvision import models
|
6 |
import gradio as gr
|
7 |
from rembg import remove # Background removal
|
8 |
-
from transformers import pipeline # For non-mushroom detection
|
9 |
-
from torchvision.models import ResNet50_Weights
|
10 |
|
11 |
# π§ Set device
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
@@ -14,8 +12,8 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
14 |
# π¦ Load your fine-tuned model
|
15 |
model = models.resnet50(weights=None) # No pretrained weights
|
16 |
model.fc = torch.nn.Sequential(
|
17 |
-
torch.nn.Dropout(0.5),
|
18 |
-
torch.nn.Linear(model.fc.in_features, 2)
|
19 |
)
|
20 |
model.load_state_dict(torch.load("resnet_mushroom_classifier.pth", map_location=device))
|
21 |
model = model.to(device)
|
@@ -36,26 +34,11 @@ transform = transforms.Compose([
|
|
36 |
[0.229, 0.224, 0.225])
|
37 |
])
|
38 |
|
39 |
-
#
|
40 |
-
label_detector = pipeline("image-classification", model="microsoft/resnet-50")
|
41 |
-
mushroom_keywords = ["mushroom", "agaric", "amanita", "fungus", "earthstar", "toadstool"]
|
42 |
-
|
43 |
-
def is_mushroom_image(image):
|
44 |
-
try:
|
45 |
-
result = label_detector(image)
|
46 |
-
top_label = result[0]["label"].lower()
|
47 |
-
return any(keyword in top_label for keyword in mushroom_keywords)
|
48 |
-
except:
|
49 |
-
return False
|
50 |
-
|
51 |
-
# π§ Classification function with validation
|
52 |
CONFIDENCE_THRESHOLD = 85.0 # Minimum confidence considered safe enough to show suggestion
|
53 |
|
54 |
def classify_mushroom(image: Image.Image):
|
55 |
try:
|
56 |
-
if not is_mushroom_image(image):
|
57 |
-
return "Not a mushroom", "ΰΉΰΈ‘ΰΉΰΉΰΈΰΉΰΉΰΈ«ΰΉΰΈ", "0.00%", "Please upload a mushroom photo."
|
58 |
-
|
59 |
image = image.convert("RGBA")
|
60 |
image_no_bg = remove(image).convert("RGB")
|
61 |
tensor = transform(image_no_bg).unsqueeze(0).to(device)
|
@@ -108,7 +91,7 @@ if __name__ == "__main__":
|
|
108 |
gr.Markdown("---")
|
109 |
gr.Markdown(manual_link) # π Display clickable user manual
|
110 |
gr.Markdown(feedback_link) # π Display clickable user manual
|
111 |
-
gr.Markdown("App version: 1.1.
|
112 |
|
113 |
demo.launch()
|
114 |
|
|
|
5 |
from torchvision import models
|
6 |
import gradio as gr
|
7 |
from rembg import remove # Background removal
|
|
|
|
|
8 |
|
9 |
# π§ Set device
|
10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
12 |
# π¦ Load your fine-tuned model
|
13 |
model = models.resnet50(weights=None) # No pretrained weights
|
14 |
model.fc = torch.nn.Sequential(
|
15 |
+
torch.nn.Dropout(0.5), # π§ Dropout to reduce overfitting
|
16 |
+
torch.nn.Linear(model.fc.in_features, 2) # 2 classes: Edible, Poisonous
|
17 |
)
|
18 |
model.load_state_dict(torch.load("resnet_mushroom_classifier.pth", map_location=device))
|
19 |
model = model.to(device)
|
|
|
34 |
[0.229, 0.224, 0.225])
|
35 |
])
|
36 |
|
37 |
+
# π§ Classification function with confidence filtering
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
CONFIDENCE_THRESHOLD = 85.0 # Minimum confidence considered safe enough to show suggestion
|
39 |
|
40 |
def classify_mushroom(image: Image.Image):
|
41 |
try:
|
|
|
|
|
|
|
42 |
image = image.convert("RGBA")
|
43 |
image_no_bg = remove(image).convert("RGB")
|
44 |
tensor = transform(image_no_bg).unsqueeze(0).to(device)
|
|
|
91 |
gr.Markdown("---")
|
92 |
gr.Markdown(manual_link) # π Display clickable user manual
|
93 |
gr.Markdown(feedback_link) # π Display clickable user manual
|
94 |
+
gr.Markdown("App version: 1.1.1 | Updated: August 2025")
|
95 |
|
96 |
demo.launch()
|
97 |
|