trapezius60 commited on
Commit
40f5fb8
Β·
verified Β·
1 Parent(s): 381d52d

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(...)

Files changed (1) hide show
  1. app.py +4 -21
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
- # πŸ” Pretrained image classifier for screening non-mushroom
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.0 | Updated: August 2025")
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