nkanungo's picture
Update app.py
36de997
import torch, torchvision
from torchvision import transforms
import numpy as np
import gradio as gr
from PIL import Image
from pytorch_grad_cam import GradCAM
from pytorch_grad_cam.utils.image import show_cam_on_image
from custom_resnet import Assignment12Resnet
import random
import os
pl_model = Assignment12Resnet.load_from_checkpoint("epoch=22-step=4140.ckpt",map_location=torch.device("cpu"))
inv_normalize = transforms.Normalize(
mean=[-0.50/0.23, -0.50/0.23, -0.50/0.23],
std=[1/0.23, 1/0.23, 1/0.23]
)
classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
model = pl_model.model
model_dict = dict(zip([-3,-2,-1],[pl_model.model.layer3.transition_block.transition_block,pl_model.model.layer3.conv_block1.conv_bn_block,pl_model.model.layer3.conv_block2.conv_bn_block]))
# Function to load images from a folder
def load_images_from_folder(num_misclassified,folder=None):
print(type(num_misclassified))
images = []
for filename in os.listdir(folder):
if filename.endswith(".jpg") or filename.endswith(".png"):
img = Image.open(os.path.join(folder, filename))
images.append(img)
return random.choices(images, k=int(num_misclassified))
def inference(input_img, show_gradcam = True ,num_gradcam_images=1, target_layer_number =-1,opacity= 0.5,show_misclassified = True,num_misclassified_images =10,num_top_classes=3):
#transform = pl_model.test_transform()
org_img = input_img
input_img = pl_model.test_transform(input_img)
input_img = input_img.unsqueeze(0)
model.eval()
outputs = model(input_img)
softmax = torch.nn.Softmax(dim=0)
o = softmax(outputs.flatten())
confidences = {classes[i]: float(o[i]) for i in range(10)}
_, prediction = torch.max(outputs, 1)
if show_gradcam:
target_layers = model_dict[target_layer_number]
cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
grayscale_cam = cam(input_tensor=input_img, targets=None)
grayscale_cam = grayscale_cam[0, :]
img = input_img.squeeze(0)
img = inv_normalize(img)
rgb_img = np.transpose(img, (1, 2, 0))
rgb_img = rgb_img.numpy()
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=opacity)
else:
visualization = org_img
misclassified_images = None
if show_misclassified:
misclassified_images = load_images_from_folder(num_misclassified_images,folder = './misclassified_images')
return confidences, visualization, misclassified_images
title = "CIFAR10 trained on Custom ResNet Model with GradCAM"
description = "A simple Gradio interface to infer on Custom ResNet model and get GradCAM results"
examples = [["cat.jpg",True,1,-2, 0.5, True,5,3], ["dog.jpg",True,1,-2, 0.5, True,5,3 ],["bird.jpg",True,1,-2, 0.5, True,5,3],["ship.jpg",True,1,-2, 0.5, True,5,3],["truck.jpg",True,1,-2, 0.5, True,5,3],["deer.jpg",True,1,-2, 0.5, True,5,3],["frog.jpg",True,1,-2, 0.5, True,5,3],["horse.jpg",True,1,-2, 0.5, True,5,3],["plane.jpg",True,1,-2, 0.5, True,5,3]]
demo = gr.Interface(inference,inputs=[ gr.Image(shape=(32, 32)),
gr.Checkbox(value=True,label="Show GradCAM Images",show_label=True),
gr.Number(value=1, label="Number of GradCAM Images", minimum=1, maximum=1),
gr.Slider(minimum = -3,maximum=-1, value=-1, step=1, label="Which Layer?"),
gr.Slider(minimum =0, maximum = 1.0, value=0.5, label="Opacity of GradCAM"),
gr.Checkbox(label="Show Misclassified Images", value=True,show_label=True),
gr.Number(value=5, label="Number of Misclassified Images (max 10)", minimum=1, maximum=10),
gr.Number(value=3, label="Number of Top Classes (max 10)", minimum=1, maximum=10)
],
outputs=[
gr.Label(num_top_classes=3),
gr.Image(shape=(32, 32), label="Output").style(width=128, height=128),
gr.Gallery(label="Misclassified Images")
],
title=title,
description=description,
examples=examples,
)
# Launch the Gradio interface
demo.launch()