forked from Hulk11/Memorability-map-generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_map.py
167 lines (133 loc) · 5.44 KB
/
gen_map.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
import numpy as np
import keras
from keras.models import Sequential
from keras.applications.vgg16 import VGG16
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from keras.models import Model
from keras.layers import Input
from keras import backend as K
from PIL import Image
import matplotlib.image as img
import matplotlib.pyplot as plt
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.imagenet_utils import preprocess_input
import h5py
import imageio
# Load the latest saved model (model_name.h5)
model = load_model('model_name.h5')
xtl = []
# path of the image
path = "codes_GRBM/codes_GRBM/target_images/3.jpg"
img = keras.utils.load_img(path)
img = keras.utils.img_to_array(img)
# CONSTANTS
imagewidth = 256
imageheight = 256
windowsize = 32
stride = 2
# iterations
iterations_w = (imagewidth - windowsize)/stride + 1
iterations_h = (imageheight - windowsize)/stride + 1
# SCORES ARRAY INSTEAD OF THIS
score = np.random.uniform(low=0, high=1, size=(iterations_h*iterations_w,))
# scores' data
lowest_score = np.amin(score)
highest_score = np.amax(score)
step_size = (highest_score - lowest_score)/5
# image for heatmap
newim = np.zeros((3, 256, 256), dtype='float32')
# intensity control factor (to change contrast)
icf = 20
# increment counter
l = 0
def rgb(value):
# minimum, maximum = lowest_score, highest_score
ratio = 2 * (value - lowest_score) / (highest_score - lowest_score)
b = int(max(0, 255*(1 - ratio)))
r = int(max(0, 255*(ratio - 1)))
g = 255 - b - r
return r, g, b
# define standard constant colors
colors = [(0, 0, 255), (0, 255, 0), (255, 0, 0)]
# set tolerance
EPSILON = (highest_score - lowest_score)/5
# processing image data
def rgb(val):
fi = float(val - lowest_score) / \
float(highest_score - lowest_score) * (len(colors)-1)
i = int(fi)
f = fi - i
if f < EPSILON:
return colors[i]
else:
(r1, g1, b1), (r2, g2, b2) = colors[i], colors[i+1]
return int(r1 + f*(r2-r1)), int(g1 + f*(g2-g1)), int(b1 + f*(b2-b1))
"""
Color coding scheme
Pixel intensity is set according to the memorability score of a window stride
"""
for i in range(int(iterations_w)):
for j in range(int(iterations_h)):
# (r,g,b)
a = rgb(score[l])
# defining intervals for color coding
if(score[l] >= lowest_score and score[l] < lowest_score+step_size):
newim[0][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = a[0]
newim[1][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = a[1]
newim[2][(j*stride):(j*stride)+windowsize, (i*stride):(i *
stride)+windowsize] = a[2] # (score[l] - lowest_score)*icf
elif(score[l] >= lowest_score+step_size and score[l] < lowest_score+2*step_size):
newim[0][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 0
newim[1][(j*stride):(j*stride)+windowsize, (i*stride)
:(i*stride)+windowsize] = (score[l] - lowest_score)*icf
newim[2][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 1
elif(score[l] >= lowest_score+2*step_size and score[l] < lowest_score+3*step_size):
newim[0][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 0
newim[1][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 1
newim[2][(j*stride):(j*stride)+windowsize, (i*stride)
:(i*stride)+windowsize] = (highest_score - score[l])*icf
elif(score[l] >= lowest_score+3*step_size and score[l] < lowest_score+4*step_size):
newim[0][(j*stride):(j*stride)+windowsize, (i*stride)
:(i*stride)+windowsize] = (score[l] - lowest_score)*icf
newim[1][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 1
newim[2][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 0
else:
# if(score[l]>=lowest_score+4*step_size and score[l]<=lowest_score+5*step_size)
newim[0][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 1
newim[1][(j*stride):(j*stride)+windowsize, (i*stride)
:(i*stride)+windowsize] = (highest_score - score[l])*icf
newim[2][(j*stride):(j*stride)+windowsize,
(i*stride):(i*stride)+windowsize] = 0
l += 1
# changing dimension to display image
newim = np.rollaxis(newim, 0, 3)
"""
FOR SAVING HEATMAP NEW IMAGE
'outfile.jpg' is the name of map image
"""
# scipy.misc.imsave('outfile.jpg', newim) // deprecated
imageio.imwrite('outfile.jpg', newim)
# Load the image and the image map
img_map = keras.utils.load_img('outfile.jpg')
orig_image = keras.utils.load_img(path)
# display the image
plt.imshow(Image.blend(orig_image, img_map, alpha=.9))
plt.show()