swapniel99 commited on
Commit
d0c355b
·
1 Parent(s): 1fb81b2

update app

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -49,7 +49,7 @@ def image_classifier(input_image, top_classes=3, show_cam=True, target_layer=-2,
49
  targets = [ClassifierOutputTarget(label)]
50
  grayscale_cam = grad_cam(input_tensor=input, targets=targets)
51
  grayscale_cam = grayscale_cam[0, :]
52
- output_image = show_cam_on_image(input_image/255, grayscale_cam, use_rgb=True, image_weight=transparency)
53
 
54
  return output_image if show_cam else input_image, confidences
55
 
@@ -58,10 +58,10 @@ demo1 = gr.Interface(
58
  fn=image_classifier,
59
  inputs=[
60
  gr.Image(shape=(32, 32), label="Input Image", value='examples/cat.jpg'),
61
- gr.Slider(1, 10, value = 3, step=1, label="Number of Top Classes"),
62
  gr.Checkbox(label="Show GradCAM?", value=True),
63
- gr.Slider(-4, -1, value = -2, step=1, label="Which Layer?"),
64
- gr.Slider(0, 1, value = 0.7, label="Transparency", step=0.1)
65
  ],
66
  outputs=[gr.Image(shape=(32, 32), label="Output Image"),
67
  gr.Label(label='Top Classes')],
@@ -69,13 +69,18 @@ demo1 = gr.Interface(
69
  )
70
 
71
 
72
- def show_incorrect(num_examples=10, show_cam=True, target_layer=-1, transparency=0.5):
73
  result = list()
74
- for index, row in missed_df.head(num_examples).iterrows():
75
  image = np.asarray(Image.open(f'missed_examples/{index}.jpg'))
76
- output_image, confidence = image_classifier(image, show_cam=show_cam, target_layer=target_layer, transparency=transparency)
 
 
77
  predicted = list(confidence)[0]
78
- result.append((output_image, f"{row['ground_truths']} / {row['predicted_vals']}"))
 
 
 
79
  return result
80
 
81
 
@@ -84,8 +89,8 @@ demo2 = gr.Interface(
84
  inputs=[
85
  gr.Number(value=20, minimum=1, maximum=len(missed_df), label="No. of missclassified Examples", precision=0),
86
  gr.Checkbox(label="Show GradCAM?", value=True),
87
- gr.Slider(-4, -1, value = -2, step=1, label="Which Layer?"),
88
- gr.Slider(0, 1, value = 0.7, label="Transparency", step=0.1),
89
  ],
90
  outputs=[gr.Gallery(label="Missclassified Images (Truth / Predicted)", columns=4)]
91
  )
 
49
  targets = [ClassifierOutputTarget(label)]
50
  grayscale_cam = grad_cam(input_tensor=input, targets=targets)
51
  grayscale_cam = grayscale_cam[0, :]
52
+ output_image = show_cam_on_image(input_image / 255, grayscale_cam, use_rgb=True, image_weight=transparency)
53
 
54
  return output_image if show_cam else input_image, confidences
55
 
 
58
  fn=image_classifier,
59
  inputs=[
60
  gr.Image(shape=(32, 32), label="Input Image", value='examples/cat.jpg'),
61
+ gr.Slider(1, 10, value=3, step=1, label="Number of Top Classes"),
62
  gr.Checkbox(label="Show GradCAM?", value=True),
63
+ gr.Slider(-4, -1, value=-2, step=1, label="Which Layer?"),
64
+ gr.Slider(0, 1, value=0.7, label="Transparency", step=0.1)
65
  ],
66
  outputs=[gr.Image(shape=(32, 32), label="Output Image"),
67
  gr.Label(label='Top Classes')],
 
69
  )
70
 
71
 
72
+ def show_incorrect(num_examples=20, show_cam=True, target_layer=-1, transparency=0.5):
73
  result = list()
74
+ for index, row in missed_df.iterrows():
75
  image = np.asarray(Image.open(f'missed_examples/{index}.jpg'))
76
+ output_image, confidence = image_classifier(image, show_cam=show_cam, target_layer=target_layer,
77
+ transparency=transparency)
78
+ truth = row['ground_truths']
79
  predicted = list(confidence)[0]
80
+ if truth != predicted:
81
+ result.append((output_image, f"{row['ground_truths']} / {predicted}"))
82
+ if len(result) >= num_examples:
83
+ break
84
  return result
85
 
86
 
 
89
  inputs=[
90
  gr.Number(value=20, minimum=1, maximum=len(missed_df), label="No. of missclassified Examples", precision=0),
91
  gr.Checkbox(label="Show GradCAM?", value=True),
92
+ gr.Slider(-4, -1, value=-2, step=1, label="Which Layer?"),
93
+ gr.Slider(0, 1, value=0.7, label="Transparency", step=0.1),
94
  ],
95
  outputs=[gr.Gallery(label="Missclassified Images (Truth / Predicted)", columns=4)]
96
  )