Zoyah24 commited on
Commit
b03013f
·
1 Parent(s): 428de7e

first commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ effieciet_b0_model_pretrained.pth filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnetB0_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ class_names = ['Apple(1-5)',
11
+ 'Apple(11-15)',
12
+ 'Apple(16-20)',
13
+ 'Apple(6-10)',
14
+ 'AppleExpired',
15
+ 'Banana(1-2)',
16
+ 'Banana(3-4)',
17
+ 'Banana(5-7)',
18
+ 'Banana(8-10)',
19
+ 'BananaExpired']
20
+
21
+ effnetb0, effnetb0_transforms = create_effnetB0_model(
22
+ num_classes=len(class_names)
23
+ )
24
+
25
+ # Load saved weights
26
+ effnetb0.load_state_dict(
27
+ torch.load(
28
+ f="effieciet_b0_model_pretrained.pth",
29
+ map_location=torch.device("cpu"), # load to CPU
30
+ )
31
+ )
32
+
33
+ ### 3. Predict function ###
34
+
35
+ # Create predict function
36
+ def predict(img) -> Tuple[Dict, float]:
37
+ """Transforms and performs a prediction on img and returns prediction and time taken.
38
+ """
39
+ # Start the timer
40
+ start_time = timer()
41
+
42
+ # Transform the target image and add a batch dimension
43
+ img = effnetb0_transforms(img).unsqueeze(0)
44
+
45
+ # Put model into evaluation mode and turn on inference mode
46
+ effnetb0.eval()
47
+ with torch.inference_mode():
48
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
49
+ pred_probs = torch.softmax(effnetb0(img), dim=1)
50
+
51
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
52
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
53
+
54
+ # Calculate the prediction time
55
+ pred_time = round(timer() - start_time, 5)
56
+
57
+ # Return the prediction dictionary and prediction time
58
+ return pred_labels_and_probs, pred_time
59
+
60
+ ### 4. Gradio app ###
61
+
62
+ # Create title, description and article strings
63
+ title = "Fruit Vision "
64
+
65
+ # Create examples list from "examples/" directory
66
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
67
+
68
+ # Create the Gradio demo
69
+ demo = gr.Interface(fn=predict, # mapping function from input to output
70
+ inputs=gr.Image(type="pil"), # what are the inputs?
71
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
72
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
73
+ # Create examples list from "examples/" directory
74
+ examples=example_list,
75
+ title=title
76
+ )
77
+
78
+ # Launch the demo!
79
+ demo.launch(share=True)
effieciet_b0_model_pretrained.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:92945580ee040163d1412408b0828fdf95a05c3d968f0c592f111bcd325ead3c
3
+ size 16382634
examples/banana-isolated-fruit-white-background-49540709.jpg ADDED
examples/beautiful-red-apple_146671-8483.jpg ADDED
examples/red apple with some wrinkles_86.jpg ADDED
model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetB0_model(num_classes:int=3,
8
+ seed:int=42):
9
+ """Creates an EfficientNetB2 feature extractor model and transforms.
10
+
11
+ Args:
12
+ num_classes (int, optional): number of classes in the classifier head.
13
+ Defaults to 3.
14
+ seed (int, optional): random seed value. Defaults to 42.
15
+
16
+ Returns:
17
+ model (torch.nn.Module): EffNetB0 feature extractor model.
18
+ transforms (torchvision.transforms): EffNetB0 image transforms.
19
+ """
20
+ # Create EffNetB0 pretrained weights, transforms and model
21
+ weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT
22
+ transforms = weights.transforms()
23
+ model = torchvision.models.efficientnet_b0(weights=weights)
24
+
25
+ # Freeze all layers in base model
26
+ for param in model.parameters():
27
+ param.requires_grad = False
28
+
29
+ # Change classifier head with random seed for reproducibility
30
+ torch.manual_seed(seed)
31
+ model.classifier = nn.Sequential(
32
+ nn.Dropout(p=0.3, inplace=True),
33
+ nn.Linear(in_features=1280, out_features=num_classes),
34
+ )
35
+
36
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.2.1
2
+ torchvision==0.17.1
3
+ gradio==4.25.0