-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_stimuli_levels.py
executable file
·300 lines (228 loc) · 11.7 KB
/
plot_stimuli_levels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
import math
from PIL import Image
from skimage.io import imread, imshow
from matplotlib.font_manager import FontProperties
from matplotlib import cm, colors
from scipy import ndimage
from skimage.color import rgb2grey
from pylab import *
# import manipulation functions
import sys
sys.path.append('../../code')
from image_manipulation import (high_pass_filter, low_pass_filter,
phase_scrambling, salt_and_pepper_noise,
false_colour, power_equalisation,
grayscale_contrast, uniform_noise,
eidolon_partially_coherent_disarray)
"""Functionality to plot stimuli at a certain level of manipulation."""
humans = '##A51E37'
googlenet = "#50AAC8"
vgg19 = "#0069AA"
resnet152 = "#415A8C"
plot_colors = [humans, googlenet, vgg19, resnet152]
def plot_stimuli_all_conditions(imglist, stimulus_levels,
img_manip_func, multiply_labels_by=1,
labels_to_int=True,
ylabel=None, filename=None,
thresholds=None, set_vmin_vmax=True,
rotate_angle=0,
reduced_space=False,
is_eidolon=False):
"""Plot a nxm matrix of n stimulus_levels for m images.
parameters:
- imglist: vector of images
- stimulus_levels: numeric vector of input values to img_manip_func
- img_manip_func: Function(image, stimulus_level) -> manipulated img
- labels_to_int: multiply labels by 100 and convert them to int?
- ylabel: plot y-axis-label
- filename: where the resulting plot should be saved
- thresholds: optional list of stimulus levels that should be plotted in
color. The order is important:
[human_observer, GoogLeNet, VGG-19, ResNet-152]
- reduced_space: reduce plotting area for paper
"""
num_imgs = len(imglist)
num_stimuli = len(stimulus_levels)
offset = 0
if thresholds is not None:
assert(len(thresholds) == 4), "len (thresholds) needs to be 4"
if len(stimulus_levels) is len(thresholds):
offset = 0.3 # add some more vspace
fig = plt.figure(figsize=(-0.2+2*len(imglist),len(stimulus_levels)*1.6+offset))
fig.subplots_adjust(top=0.98, bottom=0.02,
right=0.93, left=0.09,
hspace=0.05)
counter = 0
def plot_rectangle(color, subplot):
autoAxis = subplot.axis()
rec = Rectangle((autoAxis[0]-0.7,autoAxis[2]-0.2),(autoAxis[1]-autoAxis[0])+1,(autoAxis[3]-autoAxis[2])+0.4,fill=False,lw=5, color=color)
rec = subplot.add_patch(rec)
rec.set_clip_on(False)
for stimulus_counter, s in enumerate(stimulus_levels):
for i, img in enumerate(imglist):
subplot = fig.add_subplot(num_stimuli,num_imgs, counter+1)
######break inserted here
if not is_eidolon:
# clip to 0..1 range
assert np.allclose(img[img < 0], 0) and np.allclose(img[img > 1], 1)
img[img < 0] = 0
img[img > 1] = 1
if set_vmin_vmax:
plt.imshow(ndimage.rotate(img_manip_func(img, s), rotate_angle),
cmap="gray", vmin=0.0, vmax=1.0)
else:
plt.imshow(ndimage.rotate(img_manip_func(img, s), rotate_angle),
cmap="gray")
subplot.get_yaxis().set_ticks([])
subplot.get_xaxis().set_ticks([])
if thresholds is not None:
if s in thresholds:
plot_rectangle(plot_colors[stimulus_counter], subplot)
if i is 0:
if labels_to_int:
subplot.set_ylabel(str(int(s*multiply_labels_by)),
fontsize=12)#, fontsize=16)
else:
subplot.set_ylabel(str(s*multiply_labels_by), fontsize=12)#, fontsize=16)
counter += 1
if ylabel is None:
ylabel = ""
fig.text(0.01, 0.5, ylabel, va='center', rotation='vertical', fontsize=12)#, fontsize=16)
#fig.tight_layout()
#plt.show()
if filename is None:
plt.show()
else:
plt.savefig(filename)
def main(number):
print("---main executing---")
im1_col = imread("../randomly_selected_imgs/n03792782_1155_224x224.JPEG") / 255.0
im2_col = imread("../randomly_selected_imgs/n02099601_634_224x224.JPEG") / 255.0
im3_col = imread("../randomly_selected_imgs/n04505470_10690_224x224.JPEG") / 255.0
im1 = rgb2grey(im1_col)
im2 = rgb2grey(im2_col)
im3 = rgb2grey(im3_col)
im1_gray = imread("../randomly_selected_imgs/random_bicycle.JPEG")
im2_gray = imread("../randomly_selected_imgs/random_dog.JPEG")
im3_gray = imread("../randomly_selected_imgs/random_keyboard.JPEG")
# import npy files
x_gamma_function = np.load('../../code/x_gamma_function.npy')
avg_power_spectrum = np.load('../../code/mean_power_spectrum_grey.npy')
if number is 1: # uniform noise experiment
rng = np.random.RandomState(seed=42)
noise_levels = [0.0, 0.03, 0.05, 0.1, 0.2, 0.35, 0.6, 0.9]
u_noise = lambda i, x: uniform_noise(i, x, 0.3, rng)
plot_stimuli_all_conditions([im1, im2, im3],
noise_levels,
u_noise,
labels_to_int=False,
ylabel= "Uniform noise width",
filename="../../figures/methods/noise_all-conditions.png")
if number is 2: # contrast experiment
contrast_levels = [1.0, 0.5, 0.3, 0.15, 0.10, 0.05, 0.03, 0.01]
plot_stimuli_all_conditions([im1, im2, im3],
contrast_levels,
grayscale_contrast,
labels_to_int=True,
multiply_labels_by=100,
ylabel= "Contrast level in percent",
filename="../../figures/methods/contrast_all-conditions.png")
if number is 3: # Eidolon experiments
grain = 10.0
coherence_levels = [0.0, 0.3, 1.0]
coh_in_filename = ["00", "03", "10"]
reach_levels = [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0]
for i, c in enumerate(coherence_levels):
eidolon_function = lambda i, reach: eidolon_partially_coherent_disarray(i, reach, c, grain)
plot_stimuli_all_conditions([im1_gray, im2_gray, im3_gray],
reach_levels,
eidolon_function,
ylabel= "Reach level",
filename=("../../figures/methods/eidolon_coh="+
coh_in_filename[i]+"_all-conditions.png"),
thresholds=None, set_vmin_vmax=False, is_eidolon=True)
if number is 4: # opponent colour experiment
colour = ["true", "opponent"]
def opponent_colours(img, colour):
if colour == "opponent":
return false_colour(img, x_gamma_function)
else:
return img
plot_stimuli_all_conditions([im1_col, im2_col, im3_col],
colour,
opponent_colours,
labels_to_int=False,
ylabel= "Colour",
filename="../../figures/methods/false-colour_all-conditions.png")
if number is 5: # power equalisation experiment
power = ["original", "equalised"]
def equalise_power(img, power):
if power == "equalised":
return power_equalisation(img, avg_power_spectrum)
else:
return img
plot_stimuli_all_conditions([im1, im2, im3],
power,
equalise_power,
labels_to_int=False,
ylabel= "Power spectrum",
filename="../../figures/methods/power-equalisation_all-conditions.png")
if number is 6: # Highpass experiment
standard_deviations = ["inf", 3, 1.5, 1, 0.7, 0.55, 0.45, 0.4]
def high_pass_filter_with_inf(img, sd):
if sd == "inf":
return img
else:
return high_pass_filter(img, sd)
plot_stimuli_all_conditions([im1, im2, im3],
standard_deviations,
high_pass_filter_with_inf,
labels_to_int=False,
ylabel= "Filter standard deviation [pixels]",
filename="../../figures/methods/highpass_all-conditions_paper.png",
reduced_space = False)
if number is 7: # Lowpass experiment
standard_deviations = [0, 1, 3, 5, 7, 10, 15, 40]
plot_stimuli_all_conditions([im1, im2, im3],
standard_deviations,
low_pass_filter,
labels_to_int=True,
ylabel= "Filter standard deviation [pixels]",
filename="../../figures/methods/lowpass_all-conditions_paper.png",
reduced_space = False)
if number is 8: # Phase noise experiment
standard_deviations = [0, 30, 60, 90, 120, 150, 180]
plot_stimuli_all_conditions([im1, im2, im3],
standard_deviations,
phase_scrambling,
labels_to_int=True,
ylabel= "Phase noise width [deg]",
filename="../../figures/methods/phase-noise_all-conditions_paper.png",
reduced_space = False)
if number is 9: # Salt-and-pepper experiment
noise_levels = [0.0, 0.1, 0.2, 0.35, 0.5, 0.65, 0.8, 0.95]
contrast_level_3 = 0.3
rng = np.random.RandomState(seed=42)
def salt_and_pepper_noise_worker(img, p):
return salt_and_pepper_noise(img, p=p,
contrast_level=contrast_level_3,
rng=rng)
plot_stimuli_all_conditions([im1, im2, im3],
noise_levels,
salt_and_pepper_noise_worker,
labels_to_int=True,
multiply_labels_by=100,
ylabel= "Percent noise pixels [%]",
filename="../../figures/methods/salt-and-pepper_all-conditions_paper.png",
reduced_space = False)
if __name__ == "__main__":
# non-Eidolon stimuli: uncomment and execute with Python 3.5
#for number in [1,2,4,5,6,7,8,9]:
# main(number)
# eidolon stimuli: uncomment and execute with Python 2.7
#for number in [3]:
# main(number)
print("Please choose between plotting Eidolon stimuli (Python 2.7) and other stimuli (Python 3.5) by uncommenting the respective functions in the main method.")