mrdbourke commited on
Commit
ac5165e
β€’
1 Parent(s): dc80c79

initial 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_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:24a803b0e458a9949a7725d651f780c5c77592042d159c7dcd3e658e95e5b96d
3
+ size 31273033
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+
4
+ from model import create_effnetb2_model
5
+ from timeit import default_timer as timer
6
+
7
+ # Setup class names
8
+ class_names = ["pizza", "steak", "sushi"]
9
+
10
+ # Create model
11
+ model, transforms = create_effnetb2_model(
12
+ num_classes=3,
13
+ )
14
+
15
+ # Load saved weights
16
+ model.load_state_dict(
17
+ torch.load(
18
+ f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth",
19
+ map_location=torch.device("cpu"), # load to CPU
20
+ )
21
+ )
22
+
23
+ # Create prediction code
24
+ def predict(img):
25
+ start_time = timer()
26
+ img = transforms(img).unsqueeze(0)
27
+ model.eval()
28
+ with torch.inference_mode():
29
+ pred_probs = torch.softmax(model(img), dim=1)
30
+ pred_labels_and_probs = {
31
+ class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))
32
+ }
33
+ pred_time = round(timer() - start_time, 5)
34
+ return pred_labels_and_probs, pred_time
35
+
36
+
37
+ # Create Gradio app
38
+ title = "FoodVision Mini πŸ•πŸ₯©πŸ£"
39
+ description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
40
+ article = "Created at [09. PyTorch Model Deployment](https://www.learnpytorch.io/09_pytorch_model_deployment/)."
41
+ example_dir = "demo/examples"
42
+
43
+ demo = gr.Interface(
44
+ fn=predict,
45
+ inputs=gr.Image(type="pil"),
46
+ outputs=[
47
+ gr.Label(num_top_classes=3, label="Predictions"),
48
+ gr.Number(label="Prediction time (s)"),
49
+ ],
50
+ # examples="demo/foodvision_mini/examples",
51
+ interpretation="default",
52
+ title=title,
53
+ description=description,
54
+ article=article,
55
+ )
56
+
57
+ demo.launch()
model.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torchvision
2
+
3
+ from torch import nn
4
+
5
+
6
+ def create_effnetb2_model(num_classes: int):
7
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
8
+ transforms = weights.transforms()
9
+ model = torchvision.models.efficientnet_b2(weights=weights)
10
+
11
+ # Freeze base model
12
+ for param in model.parameters():
13
+ param.requires_grad = False
14
+
15
+ # Change classifier head
16
+ model.classifier = nn.Sequential(
17
+ nn.Dropout(p=0.3, inplace=True),
18
+ nn.Linear(in_features=1408, out_features=num_classes),
19
+ )
20
+ 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