Mac commited on
Commit
a81329e
·
1 Parent(s): ad2d7a9

innital commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,7 @@ 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
+ pretrained_effnetb3_flowers102_10_epochs.pth filter=lfs diff=lfs merge=lfs -text
37
+ .pth filter=lfs diff=lfs merge=lfs -text
38
+ *.pth
39
+ filter=lfs diff=lfs merge=lfs -text
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 effnet_feature_extractor
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
+ effnetb3, effnetb3_transforms = effnet_feature_extractor(
18
+ num_classes=102, # could also use len(class_names)
19
+ )
20
+
21
+ # Load saved weights
22
+ effnetb3.load_state_dict(
23
+ torch.load(
24
+ f="pretrained_effnetb3_flowers102.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 = auto_transform(img).unsqueeze(0)
40
+
41
+ # Put model into evaluation mode and turn on inference mode
42
+ effnetb3.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(effnetb3(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 = "Flowers102 🌷🌺🪷🌸"
60
+ description = "An EfficientNetB3 feature extractor computer vision model to classify images of flowers into [102 different classes](https://github.com/mrdbourke/pytorch-deep-learning/blob/main/extras/food101_class_names.txt)."
61
+ article = "Created at "
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()
examples/image_05142.jpg ADDED
examples/image_05867.jpg ADDED
examples/image_06700.jpg ADDED
model.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ def effnet_feature_extractor(
7
+ num_classes: int=102,
8
+ seed: int=42):
9
+ # 1, 2, 3,
10
+ weights= torchvision.models.EfficientNet_B3_Weights.DEFAULT
11
+ transforms = weights.transforms()
12
+ model = torchvision.models.efficientnet_b3(weights=weights)
13
+
14
+ # 4. freeze
15
+ for param in model.parameters():
16
+ param.requires_grad = False
17
+
18
+ # 5. CHange head
19
+ torch.manual_seed(seed)
20
+ model.classifier= nn.Sequential(
21
+ nn.Dropout(p=0.2, inplace=True),
22
+ nn.Linear(in_features=1536,
23
+ out_features=num_classes)
24
+ )
25
+ return model, transforms
pretrained_effnetb3_flowers102_10_epochs.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d4ef667ac404e6d169d37dbce9aa83ed8c3356e39b7df0656f2fbc84d9c6719d
3
+ size 43991736
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.2.0
2
+ torchvision==0.17.0
3
+ gradio==4.28.3