RayanRen commited on
Commit
4a8e70b
·
1 Parent(s): 26d0611

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
+ 09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth filter=lfs diff=lfs merge=lfs -text
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:37b329ab24c80214f862782cf003468562f4f3eef4b96ebe1296bb096e5e2c36
3
+ size 31313869
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+ from model import create_effnetb2_model
6
+ from timeit import deafult_timer as timer
7
+ from typing import Tuple,Dict
8
+
9
+ class_names=["pissa", "steak", "sushi"]
10
+ effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
11
+ effnetb2.load_state_dict(
12
+ torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
13
+ map_location=torch.device("cpu")
14
+ )
15
+ )
16
+
17
+ #Predict fn
18
+ def predict(img):
19
+ start_time = timer()
20
+ img = effnetb2_transforms(img).unsqueeze(0)
21
+ effnetb2.eval()
22
+ with torch.inference_mode():
23
+ preds = torch.softmax(effnetb2(img), dim=1)
24
+ pred_labels_and_probs = {class_names[i]: float(preds[0][i]) for i in range(len(class_names))}
25
+ pred_time = round(timer()-start_time, 5)
26
+ return pred_labels_and_probs, pred_time
27
+
28
+ #Gradio app
29
+ # Create title, description and article strings
30
+ title = "FoodVision Mini 🍕🥩🍣"
31
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
32
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
33
+ example_list = [[str(filepath)] for filepath in random.sample(test_data_paths, k=3)]
34
+ # Create the Gradio demo
35
+ demo = gr.Interface(fn=predict, # mapping function from input to output
36
+ inputs=gr.Image(type="pil"), # what are the inputs?
37
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
38
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
39
+ examples=example_list,
40
+ title=title,
41
+ description=description,
42
+ article=article)
43
+
44
+ demo.launch()
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetb2_model(num_classes:int=3,
8
+ seed:int=42):
9
+ """Creates an EfficientNetB2 feature extractor model and transforms.
10
+
11
+ Args:
12
+ num_classes (int, optional): number of classes in the classifier head.
13
+ Defaults to 3.
14
+ seed (int, optional): random seed value. Defaults to 42.
15
+
16
+ Returns:
17
+ model (torch.nn.Module): EffNetB2 feature extractor model.
18
+ transforms (torchvision.transforms): EffNetB2 image transforms.
19
+ """
20
+ # Create EffNetB2 pretrained weights, transforms and model
21
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
22
+ transforms = weights.transforms()
23
+ model = torchvision.models.efficientnet_b2(weights=weights)
24
+
25
+ # Freeze all layers in base model
26
+ for param in model.parameters():
27
+ param.requires_grad = False
28
+
29
+ # Change classifier head with random seed for reproducibility
30
+ torch.manual_seed(seed)
31
+ model.classifier = nn.Sequential(
32
+ nn.Dropout(p=0.3, inplace=True),
33
+ nn.Linear(in_features=1408, out_features=num_classes),
34
+ )
35
+
36
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.12.0
2
+ torchvision==0.13.0
3
+ gradio==3.1.4