LadislavVasina1 commited on
Commit
fe4e7e6
1 Parent(s): f6b82d3
09_pretrained_vit_feature_extractor_foodvision101.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:15e0d245cb68f96c0437ab697c24c2e30f883c9e6282a9be1f6e2752f59d583c
3
+ size 343572169
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_vit_model
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: # reading them in from class_names.txt
12
+ class_names = [food_name.strip() for food_name in f.readlines()]
13
+
14
+ ### 2. Model and transforms preparation ###
15
+
16
+ # Create model
17
+ vit_food101, vit_transforms = create_vit_model(
18
+ num_classes=len(class_names),
19
+ )
20
+
21
+ # Load saved weights
22
+ vit_food101.load_state_dict(
23
+ torch.load(
24
+ f="09_pretrained_vit_feature_extractor_foodvision101.pth",
25
+ map_location=torch.device("cpu"), # load to CPU
26
+ )
27
+ )
28
+
29
+ ### 3. Predict function ###
30
+
31
+ # Create predict function
32
+ def predict(img) -> Tuple[Dict, float]:
33
+ """Transforms and performs a prediction on img and returns prediction and time taken.
34
+ """
35
+ # Start the timer
36
+ start_time = timer()
37
+
38
+ # Transform the target image and add a batch dimension
39
+ img = vit_transforms(img).unsqueeze(0)
40
+
41
+ # Put model into evaluation mode and turn on inference mode
42
+ vit_food101.eval()
43
+ with torch.inference_mode():
44
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
45
+ pred_probs = torch.softmax(vit_food101(img), dim=1)
46
+
47
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
48
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
49
+
50
+ # Calculate the prediction time
51
+ pred_time = round(timer() - start_time, 5)
52
+
53
+ # Return the prediction dictionary and prediction time
54
+ return pred_labels_and_probs, pred_time
55
+
56
+ ### 4. Gradio app ###
57
+
58
+ # Create title, description and article strings
59
+ title = "FoodVision Vision Transformer 🥩👀🤖"
60
+ description = "An ViT feature extractor computer vision model to classify images of food into [101 different classes](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/food101_class_names.txt)."
61
+ article = "Tested accuracy 77 %"
62
+
63
+ # Create examples list from "examples/" directory
64
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
65
+
66
+ # Create Gradio interface
67
+ demo = gr.Interface(
68
+ fn=predict,
69
+ inputs=gr.Image(type="pil"),
70
+ outputs=[
71
+ gr.Label(num_top_classes=5, label="Predictions"),
72
+ gr.Number(label="Prediction time (s)"),
73
+ ],
74
+ examples=example_list,
75
+ title=title,
76
+ description=description,
77
+ article=article,
78
+ )
79
+
80
+ # Launch the app!
81
+ 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/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_vit_model(num_classes:int=3,
8
+ seed:int=42):
9
+ """Creates a ViT-B/16 feature extractor model and transforms.
10
+
11
+ Args:
12
+ num_classes (int, optional): number of target classes. Defaults to 3.
13
+ seed (int, optional): random seed value for output layer. Defaults to 42.
14
+
15
+ Returns:
16
+ model (torch.nn.Module): ViT-B/16 feature extractor model.
17
+ transforms (torchvision.transforms): ViT-B/16 image transforms.
18
+ """
19
+ # Create ViT_B_16 pretrained weights, transforms and model
20
+ weights = torchvision.models.ViT_B_16_Weights.DEFAULT
21
+ transforms = weights.transforms()
22
+ model = torchvision.models.vit_b_16(weights=weights)
23
+
24
+ # Freeze all layers in model
25
+ for param in model.parameters():
26
+ param.requires_grad = False
27
+
28
+ # Change classifier head to suit our needs (this will be trainable)
29
+ torch.manual_seed(seed)
30
+ model.heads = nn.Sequential(nn.Linear(in_features=768, # keep this the same as original model
31
+ out_features=num_classes)) # update to reflect target number of classes
32
+
33
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio