sanket03 commited on
Commit
998c6fe
·
1 Parent(s): f07e6ce

updated 10 classes examples

Browse files
Files changed (1) hide show
  1. app.py +25 -42
app.py CHANGED
@@ -6,8 +6,6 @@ from PIL import Image
6
  from pytorch_grad_cam import GradCAM
7
  from pytorch_grad_cam.utils.image import show_cam_on_image
8
  from custom_resnet import Net
9
- from PIL import Image
10
- import io
11
 
12
  model = Net('batch')
13
  model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')), strict=False)
@@ -15,37 +13,26 @@ model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')),
15
  classes = ('plane', 'car', 'bird', 'cat', 'deer',
16
  'dog', 'frog', 'horse', 'ship', 'truck')
17
 
18
- def inference(input_img_files, transparency = 0.5, target_layer_number = -1):
19
- confidences_list = []
20
- visualizations_list = []
21
-
22
- for input_img_file in input_img_files:
23
- # Convert the temporary file wrapper to a PIL Image
24
- with open(input_img_file.name, "rb") as f:
25
- input_img = Image.open(io.BytesIO(f.read())).convert("RGB")
26
- transform = transforms.ToTensor()
27
- org_img = input_img
28
- input_img = transform(input_img)
29
- # input_img = input_img
30
- input_img = input_img.unsqueeze(0)
31
- outputs = model(input_img)
32
- softmax = torch.nn.Softmax(dim=0)
33
- o = softmax(outputs.flatten())
34
- confidences = {classes[i]: float(o[i]) for i in range(10)}
35
- confidences_list.append(confidences)
36
-
37
- _, prediction = torch.max(outputs, 1)
38
- target_layers = [model.layer2[target_layer_number]]
39
- cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
40
- grayscale_cam = cam(input_tensor=input_img, targets=None)
41
- grayscale_cam = grayscale_cam[0, :]
42
- img = input_img.squeeze(0)
43
- rgb_img = np.transpose(img, (1, 2, 0))
44
- rgb_img = rgb_img.numpy()
45
- visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
46
- visualizations_list.append(visualization)
47
-
48
- return confidences_list, visualizations_list
49
 
50
  title = "CIFAR10 trained on ResNet18 Model with GradCAM"
51
  description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
@@ -55,15 +42,11 @@ examples = [["airplane.png", 0.5, -1],["bird.jpeg", 0.5, -1], ["car.jpeg", 0.5,
55
 
56
  demo = gr.Interface(
57
  inference,
58
- inputs=[
59
- gr.inputs.File(file_count="multiple"),
60
- gr.Slider(0, 1, value=0.5, label="Opacity of GradCAM"),
61
- gr.Slider(-2, -1, value=-2, step=1, label="Which Layer?")
62
- ],
63
- outputs = [gr.Label(num_top_classes=3), gr.Image(shape=(32, 32), label="Output", style={"width": "128px", "height": "128px"})],
64
- title=title,
65
- description=description,
66
- examples=examples,
67
  )
68
 
69
  demo.launch()
 
6
  from pytorch_grad_cam import GradCAM
7
  from pytorch_grad_cam.utils.image import show_cam_on_image
8
  from custom_resnet import Net
 
 
9
 
10
  model = Net('batch')
11
  model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')), strict=False)
 
13
  classes = ('plane', 'car', 'bird', 'cat', 'deer',
14
  'dog', 'frog', 'horse', 'ship', 'truck')
15
 
16
+ def inference(input_img, transparency = 0.5, target_layer_number = -1):
17
+ transform = transforms.ToTensor()
18
+ org_img = input_img
19
+ input_img = transform(input_img)
20
+ # input_img = input_img
21
+ input_img = input_img.unsqueeze(0)
22
+ outputs = model(input_img)
23
+ softmax = torch.nn.Softmax(dim=0)
24
+ o = softmax(outputs.flatten())
25
+ confidences = {classes[i]: float(o[i]) for i in range(10)}
26
+ _, prediction = torch.max(outputs, 1)
27
+ target_layers = [model.layer2[target_layer_number]]
28
+ cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
29
+ grayscale_cam = cam(input_tensor=input_img, targets=None)
30
+ grayscale_cam = grayscale_cam[0, :]
31
+ img = input_img.squeeze(0)
32
+ rgb_img = np.transpose(img, (1, 2, 0))
33
+ rgb_img = rgb_img.numpy()
34
+ visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
35
+ return confidences, visualization
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  title = "CIFAR10 trained on ResNet18 Model with GradCAM"
38
  description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
 
42
 
43
  demo = gr.Interface(
44
  inference,
45
+ inputs = [gr.Image(shape=(32, 32), label="Input Image"), gr.Slider(0, 1, value = 0.5, label="Opacity of GradCAM"), gr.Slider(-2, -1, value = -2, step=1, label="Which Layer?")],
46
+ outputs = [gr.Label(num_top_classes=3), gr.Image(shape=(32, 32), label="Output", style={"width": "128px", "height": "128px"})],
47
+ title = title,
48
+ description = description,
49
+ examples = examples,
 
 
 
 
50
  )
51
 
52
  demo.launch()