AbdoTaha15 commited on
Commit
29704aa
·
1 Parent(s): f1608a3

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
+ 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
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:063541830c9667af5968c73d01e895e3a07de182df71a3dfe926cc1f8a638a3b
3
+ size 31313869
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import torch
4
+ from timeit import default_timer as timer
5
+ from typing import Tuple, Dict
6
+
7
+ from model import create_effnetb2_model
8
+
9
+ class_names = ["pizza", "steak", "sushi"]
10
+
11
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
12
+
13
+ effnetb2_weigths = effnetb2.load_state_dict(
14
+ torch.load(
15
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
16
+ map_location=torch.device("cpu"),
17
+ )
18
+ )
19
+
20
+
21
+ def predict(img) -> Tuple[Dict, float]:
22
+ start = timer()
23
+
24
+ img = effnetb2_transforms(img).unsqueeze(0)
25
+
26
+ effnetb2.eval()
27
+ with torch.inference_mode():
28
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
29
+
30
+ pred_labels_and_probs = {
31
+ class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
32
+ }
33
+
34
+ end = timer()
35
+ pred_time = round(end - start, 4)
36
+
37
+ return pred_labels_and_probs, pred_time
38
+
39
+
40
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
41
+
42
+
43
+ app = gr.Interface(
44
+ fn=predict,
45
+ inputs=gr.Image(type="pil"),
46
+ outputs=[
47
+ gr.Label(num_top_classes=3, label="Predictions"),
48
+ gr.Number(label="Prediction time (s)"),
49
+ ],
50
+ examples=example_list,
51
+ title="FoodVision Mini",
52
+ description="An EFficientNetB2 feature extractor for pizza, sushi. and steak",
53
+ article="Created at [09_model_deployment]",
54
+ )
55
+
56
+ app.launch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torch import nn
4
+
5
+
6
+ def create_effnetb2_model(num_classes: int = 3, seed: int = 42):
7
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
8
+ transforms = weights.transforms()
9
+ model = torchvision.models.efficientnet_b2(weights=weights)
10
+
11
+ for param in model.parameters():
12
+ param.requires_grad = False
13
+
14
+ torch.manual_seed(seed)
15
+ model.classifier = nn.Sequential(
16
+ nn.Dropout(p=0.3, inplace=True),
17
+ nn.Linear(in_features=1408, out_features=num_classes),
18
+ )
19
+
20
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio