Spaces:
Build error
Build error
first commit
Browse files- .gitattributes +1 -0
- app.py +50 -0
- effnet_b2-20%-10epochs.pth +3 -0
- model.py +26 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
effnet_b2-20%-10epochs.pth filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
import os
|
5 |
+
import torch.nn as nn
|
6 |
+
import torchvision
|
7 |
+
from model import create_model
|
8 |
+
|
9 |
+
from timeit import default_timer as timer
|
10 |
+
from typing import Tuple, Dict
|
11 |
+
|
12 |
+
#################
|
13 |
+
title = 'FoodVisionMini - Diljith'
|
14 |
+
description = 'Classifies an image of food item into either one of 3 classes : pizza, steak or sushi'
|
15 |
+
|
16 |
+
effnetb2, transforms, optimizer, lossFunc = create_model(num_classes = 3)
|
17 |
+
effnetb2.load_state_dict(torch.load(f = 'effnet_b2-20%-10epochs.pth'), map_location = torch.device('cpu'))
|
18 |
+
|
19 |
+
|
20 |
+
class_names = ['pizza', 'steak', 'sushi']
|
21 |
+
|
22 |
+
def predict(img):
|
23 |
+
img = transforms(img).unsqueeze(0)
|
24 |
+
effnetb2.eval()
|
25 |
+
probs_dict = {}
|
26 |
+
start = timer()
|
27 |
+
with torch.inference_mode():
|
28 |
+
pred_probs = effnetb2(img).softmax(dim = 1)
|
29 |
+
pred_label = pred_probs.argmax(dim = 1)
|
30 |
+
|
31 |
+
probs_dict = {class_names[i] : float(pred_probs[0][i]) for i in range(len(class_names))}
|
32 |
+
end = timer
|
33 |
+
|
34 |
+
return probs_dict, end-start
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
example_list = ['examples/' + example for example in os.listdir(examples_path)]
|
39 |
+
|
40 |
+
projectApp = gr.Interface(
|
41 |
+
fn = predict,
|
42 |
+
inputs = gr.Image(type = 'pil'),
|
43 |
+
outputs = [gr.Label(num_top_classes = len(class_names), label = 'Predictions'),
|
44 |
+
gr.Number(label = 'Prediction time(s)')],
|
45 |
+
examples = example_list,
|
46 |
+
title = title,
|
47 |
+
description = description
|
48 |
+
)
|
49 |
+
|
50 |
+
projectApp.launch()
|
effnet_b2-20%-10epochs.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c33bb61dc620af399b1377d061f311da9eff22899b992bf90ae726f9a60333dc
|
3 |
+
size 31281451
|
model.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import torch.nn as nn
|
5 |
+
import torchvision
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
def create_model(num_classes, seed = 42):
|
10 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
11 |
+
auto_transforms = weights.transforms()
|
12 |
+
|
13 |
+
model = torchvision.models.efficientnet_b2(weights = weights)
|
14 |
+
|
15 |
+
for param in model.parameters():
|
16 |
+
param.requires_grad = False
|
17 |
+
|
18 |
+
model.classifier = nn.Sequential(
|
19 |
+
nn.Dropout(p = 0.3, inplace = True),
|
20 |
+
nn.Linear(in_features = 1408, out_features = num_classes, bias = True)
|
21 |
+
)
|
22 |
+
|
23 |
+
optimizer = torch.optim.Adam(params = model.parameters(), lr = 0.001)
|
24 |
+
lossFunc = nn.CrossEntropyLoss()
|
25 |
+
|
26 |
+
return model, auto_transforms, optimizer, lossFunc
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
torch == 1.12.0
|
3 |
+
torchvision == 0.13.0
|
4 |
+
gradio == 3.1.4
|