annadurai003 commited on
Commit
4dbe437
·
1 Parent(s): b375423

FoodVision Mini Initial commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ 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
+ 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
37
+ .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:295250b9a37aff1ebe63f1e1b177a242468e2687614ad20c49c065af0d1bb5fb
3
+ size 31314554
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ### 1. Import and class names setup ###
3
+ import gradio as gr
4
+ import os
5
+ import torch
6
+
7
+ from model import create_effnetb2_model
8
+ from timeit import default_timer as timer
9
+ from typing import Tuple, Dict
10
+
11
+ # Setup class names
12
+ class_names = ['pizza', 'steak', 'sushi']
13
+
14
+
15
+ ### 2. Model and Transforms perparation ###
16
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
17
+ num_classes=3)
18
+
19
+ # Load save weights
20
+ effnetb2.load_state_dict(
21
+ torch.load(
22
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
23
+ map_location=torch.device("cpu")
24
+ )
25
+ )
26
+
27
+ ### 3. Predict fucntin ###
28
+
29
+ def predict(img) -> Tuple[Dict, float]:
30
+ # Start a timer
31
+ start_time = timer()
32
+ # Transform the input image for use with EffNetB2
33
+ transform_img = effnetb2_transformer(img).unsqueeze(0)
34
+
35
+ # Put model into eval mode, main prediction
36
+ effnetb2.eval()
37
+ with torch.inference_mode():
38
+ pred=effnetb2(transform_img)
39
+ pred_prob = torch.softmax(pred,dim=1)
40
+
41
+
42
+
43
+
44
+
45
+ end_time = timer()
46
+ # Create a prediction label and prediction probability dictionary
47
+ pred_labels_and_probs = {class_names[i]:float(pred_prob[0][i]) for i in range(len(class_names))}
48
+
49
+ # Calculate pred time
50
+ time = round(end_time-start_time,4)
51
+
52
+ # Return pred dict and pred time
53
+ return pred_labels_and_probs,time
54
+
55
+
56
+ ### 4. Gradio app ###
57
+
58
+ # Create title , description and article
59
+ title = "FoodVision Mini 🍕🥩🍣"
60
+ description = " An [EfficinetNetB2](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html) feature extractor computer vision model to classify images as pizza, steak, sushi"
61
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)"
62
+
63
+ # Create example list
64
+ example_list = [["example/"+example] for example in os.listdir("examples")]
65
+
66
+
67
+ # Create the Graio demo
68
+ demo = gr.Interface(fn=predict, # maps inputs to ouputs
69
+ inputs=gr.Image(type="pil"),
70
+ outputs=[gr.Label(num_top_classes=3,label='Predictions'),
71
+ gr.Number(label="Predicition time (s)")],
72
+ examples=example_list,
73
+ title=title,
74
+ description=description,
75
+ article=article
76
+ )
77
+
78
+ # Launch the demo!
79
+ demo.launch(debug=False,
80
+ share=True)
examples/2378406.jpg ADDED
examples/3553838.jpg ADDED
examples/930553.jpg ADDED
model.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import torch
4
+ import torchvision
5
+ from torch import nn
6
+
7
+ def create_effnetb2_model(num_classes:int=3,
8
+ seed:int=42):
9
+
10
+
11
+
12
+ # 1. Setup pretrained EffNetB2 weights
13
+ effnetb2_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
14
+
15
+ # 2. Get EffNetB2 transforms
16
+ effnetb2_transforms = effnetb2_weights.transforms()
17
+
18
+ # 3. Setup pretrained model instance
19
+ effnetb2 = torchvision.models.efficientnet_b2(weights=effnetb2_weights)
20
+
21
+ # Set seeds
22
+ torch.manual_seed(seed=seed)
23
+
24
+ # 4. Freeze the base layer in the model (this will stop all layers form training)
25
+ for params in effnetb2.parameters():
26
+ params.requires_grad = False
27
+
28
+ # 5. Chage the output layer (or header layer) classifier
29
+ effnetb2.classifier = nn.Sequential(
30
+ nn.Dropout(p=0.3, inplace=True),
31
+ nn.Linear(in_features=1408,
32
+ out_features=num_classes,
33
+ bias=True)
34
+ )
35
+ return effnetb2, effnetb2_transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.5.0
2
+ torchvision==0.20.0
3
+ gradio==5.6.0