AllanK24 commited on
Commit
5cf6a0b
Β·
verified Β·
1 Parent(s): 10884ad

FoodVision Big all files added

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
+ examples/04-pizza-dad.jpg 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:bdbba73fd4e8ba6431756c1f4cab806bb10e3f3ff9f0165c0b952f69ca510ff2
3
+ size 31857210
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ### 1. Imports and class names setup ###
3
+ import gradio as gr
4
+ import torch
5
+ import os
6
+
7
+ from model import create_effnetb2_model
8
+ from timeit import default_timer as timer
9
+
10
+ # Setup class names
11
+ with open('/content/demos/foodvision_big/class_names.txt', 'r') as f:
12
+ class_names = [food_name.strip() for food_name in f.readlines()]
13
+
14
+ ### 2. Model and transforms preparation ###
15
+ # Create model and transforms
16
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
17
+ num_classes=len(class_names),
18
+ )
19
+
20
+ # Load save weights
21
+ effnetb2.load_state_dict(torch.load('demos/foodvision_big/09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth', map_location=torch.device("cpu"))) # load the model to the CPU
22
+
23
+ ### 3. Predict function ###
24
+ def predict(img) -> tuple[dict, float]:
25
+ # Start a timer
26
+ start_time = timer()
27
+
28
+ # Transform the input image for use with EffNetB2
29
+ img = effnetb2_transforms(img).unsqueeze(dim=0) # unsqueeze = add batch dimension on 0th index
30
+
31
+ # Put model into eval mode, make prediction
32
+ effnetb2.eval()
33
+ with torch.inference_mode():
34
+ # Pass transformed image through the model and turn the prediction logits into probabilities
35
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
36
+
37
+ # Create a prediction label and prediction probability dictionary
38
+ pred_labels_and_probs = {
39
+ class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
40
+ }
41
+
42
+ # Calculate pred time
43
+ end_time = timer()
44
+ pred_time = round(end_time - start_time, 4)
45
+
46
+ # Return pred dict and pred time
47
+ return pred_labels_and_probs, pred_time
48
+
49
+ ### 4. Gradio app ###
50
+
51
+ # Create title, description and article
52
+ title = "FoodVision BIG πŸ”πŸ‘πŸ’ͺ"
53
+ descripton = "An EfficientNetB2 Feature Extractor computer vision model to classify 101 classes of food from the Food101 dataset."
54
+ article = "Created at 09. PyTorch Model Deployment."
55
+
56
+ # Create example list
57
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
58
+
59
+ # Create the Gradio demo
60
+ demo = gr.Interface(fn=predict, # maps inputs to outputs
61
+ inputs=gr.Image(type="pil"),
62
+ outputs=[gr.Label(num_top_classes=5, label="Predictions"),
63
+ gr.Number(label="Prediction time (s)")],
64
+ examples=example_list,
65
+ title=title,
66
+ description=descripton,
67
+ article=article)
68
+
69
+ # Launch Demo!
70
+ demo.launch()
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/04-pizza-dad.jpg ADDED

Git LFS Details

  • SHA256: 0f00389758009e8430ca17c9a21ebb4564c6945e0c91c58cf058e6a93d267dc8
  • Pointer size: 132 Bytes
  • Size of remote file: 2.87 MB
model.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+ def create_effnetb2_model(num_classes:int=3, # default output classes = 3 (pizza, steak, sushi)
8
+ seed:int=42):
9
+ # 1, 2, 3 Create EffNetB2 pretrained weights, transforms and model
10
+ weights = torchvision.models.efficientnet.EfficientNet_B2_Weights.DEFAULT
11
+ transforms = weights.transforms()
12
+ model = torchvision.models.efficientnet.efficientnet_b2(weights=weights).to(device)
13
+
14
+ # 4. Freeze all layers in the base model
15
+ for param in model.parameters():
16
+ param.requires_grad = False
17
+
18
+ # 5. Change classifier head with random seed for reproducibility
19
+ torch.manual_seed(seed)
20
+ model.classifier = nn.Sequential(
21
+ nn.Dropout(p=0.3, inplace=True),
22
+ nn.Linear(in_features=1408, out_features=num_classes, bias=True)
23
+ ).to(device)
24
+
25
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+
2
+ torch
3
+ torchvision
4
+ gradio