theschoolofai commited on
Commit
e488a16
·
1 Parent(s): 4501c57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -2
app.py CHANGED
@@ -25,6 +25,9 @@ def inference(input_img, transparency = 0.5, target_layer_number = -1):
25
  input_img = input_img
26
  input_img = input_img.unsqueeze(0)
27
  outputs = model(input_img)
 
 
 
28
  _, prediction = torch.max(outputs, 1)
29
  target_layers = [model.layer2[target_layer_number]]
30
  cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
@@ -35,7 +38,7 @@ def inference(input_img, transparency = 0.5, target_layer_number = -1):
35
  rgb_img = np.transpose(img, (1, 2, 0))
36
  rgb_img = rgb_img.numpy()
37
  visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
38
- return classes[prediction[0].item()], visualization
39
 
40
  title = "CIFAR10 trained on ResNet18 Model with GradCAM"
41
  description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
@@ -43,7 +46,7 @@ examples = [["cat.jpg", 0.5, -1], ["dog.jpg", 0.5, -1]]
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 = ["text", gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)],
47
  title = title,
48
  description = description,
49
  examples = examples,
 
25
  input_img = input_img
26
  input_img = input_img.unsqueeze(0)
27
  outputs = model(input_img)
28
+ softmax = torch.nn.Softmax(dim=0)
29
+ o = softmax(outputs.flatten())
30
+ confidences = {classes[i]: float(o[i]) for i in range(10)}
31
  _, prediction = torch.max(outputs, 1)
32
  target_layers = [model.layer2[target_layer_number]]
33
  cam = GradCAM(model=model, target_layers=target_layers, use_cuda=False)
 
38
  rgb_img = np.transpose(img, (1, 2, 0))
39
  rgb_img = rgb_img.numpy()
40
  visualization = show_cam_on_image(org_img/255, grayscale_cam, use_rgb=True, image_weight=transparency)
41
+ return confidences, visualization
42
 
43
  title = "CIFAR10 trained on ResNet18 Model with GradCAM"
44
  description = "A simple Gradio interface to infer on ResNet model, and get GradCAM results"
 
46
  demo = gr.Interface(
47
  inference,
48
  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?")],
49
+ outputs = [gr.Label(num_top_classes=3), gr.Image(shape=(32, 32), label="Output").style(width=128, height=128)],
50
  title = title,
51
  description = description,
52
  examples = examples,