Update app.py
Browse files
app.py
CHANGED
|
@@ -6,48 +6,44 @@ from PIL import Image
|
|
| 6 |
from pytorch_grad_cam import GradCAM
|
| 7 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 8 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
from model import YOLOv3
|
| 11 |
import config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
model = YOLOv3(num_classes=config.NUM_CLASSES)
|
| 14 |
model.load_state_dict(torch.load("checkpoint.pth.tar", map_location=torch.device('cpu')), strict=False)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
-
classes = ('plane', 'car', 'bird', 'cat', 'deer',
|
| 21 |
-
'dog', 'frog', 'horse', 'ship', 'truck')
|
| 22 |
|
| 23 |
def inference(input_img,show_gradcam="yes", transparency = 0.5, target_layer_number = -1):
|
|
|
|
| 24 |
transform = transforms.ToTensor()
|
| 25 |
org_img = input_img
|
| 26 |
input_img = transform(input_img)
|
| 27 |
input_img = input_img
|
| 28 |
input_img = input_img.unsqueeze(0)
|
| 29 |
-
model.
|
| 30 |
-
|
| 31 |
-
softmax = torch.nn.Softmax(dim=0)
|
| 32 |
-
#o = softmax(outputs.flatten())
|
| 33 |
-
#confidences = {classes[i]: float(o[i]) for i in range(10)}
|
| 34 |
-
#sorted_confidences = {k: v for k, v in sorted(confidences.items(), key=lambda item: item[1], reverse=True)}
|
| 35 |
-
#sorted_confidences = dict(list(sorted_confidences.items())[:num_classes])
|
| 36 |
-
#_, prediction = torch.max(outputs, 1)
|
| 37 |
-
#target_layers = [model.convblockL3R1[target_layer_number]]
|
| 38 |
-
#cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
|
| 39 |
-
#grayscale_cam = cam(input_tensor=input_img, targets=None)
|
| 40 |
-
#grayscale_cam = grayscale_cam[0, :]
|
| 41 |
-
#img = input_img.squeeze(0)
|
| 42 |
-
#img = inv_normalize(img)
|
| 43 |
-
#rgb_img = np.transpose(img, (1, 2, 0))
|
| 44 |
-
#rgb_img = rgb_img.numpy()
|
| 45 |
-
#visualization = None
|
| 46 |
-
#if (show_gradcam == "yes") :
|
| 47 |
-
# visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
|
| 48 |
-
#else :
|
| 49 |
-
# visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=1)
|
| 50 |
-
return input_img, org_img
|
| 51 |
|
| 52 |
title = "TSAI S13 Assignment: YOLO V3 trained on PASCAL VOC Dataset"
|
| 53 |
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'."
|
|
@@ -56,7 +52,7 @@ examples = [["cat.jpg","yes", 0.5, -1]
|
|
| 56 |
demo = gr.Interface(
|
| 57 |
inference,
|
| 58 |
inputs = [gr.Image(shape=(416, 416), label="Input Image"), 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?")],
|
| 59 |
-
outputs = [
|
| 60 |
title = title,
|
| 61 |
description = description,
|
| 62 |
examples = examples,
|
|
|
|
| 6 |
from pytorch_grad_cam import GradCAM
|
| 7 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 8 |
import gradio as gr
|
| 9 |
+
import albumentations as A
|
| 10 |
+
from albumentations.pytorch import ToTensorV2
|
| 11 |
+
import config
|
| 12 |
+
|
| 13 |
+
from utils import plot_single_image
|
| 14 |
|
| 15 |
from model import YOLOv3
|
| 16 |
import config
|
| 17 |
+
from torchvision import transforms
|
| 18 |
+
|
| 19 |
+
scaled_anchors = (
|
| 20 |
+
torch.tensor(config.ANCHORS)
|
| 21 |
+
* torch.tensor(config.S).unsqueeze(1).unsqueeze(1).repeat(1, 3, 2)
|
| 22 |
+
).to('cpu')
|
| 23 |
|
| 24 |
model = YOLOv3(num_classes=config.NUM_CLASSES)
|
| 25 |
model.load_state_dict(torch.load("checkpoint.pth.tar", map_location=torch.device('cpu')), strict=False)
|
| 26 |
|
| 27 |
+
test_transforms_exp = A.Compose(
|
| 28 |
+
[
|
| 29 |
+
A.LongestMaxSize(max_size=config.IMAGE_SIZE),
|
| 30 |
+
A.PadIfNeeded(
|
| 31 |
+
min_height=config.IMAGE_SIZE, min_width=config.IMAGE_SIZE
|
| 32 |
+
),
|
| 33 |
+
A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
|
| 34 |
+
ToTensorV2(),
|
| 35 |
+
]
|
| 36 |
)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def inference(input_img,show_gradcam="yes", transparency = 0.5, target_layer_number = -1):
|
| 39 |
+
|
| 40 |
transform = transforms.ToTensor()
|
| 41 |
org_img = input_img
|
| 42 |
input_img = transform(input_img)
|
| 43 |
input_img = input_img
|
| 44 |
input_img = input_img.unsqueeze(0)
|
| 45 |
+
out_fig = plot_single_image(model, input_img, 0.6, 0.5,scaled_anchors)
|
| 46 |
+
return out_fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
title = "TSAI S13 Assignment: YOLO V3 trained on PASCAL VOC Dataset"
|
| 49 |
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'."
|
|
|
|
| 52 |
demo = gr.Interface(
|
| 53 |
inference,
|
| 54 |
inputs = [gr.Image(shape=(416, 416), label="Input Image"), 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?")],
|
| 55 |
+
outputs = [gr.Plot(label="Plot")],
|
| 56 |
title = title,
|
| 57 |
description = description,
|
| 58 |
examples = examples,
|