Spaces:
Runtime error
Runtime error
img classification
Browse files- .gitignore +2 -0
- app-2.py +65 -0
- app.py +49 -13
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/app.cpython-310.pyc
|
| 2 |
+
data
|
app-2.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Importing necessary libraries
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from torchvision.models import resnet18
|
| 6 |
+
from torchvision.datasets import CIFAR10
|
| 7 |
+
from torch.nn import functional as F
|
| 8 |
+
import numpy as np
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
# Load CIFAR10 pretrained model
|
| 13 |
+
model = resnet18(pretrained=True)
|
| 14 |
+
model.eval()
|
| 15 |
+
|
| 16 |
+
# Define transformation for CIFAR10
|
| 17 |
+
transform = transforms.Compose([
|
| 18 |
+
transforms.Resize((224, 224)),
|
| 19 |
+
transforms.ToTensor(),
|
| 20 |
+
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
| 21 |
+
])
|
| 22 |
+
|
| 23 |
+
# Load CIFAR10 dataset for example images
|
| 24 |
+
cifar10_dataset = CIFAR10(root='./data', train=False, download=True, transform=transform)
|
| 25 |
+
example_images = [cifar10_dataset[i][0] for i in range(10)]
|
| 26 |
+
|
| 27 |
+
# Define class names for CIFAR10
|
| 28 |
+
class_names = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
|
| 29 |
+
|
| 30 |
+
def predict(img, gradcam, num_gradcam, layer, opacity, misclassified, num_misclassified, top_classes):
|
| 31 |
+
# Transform and predict
|
| 32 |
+
img_tensor = transform(img).unsqueeze(0)
|
| 33 |
+
outputs = model(img_tensor)
|
| 34 |
+
_, predicted = outputs.max(1)
|
| 35 |
+
probs = F.softmax(outputs, dim=1)[0] * 100
|
| 36 |
+
|
| 37 |
+
# Get top classes
|
| 38 |
+
top_probs, top_labels = torch.topk(probs, min(top_classes, 10))
|
| 39 |
+
top_classes = [class_names[label] for label in top_labels]
|
| 40 |
+
|
| 41 |
+
# GradCAM
|
| 42 |
+
gradcam_images = []
|
| 43 |
+
if gradcam:
|
| 44 |
+
# TODO: Implement GradCAM
|
| 45 |
+
pass
|
| 46 |
+
|
| 47 |
+
# Misclassified images
|
| 48 |
+
misclassified_images = []
|
| 49 |
+
if misclassified:
|
| 50 |
+
# TODO: Get misclassified images
|
| 51 |
+
pass
|
| 52 |
+
|
| 53 |
+
return {'Prediction': top_classes, 'Probabilities': top_probs.tolist(), 'GradCAM': gradcam_images, 'Misclassified': misclassified_images}
|
| 54 |
+
|
| 55 |
+
# Gradio Interface
|
| 56 |
+
image = gr.inputs.Image()
|
| 57 |
+
gradcam = gr.inputs.Checkbox(label='Show GradCAM Images')
|
| 58 |
+
num_gradcam = gr.inputs.Number(label='Number of GradCAM Images', default=1, minimum=1, maximum=10)
|
| 59 |
+
layer = gr.inputs.Dropdown(choices=['layer1', 'layer2', 'layer3', 'layer4'], label='GradCAM Layer')
|
| 60 |
+
opacity = gr.inputs.Slider(minimum=0, maximum=1, default=0.5, label='Opacity')
|
| 61 |
+
misclassified = gr.inputs.Checkbox(label='Show Misclassified Images')
|
| 62 |
+
num_misclassified = gr.inputs.Number(label='Number of Misclassified Images', default=1, minimum=1, maximum=10)
|
| 63 |
+
top_classes = gr.inputs.Number(label='Number of Top Classes to Show', default=1, minimum=1, maximum=10)
|
| 64 |
+
|
| 65 |
+
gr.Interface(fn=predict, inputs=[image, gradcam, num_gradcam, layer, opacity, misclassified, num_misclassified, top_classes], outputs='json').launch()
|
app.py
CHANGED
|
@@ -1,15 +1,51 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
sepia_filter = np.array([
|
| 6 |
-
[0.393, 0.769, 0.189],
|
| 7 |
-
[0.349, 0.686, 0.168],
|
| 8 |
-
[0.272, 0.534, 0.131]
|
| 9 |
-
])
|
| 10 |
-
sepia_img = input_img.dot(sepia_filter.T)
|
| 11 |
-
sepia_img /= sepia_img.max()
|
| 12 |
-
return sepia_img
|
| 13 |
-
|
| 14 |
-
iface = gr.Interface(sepia, gr.Image(shape=(200, 200)), "image")
|
| 15 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
# Stub function for model inference
|
| 5 |
+
def cifar10_inference(image, gradcam, num_gradcam, layer, opacity, view_misclassified, num_misclassified, top_classes):
|
| 6 |
+
# Placeholder for model inference
|
| 7 |
+
# You would replace this with your actual model inference code
|
| 8 |
+
predictions = {"class_1": 0.1, "class_2": 0.2} # Example prediction dictionary
|
| 9 |
+
|
| 10 |
+
# Placeholder for GradCAM images
|
| 11 |
+
gradcam_images = []
|
| 12 |
+
if gradcam:
|
| 13 |
+
# Generate GradCAM images for the specified layer and number
|
| 14 |
+
for i in range(num_gradcam):
|
| 15 |
+
gradcam_images.append(np.random.rand(32, 32, 3)) # Example random image
|
| 16 |
+
|
| 17 |
+
# Placeholder for misclassified images
|
| 18 |
+
misclassified_images = []
|
| 19 |
+
if view_misclassified:
|
| 20 |
+
# Get misclassified images
|
| 21 |
+
for i in range(num_misclassified):
|
| 22 |
+
misclassified_images.append(np.random.rand(32, 32, 3)) # Example random image
|
| 23 |
+
|
| 24 |
+
return predictions, gradcam_images, misclassified_images
|
| 25 |
+
|
| 26 |
+
# Gradio interface
|
| 27 |
+
iface = gr.Interface(
|
| 28 |
+
fn=cifar10_inference,
|
| 29 |
+
inputs=[
|
| 30 |
+
gr.inputs.Image(shape=(32, 32), label="Upload Image"),
|
| 31 |
+
gr.inputs.Checkbox(label="View GradCAM images?"),
|
| 32 |
+
gr.inputs.Number(default=1, label="Number of GradCAM images", min=1, max=10),
|
| 33 |
+
gr.inputs.Dropdown(choices=["layer_1", "layer_2"], label="Choose a layer for GradCAM"), # Replace with your layer names
|
| 34 |
+
gr.inputs.Slider(minimum=0, maximum=1, default=0.5, label="Opacity"),
|
| 35 |
+
gr.inputs.Checkbox(label="View misclassified images?"),
|
| 36 |
+
gr.inputs.Number(default=1, label="Number of misclassified images", min=1, max=10),
|
| 37 |
+
gr.inputs.Number(default=1, label="Number of top classes to show", min=1, max=10),
|
| 38 |
+
],
|
| 39 |
+
outputs=[
|
| 40 |
+
gr.outputs.Label(num_top_classes=10, label="Predictions"),
|
| 41 |
+
gr.outputs.Image(type="numpy", label="GradCAM Images"),
|
| 42 |
+
gr.outputs.Image(type="numpy", label="Misclassified Images"),
|
| 43 |
+
],
|
| 44 |
+
examples=[
|
| 45 |
+
# Add paths to your 10 example images
|
| 46 |
+
["example1.jpg", False, 1, "layer_1", 0.5, False, 1, 3],
|
| 47 |
+
["example2.jpg", False, 1, "layer_1", 0.5, False, 1, 3],
|
| 48 |
+
]
|
| 49 |
+
)
|
| 50 |
|
| 51 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|