kieranberton commited on
Commit
d563f38
·
1 Parent(s): 7c3de8f

Added predictor script for inference

Browse files
Files changed (2) hide show
  1. app.py +3 -2
  2. predictor.py +97 -0
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import base64
 
3
 
4
  def add_bg_from_local(image_file):
5
  with open(image_file, "rb") as image_file:
@@ -25,8 +26,8 @@ def header_white_bg(text, fontsize = 40, bold = True):
25
  )
26
 
27
  def diagnose_health(file):
28
- return "Healthy" # Placeholder result for demonstration purposes, waiting to integrate model
29
-
30
 
31
  def app():
32
  add_bg_from_local('assets/background.png')
 
1
  import streamlit as st
2
  import base64
3
+ import predictor
4
 
5
  def add_bg_from_local(image_file):
6
  with open(image_file, "rb") as image_file:
 
26
  )
27
 
28
  def diagnose_health(file):
29
+ prediction = predictor(file)
30
+ return prediction
31
 
32
  def app():
33
  add_bg_from_local('assets/background.png')
predictor.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def predictor(image_file):
2
+ import torch
3
+ import numpy as np
4
+ import cv2
5
+ import matplotlib.pyplot as plt
6
+ import torchvision.models as models
7
+ import os
8
+ from PIL import Image
9
+ from IPython.display import display
10
+ from torchvision import datasets, transforms
11
+ from torch.utils.data import DataLoader, Subset
12
+
13
+ #load model with params
14
+ model = models.efficientnet_b0(weights=None)
15
+ model.load_state_dict(torch.load('best_model.pth', map_location=torch.device('cpu')))
16
+ device = torch.device('cpu')
17
+
18
+ classes = [
19
+ "Apple___Apple_scab",
20
+ "Apple___Black_rot",
21
+ "Apple___Cedar_apple_rust",
22
+ "Apple___healthy",
23
+ "Blueberry___healthy",
24
+ "Cherry_(including_sour)___Powdery_mildew",
25
+ "Cherry_(including_sour)___healthy",
26
+ "Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot",
27
+ "Corn_(maize)___Common_rust_",
28
+ "Corn_(maize)___Northern_Leaf_Blight",
29
+ "Corn_(maize)___healthy",
30
+ "Grape___Black_rot",
31
+ "Grape___Esca_(Black_Measles)",
32
+ "Grape___Leaf_blight_(Isariopsis_Leaf_Spot)",
33
+ "Grape___healthy",
34
+ "Orange___Haunglongbing_(Citrus_greening)",
35
+ "Peach___Bacterial_spot",
36
+ "Peach___healthy",
37
+ "Pepper,_bell___Bacterial_spot",
38
+ "Pepper,_bell___healthy",
39
+ "Potato___Early_blight",
40
+ "Potato___Late_blight",
41
+ "Potato___healthy",
42
+ "Raspberry___healthy",
43
+ "Soybean___healthy",
44
+ "Squash___Powdery_mildew",
45
+ "Strawberry___Leaf_scorch",
46
+ "Strawberry___healthy",
47
+ "Tomato___Bacterial_spot",
48
+ "Tomato___Early_blight",
49
+ "Tomato___Late_blight",
50
+ "Tomato___Leaf_Mold",
51
+ "Tomato___Septoria_leaf_spot",
52
+ "Tomato___Spider_mites Two-spotted_spider_mite",
53
+ "Tomato___Target_Spot",
54
+ "Tomato___Tomato_Yellow_Leaf_Curl_Virus",
55
+ "Tomato___Tomato_mosaic_virus",
56
+ "Tomato___healthy"
57
+ ]
58
+
59
+ def pred_image(image_path, model):
60
+ topk = 3
61
+
62
+ image = Image.open(image_path).convert('RGB')
63
+ transform = transforms.Compose([
64
+ transforms.Resize(256),
65
+ transforms.CenterCrop(224),
66
+ transforms.ToTensor(),
67
+ transforms.Normalize(mean=[0.485, 0.456, 0.406],
68
+ std=[0.229, 0.224, 0.225])])
69
+ img_normalized = transform(image).unsqueeze(0)
70
+
71
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
72
+ img_normalized = img_normalized.to(device)
73
+
74
+ with torch.no_grad():
75
+ model.eval()
76
+ output = model(img_normalized)
77
+ probs, indices = torch.topk(torch.softmax(output, dim=1), topk)
78
+ # index = output.data.cpu().numpy().argmax()
79
+ tmp_lst = []
80
+ # print(indices)
81
+ # print(probs)
82
+ for j in range(topk):
83
+ tmp_dct = {}
84
+ label_indx = indices[0][j]
85
+ # print(label_indx)
86
+ class_name = class_names[label_indx]
87
+ tmp_dct["predicted"] = class_name
88
+ tmp_dct["probability"] = probs[0][j]
89
+ tmp_lst.append(tmp_dct)
90
+
91
+ # print(f"Prediction {j+1}: label index: {indices[i][j]}, probability: {probs[i][j]:.4f}")
92
+
93
+ # class_name = class_names[index]
94
+ return tmp_lst
95
+
96
+ predicted_label = pred_image(image_file,model)
97
+ return predicted_label