Elbhnasy commited on
Commit
64bfa89
·
1 Parent(s): e53d6fa

commit app files

Browse files
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ pretrained_effnetb1_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnetb1_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Setup class names
11
+ class_names = ["pizza", "steak", "sushi"]
12
+
13
+ ### 2. Model and transforms preparation ###
14
+
15
+ # Create EffNetB2 model
16
+ effnetb1, effnetb1_transforms = create_effnetb1_model(num_classes=len(class_names) )
17
+
18
+ # Load saved weights
19
+ effnetb1.load_state_dict(torch.load(f="pretrained_effnetb1_feature_extractor_pizza_steak_sushi_20_percent.pth",
20
+ map_location=torch.device("cpu"),))
21
+ ### 3. Predict function ###
22
+ # Create predict function
23
+ def predict(img) -> Tuple[Dict, float]:
24
+ """
25
+ Transforms and performs a prediction on img.
26
+ :param img: target image .
27
+ :return: prediction and time taken.
28
+ """
29
+ # Start the timer
30
+ start_time = timer()
31
+
32
+ # Transform the target image and add a batch dimension
33
+ img = effnetb1_transforms(img).unsqueeze(0)
34
+
35
+ # Put model into evaluation mode and turn on inference mode
36
+ effnetb1.eval()
37
+ with torch.inference_mode():
38
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
39
+ pred_probs = torch.softmax(effnetb1(img), dim=1)
40
+
41
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
42
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
43
+
44
+ # Calculate the prediction time
45
+ pred_time = round(timer() - start_time, 5)
46
+
47
+ # Return the prediction dictionary and prediction time
48
+ return pred_labels_and_probs, pred_time
49
+
50
+ ### 4. Gradio app ###
51
+
52
+ # Create title, description and article strings
53
+ title = "FoodVision Mini 🍕🥩🍣"
54
+ description = "An EfficientNetB1 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
55
+ article = "I will add it soon wait.."
56
+
57
+ # Create examples list from "examples/" directory
58
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
59
+
60
+ # Create the Gradio demo
61
+ demo = gr.Interface(fn=predict, # mapping function from input to output
62
+ inputs=gr.Image(type="pil"), # what are the inputs?
63
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
64
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
65
+ # Create examples list from "examples/" directory
66
+ examples=example_list,
67
+ title=title,
68
+ description=description,
69
+ article=article)
70
+
71
+ # Launch the demo!
72
+ demo.launch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetb1_model(num_classes:int=3,seed:int=42):
8
+ """
9
+ Creates an EFFicientNetB1 feature extractor model and transforms.
10
+ :param num_classes: number of classes in classifier head.
11
+ Defaults to 3.
12
+ :param seed: random seed value.
13
+ Defaults to 42.
14
+ :return: feature extractor model.
15
+ transforms (torchvision.transforms): EffNetB1 image transforms.
16
+ """
17
+ # 1. Setup pretrained EffNetB1 weights
18
+ weigts = torchvision.models.EfficientNet_B1_Weights.DEFAULT
19
+ # 2. Get EffNetB2 transforms
20
+ transforms= weigts.transforms()
21
+
22
+ # 3. Setup pretrained model
23
+ model=torchvision.models.efficientnet_b1(weights= "DEFAULT")
24
+
25
+ # 4. Freeze the base layers in the model (this will freeze all layers to begin with)
26
+ for param in model.parameters():
27
+ param.requires_grad=False
28
+
29
+ # 5. Change classifier head with random seed for reproducibility
30
+ torch.manual_seed(seed)
31
+ model.classifier=nn.Sequential(nn.Dropout(p=0.2,inplace=True),
32
+ nn.Linear(in_features=1280,out_features=num_classes))
33
+ return model,transforms
pretrained_effnetb1_feature_extractor_pizza_steak_sushi_20_percent.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b2b5243d08f41bba16d28533e61036a578e494d4bf31b5f2705c78121cc7b297
3
+ size 26536531
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.13.1
2
+ torchvision==0.14.1
3
+ gradio==3.16.2