Jahnavibh Claude commited on
Commit
1570bd3
·
1 Parent(s): e4b1c0a

Redesign demo with clean professional interface

Browse files

- White background with #1975cf blue accents
- Removed all emojis for professional appearance
- Added model selection dropdown in top right
- Left side: upload section and 4 example image buttons
- Right side: Vela stats, model stats, and top 3 predictions
- Clean title without subtitle
- Improved layout and styling

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +107 -72
app.py CHANGED
@@ -7,12 +7,14 @@ import tensorflow as tf
7
  from huggingface_hub import hf_hub_download
8
 
9
  # Download the TFLite model and labels from your Hugging Face repository
10
- MODEL_REPO = "JahnaviBhansali/MobileNet"
11
- MODEL_FILE = "mobilenet_v2_1.0_224_INT8.tflite"
 
12
  LABELS_FILE = "labelmappings.txt"
13
 
14
  print("Downloading model and labels from Hugging Face...")
15
  model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
 
16
  labels_path = hf_hub_download(repo_id=MODEL_REPO, filename=LABELS_FILE)
17
 
18
  # Load the TFLite model
@@ -29,6 +31,8 @@ with open(labels_path, 'r') as f:
29
 
30
  print(f"Model loaded successfully! Input shape: {input_details[0]['shape']}")
31
  print(f"Number of classes: {len(class_labels)}")
 
 
32
 
33
  def preprocess_image(image):
34
  """
@@ -55,7 +59,7 @@ def preprocess_image(image):
55
 
56
  def classify_image(image):
57
  """
58
- Classify the input image and return top-5 predictions with confidence scores.
59
  """
60
  if image is None:
61
  return "Please upload an image."
@@ -93,23 +97,23 @@ def classify_image(image):
93
  # Apply softmax to get probabilities
94
  predictions = tf.nn.softmax(predictions).numpy()
95
 
96
- # Get top-5 predictions
97
- top5_indices = np.argsort(predictions)[-5:][::-1]
98
 
99
  # Format results
100
  results = []
101
- for i, idx in enumerate(top5_indices):
102
  class_name = class_labels[idx] if idx < len(class_labels) else f"Class {idx}"
103
  confidence = predictions[idx]
104
  results.append(f"**{class_name}**: {confidence:.1%}")
105
 
106
  # Create formatted output
107
- result_text = "## 🎯 Top 5 Predictions:\n\n" + "\n".join(f"{i+1}. {result}" for i, result in enumerate(results))
108
 
109
  return result_text
110
 
111
  except Exception as e:
112
- return f"❌ Error processing image: {str(e)}"
113
 
114
  def load_example_image(example_path):
115
  """Load example images for demonstration."""
@@ -131,48 +135,75 @@ def load_example_image(example_path):
131
 
132
  # Create Gradio interface
133
  with gr.Blocks(
134
- theme=gr.themes.Soft(),
135
- title="📱 MobileNetV2 INT8 Image Classification",
136
  css="""
137
  .gradio-container {
138
- max-width: 1000px !important;
139
  margin: auto !important;
 
140
  }
141
  .main-header {
142
  text-align: center;
143
  margin-bottom: 2rem;
144
- background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
145
- -webkit-background-clip: text;
146
- -webkit-text-fill-color: transparent;
147
- background-clip: text;
148
  }
149
- .description {
150
- text-align: center;
151
- margin-bottom: 2rem;
152
- font-size: 1.1em;
153
- color: #666;
 
 
 
 
 
 
 
154
  }
155
- .example-section {
156
- margin-top: 1rem;
 
157
  padding: 1rem;
158
- background: #f8f9fa;
159
- border-radius: 10px;
 
 
 
 
 
 
 
 
 
 
160
  }
161
  """
162
  ) as demo:
163
 
164
- gr.HTML("""
165
- <div class="main-header">
166
- <h1>MobileNetV2 INT8 Image Classification</h1>
167
- </div>
168
- <div class="description">
169
- <p><strong>Purpose:</strong> Demonstrate efficient image classification using MobileNetV2 INT8 quantized model, optimized for mobile deployment. This TensorFlow Lite model provides excellent performance with reduced model size and faster inference.</p>
170
- </div>
171
- """)
 
 
 
 
 
 
172
 
173
  with gr.Row():
174
  with gr.Column(scale=1):
175
- gr.HTML("### 📤 Upload Image")
 
 
 
 
176
 
177
  input_image = gr.Image(
178
  label="Choose an image to classify",
@@ -181,65 +212,70 @@ with gr.Blocks(
181
  )
182
 
183
  classify_btn = gr.Button(
184
- "🔍 Classify Image",
185
  variant="primary",
186
  size="lg"
187
  )
188
 
189
  gr.HTML("""
190
- <div style="margin-top: 1rem; padding: 1rem; background: #2d2d2d; border-radius: 8px; border: 1px solid #444;">
191
- <h3 style="margin: 0; color: #fff; font-size: 1rem;">Try an examples</h3>
192
  </div>
193
  """)
194
 
195
  with gr.Row():
196
  example_cat = gr.Button("Cat", size="sm")
197
  example_dog = gr.Button("Dog", size="sm")
198
- example_car = gr.Button("Car", size="sm")
199
 
200
  with gr.Row():
 
201
  example_food = gr.Button("Food", size="sm")
202
- example_nature = gr.Button("Nature", size="sm")
203
 
204
  with gr.Column(scale=1):
205
- gr.HTML("### 🎯 Classification Results")
206
-
207
- output_text = gr.Markdown(
208
- value="Upload an image and click 'Classify Image' to see predictions...",
209
- label="Predictions"
210
- )
 
 
 
 
 
 
 
 
 
 
211
 
212
- gr.HTML(f"""
213
- <div style="margin-top: 2rem; padding: 1.5rem; background: #1a1a1a; border-radius: 8px; border: 1px solid #333;">
214
- <h4 style="color: #ffffff; margin-bottom: 1rem;">📊 MobileNetV2 INT8 Performance Metrics</h4>
215
- <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; color: #e0e0e0;">
216
- <div style="background: #2a2a2a; padding: 1rem; border-radius: 6px;">
217
- <strong style="color: #4ade80;">📈 Accuracy:</strong><br>
218
- • ImageNet Top-1: 69.7%<br>
219
- • Optimized for mobile
220
- </div>
221
- <div style="background: #2a2a2a; padding: 1rem; border-radius: 6px;">
222
- <strong style="color: #60a5fa;">⚡ Performance:</strong><br>
223
- • INT8 Quantized<br>
224
- • Ultra-fast inference
225
- </div>
226
- <div style="background: #2a2a2a; padding: 1rem; border-radius: 6px;">
227
- <strong style="color: #f59e0b;">💾 Model Size:</strong><br>
228
- • Size: ~4MB<br>
229
- • Quantized weights
230
- </div>
231
- <div style="background: #2a2a2a; padding: 1rem; border-radius: 6px;">
232
- <strong style="color: #ec4899;">🎯 Optimization:</strong><br>
233
- • TensorFlow Lite<br>
234
- • Mobile-ready
235
- </div>
236
  </div>
237
- <div style="margin-top: 1rem; padding: 1rem; background: #333; border-radius: 6px;">
238
- <p style="margin: 0; color: #fff;"><strong>Model:</strong> {MODEL_REPO}</p>
239
- <p style="margin: 0.5rem 0 0 0; color: #ccc;"><strong>Classes:</strong> {len(class_labels)} ImageNet categories</p>
 
240
  </div>
241
  </div>
242
  """)
 
 
 
 
 
 
 
 
 
 
 
243
 
244
  # Set up event handlers
245
  classify_btn.click(
@@ -253,7 +289,6 @@ with gr.Blocks(
253
  example_dog.click(lambda: load_example_image("Dog"), outputs=input_image)
254
  example_car.click(lambda: load_example_image("Car"), outputs=input_image)
255
  example_food.click(lambda: load_example_image("Food"), outputs=input_image)
256
- example_nature.click(lambda: load_example_image("Nature"), outputs=input_image)
257
 
258
  # Auto-classify when image is uploaded
259
  input_image.change(
 
7
  from huggingface_hub import hf_hub_download
8
 
9
  # Download the TFLite model and labels from your Hugging Face repository
10
+ MODEL_REPO = "JahnaviBhansali/mobilenet-v2-ethos-u55"
11
+ MODEL_FILE = "mobilenet_v2_1.0_224_INT8.tflite" # Using original INT8 model for Gradio compatibility
12
+ VELA_MODEL_FILE = "mobilenet_v2_1.0_224_INT8_vela.tflite" # Vela-optimized model for Ethos-U55
13
  LABELS_FILE = "labelmappings.txt"
14
 
15
  print("Downloading model and labels from Hugging Face...")
16
  model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
17
+ vela_model_path = hf_hub_download(repo_id=MODEL_REPO, filename=VELA_MODEL_FILE) # Download Vela model for reference
18
  labels_path = hf_hub_download(repo_id=MODEL_REPO, filename=LABELS_FILE)
19
 
20
  # Load the TFLite model
 
31
 
32
  print(f"Model loaded successfully! Input shape: {input_details[0]['shape']}")
33
  print(f"Number of classes: {len(class_labels)}")
34
+ print(f"Vela-optimized model also available: {VELA_MODEL_FILE}")
35
+ print(f"Repository: {MODEL_REPO}")
36
 
37
  def preprocess_image(image):
38
  """
 
59
 
60
  def classify_image(image):
61
  """
62
+ Classify the input image and return top-3 predictions with confidence scores.
63
  """
64
  if image is None:
65
  return "Please upload an image."
 
97
  # Apply softmax to get probabilities
98
  predictions = tf.nn.softmax(predictions).numpy()
99
 
100
+ # Get top-3 predictions
101
+ top3_indices = np.argsort(predictions)[-3:][::-1]
102
 
103
  # Format results
104
  results = []
105
+ for i, idx in enumerate(top3_indices):
106
  class_name = class_labels[idx] if idx < len(class_labels) else f"Class {idx}"
107
  confidence = predictions[idx]
108
  results.append(f"**{class_name}**: {confidence:.1%}")
109
 
110
  # Create formatted output
111
+ result_text = "\n".join(f"{i+1}. {result}" for i, result in enumerate(results))
112
 
113
  return result_text
114
 
115
  except Exception as e:
116
+ return f"Error processing image: {str(e)}"
117
 
118
  def load_example_image(example_path):
119
  """Load example images for demonstration."""
 
135
 
136
  # Create Gradio interface
137
  with gr.Blocks(
138
+ theme=gr.themes.Default(),
139
+ title="MobileNetV2 Classification",
140
  css="""
141
  .gradio-container {
142
+ max-width: 1200px !important;
143
  margin: auto !important;
144
+ background-color: white !important;
145
  }
146
  .main-header {
147
  text-align: center;
148
  margin-bottom: 2rem;
149
+ color: #1975cf !important;
150
+ font-weight: bold;
 
 
151
  }
152
+ .stats-box {
153
+ background: #1975cf !important;
154
+ color: white !important;
155
+ padding: 1rem;
156
+ border-radius: 8px;
157
+ margin-bottom: 1rem;
158
+ }
159
+ .predictions-box {
160
+ background: #1975cf !important;
161
+ color: white !important;
162
+ padding: 1rem;
163
+ border-radius: 8px;
164
  }
165
+ .upload-section {
166
+ background: #1975cf !important;
167
+ color: white !important;
168
  padding: 1rem;
169
+ border-radius: 8px;
170
+ margin-bottom: 1rem;
171
+ }
172
+ .examples-section {
173
+ background: #1975cf !important;
174
+ color: white !important;
175
+ padding: 1rem;
176
+ border-radius: 8px;
177
+ }
178
+ body {
179
+ background-color: white !important;
180
+ color: black !important;
181
  }
182
  """
183
  ) as demo:
184
 
185
+ with gr.Row():
186
+ with gr.Column(scale=3):
187
+ gr.HTML("""
188
+ <div class="main-header">
189
+ <h1>MobileNetV2 Classification</h1>
190
+ </div>
191
+ """)
192
+ with gr.Column(scale=1):
193
+ model_dropdown = gr.Dropdown(
194
+ choices=["INT8 Model", "Vela Optimized (FVP)"],
195
+ value="INT8 Model",
196
+ label="Model Selection",
197
+ interactive=True
198
+ )
199
 
200
  with gr.Row():
201
  with gr.Column(scale=1):
202
+ gr.HTML("""
203
+ <div class="upload-section">
204
+ <h3 style="margin: 0; color: white;">Upload Image</h3>
205
+ </div>
206
+ """)
207
 
208
  input_image = gr.Image(
209
  label="Choose an image to classify",
 
212
  )
213
 
214
  classify_btn = gr.Button(
215
+ "Classify Image",
216
  variant="primary",
217
  size="lg"
218
  )
219
 
220
  gr.HTML("""
221
+ <div class="examples-section">
222
+ <h3 style="margin: 0; color: white; margin-bottom: 1rem;">Example Images</h3>
223
  </div>
224
  """)
225
 
226
  with gr.Row():
227
  example_cat = gr.Button("Cat", size="sm")
228
  example_dog = gr.Button("Dog", size="sm")
 
229
 
230
  with gr.Row():
231
+ example_car = gr.Button("Car", size="sm")
232
  example_food = gr.Button("Food", size="sm")
 
233
 
234
  with gr.Column(scale=1):
235
+ gr.HTML("""
236
+ <div class="stats-box">
237
+ <h3 style="margin: 0; color: white; margin-bottom: 1rem;">Vela Stats</h3>
238
+ <div style="margin-bottom: 1rem;">
239
+ <strong>Performance:</strong><br>
240
+ • 6M cycles/inference<br>
241
+ • 15.14ms @ 400MHz<br>
242
+ • NPU Coverage: 100%
243
+ </div>
244
+ <div>
245
+ <strong>Memory Usage:</strong><br>
246
+ • SRAM: 353.5 KiB<br>
247
+ • Flash: 3.6 MiB
248
+ </div>
249
+ </div>
250
+ """)
251
 
252
+ gr.HTML("""
253
+ <div class="stats-box">
254
+ <h3 style="margin: 0; color: white; margin-bottom: 1rem;">Model Stats</h3>
255
+ <div style="margin-bottom: 1rem;">
256
+ <strong>Architecture:</strong><br>
257
+ • MobileNetV2<br>
258
+ • INT8 Quantized<br>
259
+ • Input: 224x224x3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
  </div>
261
+ <div>
262
+ <strong>Accuracy:</strong><br>
263
+ • ImageNet Top-1: 69.7%<br>
264
+ • Classes: 1000
265
  </div>
266
  </div>
267
  """)
268
+
269
+ gr.HTML("""
270
+ <div class="predictions-box">
271
+ <h3 style="margin: 0; color: white; margin-bottom: 1rem;">Top 3 Predictions</h3>
272
+ </div>
273
+ """)
274
+
275
+ output_text = gr.Markdown(
276
+ value="Upload an image to see predictions...",
277
+ label=""
278
+ )
279
 
280
  # Set up event handlers
281
  classify_btn.click(
 
289
  example_dog.click(lambda: load_example_image("Dog"), outputs=input_image)
290
  example_car.click(lambda: load_example_image("Car"), outputs=input_image)
291
  example_food.click(lambda: load_example_image("Food"), outputs=input_image)
 
292
 
293
  # Auto-classify when image is uploaded
294
  input_image.change(