raulminan commited on
Commit
14787c6
1 Parent(s): 37210bc

first commit

Browse files
.gitattributes CHANGED
@@ -29,3 +29,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
29
  *.zip filter=lfs diff=lfs merge=lfs -text
30
  *.zst filter=lfs diff=lfs merge=lfs -text
31
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
29
  *.zip filter=lfs diff=lfs merge=lfs -text
30
  *.zst filter=lfs diff=lfs merge=lfs -text
31
  *tfevents* filter=lfs diff=lfs merge=lfs -text
32
+ 09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth filter=lfs diff=lfs merge=lfs -text
09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0905213cdb284cea170521d1e7c5f65a37e85b08866740a0b8c4db0138b8ab8b
3
+ size 31856609
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!usr/bin/env python
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
+ from PIL import Image
10
+
11
+ with open("class_names.txt", "r") as f:
12
+ class_names = [food_name.strip() for food_name in f.readlines()]
13
+
14
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
15
+ num_classes=len(class_names)
16
+ )
17
+
18
+ effnetb2.load_state_dict(
19
+ torch.load(
20
+ f="09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth",
21
+ map_location=torch.device("cpu")
22
+ )
23
+ )
24
+
25
+ def predict(img: Image) -> Tuple[Dict, float]:
26
+ """Transforms and performs a prediction on an image and returns the
27
+ prediction and the time taken
28
+
29
+ Parameters
30
+ ----------
31
+ img : Image
32
+ an Image
33
+
34
+ Returns
35
+ -------
36
+ Tuple[Dict, float]
37
+ Tuple[prediction probabilities, time taken]
38
+ """
39
+
40
+ start = timer()
41
+
42
+ # trasnform and add batch dimension
43
+ img = effnetb2_transforms(img).unsqueeze(0)
44
+
45
+ # put model on eval mode and turn on inference
46
+ effnetb2.eval()
47
+ with torch.inference_mode():
48
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
49
+
50
+ # create a prediction label: prediction prob dict
51
+ pred_labels_and_probs = {
52
+ class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
53
+ }
54
+
55
+ # calculate pred time
56
+ pred_time = round(timer() - start, 5)
57
+
58
+ return pred_labels_and_probs, pred_time
59
+
60
+ ### Gradio App
61
+
62
+ title = "FoodVision Big"
63
+ description = "An EfficientNetB2 frature extractor CV model to classify images of food"
64
+ article = "TODO"
65
+
66
+ # Create examples list from "examples/" directory
67
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
68
+
69
+ def main():
70
+ # create Gradio interface
71
+ demo = gr.Interface(
72
+ fn=predict,
73
+ inputs=gr.Image(type="pil"),
74
+ outputs=[
75
+ gr.Label(num_top_classes=5, label="Predictions"),
76
+ gr.Number(label="Prediction time (s)")
77
+ ],
78
+ examples=example_list,
79
+ title=title,
80
+ description=description,
81
+ article=article
82
+ )
83
+ demo.launch()
84
+
85
+ if __name__ == "__main__":
86
+ main()
class_names.txt ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ apple_pie
2
+ baby_back_ribs
3
+ baklava
4
+ beef_carpaccio
5
+ beef_tartare
6
+ beet_salad
7
+ beignets
8
+ bibimbap
9
+ bread_pudding
10
+ breakfast_burrito
11
+ bruschetta
12
+ caesar_salad
13
+ cannoli
14
+ caprese_salad
15
+ carrot_cake
16
+ ceviche
17
+ cheese_plate
18
+ cheesecake
19
+ chicken_curry
20
+ chicken_quesadilla
21
+ chicken_wings
22
+ chocolate_cake
23
+ chocolate_mousse
24
+ churros
25
+ clam_chowder
26
+ club_sandwich
27
+ crab_cakes
28
+ creme_brulee
29
+ croque_madame
30
+ cup_cakes
31
+ deviled_eggs
32
+ donuts
33
+ dumplings
34
+ edamame
35
+ eggs_benedict
36
+ escargots
37
+ falafel
38
+ filet_mignon
39
+ fish_and_chips
40
+ foie_gras
41
+ french_fries
42
+ french_onion_soup
43
+ french_toast
44
+ fried_calamari
45
+ fried_rice
46
+ frozen_yogurt
47
+ garlic_bread
48
+ gnocchi
49
+ greek_salad
50
+ grilled_cheese_sandwich
51
+ grilled_salmon
52
+ guacamole
53
+ gyoza
54
+ hamburger
55
+ hot_and_sour_soup
56
+ hot_dog
57
+ huevos_rancheros
58
+ hummus
59
+ ice_cream
60
+ lasagna
61
+ lobster_bisque
62
+ lobster_roll_sandwich
63
+ macaroni_and_cheese
64
+ macarons
65
+ miso_soup
66
+ mussels
67
+ nachos
68
+ omelette
69
+ onion_rings
70
+ oysters
71
+ pad_thai
72
+ paella
73
+ pancakes
74
+ panna_cotta
75
+ peking_duck
76
+ pho
77
+ pizza
78
+ pork_chop
79
+ poutine
80
+ prime_rib
81
+ pulled_pork_sandwich
82
+ ramen
83
+ ravioli
84
+ red_velvet_cake
85
+ risotto
86
+ samosa
87
+ sashimi
88
+ scallops
89
+ seaweed_salad
90
+ shrimp_and_grits
91
+ spaghetti_bolognese
92
+ spaghetti_carbonara
93
+ spring_rolls
94
+ steak
95
+ strawberry_shortcake
96
+ sushi
97
+ tacos
98
+ takoyaki
99
+ tiramisu
100
+ tuna_tartare
101
+ waffles
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+ from typing import Tuple
6
+
7
+ def create_effnetb2_model(
8
+ num_classes: int = 3,
9
+ seed: int = 42) -> Tuple[torch.nn.Module, torchvision.transforms.Compose]:
10
+ """Creates an EfficientNetB2 feature extractor model and transforms
11
+
12
+ Parameters
13
+ ----------
14
+ num_classes : int, optional
15
+ Number of classes in the classifier head, by default 3
16
+ seed : int, optional
17
+ random seed value, by default 42
18
+
19
+ Returns
20
+ -------
21
+ Tuple[torch.nn.Module, torchvision.transforms.Compose]
22
+ Tuple[EffnetB2 feature extractor model, EffNetb2 image transforms]
23
+ """
24
+ # Create EffNetB2 pretrained weights, transforms and model
25
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
26
+ transforms = weights.transforms()
27
+ model = torchvision.models.efficientnet_b2(weights=weights)
28
+
29
+ # freeze parameters
30
+ for param in model.parameters():
31
+ param.requires_grad = False
32
+
33
+ # change classifier head
34
+ torch.manual_seed(seed)
35
+ model.classifier = nn.Sequential(
36
+ nn.Dropout(p=0.3, inplace=True),
37
+ nn.Linear(in_features=1408, out_features=num_classes)
38
+ )
39
+
40
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ torchmetrics
4
+ requests
5
+ tqdm