umasiyer commited on
Commit
576d9cf
1 Parent(s): be95fb2

adding files for breakfast classifier

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ 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
+ indi-bf-classifier.pth filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### 1. Imports and class names setup ###
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from model import create_effnetb2_model
7
+ from timeit import default_timer as timer
8
+ from typing import Tuple, Dict
9
+
10
+ # Setup class names
11
+ class_names = ['dosa', 'idly', 'kichdi', 'pongal', 'poori', 'vada']
12
+ ### 2. Model and transforms preparation ###
13
+
14
+ # Create model
15
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
16
+ num_classes=101, # could also use len(class_names)
17
+ )
18
+
19
+ # Load saved weights
20
+ effnetb2.load_state_dict(
21
+ torch.load(
22
+ f="09_exercise_caltech101_effnetb2.pth",
23
+ map_location=torch.device("cpu"), # load to CPU
24
+ )
25
+ )
26
+
27
+ ### 3. Predict function ###
28
+
29
+ # Create predict function
30
+ def predict(img) -> Tuple[Dict, float]:
31
+ """Transforms and performs a prediction on img and returns prediction and time taken.
32
+ """
33
+ # Start the timer
34
+ start_time = timer()
35
+
36
+ # Transform the target image and add a batch dimension
37
+ img = effnetb2_transforms(img).unsqueeze(0)
38
+
39
+ # Put model into evaluation mode and turn on inference mode
40
+ effnetb2.eval()
41
+ with torch.inference_mode():
42
+ # Pass the transformed image through the model and turn the prediction logits into prediction probabilities
43
+ pred_probs = torch.softmax(effnetb2(img), dim=1)
44
+
45
+ # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
46
+ pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
47
+
48
+ # Calculate the prediction time
49
+ pred_time = round(timer() - start_time, 5)
50
+
51
+ # Return the prediction dictionary and prediction time
52
+ return pred_labels_and_probs, pred_time
53
+
54
+ ### 4. Gradio app ###
55
+
56
+ # Create title, description and article strings
57
+ title = "🍛Classify South-Indian Breakfast🍲"
58
+ description = "An EfficientNetB2 feature extractor computer vision model to classify south indian breakfast varieties such as idly, Dosa, Vada, Poori, Kichdi & Pongal"
59
+ article = "Created at based custom dataset."
60
+
61
+ # Create examples list from "examples/" directory
62
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
63
+
64
+ # Create Gradio interface
65
+ demo = gr.Interface(
66
+ fn=predict,
67
+ inputs=gr.Image(type="pil"),
68
+ outputs=[
69
+ gr.Label(num_top_classes=2, label="Predictions"),
70
+ gr.Number(label="Prediction time (s)"),
71
+ ],
72
+ examples=example_list,
73
+ title=title,
74
+ description=description,
75
+ article=article,
76
+ )
77
+
78
+ # Launch the app!
79
+ demo.launch()
examples/idly_46.jpeg ADDED
examples/poori_98.jpeg ADDED
examples/vada_95.jpeg ADDED
indi-bf-classifier.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f2736e8e8ed65b3df84d053aae0630edbb36344daff78039c715c6daa5a8479a
3
+ size 31296570
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=6,
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==2.3.0
2
+ torchvision==0.18.0
3
+ gradio