Upload 6 files
Browse files- app.py +164 -20
- gold_fish.png +0 -0
- kite.png +0 -0
- vulture.png +0 -0
app.py
CHANGED
|
@@ -97,37 +97,181 @@ def predict_images(
|
|
| 97 |
return results
|
| 98 |
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
with gr.Row():
|
| 110 |
-
with gr.Column():
|
| 111 |
input_images = gr.Image(
|
| 112 |
-
label="Upload
|
| 113 |
type="pil",
|
| 114 |
sources=["upload", "clipboard"],
|
|
|
|
| 115 |
)
|
|
|
|
| 116 |
gr.Examples(
|
| 117 |
examples=[
|
| 118 |
-
"
|
| 119 |
-
"
|
| 120 |
-
"
|
| 121 |
],
|
| 122 |
inputs=input_images,
|
| 123 |
-
label="
|
| 124 |
)
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
run_btn.click(fn=predict_images, inputs=[input_images, topk], outputs=output)
|
|
|
|
| 131 |
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
|
|
|
| 97 |
return results
|
| 98 |
|
| 99 |
|
| 100 |
+
# Custom CSS for modern UI
|
| 101 |
+
custom_css = """
|
| 102 |
+
.gradio-container {
|
| 103 |
+
font-family: 'IBM Plex Sans', sans-serif;
|
| 104 |
+
max-width: 1400px !important;
|
| 105 |
+
}
|
| 106 |
+
.header-box {
|
| 107 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 108 |
+
padding: 40px;
|
| 109 |
+
border-radius: 15px;
|
| 110 |
+
color: white;
|
| 111 |
+
text-align: center;
|
| 112 |
+
margin-bottom: 30px;
|
| 113 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
|
| 114 |
+
}
|
| 115 |
+
.stats-card {
|
| 116 |
+
background: linear-gradient(145deg, #f8f9fa 0%, #e9ecef 100%);
|
| 117 |
+
padding: 20px;
|
| 118 |
+
border-radius: 12px;
|
| 119 |
+
border-left: 5px solid #667eea;
|
| 120 |
+
margin: 10px 0;
|
| 121 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
|
| 122 |
+
}
|
| 123 |
+
.prediction-box {
|
| 124 |
+
background: #ffffff;
|
| 125 |
+
border-radius: 12px;
|
| 126 |
+
padding: 20px;
|
| 127 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.08);
|
| 128 |
+
}
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
with gr.Blocks(title="ResNet-50 ImageNet-1k Classifier", css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 132 |
+
# Header
|
| 133 |
+
gr.HTML("""
|
| 134 |
+
<div class="header-box">
|
| 135 |
+
<h1 style="margin: 0; font-size: 3em; font-weight: 700;">π― ResNet50 ImageNet Classifier</h1>
|
| 136 |
+
<p style="margin: 15px 0 0 0; font-size: 1.3em; opacity: 0.95;">
|
| 137 |
+
Trained from Scratch on ImageNet-1K | 75%+ Top-1 Accuracy
|
| 138 |
+
</p>
|
| 139 |
+
<p style="margin: 10px 0 0 0; font-size: 1em; opacity: 0.85;">
|
| 140 |
+
1000 classes β’ 25.6M parameters β’ 98MB model
|
| 141 |
+
</p>
|
| 142 |
+
</div>
|
| 143 |
+
""")
|
| 144 |
+
|
| 145 |
+
# Stats row
|
| 146 |
+
with gr.Row():
|
| 147 |
+
with gr.Column(scale=1):
|
| 148 |
+
gr.HTML("""
|
| 149 |
+
<div class="stats-card">
|
| 150 |
+
<h3 style="margin: 0 0 10px 0; color: #667eea;">π Dataset</h3>
|
| 151 |
+
<p style="margin: 5px 0;"><strong>1.28M</strong> training images</p>
|
| 152 |
+
<p style="margin: 5px 0;"><strong>1000</strong> ImageNet classes</p>
|
| 153 |
+
</div>
|
| 154 |
+
""")
|
| 155 |
+
with gr.Column(scale=1):
|
| 156 |
+
gr.HTML("""
|
| 157 |
+
<div class="stats-card">
|
| 158 |
+
<h3 style="margin: 0 0 10px 0; color: #667eea;">π― Performance</h3>
|
| 159 |
+
<p style="margin: 5px 0;"><strong>75-77%</strong> top-1 accuracy</p>
|
| 160 |
+
<p style="margin: 5px 0;"><strong>92-94%</strong> top-5 accuracy</p>
|
| 161 |
+
</div>
|
| 162 |
+
""")
|
| 163 |
+
with gr.Column(scale=1):
|
| 164 |
+
gr.HTML("""
|
| 165 |
+
<div class="stats-card">
|
| 166 |
+
<h3 style="margin: 0 0 10px 0; color: #667eea;">β‘ Architecture</h3>
|
| 167 |
+
<p style="margin: 5px 0;"><strong>ResNet50</strong> (Bottleneck)</p>
|
| 168 |
+
<p style="margin: 5px 0;"><strong>25.6M</strong> parameters</p>
|
| 169 |
+
</div>
|
| 170 |
+
""")
|
| 171 |
+
|
| 172 |
+
gr.Markdown("---")
|
| 173 |
+
gr.Markdown("## πΈ Upload an Image for Classification")
|
| 174 |
+
|
| 175 |
+
# Main interface
|
| 176 |
with gr.Row():
|
| 177 |
+
with gr.Column(scale=1):
|
| 178 |
input_images = gr.Image(
|
| 179 |
+
label="Upload Image",
|
| 180 |
type="pil",
|
| 181 |
sources=["upload", "clipboard"],
|
| 182 |
+
height=400
|
| 183 |
)
|
| 184 |
+
|
| 185 |
gr.Examples(
|
| 186 |
examples=[
|
| 187 |
+
"gold_fish.png",
|
| 188 |
+
"kite.png",
|
| 189 |
+
"vulture.png",
|
| 190 |
],
|
| 191 |
inputs=input_images,
|
| 192 |
+
label="π Try these example images"
|
| 193 |
)
|
| 194 |
+
|
| 195 |
+
with gr.Row():
|
| 196 |
+
topk = gr.Slider(1, 10, value=5, step=1, label="Top-K Predictions")
|
| 197 |
+
|
| 198 |
+
with gr.Row():
|
| 199 |
+
clear_btn = gr.Button("π Clear", variant="secondary", scale=1)
|
| 200 |
+
run_btn = gr.Button("π Classify", variant="primary", scale=2)
|
| 201 |
+
|
| 202 |
+
with gr.Column(scale=1):
|
| 203 |
+
gr.HTML('<div class="prediction-box">')
|
| 204 |
+
output = gr.JSON(label="π Top Predictions", show_label=True)
|
| 205 |
+
gr.HTML('</div>')
|
| 206 |
+
|
| 207 |
+
gr.Markdown("""
|
| 208 |
+
### π‘ Tips for Best Results
|
| 209 |
+
- Upload **clear, well-lit** images
|
| 210 |
+
- Works best with **centered objects**
|
| 211 |
+
- Supports **1000 ImageNet categories**
|
| 212 |
+
- Processing time: **~1-2 seconds**
|
| 213 |
+
""")
|
| 214 |
+
|
| 215 |
+
# Technical accordion
|
| 216 |
+
with gr.Accordion("π Technical Details", open=False):
|
| 217 |
+
gr.Markdown("""
|
| 218 |
+
### Model Architecture
|
| 219 |
+
**ResNet50** trained from scratch (no pre-trained weights) on ImageNet-1K
|
| 220 |
+
|
| 221 |
+
**Training Configuration:**
|
| 222 |
+
- **Optimizer:** SGD with momentum (0.9), weight decay (1e-4)
|
| 223 |
+
- **Learning Rate:** Cosine annealing with warmup (0.1 β 0.0005)
|
| 224 |
+
- **Augmentation:** AutoAugment (ImageNet), RandomErasing, Mixup
|
| 225 |
+
- **Precision:** Mixed FP16 with gradient scaling
|
| 226 |
+
- **Epochs:** 75 with early stopping
|
| 227 |
+
|
| 228 |
+
**Architecture Details:**
|
| 229 |
+
```
|
| 230 |
+
Input (224Γ224Γ3)
|
| 231 |
+
β
|
| 232 |
+
Conv1 (7Γ7, stride=2) + BN + ReLU β 112Γ112Γ64
|
| 233 |
+
MaxPool (3Γ3, stride=2) β 56Γ56Γ64
|
| 234 |
+
β
|
| 235 |
+
Layer1: 3Γ Bottleneck β 56Γ56Γ256
|
| 236 |
+
Layer2: 4Γ Bottleneck β 28Γ28Γ512
|
| 237 |
+
Layer3: 6Γ Bottleneck β 14Γ14Γ1024
|
| 238 |
+
Layer4: 3Γ Bottleneck β 7Γ7Γ2048
|
| 239 |
+
β
|
| 240 |
+
Global Average Pool β 1Γ1Γ2048
|
| 241 |
+
Fully Connected β 1000 classes
|
| 242 |
+
```
|
| 243 |
+
""")
|
| 244 |
+
|
| 245 |
+
with gr.Accordion("π Links & Resources", open=False):
|
| 246 |
+
gr.Markdown("""
|
| 247 |
+
### Project Links
|
| 248 |
+
- π [GitHub Repository](https://github.com/godsofheaven/Resnet50-from-Scratch-on-Imagenet-1K)
|
| 249 |
+
- π [Original ResNet Paper (He et al., 2016)](https://arxiv.org/abs/1512.03385)
|
| 250 |
+
- ποΈ [ImageNet Dataset](https://huggingface.co/datasets/ILSVRC/imagenet-1k)
|
| 251 |
+
|
| 252 |
+
### Citation
|
| 253 |
+
```bibtex
|
| 254 |
+
@inproceedings{he2016deep,
|
| 255 |
+
title={Deep residual learning for image recognition},
|
| 256 |
+
author={He, Kaiming and Zhang, Xiangyu and Ren, Shaoqing and Sun, Jian},
|
| 257 |
+
booktitle={CVPR},
|
| 258 |
+
year={2016}
|
| 259 |
+
}
|
| 260 |
+
```
|
| 261 |
+
""")
|
| 262 |
+
|
| 263 |
+
# Footer
|
| 264 |
+
gr.Markdown("""
|
| 265 |
+
---
|
| 266 |
+
<div style="text-align: center; opacity: 0.7; padding: 20px;">
|
| 267 |
+
<p style="margin: 5px 0;">π Built with Gradio β’ Trained on AWS EC2 β’ Deployed on π€ Hugging Face Spaces</p>
|
| 268 |
+
<p style="margin: 5px 0;">Model trained from scratch achieving 76.12% top-1 accuracy on ImageNet-1K</p>
|
| 269 |
+
</div>
|
| 270 |
+
""")
|
| 271 |
+
|
| 272 |
+
# Button actions
|
| 273 |
run_btn.click(fn=predict_images, inputs=[input_images, topk], outputs=output)
|
| 274 |
+
clear_btn.click(lambda: (None, None), outputs=[input_images, output])
|
| 275 |
|
| 276 |
|
| 277 |
if __name__ == "__main__":
|
gold_fish.png
ADDED
|
kite.png
ADDED
|
vulture.png
ADDED
|