Apoorv Masta commited on
Commit
9829771
β€’
1 Parent(s): 92f2e64

initial commit

Browse files
.gitattributes CHANGED
@@ -31,3 +31,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
31
  *.zip filter=lfs diff=lfs merge=lfs -text
32
  *.zst filter=lfs diff=lfs merge=lfs -text
33
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
31
  *.zip filter=lfs diff=lfs merge=lfs -text
32
  *.zst filter=lfs diff=lfs merge=lfs -text
33
  *tfevents* filter=lfs diff=lfs merge=lfs -text
34
+ 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
35
+ .pth filter=lfs diff=lfs merge=lfs -text
09_pretrained_effnetb2_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:08d5dbabfa16593cce91a20c189cbd7730aea2cf9f75cc74a47396a89e31d921
3
+ size 31265929
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnetb2_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 adn transforms preparation ###
14
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
15
+ num_classes = 3
16
+ )
17
+
18
+ # Load save weights
19
+ effnetb2.load_state_dict(
20
+ torch.load(
21
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
22
+ map_location = torch.device("cpu") # load the model to the CPU
23
+ )
24
+ )
25
+
26
+ ### 3. Prediction function ###
27
+ def predict(img) -> Tuple[Dict, float]:
28
+ #Start a timer
29
+ start_time = timer()
30
+
31
+ # Transform the input image for use with EffNetB2
32
+ transformed_img = effnetb2_transforms(img).unsqueeze(0) #unsqueeze = add batch dimension on 0th index
33
+
34
+ #Put model into eval mode, make prediciton
35
+ effnetb2.eval()
36
+ with torch.inference_mode():
37
+ # Pass the transformed image through the model and turn the prdiciton logits into probability
38
+ # pred_logit = effnetb2(transformed_img)
39
+ pred_probs = torch.softmax(effnetb2(transformed_img), dim = 1)
40
+ # pred_label = torch.argmax(pred_probs, dim = 1)
41
+ # class_name = class_names[pred_label]
42
+
43
+ # Create a prediction label and prediction probability dictionary
44
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
45
+
46
+ # cAlculate pred time
47
+ end_time = timer()
48
+ pred_time = round(end_time - start_time, 4)
49
+
50
+ # Return pred dict and pred time
51
+ return pred_labels_and_probs, pred_time
52
+
53
+
54
+ ### 4. Gradio App ###
55
+
56
+
57
+ # Create title, description and article
58
+ title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
59
+ description = "An [EfficientNetB2 feature extractor] (https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2) computer vision model to classify images as pizza, steak or sushi."
60
+ article = "Created at PyTorch Model Deployment"
61
+
62
+ # Create example list
63
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
64
+
65
+ # Create the Gradio Demo
66
+ demo = gr.Interface(fn = predict, #maps inputs to outputs
67
+ inputs = gr.Image(type = "pil"),
68
+ outputs = [gr.Label(num_top_classes = 3, label = "predictions"),
69
+ gr.Number(label="Prediciton time (s)")],
70
+ examples = example_list,
71
+ title = title,
72
+ description = description,
73
+ article = article
74
+ )
75
+
76
+ #Launch the demo:
77
+ demo.launch(debug = False, #print errors locally ?
78
+ share = True) # generate a publically shareable URL
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+
5
+ from torch import nn
6
+
7
+ def create_effnetb2_model(num_classes: int = 3, #default output classes = 3 (pizza, steak, sushi)
8
+ seed: int = 42
9
+ ):
10
+ # 1, 2, 3 Create EffNetB2 pretrained weights, transforms and model
11
+
12
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
13
+ transforms = weights.transforms()
14
+ model = torchvision.models.efficientnet_b2(weights = 'DEFAULT')
15
+
16
+ #4. Freeze all layers in the base model
17
+ for param in model.parameters():
18
+ param.requires_grad = False
19
+
20
+ #5. Change the classifier head with random seed for reproducibility
21
+ torch.manual_seed(seed)
22
+ model.classifier = nn.Sequential(
23
+ nn.Dropout(p = 0.3, inplace = True),
24
+ nn.Linear(in_features = 1408, out_features = num_classes)
25
+ )
26
+
27
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch == 1.12.0
2
+ torchvision == 0.13.0
3
+ gradio == 3.6