abjayakar commited on
Commit
94e0d76
Β·
1 Parent(s): 1c48740

initial commit

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ 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
+ effnetb2_feature_extractor_20_percent_10_epochs.pth.pth filter=lfs diff=lfs merge=lfs -text
37
+ effnetb2_feature_extractor_20_percent_10_epochs.pth filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ### 1. Imports and class names setup ###
3
+ import gradio as gr
4
+ import os
5
+ import torch
6
+
7
+ from model import create_effnetb2_model
8
+ from timeit import default_timer as timer
9
+ from typing import Tuple, Dict
10
+
11
+ # Setup class names
12
+ class_names = ["pizza", "steak", "sushi"]
13
+
14
+ ### 2. Model and transforms preparation ###
15
+
16
+ # Create EffNetB2 model
17
+ effnetb2, effnetb2_transforms = create_effnetb2_model(
18
+ num_classes=3, # len(class_names) would also work
19
+ )
20
+
21
+ # Load saved weights
22
+ effnetb2.load_state_dict(
23
+ torch.load(
24
+ f="effnetb2_feature_extractor_20_percent_10_epochs.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 = effnetb2_transforms(img).unsqueeze(0)
40
+
41
+ # Put model into evaluation mode and turn on inference mode
42
+ effnetb2.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(effnetb2(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 = r"FoodVision Mini πŸ•πŸ—πŸ₯—"
60
+ description = """
61
+ # FoodVision Mini
62
+
63
+ FoodVision Mini is a hands-on project inspired by Daniel Bourke's PyTorch tutorial series, focusing on food image classification. Throughout this project, I developed essential skills in **deep learning**, particularly in utilizing **transfer learning** techniques with the **EfficientNetB2** architecture to optimize model performance.
64
+
65
+ ## Key Skills Acquired
66
+
67
+ - **Data Preprocessing**: Learning how to prepare and augment datasets for improved model training.
68
+ - **Model Training and Evaluation**: Gaining experience in training neural networks, monitoring performance metrics, and fine-tuning hyperparameters.
69
+ - **Deployment Techniques**: Understanding how to deploy machine learning models using frameworks like Gradio for user interaction.
70
+
71
+ The project emphasizes practical application by building a lightweight model capable of classifying images of **Pizza**, **Steak**, and **Sushi**, showcasing the effectiveness of EfficientNetB2 in achieving high accuracy with limited computational resources. Overall, FoodVision Mini serves as a comprehensive introduction to real-world applications of machine learning in food recognition, leveraging advanced techniques learned from the tutorial series.
72
+ """
73
+ article = "This is an amazing project that aims to help learn the Basics of Model Deployment (MLOPS)"
74
+
75
+
76
+ # Create examples list from "examples/" directory
77
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
78
+
79
+ # Create the Gradio demo
80
+ demo = gr.Interface(fn=predict, # mapping function from input to output
81
+ inputs=gr.Image(type="pil"), # what are the inputs?
82
+ outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?
83
+ gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
84
+ # Create examples list from "examples/" directory
85
+ examples=example_list,
86
+ title=title,
87
+ description=description,
88
+ article=article)
89
+
90
+ # Launch the demo!
91
+ demo.launch()
effnetb2_feature_extractor_20_percent_10_epochs.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75b6fb7079a80d838ec11d46a46a8df95f016dc3b7838c453a54f69da1b72306
3
+ size 31298170
examples/2582289.jpg ADDED
examples/3622237.jpg ADDED
examples/592799.jpg ADDED
model.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import torchvision
4
+ from torch import nn
5
+
6
+ def create_effnetb2_model(num_classes:int=3,
7
+ seed:int=42):
8
+ """Creates an EfficientNetB2 feature extractor model and transforms.
9
+
10
+ Args:
11
+ num_classes (int, optional): number of classes in the classifier head.
12
+ Defaults to 3.
13
+ seed (int, optional): random seed value. Defaults to 42.
14
+
15
+ Returns:
16
+ model (torch.nn.Module): EffNetB2 feature extractor model.
17
+ transforms (torchvision.transforms): EffNetB2 image transforms.
18
+ """
19
+ # Create EffNetB2 pretrained weights, transforms and model
20
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
21
+ transforms = weights.transforms()
22
+ model = torchvision.models.efficientnet_b2(weights=weights)
23
+
24
+ # Freeze all layers in base model
25
+ for param in model.parameters():
26
+ param.requires_grad = False
27
+
28
+ # Change classifier head with random seed for reproducibility
29
+ torch.manual_seed(seed)
30
+ model.classifier = nn.Sequential(
31
+ nn.Dropout(p=0.3, inplace=True),
32
+ nn.Linear(in_features=1408, out_features=num_classes),
33
+ )
34
+
35
+ return model, transforms
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.4.0
2
+ torchvision==0.19.0
3
+ gradio=5.0.2