Mojo
commited on
Commit
·
f4156d7
1
Parent(s):
62ad401
Added files
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from torchvision import transforms
|
|
8 |
import modules.config as config
|
9 |
import numpy as np
|
10 |
import torch
|
|
|
11 |
|
12 |
|
13 |
TITLE = "CIFAR10 Image classification using a Custom ResNet Model"
|
@@ -118,7 +119,7 @@ def app_interface(
|
|
118 |
num_gradcam_misclassified,
|
119 |
):
|
120 |
"""Function which provides the Gradio interface"""
|
121 |
-
|
122 |
# Get the prediction for the input image along with confidence and display_image
|
123 |
confidences, display_image = generate_prediction(input_image, num_classes, show_gradcam, transparency, layer_name)
|
124 |
|
@@ -150,6 +151,26 @@ def app_interface(
|
|
150 |
|
151 |
return confidences, display_image, misclassified_fig, gradcam_fig
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
|
155 |
|
@@ -157,7 +178,7 @@ inference_app = gr.Interface(
|
|
157 |
app_interface,
|
158 |
inputs=[
|
159 |
# This accepts the image after resizing it to 32x32 which is what our model expects
|
160 |
-
gr.Image(
|
161 |
gr.Number(value=3, maximum=10, minimum=1, step=1.0, precision=0, label="#Classes to show"),
|
162 |
gr.Checkbox(True, label="Show GradCAM Image"),
|
163 |
gr.Dropdown(model_layer_names, value="layer3_x", label="Visulalization Layer from Model"),
|
|
|
8 |
import modules.config as config
|
9 |
import numpy as np
|
10 |
import torch
|
11 |
+
from PIL import Image
|
12 |
|
13 |
|
14 |
TITLE = "CIFAR10 Image classification using a Custom ResNet Model"
|
|
|
119 |
num_gradcam_misclassified,
|
120 |
):
|
121 |
"""Function which provides the Gradio interface"""
|
122 |
+
input_img = resize_image_pil(input_img, 32, 32)
|
123 |
# Get the prediction for the input image along with confidence and display_image
|
124 |
confidences, display_image = generate_prediction(input_image, num_classes, show_gradcam, transparency, layer_name)
|
125 |
|
|
|
151 |
|
152 |
return confidences, display_image, misclassified_fig, gradcam_fig
|
153 |
|
154 |
+
def resize_image_pil(image, new_width, new_height):
|
155 |
+
|
156 |
+
# Convert to PIL image
|
157 |
+
img = Image.fromarray(np.array(image))
|
158 |
+
|
159 |
+
# Get original size
|
160 |
+
width, height = img.size
|
161 |
+
|
162 |
+
# Calculate scale
|
163 |
+
width_scale = new_width / width
|
164 |
+
height_scale = new_height / height
|
165 |
+
scale = min(width_scale, height_scale)
|
166 |
+
|
167 |
+
# Resize
|
168 |
+
resized = img.resize((int(width*scale), int(height*scale)), Image.NEAREST)
|
169 |
+
|
170 |
+
# Crop to exact size
|
171 |
+
resized = resized.crop((0, 0, new_width, new_height))
|
172 |
+
|
173 |
+
return resized
|
174 |
|
175 |
|
176 |
|
|
|
178 |
app_interface,
|
179 |
inputs=[
|
180 |
# This accepts the image after resizing it to 32x32 which is what our model expects
|
181 |
+
gr.Image(width=256, height=256, label="Input Image"),
|
182 |
gr.Number(value=3, maximum=10, minimum=1, step=1.0, precision=0, label="#Classes to show"),
|
183 |
gr.Checkbox(True, label="Show GradCAM Image"),
|
184 |
gr.Dropdown(model_layer_names, value="layer3_x", label="Visulalization Layer from Model"),
|