Spaces:
Sleeping
Sleeping
Add application files
Browse files- .gitattributes +1 -0
- __init__.py +0 -0
- app.py +81 -0
- effnetB2_2025-11-29_epoch4.pt +3 -0
- examples/100274.jpg +0 -0
- examples/301603.jpg +0 -0
- examples/3497151.jpg +0 -0
- model.py +20 -0
- requirements.txt +4 -0
.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 |
+
effnetB2_2025-11-29_epoch4.pt filter=lfs diff=lfs merge=lfs -text
|
__init__.py
ADDED
|
File without changes
|
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torchvision.models import EfficientNet_B2_Weights
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from model import EffnetB2
|
| 5 |
+
from timeit import default_timer as timer
|
| 6 |
+
from typing import Tuple
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
device = "cpu"
|
| 11 |
+
|
| 12 |
+
# laoding our model
|
| 13 |
+
model = EffnetB2()
|
| 14 |
+
checkpoint = torch.load(
|
| 15 |
+
"effnetB2_2025-11-29_epoch4.pt",
|
| 16 |
+
map_location=device,
|
| 17 |
+
)
|
| 18 |
+
model.load_state_dict(checkpoint)
|
| 19 |
+
model.to(device)
|
| 20 |
+
|
| 21 |
+
class_names = ["pizza", "steak", "sushi"]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def predict(img) -> Tuple[dict, float]:
|
| 25 |
+
"""_summary_
|
| 26 |
+
Takes an image and make predictions
|
| 27 |
+
Args:
|
| 28 |
+
img (_type_): An Image
|
| 29 |
+
|
| 30 |
+
Returns:
|
| 31 |
+
Tuple[dict, float]: a dict for the confidence of each class, and float for the inference time
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
# Transform the image to work with effnetB2
|
| 35 |
+
transform = (
|
| 36 |
+
EfficientNet_B2_Weights.DEFAULT.transforms()
|
| 37 |
+
) # getting the model transforms
|
| 38 |
+
transformed_img = transform(img).unsqueeze(0).to(device)
|
| 39 |
+
|
| 40 |
+
# Put model into eval mode and make predictions
|
| 41 |
+
start = timer() # start timer
|
| 42 |
+
model.eval()
|
| 43 |
+
|
| 44 |
+
with torch.inference_mode():
|
| 45 |
+
logits = model(transformed_img)
|
| 46 |
+
pred_probs = torch.softmax(logits, dim=1)
|
| 47 |
+
pred_labels = torch.argmax(pred_probs, dim=1)
|
| 48 |
+
|
| 49 |
+
# Creating a prediction label and a preiction probability dict
|
| 50 |
+
pred_dict = {
|
| 51 |
+
k: v for k, v in zip(class_names, pred_probs.squeeze(0).cpu().tolist())
|
| 52 |
+
}
|
| 53 |
+
end = timer()
|
| 54 |
+
pred_time = round(end - start, 4)
|
| 55 |
+
return (pred_dict, pred_time)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# Creating a list of exmaple images for Gradio Demo
|
| 59 |
+
example_list = [
|
| 60 |
+
["examples/" + example]
|
| 61 |
+
for example in os.listdir("examples")
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
title = "Food Vision Mini 🍕🥩🍣"
|
| 65 |
+
description = "An efficientNetB2 feature extractor computr vision model to classify images as pizza, steak and sushi."
|
| 66 |
+
article = "Created at [09.Pytorch Model Deployement.](https://www.learnpytorch.io/09_pytorch_model_deployment/)"
|
| 67 |
+
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=gr.Image(type="pil"),
|
| 71 |
+
outputs=[
|
| 72 |
+
gr.Label(num_top_classes=3, label="Predictions"),
|
| 73 |
+
gr.Number(label="Prediction Time(s)"),
|
| 74 |
+
],
|
| 75 |
+
examples=example_list,
|
| 76 |
+
title=title,
|
| 77 |
+
description=description,
|
| 78 |
+
article=article,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
demo.launch(debug=False, share=True)
|
effnetB2_2025-11-29_epoch4.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:252bc61e6a438b7c8a59c5141cc63d09bad1972a3f413be35b6f55774f841a3e
|
| 3 |
+
size 31289914
|
examples/100274.jpg
ADDED
|
examples/301603.jpg
ADDED
|
examples/3497151.jpg
ADDED
|
model.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch.nn as nn
|
| 2 |
+
from torchvision.models import efficientnet_b2, EfficientNet_B2_Weights
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class EffnetB2(nn.Module):
|
| 6 |
+
def __init__(self, num_classes=3):
|
| 7 |
+
super().__init__()
|
| 8 |
+
self.model = efficientnet_b2(weights=EfficientNet_B2_Weights.DEFAULT)
|
| 9 |
+
for param in self.model.parameters():
|
| 10 |
+
param.requires_grad = False
|
| 11 |
+
# print(self.model)
|
| 12 |
+
in_features = self.model.classifier.get_submodule("1").in_features
|
| 13 |
+
self.model.classifier = nn.Sequential(
|
| 14 |
+
nn.Linear(in_features=in_features, out_features=num_classes)
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
return self.model(x)
|
| 19 |
+
|
| 20 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
gradio
|
| 4 |
+
timeit
|