abhi commited on
Commit
4e2cf68
·
1 Parent(s): 591254e

initial commit

Browse files
Files changed (4) hide show
  1. .gitignore +18 -0
  2. README.md +20 -7
  3. app.py +59 -0
  4. requirements.txt +5 -0
.gitignore ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python
2
+ __pycache__/
3
+ *.pyc
4
+ .venv/
5
+ venv/
6
+ env/
7
+
8
+ # Gradio local run artifacts
9
+ flagged/
10
+ .gradio/
11
+
12
+ # OS / editor
13
+ .DS_Store
14
+ .vscode/
15
+ .idea/
16
+
17
+ # Env/secrets
18
+ .env
README.md CHANGED
@@ -1,15 +1,28 @@
1
  ---
2
- title: Instant Gram
3
- emoji: 🏢
4
- colorFrom: pink
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 6.28.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
  license: mit
12
  short_description: Image Classifier for Indian Lentils
13
  ---
14
 
15
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Instant-Gram
3
+ emoji: 🫘
4
+ colorFrom: yellow
5
+ colorTo: green
6
  sdk: gradio
7
+ sdk_version: 4.44.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: mit
11
  short_description: Image Classifier for Indian Lentils
12
  ---
13
 
14
+ # Instant-Gram 🫘
15
+
16
+ A ResNet18 image classifier fine-tuned on the [Indian Lentils dataset](https://data.mendeley.com/datasets/r4yhfnzc5y/1)
17
+ — 20 varieties of lentils, beans, chickpeas, and grams (Kidney Bean, Horse Gram,
18
+ Chana, Black-Eyed Pea, and more).
19
+
20
+ Upload a photo, or use your camera, to classify a lentil/bean/chickpea variety
21
+ in real time.
22
+
23
+ **Model:** ResNet18 (ImageNet-pretrained, fine-tuned), 99.9% held-out accuracy
24
+ — matches an EfficientNetB0 baseline (the architecture a [published benchmark
25
+ study](https://www.sciencedirect.com/science/article/pii/S2666154323004507#cited-by) found best for this dataset) while training 1.55x faster (T4 GPU), via per-architecture learning-rate tuning.
26
+
27
+ Full methodology, GPU performance benchmarking (mixed precision, `torch.compile`,
28
+ cross-architecture comparison), and findings: see the project write-up.
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ import torch.nn as nn
5
+ from torchvision import models, transforms
6
+ from PIL import Image
7
+
8
+ # Order matches full_train.classes from the training notebook (ImageFolder's
9
+ # alphabetical folder sort) — do not reorder without re-checking the notebook.
10
+ CLASS_NAMES = [
11
+ "Black_Beans", "Black_EyedPea", "Brown_Chickpea", "Desi_chickpea",
12
+ "Horse_Gram", "Husked_BlackGram", "Husked_RedLentil", "Kidney_Bean",
13
+ "Red_Beans", "Red_KidneyBean", "Red_chickpea", "Soya_Bean",
14
+ "White_Chickpea", "White_beans", "White_peabeans", "Whole_BlackGram",
15
+ "Whole_GreenGram", "Whole_TurkishGram", "chana1", "chana2",
16
+ ]
17
+
18
+ # ZeroGPU only grants GPU access inside a function decorated with
19
+ # @spaces.GPU — no CUDA context exists at import time, so the model is
20
+ # loaded on CPU here and moved to CUDA per-call inside predict().
21
+ model = models.resnet18(weights=None)
22
+ model.fc = nn.Linear(model.fc.in_features, len(CLASS_NAMES))
23
+ model.load_state_dict(torch.load("resnet_lentils.pth", map_location="cpu"))
24
+ model.eval()
25
+
26
+ # Same eval_transform used during evaluation in the training notebook.
27
+ transform = transforms.Compose([
28
+ transforms.Resize(256),
29
+ transforms.CenterCrop(224),
30
+ transforms.ToTensor(),
31
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
32
+ ])
33
+
34
+
35
+ @spaces.GPU
36
+ def predict(image: Image.Image):
37
+ if image is None:
38
+ return {}
39
+ device = torch.device("cuda")
40
+ model.to(device)
41
+ x = transform(image.convert("RGB")).unsqueeze(0).to(device)
42
+ with torch.no_grad():
43
+ probs = torch.softmax(model(x), dim=1)[0]
44
+ return {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
45
+
46
+
47
+ demo = gr.Interface(
48
+ fn=predict,
49
+ inputs=gr.Image(type="pil", sources=["upload", "webcam"], label="Photo of a lentil / bean / chickpea"),
50
+ outputs=gr.Label(num_top_classes=5, label="Prediction"),
51
+ title="Indian Lentils Classifier",
52
+ description=(
53
+ "ResNet18, fine-tuned on the Indian Lentils dataset (20 classes). "
54
+ "Upload a photo or use your camera to classify a lentil, bean, or chickpea variety."
55
+ ),
56
+ )
57
+
58
+ if __name__ == "__main__":
59
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio
4
+ pillow
5
+ spaces