TSAI_S12 / app.py
ToletiSri's picture
Update app.py
c7b08d2
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
import custom_resnet
from utils import nn
import gradio as gr
loss_criterion = nn.CrossEntropyLoss() #F.cross_entropy
lr = 0.1
model = custom_resnet.getModel(loss_criterion, lr)
model.load_state_dict(torch.load("saved_model.pth", map_location=torch.device('cpu')), strict=False)
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')
def inference(input_img,num_classes=3,show_gradcam="yes", transparency = 0.5, target_layer_number = -1):
transform = transforms.ToTensor()
org_img = input_img
input_img = transform(input_img)
input_img = input_img
input_img = input_img.unsqueeze(0)
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)}
sorted_confidences = {k: v for k, v in sorted(confidences.items(), key=lambda item: item[1], reverse=True)}
sorted_confidences = dict(list(sorted_confidences.items())[:num_classes])
_, prediction = torch.max(outputs, 1)
target_layers = [model.convblockL3R1[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 = None
if (show_gradcam == "yes") :
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
else :
visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=1)
return sorted_confidences, visualization,
title = "TSAI S12 Assignment: CIFAR10 trained on Custom Model with GradCAM"
description = "A simple Gradio interface to infer on Custom ResNet model, and get GradCAM results. Please use images that belong to any of these classes - 'plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'."
examples = [["cat.jpg",3,"yes", 0.5, -1],
["Elsa.jpg", 3,"yes",0.5, -1],
["horse.jpg", 3,"yes",0.5, -1],
["Frog.png", 3,"yes",0.5, -1],
["Bird.png", 3,"yes",0.5, -1],
["deer.png", 3,"yes",0.5, -1],
["Plane.png", 3,"yes",0.5, -1],
["Ship.png", 3,"yes",0.5, -1],
["car.png", 3,"yes",0.5, -1],
["truck.png", 3,"yes",0.5, -1]
]
demo = gr.Interface(
inference,
inputs = [gr.Image(shape=(32, 32), label="Input Image"), gr.Slider(2, 10, value = 3,step = 1, label="Number of top classes"), gr.Radio(["yes", "no"], label="Show Gradcam"),gr.Slider(0, 1, value = 0.5, label="If yes, Opacity of GradCAM"), gr.Slider(-2, -1, value = -2, step=1, label="If yes, Which Layer?")],
outputs = [gr.Label(num_top_classes=10), gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)],
title = title,
description = description,
examples = examples,
)
demo.launch()