Commit ·
a99d095
1
Parent(s): 7b853bd
initial commit
Browse files- app.py +40 -0
- classes.json +33 -0
- model.pt +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn.functional as F
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import timm
|
| 6 |
+
import json
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
DEVICE = "cpu"
|
| 10 |
+
IMG_SIZE = 384
|
| 11 |
+
|
| 12 |
+
# Load classes
|
| 13 |
+
with open('classes.json', 'r') as f:
|
| 14 |
+
classes = json.load(f)
|
| 15 |
+
|
| 16 |
+
def load_model():
|
| 17 |
+
model = timm.create_model("tf_efficientnetv2_s", pretrained=False, num_classes=31, drop_rate=0.3)
|
| 18 |
+
ckpt = torch.load('model.pt', map_location=DEVICE)
|
| 19 |
+
model.load_state_dict(ckpt['model_state_dict'])
|
| 20 |
+
model.eval()
|
| 21 |
+
return model
|
| 22 |
+
|
| 23 |
+
model = load_model()
|
| 24 |
+
|
| 25 |
+
val_tfms = transforms.Compose([
|
| 26 |
+
transforms.Resize((IMG_SIZE, IMG_SIZE)),
|
| 27 |
+
transforms.ToTensor(),
|
| 28 |
+
])
|
| 29 |
+
|
| 30 |
+
def predict(image):
|
| 31 |
+
img = val_tfms(image).unsqueeze(0).to(DEVICE)
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
logits = model(img)
|
| 34 |
+
probs = F.softmax(logits, dim=1)
|
| 35 |
+
top_prob, top_idx = probs.topk(1, dim=1)
|
| 36 |
+
idx_to_class = {v:k for k,v in classes.items()}
|
| 37 |
+
label = idx_to_class[top_idx[0][0].item()]
|
| 38 |
+
return f"{label} ({top_prob[0][0].item():.2f})"
|
| 39 |
+
|
| 40 |
+
gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="text").launch()
|
classes.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"Appam": 0,
|
| 3 |
+
"Beetroot poriyal": 1,
|
| 4 |
+
"Boiled Egg": 2,
|
| 5 |
+
"Carrot poriyal": 3,
|
| 6 |
+
"Chicken 65": 4,
|
| 7 |
+
"Chicken briyani": 5,
|
| 8 |
+
"Dosa": 6,
|
| 9 |
+
"Idly": 7,
|
| 10 |
+
"Kaara chutney": 8,
|
| 11 |
+
"Kali": 9,
|
| 12 |
+
"Koozh": 10,
|
| 13 |
+
"Lemon Rice": 11,
|
| 14 |
+
"Mushroom briyani": 12,
|
| 15 |
+
"Mutton Briyani": 13,
|
| 16 |
+
"Nandu masala": 14,
|
| 17 |
+
"Nei satham": 15,
|
| 18 |
+
"Paal kolukattai": 16,
|
| 19 |
+
"Paneer briyani": 17,
|
| 20 |
+
"Panner masala": 18,
|
| 21 |
+
"Parupu vada": 19,
|
| 22 |
+
"Pidi kolukattai": 20,
|
| 23 |
+
"Poorna kolukattai": 21,
|
| 24 |
+
"Prawn thokku": 22,
|
| 25 |
+
"Puthina Chutney": 23,
|
| 26 |
+
"Sambar": 24,
|
| 27 |
+
"Sambar satham": 25,
|
| 28 |
+
"Satham": 26,
|
| 29 |
+
"Thengai chutney": 27,
|
| 30 |
+
"Uzhuntha vadai": 28,
|
| 31 |
+
"Veg briyani": 29,
|
| 32 |
+
"Ven Pongal": 30
|
| 33 |
+
}
|
model.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4ea8844bf1d88fa91420b64c82d125e87ffbaa6484c706853033797878e72da3
|
| 3 |
+
size 243919325
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.1.0
|
| 2 |
+
torchvision>=0.18.0
|
| 3 |
+
timm>=0.9.0
|
| 4 |
+
Pillow>=10.0.0
|
| 5 |
+
gradio>=3.40.0
|