LuisDarioHinojosa commited on
Commit
69beb24
·
1 Parent(s): ff19993

first commit

Browse files
.gitattributes CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
32
  *.zip filter=lfs diff=lfs merge=lfs -text
33
  *.zst filter=lfs diff=lfs merge=lfs -text
34
  *tfevents* filter=lfs diff=lfs merge=lfs -text
35
+ effnetb2_food101_dict.pth filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnet_b2_instance
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Setup class names
11
+ with open("class_names.txt", "r") as f:
12
+ class_names = [food_name.strip() for food_name in f.readlines()]
13
+
14
+ # Create Food101 compatible EffNetB2 instance
15
+ effnet_transforms,effnetb2_food_101 = create_effnet_b2_instance(num_classes = len(class_names))
16
+
17
+ # Load the saved model's state_dict()
18
+ effnetb2_food_101.load_state_dict(torch.load("effnetb2_food101_dict.pth",map_location = torch.device("cpu")))
19
+
20
+
21
+ def predict(img, model = effnetb2_food_101, transforms = effnet_transforms) -> Tuple[Dict,float]:
22
+ # start a timer
23
+ start_timer = timer()
24
+ # transform the image to be used by the model
25
+ prepreocpressed_image = transforms(img).unsqueeze(0)
26
+ # turn off regularization and parameters
27
+ model.eval()
28
+ with torch.inference_mode():
29
+ prediction = model(prepreocpressed_image)
30
+ probabilities = torch.softmax(prediction,dim = 1)
31
+ prob_dict = {class_names[i]: float(probabilities[0][i]) for i in range(len(class_names))}
32
+ # calculate the time
33
+ end_timer = timer()
34
+ total_time = end_timer - start_timer
35
+ return prob_dict,total_time
36
+
37
+
38
+ # create the gradio app
39
+ title = "FoodVision Big Classifier"
40
+ description = "An EfficientNetB2 feature extractor trained on the Food101 Dataset to classify across 101 possible classes of food."
41
+ article = "Model created using pytorch"
42
+
43
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
44
+
45
+ # Create Gradio interface
46
+ demo = gr.Interface(
47
+ fn=predict,
48
+ inputs=gr.Image(type="pil"),
49
+ outputs=[
50
+ gr.Label(num_top_classes=5, label="Predictions"),
51
+ gr.Number(label="Prediction time (s)"),
52
+ ],
53
+ examples=example_list,
54
+ title=title,
55
+ description=description,
56
+ article=article,
57
+ )
58
+
59
+ # Launch the app!
60
+ 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
effnetb2_food101_dict.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cb4f75ce27688eb0ffb20ce848895ecb9794a2d2b776bf3453b8ba1e949a5e14
3
+ size 31833069
examples/clam_chowder.jpg ADDED
examples/foie_gras.jpg ADDED
examples/pizza.jpg ADDED
examples/pork_chop.jpg ADDED
examples/prime_rib.jpg ADDED
model.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+ def create_effnet_b2_instance(num_classes = 3):
7
+ # fetch the model's pretrained weights
8
+ effnetb2_pretrained_weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
9
+ # fetch the preprocessing transforms
10
+ effnetb2_transforms = effnetb2_pretrained_weights.transforms()
11
+ # get the model and load the pretrained weighits
12
+ effnetb2 = torchvision.models.efficientnet_b2(weights=effnetb2_pretrained_weights)
13
+ # freeze the feature extractor
14
+ for param in effnetb2.parameters():
15
+ param.requires_grad = False
16
+ # fix the output
17
+ effnetb2.classifier = nn.Sequential(
18
+ nn.Dropout(p = 0.3,inplace=True),
19
+ nn.Linear(in_features = 1408,out_features = num_classes)
20
+ )
21
+ return effnetb2_transforms,effnetb2
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.0.0
2
+ torchvision==0.15.1
3
+ gradio==3.28.1