piyushgrover commited on
Commit
51e2303
·
1 Parent(s): d27225f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -34,6 +34,7 @@ sample_images = [
34
  ]
35
 
36
  with gr.Blocks() as app:
 
37
  '''
38
  Select feature interface
39
  '''
@@ -58,52 +59,59 @@ with gr.Blocks() as app:
58
  grad_cam_opacity = gr.Slider(0, 1, value=0.4, step=0.1, label="Choose opacity of the gradient")
59
 
60
  with gr.Column():
61
- grad_cam_btn = gr.Button("Yes, Go Ahead")
62
 
63
  with gr.Column(visible=False) as grad_cam_output:
64
  grad_cam_output_gallery = gr.Gallery(value=[], columns=3, label='Output')
65
  # prediction_title = gr.Label(value='')
66
 
 
67
  '''
68
  Options for Missclassfied images feature
69
  '''
70
  with gr.Row(visible=False) as missclassified_col:
71
  with gr.Row():
72
  missclassified_img_count = gr.Slider(1, 20, value=5, step=1, label="Choose image count",
73
- info="How man missclassified images you want to view?")
74
- missclassified_btn = gr.Button("Click to Continue")
75
  with gr.Row(visible=False) as missclassified_img_output:
76
  missclassified_img_output_gallery = gr.Gallery(value=[], columns=5, label='Output')
77
 
 
78
  '''
79
  Option for Top prediction classes
80
  '''
81
  with gr.Row(visible=True) as top_pred_cls_col:
82
  with gr.Column():
83
- example_images = gr.Gallery(allow_preview=False, label='Select image ', info='',
84
- value=[img[0] for img in sample_images], columns=3, rows=2,
85
- object_fit='scale_down')
86
-
87
  with gr.Column():
88
  with gr.Row():
89
  top_pred_image = gr.Image(shape=(32, 32), label='Upload Image or Select from the gallery')
90
  top_class_count = gr.Slider(1, 10, value=5, step=1, label="Number of classes to predict")
91
- top_class_btn = gr.Button("Submit")
 
92
 
93
  with gr.Row(visible=True) as top_class_output:
94
- # top_class_output_img = gr.Image().style(width=256, height=256)
95
  top_class_output_labels = gr.Label(num_top_classes=top_class_count.value, label='Output')
96
-
97
-
 
 
 
 
 
 
 
 
98
  def on_select(evt: gr.SelectData):
99
  return {
100
  top_pred_image: sample_images[evt.index][0]
101
  }
102
-
103
-
104
  example_images.select(on_select, None, top_pred_image)
105
-
106
-
107
  def top_class_img_upload(input_img, top_class_count):
108
  if input_img is not None:
109
  transform = transforms.ToTensor()
@@ -116,24 +124,25 @@ with gr.Blocks() as app:
116
  o = softmax(outputs.flatten())
117
  confidences = {get_dataset_labels()[i]: float(o[i]) for i in range(10)}
118
  top_class_output_labels.num_top_classes = top_class_count
 
 
119
  return {
120
  top_class_output: gr.update(visible=True),
121
- # top_class_output_img: org_img,
122
  top_class_output_labels: confidences
123
  }
124
-
125
-
126
  top_class_btn.click(
127
  top_class_img_upload,
128
  [top_pred_image, top_class_count],
129
  [top_class_output, top_class_output_labels]
130
  )
131
-
 
 
132
  '''
133
  Missclassified Images feature
134
  '''
135
-
136
-
137
  def show_missclassified_images(img_count):
138
  imgs = []
139
  for i in range(img_count):
@@ -157,13 +166,13 @@ with gr.Blocks() as app:
157
  [missclassified_img_output_gallery, missclassified_img_output]
158
  )
159
 
 
160
  '''
161
  GradCAM Feature
162
  '''
163
-
164
-
165
  def grad_cam_submit(img_count, layer_idx, grad_opacity):
166
-
167
  target_layers = [model.get_layer(-1 * (layer_idx + 1))]
168
  cam = GradCAM(model=model, target_layers=target_layers)
169
 
@@ -204,7 +213,6 @@ with gr.Blocks() as app:
204
  Select Feature to showcase
205
  '''
206
 
207
-
208
  def select_feature(feature):
209
  if feature == 0:
210
  return {
@@ -238,8 +246,8 @@ with gr.Blocks() as app:
238
 
239
  radio_btn.change(select_feature,
240
  [radio_btn],
241
- [grad_cam_col, grad_cam_output, missclassified_col, missclassified_img_output, top_pred_cls_col,
242
- top_class_output])
243
 
244
  '''
245
  Launch the app
 
34
  ]
35
 
36
  with gr.Blocks() as app:
37
+
38
  '''
39
  Select feature interface
40
  '''
 
59
  grad_cam_opacity = gr.Slider(0, 1, value=0.4, step=0.1, label="Choose opacity of the gradient")
60
 
61
  with gr.Column():
62
+ grad_cam_btn = gr.Button("Yes, Go Ahead", variant='primary')
63
 
64
  with gr.Column(visible=False) as grad_cam_output:
65
  grad_cam_output_gallery = gr.Gallery(value=[], columns=3, label='Output')
66
  # prediction_title = gr.Label(value='')
67
 
68
+
69
  '''
70
  Options for Missclassfied images feature
71
  '''
72
  with gr.Row(visible=False) as missclassified_col:
73
  with gr.Row():
74
  missclassified_img_count = gr.Slider(1, 20, value=5, step=1, label="Choose image count",
75
+ info="How many missclassified images you want to view?")
76
+ missclassified_btn = gr.Button("Click to Continue", variant='primary')
77
  with gr.Row(visible=False) as missclassified_img_output:
78
  missclassified_img_output_gallery = gr.Gallery(value=[], columns=5, label='Output')
79
 
80
+
81
  '''
82
  Option for Top prediction classes
83
  '''
84
  with gr.Row(visible=True) as top_pred_cls_col:
85
  with gr.Column():
86
+ example_images = gr.Gallery(allow_preview=False, label='Select image ', info='', value=[img[0] for img in sample_images], columns=3, rows=2, object_fit='scale_down')
87
+
 
 
88
  with gr.Column():
89
  with gr.Row():
90
  top_pred_image = gr.Image(shape=(32, 32), label='Upload Image or Select from the gallery')
91
  top_class_count = gr.Slider(1, 10, value=5, step=1, label="Number of classes to predict")
92
+ top_class_btn = gr.Button("Submit", variant='primary')
93
+ tc_clear_btn = gr.ClearButton()
94
 
95
  with gr.Row(visible=True) as top_class_output:
96
+ #top_class_output_img = gr.Image().style(width=256, height=256)
97
  top_class_output_labels = gr.Label(num_top_classes=top_class_count.value, label='Output')
98
+
99
+
100
+ def clear_data():
101
+ return {
102
+ top_pred_image: None,
103
+ top_class_output_labels: None
104
+ }
105
+
106
+ tc_clear_btn.click(clear_data, None, [top_pred_image, top_class_output_labels])
107
+
108
  def on_select(evt: gr.SelectData):
109
  return {
110
  top_pred_image: sample_images[evt.index][0]
111
  }
112
+
 
113
  example_images.select(on_select, None, top_pred_image)
114
+
 
115
  def top_class_img_upload(input_img, top_class_count):
116
  if input_img is not None:
117
  transform = transforms.ToTensor()
 
124
  o = softmax(outputs.flatten())
125
  confidences = {get_dataset_labels()[i]: float(o[i]) for i in range(10)}
126
  top_class_output_labels.num_top_classes = top_class_count
127
+ #tc_clear_btn.add([top_pred_image, top_class_output_labels])
128
+
129
  return {
130
  top_class_output: gr.update(visible=True),
131
+ #top_class_output_img: org_img,
132
  top_class_output_labels: confidences
133
  }
134
+
 
135
  top_class_btn.click(
136
  top_class_img_upload,
137
  [top_pred_image, top_class_count],
138
  [top_class_output, top_class_output_labels]
139
  )
140
+
141
+
142
+
143
  '''
144
  Missclassified Images feature
145
  '''
 
 
146
  def show_missclassified_images(img_count):
147
  imgs = []
148
  for i in range(img_count):
 
166
  [missclassified_img_output_gallery, missclassified_img_output]
167
  )
168
 
169
+
170
  '''
171
  GradCAM Feature
172
  '''
173
+
 
174
  def grad_cam_submit(img_count, layer_idx, grad_opacity):
175
+
176
  target_layers = [model.get_layer(-1 * (layer_idx + 1))]
177
  cam = GradCAM(model=model, target_layers=target_layers)
178
 
 
213
  Select Feature to showcase
214
  '''
215
 
 
216
  def select_feature(feature):
217
  if feature == 0:
218
  return {
 
246
 
247
  radio_btn.change(select_feature,
248
  [radio_btn],
249
+ [grad_cam_col, grad_cam_output, missclassified_col, missclassified_img_output, top_pred_cls_col, top_class_output])
250
+
251
 
252
  '''
253
  Launch the app