ludusc commited on
Commit
266a83c
1 Parent(s): 50eece1

added changed images

Browse files
backend/disentangle_concepts.py CHANGED
@@ -15,10 +15,10 @@ def get_separation_space(type_bin, annotations, df):
15
  svc = SVC(gamma='auto', kernel='linear')
16
  svc.fit(x_train, y_train)
17
  print(svc.score(x_val, y_val))
18
- imp_features = (np.abs(svc.coef_) > 0.01).sum()
19
  return svc.coef_, imp_features
20
 
21
- def regenerate_images(model, z, decision_boundary, min_epsilon=-3, max_epsilon=3, count=11):
22
  device = torch.device('cpu')
23
  G = model.to(device) # type: ignore
24
 
@@ -28,7 +28,7 @@ def regenerate_images(model, z, decision_boundary, min_epsilon=-3, max_epsilon=3
28
  z = torch.from_numpy(z.copy()).to(device)
29
  decision_boundary = torch.from_numpy(decision_boundary.copy()).to(device)
30
 
31
- lambdas = np.linspace(-3, 3, 11)
32
  images = []
33
  # Generate images.
34
  for _, lambda_ in enumerate(lambdas):
@@ -44,7 +44,8 @@ def regenerate_images(model, z, decision_boundary, min_epsilon=-3, max_epsilon=3
44
  img = G(z_0, label, truncation_psi=0.7, noise_mode='random')
45
  img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
46
  images.append(PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB'))
47
- return images
 
48
 
49
  def generate_original_image(z, model):
50
  device = torch.device('cpu')
 
15
  svc = SVC(gamma='auto', kernel='linear')
16
  svc.fit(x_train, y_train)
17
  print(svc.score(x_val, y_val))
18
+ imp_features = (np.abs(svc.coef_) > 0.1).sum()
19
  return svc.coef_, imp_features
20
 
21
+ def regenerate_images(model, z, decision_boundary, min_epsilon=-3, max_epsilon=3, count=5):
22
  device = torch.device('cpu')
23
  G = model.to(device) # type: ignore
24
 
 
28
  z = torch.from_numpy(z.copy()).to(device)
29
  decision_boundary = torch.from_numpy(decision_boundary.copy()).to(device)
30
 
31
+ lambdas = np.linspace(min_epsilon, max_epsilon, count)
32
  images = []
33
  # Generate images.
34
  for _, lambda_ in enumerate(lambdas):
 
44
  img = G(z_0, label, truncation_psi=0.7, noise_mode='random')
45
  img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
46
  images.append(PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB'))
47
+
48
+ return images, lambdas
49
 
50
  def generate_original_image(z, model):
51
  device = torch.device('cpu')
pages/1_Disentanglement.py CHANGED
@@ -84,8 +84,10 @@ st.header('Output')
84
  st.subheader('Concept vector')
85
 
86
  # perform attack container
87
- header_col_1, header_col_2, header_col_3, header_col_4, header_col_5 = st.columns([1,1,1,1,1])
88
- output_col_1, output_col_2, output_col_3, output_col_4, output_col_5 = st.columns([1,1,1,1,1])
 
 
89
 
90
  # prediction error container
91
  error_container = st.empty()
@@ -115,7 +117,7 @@ with input_col_2:
115
  random_id = st.form_submit_button('Generate a random image')
116
 
117
  if random_id:
118
- image_id = random.randint(0, 50000)
119
  st.session_state.image_id = image_id
120
  chosen_image_id_input.number_input('Image ID:', format='%d', step=1, value=st.session_state.image_id)
121
 
@@ -128,7 +130,7 @@ with input_col_3:
128
  with st.form('Variate along the disentangled concept'):
129
  st.write('**Set range of change**')
130
  chosen_epsilon_input = st.empty()
131
- epsilon = chosen_epsilon_input.number_input('Epsilon:', min_value=1, format='%.1f')
132
  epsilon_button = st.form_submit_button('Choose the defined epsilon')
133
 
134
  # ---------------------------- DISPLAY COL 2 ROW 1 ------------------------------
@@ -142,83 +144,23 @@ img = generate_original_image(original_image_vec, model)
142
 
143
  with smoothgrad_col_3:
144
  st.image(img)
145
- header_col_1.write(f'Base image')
146
 
147
 
 
148
 
149
- # if pred_class_id == (input_id-1) and (epsilon_button or find_epsilon or setting_button):
150
- # with output_col_3:
151
- # if epsilon_button:
152
- # perturbed_data, new_prob, new_id, new_label = perform_attack(input_image, input_id-1, epsilon)
153
- # else:
154
- # epsilons = [i*step_epsilon for i in range(1, 1001) if i*step_epsilon <= max_epsilon]
155
- # with epsilon_container.container():
156
- # epsilon_container_text = 'Checking epsilon'
157
- # st.write(epsilon_container_text)
158
- # st.progress(0)
159
-
160
- # for i, e in enumerate(epsilons):
161
-
162
- # perturbed_data, new_prob, new_id, new_label = perform_attack(input_image, input_id-1, e)
163
- # with epsilon_container.container():
164
- # epsilon_container_text = f'Checking epsilon={e:.3f}. Confidence={new_prob*100:.1f}%'
165
- # st.write(epsilon_container_text)
166
- # st.progress(i/len(epsilons))
167
-
168
- # epsilon = e
169
-
170
- # if new_id != input_id - 1:
171
- # epsilon_container.empty()
172
- # st.balloons()
173
- # break
174
- # if i == len(epsilons)-1:
175
- # epsilon_container.error(f'FGSM failed to attack on this image at epsilon={e:.3f}. Set higher maximum value of epsilon or choose another image',
176
- # icon = "🚫")
177
-
178
- # perturbed_image = deprocess_image(perturbed_data.detach().numpy())[0].astype(np.uint8).transpose(1,2,0)
179
- # perturbed_amount = perturbed_image - input_image
180
- # header_col_3.write(f'Pertubed amount - epsilon={epsilon:.3f}')
181
- # st.image(ShowImage(perturbed_amount))
182
-
183
- # with output_col_2:
184
- # # st.write('plus sign')
185
- # st.image(LoadImage('frontend/images/plus-sign.png'))
186
-
187
- # with output_col_4:
188
- # # st.write('equal sign')
189
- # st.image(LoadImage('frontend/images/equal-sign.png'))
190
-
191
- # # ---------------------------- DISPLAY COL 5 ROW 1 ------------------------------
192
- # with output_col_5:
193
- # # st.write(f'ID {new_id+1} - {new_label}: {new_prob*100:.3f}% confidence')
194
- # st.image(ShowImage(perturbed_image))
195
- # header_col_5.write(f'Class ID {new_id+1} - {new_label}: {new_prob*100:.1f}% confidence')
196
-
197
- # # -------------------------- DISPLAY SMOOTHGRAD ---------------------------
198
- # smoothgrad_header_container.subheader('SmoothGrad visualization')
199
-
200
- # with smoothgrad_col_1:
201
- # smooth_head_1.write(f'SmoothGrad before attacked')
202
- # heatmap_image, masked_image, mask = generate_images(st.session_state.image_id, epsilon=0)
203
- # st.image(heatmap_image)
204
- # st.image(masked_image)
205
- # with smoothgrad_col_3:
206
- # smooth_head_3.write('SmoothGrad after attacked')
207
- # heatmap_image_attacked, masked_image_attacked, attacked_mask= generate_images(st.session_state.image_id, epsilon=epsilon)
208
- # st.image(heatmap_image_attacked)
209
- # st.image(masked_image_attacked)
210
-
211
- # with smoothgrad_col_2:
212
- # st.image(LoadImage('frontend/images/minus-sign-5.png'))
213
-
214
- # with smoothgrad_col_5:
215
- # smooth_head_5.write('SmoothGrad difference')
216
- # difference_mask = abs(attacked_mask-mask)
217
- # st.image(ShowHeatMap(difference_mask))
218
- # masked_image = ShowMaskedImage(difference_mask, perturbed_image)
219
- # st.image(masked_image)
220
 
221
- # with smoothgrad_col_4:
222
- # st.image(LoadImage('frontend/images/equal-sign.png'))
 
223
 
 
 
 
224
 
 
 
 
 
84
  st.subheader('Concept vector')
85
 
86
  # perform attack container
87
+ # header_col_1, header_col_2, header_col_3, header_col_4, header_col_5 = st.columns([1,1,1,1,1])
88
+ # output_col_1, output_col_2, output_col_3, output_col_4, output_col_5 = st.columns([1,1,1,1,1])
89
+ header_col_1 = st.columns([1])
90
+ output_col_1 = st.columns([1])
91
 
92
  # prediction error container
93
  error_container = st.empty()
 
117
  random_id = st.form_submit_button('Generate a random image')
118
 
119
  if random_id:
120
+ image_id = random.randint(0, 10000)
121
  st.session_state.image_id = image_id
122
  chosen_image_id_input.number_input('Image ID:', format='%d', step=1, value=st.session_state.image_id)
123
 
 
130
  with st.form('Variate along the disentangled concept'):
131
  st.write('**Set range of change**')
132
  chosen_epsilon_input = st.empty()
133
+ epsilon = chosen_epsilon_input.number_input('Epsilon:', min_value=1, step=1)
134
  epsilon_button = st.form_submit_button('Choose the defined epsilon')
135
 
136
  # ---------------------------- DISPLAY COL 2 ROW 1 ------------------------------
 
144
 
145
  with smoothgrad_col_3:
146
  st.image(img)
147
+ smooth_head_3.write(f'Base image')
148
 
149
 
150
+ images, lambdas = regenerate_images(model, original_image_vec, separation_vector, min_epsilon=-(int(epsilon)), max_epsilon=int(epsilon))
151
 
152
+ with smoothgrad_col_1:
153
+ st.image(images[0])
154
+ smooth_head_1.write(f'Change of {np.round(lambdas[0], 2)}')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
+ with smoothgrad_col_2:
157
+ st.image(images[1])
158
+ smooth_head_2.write(f'Change of {np.round(lambdas[2], 2)}')
159
 
160
+ with smoothgrad_col_4:
161
+ st.image(images[3])
162
+ smooth_head_4.write(f'Change of {np.round(lambdas[3], 2)}')
163
 
164
+ with smoothgrad_col_5:
165
+ st.image(images[4])
166
+ smooth_head_5.write(f'Change of {np.round(lambdas[4], 2)}')