-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathfeature_engineering_image.py
More file actions
353 lines (229 loc) · 8.4 KB
/
feature_engineering_image.py
File metadata and controls
353 lines (229 loc) · 8.4 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# coding: utf-8
"""
Created on Mon May 17 00:00:00 2017
@author: DIP
"""
# # Import necessary dependencies and settings
# In[1]:
import skimage
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skimage import io
get_ipython().magic('matplotlib inline')
# # Image metadata features
#
# - Image create date & time
# - Image dimensions
# - Image compression format
# - Device Make & Model
# - Image resolution & aspect ratio
# - Image Artist
# - Flash, Aperture, Focal Length & Exposure
# # Raw Image and channel pixel values
# In[2]:
cat = io.imread('datasets/cat.png')
dog = io.imread('datasets/dog.png')
df = pd.DataFrame(['Cat', 'Dog'], columns=['Image'])
print(cat.shape, dog.shape)
# In[3]:
#coffee = skimage.transform.resize(coffee, (300, 451), mode='reflect')
fig = plt.figure(figsize = (8,4))
ax1 = fig.add_subplot(1,2, 1)
ax1.imshow(cat)
ax2 = fig.add_subplot(1,2, 2)
ax2.imshow(dog)
# In[4]:
dog_r = dog[:,:,0]
dog_g = dog[:,:,1]
dog_b = dog[:,:,2]
plot_image = np.concatenate((dog_r, dog_g, dog_b), axis=1)
plt.figure(figsize = (10,4))
plt.imshow(plot_image)
# In[5]:
dog_r
# # Grayscale image pixel values
# In[6]:
from skimage.color import rgb2gray
cgs = rgb2gray(cat)
dgs = rgb2gray(dog)
print('Image shape:', cgs.shape, '\n')
# 2D pixel map
print('2D image pixel map')
print(np.round(cgs, 2), '\n')
# flattened pixel feature vector
print('Flattened pixel map:', (np.round(cgs.flatten(), 2)))
# # Binning image intensity distribution
# In[7]:
fig = plt.figure(figsize = (8,4))
ax1 = fig.add_subplot(2,2, 1)
ax1.imshow(cgs, cmap="gray")
ax2 = fig.add_subplot(2,2, 2)
ax2.imshow(dgs, cmap='gray')
ax3 = fig.add_subplot(2,2, 3)
c_freq, c_bins, c_patches = ax3.hist(cgs.flatten(), bins=30)
ax4 = fig.add_subplot(2,2, 4)
d_freq, d_bins, d_patches = ax4.hist(dgs.flatten(), bins=30)
# # Image aggregation statistics
# ## RGB ranges
# In[8]:
from scipy.stats import describe
cat_rgb = cat.reshape((168*300), 3).T
dog_rgb = dog.reshape((168*300), 3).T
cs = describe(cat_rgb, axis=1)
ds = describe(dog_rgb, axis=1)
cat_rgb_range = cs.minmax[1] - cs.minmax[0]
dog_rgb_range = ds.minmax[1] - ds.minmax[0]
rgb_range_df = pd.DataFrame([cat_rgb_range, dog_rgb_range],
columns=['R_range', 'G_range', 'B_range'])
pd.concat([df, rgb_range_df], axis=1)
# # Descriptive aggregations
# In[9]:
cat_stats= np.array([np.round(cs.mean, 2),np.round(cs.variance, 2),
np.round(cs.kurtosis, 2),np.round(cs.skewness, 2),
np.round(np.median(cat_rgb, axis=1), 2)]).flatten()
dog_stats= np.array([np.round(ds.mean, 2),np.round(ds.variance, 2),
np.round(ds.kurtosis, 2),np.round(ds.skewness, 2),
np.round(np.median(dog_rgb, axis=1), 2)]).flatten()
stats_df = pd.DataFrame([cat_stats, dog_stats],
columns=['R_mean', 'G_mean', 'B_mean',
'R_var', 'G_var', 'B_var',
'R_kurt', 'G_kurt', 'B_kurt',
'R_skew', 'G_skew', 'B_skew',
'R_med', 'G_med', 'B_med'])
pd.concat([df, stats_df], axis=1)
# # Edge detection
# In[10]:
from skimage.feature import canny
cat_edges = canny(cgs, sigma=3)
dog_edges = canny(dgs, sigma=3)
fig = plt.figure(figsize = (8,4))
ax1 = fig.add_subplot(1,2, 1)
ax1.imshow(cat_edges, cmap='binary')
ax2 = fig.add_subplot(1,2, 2)
ax2.imshow(dog_edges, cmap='binary')
# # Object detection
# In[11]:
from skimage.feature import hog
from skimage import exposure
fd_cat, cat_hog = hog(cgs, orientations=8, pixels_per_cell=(8, 8),
cells_per_block=(3, 3), visualise=True)
fd_dog, dog_hog = hog(dgs, orientations=8, pixels_per_cell=(8, 8),
cells_per_block=(3, 3), visualise=True)
# rescaling intensity to get better plots
cat_hogs = exposure.rescale_intensity(cat_hog, in_range=(0, 0.04))
dog_hogs = exposure.rescale_intensity(dog_hog, in_range=(0, 0.04))
fig = plt.figure(figsize = (10,4))
ax1 = fig.add_subplot(1,2, 1)
ax1.imshow(cat_hogs, cmap='binary')
ax2 = fig.add_subplot(1,2, 2)
ax2.imshow(dog_hogs, cmap='binary')
# In[12]:
print(fd_cat, fd_cat.shape)
# # Localized feature extraction
#
# In[13]:
from mahotas.features import surf
import mahotas as mh
cat_mh = mh.colors.rgb2gray(cat)
dog_mh = mh.colors.rgb2gray(dog)
cat_surf = surf.surf(cat_mh, nr_octaves=8, nr_scales=16, initial_step_size=1, threshold=0.1, max_points=50)
dog_surf = surf.surf(dog_mh, nr_octaves=8, nr_scales=16, initial_step_size=1, threshold=0.1, max_points=54)
fig = plt.figure(figsize = (10,4))
ax1 = fig.add_subplot(1,2, 1)
ax1.imshow(surf.show_surf(cat_mh, cat_surf))
ax2 = fig.add_subplot(1,2, 2)
ax2.imshow(surf.show_surf(dog_mh, dog_surf))
# In[14]:
cat_surf_fds = surf.dense(cat_mh, spacing=10)
dog_surf_fds = surf.dense(dog_mh, spacing=10)
cat_surf_fds.shape
# # Visual Bag of Words model
# ## Engineering features from SURF feature descriptions with clustering
# In[15]:
from sklearn.cluster import KMeans
k = 20
km = KMeans(k, n_init=100, max_iter=100)
surf_fd_features = np.array([cat_surf_fds, dog_surf_fds])
km.fit(np.concatenate(surf_fd_features))
vbow_features = []
for feature_desc in surf_fd_features:
labels = km.predict(feature_desc)
vbow = np.bincount(labels, minlength=k)
vbow_features.append(vbow)
vbow_df = pd.DataFrame(vbow_features)
pd.concat([df, vbow_df], axis=1)
# ## Trying out the VBOW pipeline on a new image
# In[16]:
new_cat = io.imread('datasets/new_cat.png')
newcat_mh = mh.colors.rgb2gray(new_cat)
newcat_surf = surf.surf(newcat_mh, nr_octaves=8, nr_scales=16, initial_step_size=1, threshold=0.1, max_points=50)
fig = plt.figure(figsize = (10,4))
ax1 = fig.add_subplot(1,2, 1)
ax1.imshow(surf.show_surf(newcat_mh, newcat_surf))
# In[17]:
new_surf_fds = surf.dense(newcat_mh, spacing=10)
labels = km.predict(new_surf_fds)
new_vbow = np.bincount(labels, minlength=k)
pd.DataFrame([new_vbow])
# In[18]:
from sklearn.metrics.pairwise import euclidean_distances, cosine_similarity
eucdis = euclidean_distances(new_vbow.reshape(1,-1) , vbow_features)
cossim = cosine_similarity(new_vbow.reshape(1,-1) , vbow_features)
result_df = pd.DataFrame({'EuclideanDistance': eucdis[0],
'CosineSimilarity': cossim[0]})
pd.concat([df, result_df], axis=1)
# # Automated Feature Engineering with Deep Learning
# In[19]:
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras import backend as K
# ## Build a basic 2-layer CNN
# In[55]:
model = Sequential()
model.add(Conv2D(4, (4, 4), input_shape=(168, 300, 3), activation='relu',
kernel_initializer='glorot_uniform'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(4, (4, 4), activation='relu',
kernel_initializer='glorot_uniform'))
# ## Visualize the CNN architecture
# In[21]:
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
SVG(model_to_dot(model, show_shapes=True,
show_layer_names=True, rankdir='TB').create(prog='dot', format='svg'))
# ## Build functions to extract features from intermediate layers
# In[56]:
first_conv_layer = K.function([model.layers[0].input, K.learning_phase()],
[model.layers[0].output])
second_conv_layer = K.function([model.layers[0].input, K.learning_phase()],
[model.layers[2].output])
# ## Extract and visualize image representation features
# In[57]:
catr = cat.reshape(1, 168, 300,3)
# extract feaures
first_conv_features = first_conv_layer([catr])[0][0]
second_conv_features = second_conv_layer([catr])[0][0]
# view feature representations
fig = plt.figure(figsize = (14,4))
ax1 = fig.add_subplot(2,4, 1)
ax1.imshow(first_conv_features[:,:,0])
ax2 = fig.add_subplot(2,4, 2)
ax2.imshow(first_conv_features[:,:,1])
ax3 = fig.add_subplot(2,4, 3)
ax3.imshow(first_conv_features[:,:,2])
ax4 = fig.add_subplot(2,4, 4)
ax4.imshow(first_conv_features[:,:,3])
ax5 = fig.add_subplot(2,4, 5)
ax5.imshow(second_conv_features[:,:,0])
ax6 = fig.add_subplot(2,4, 6)
ax6.imshow(second_conv_features[:,:,1])
ax7 = fig.add_subplot(2,4, 7)
ax7.imshow(second_conv_features[:,:,2])
ax8 = fig.add_subplot(2,4, 8)
ax8.imshow(second_conv_features[:,:,3])
# In[60]:
sample_features = np.round(np.array(first_conv_features[:,:,1], dtype='float'), 2)
print(sample_features)
print(sample_features.shape)