-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_pca.py
248 lines (222 loc) · 9.41 KB
/
train_pca.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from constants import NUM_CLASSES, SEED
from keras.applications import (inception_v3,
resnet50,
densenet,
inception_resnet_v2)
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import (BatchNormalization,
Dense,
Dropout,
GlobalAveragePooling2D,
Input,
Concatenate)
from keras.models import Model
from keras.optimizers import SGD
from time import time
from tqdm import tqdm
from util import get_labels, get_images, one_hot
from sklearn.decomposition import PCA, FastICA
from sklearn.preprocessing import MinMaxScaler
from sklearn import linear_model
from cv2 import (Canny,
cornerHarris,
dilate)
#from genetic_selection import GeneticSelectionCV
import keras.backend as K
import numpy as np
import matplotlib.pyplot as plt
# Define constants
fname = 'model1_finetune.h5'
log_dir = f'./training_log/{time()}'
np.random.seed(seed=SEED)
INPUT_SIZE = 299
n_pre_epochs = 10
n_epochs = 100
batch_size = 32
n_images = 300
USE_PCA = True
USE_GENSEL = False
USE_AUTOENC = False #TODO
USE_ICA = True
USE_CANNY = True
USE_CORNERHARRIS = True
TRAIN = False
CHANNELS = 3
# Load labels
print('Load labels...')
labels = get_labels()[:n_images]
# Load training data
print('Load training data...')
x_train = np.zeros((n_images, INPUT_SIZE, INPUT_SIZE, 3), dtype=K.floatx())
for i, (img, img_id) in tqdm(enumerate(get_images('train', INPUT_SIZE, amount=n_images))):
x = inception_v3.preprocess_input(np.expand_dims(img, axis=0))
x_train[i] = x
y_train = one_hot(labels['breed'].values, num_classes=NUM_CLASSES)
# Arguments of ImageDataGenerator define types of augmentation to be performed
# E.g: Horizontal flip, rotation, etc...
# no fitting required since we don't use centering/normalization/whitening
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=.2,
height_shift_range=.2,
horizontal_flip=True,
validation_split=.1)
# PCA feature extraction:
NCOMPONENTS_PCA = 50
NCOMPONENTS_ICA = 50
plot_idx = 1
x_train_pca = np.zeros((n_images,NCOMPONENTS_PCA,3)) # variable containing pca features
train_inv_pca = np.zeros((n_images,INPUT_SIZE*INPUT_SIZE,3)) # variable inverting pca features for visualization
x_train_ica = np.zeros((n_images,NCOMPONENTS_ICA,3)) # variable containing ica features
train_inv_ica = np.zeros((n_images,INPUT_SIZE*INPUT_SIZE,3)) # variable inverting ica features for visualization
x_train_canny = np.zeros((n_images,INPUT_SIZE,INPUT_SIZE,3)) # variable containing edges detected by canny edge detector
x_train_cornerharris = np.zeros((n_images,INPUT_SIZE,INPUT_SIZE,3))
for i in range(CHANNELS):
if USE_PCA:
pca = PCA(n_components=NCOMPONENTS_PCA)
x_tr = np.reshape(x_train[:, :, :, i], (n_images, INPUT_SIZE*INPUT_SIZE))
x_train_pca[:, :, i] = pca.fit_transform(x_tr)
print(pca.explained_variance_ratio_)
inv_pca = pca.inverse_transform(x_train_pca[:, :, i])
train_inv_pca[:,:,i] = inv_pca
if USE_ICA:
ica = FastICA(n_components=NCOMPONENTS_ICA)
x_tr = np.reshape(x_train[:, :, :, i], (n_images, INPUT_SIZE * INPUT_SIZE))
x_train_ica[:, :, i] = ica.fit_transform(x_tr)
print(pca.explained_variance_ratio_)
inv_ica = ica.inverse_transform(x_train_ica[:, :, i])
train_inv_ica[:, :, i] = inv_ica
if USE_GENSEL:
# Genetic feature selection: #very slow
clsfr = linear_model.LogisticRegression()
gs = GeneticSelectionCV(clsfr)
print(np.shape(x_tr))
print(np.shape(y_train))
print(np.array(np.argmax(y_train, axis=1)))
gs_features = gs.fit(x_tr, np.array(np.argmax(y_train, axis=1)))
gs_features.support_
# note: images of dogs are quite dark. possible clipping issue
if USE_PCA:
# print(np.shape(train_inv_pca[plot_idx, :, :]))
# print(train_inv_pca[plot_idx, :, :])
mms = MinMaxScaler()
X = mms.fit_transform(train_inv_pca[plot_idx, :, :])
X2 = x_train.copy()
for c in range(CHANNELS):
X2[plot_idx,:,:,c] = mms.fit_transform(X2[plot_idx, :, :, c])
plt.subplot(121)
plt.imshow(X2[plot_idx, :, :, :])
plt.subplot(122)
plt.imshow(np.reshape(X, (INPUT_SIZE, INPUT_SIZE,3)))
plt.show()
if USE_ICA:
# print(np.shape(train_inv_ica[plot_idx, :, :]))
# print(train_inv_ica[plot_idx, :, :])
mms = MinMaxScaler()
X = mms.fit_transform(train_inv_ica[plot_idx, :, :])
X2 = x_train.copy()
for c in range(CHANNELS):
X2[plot_idx, :, :, c] = mms.fit_transform(X2[plot_idx, :, :, c])
plt.subplot(121)
plt.imshow(X2[plot_idx, :, :, :])
plt.subplot(122)
plt.imshow(np.reshape(X, (INPUT_SIZE, INPUT_SIZE, 3)))
plt.show()
if USE_CANNY:
mms = MinMaxScaler()
for i in range(n_images):
for c in range(CHANNELS):
img = x_train[i, :, :, c].astype(np.uint8)
x_train_canny[i, :, :, c] = Canny(img, np.mean(img)-(np.mean(img)*0.25), np.mean(img)+(np.mean(img)*0.25)) #thresholding such that max = mean + 25% and min = mean - 25%
X2 = x_train.copy()
for c in range(CHANNELS):
X2[plot_idx, :, :, c] = mms.fit_transform(X2[plot_idx, :, :, c])
print(np.shape(X2))
plt.subplot(121)
plt.imshow(X2[plot_idx, :, :, :])
plt.subplot(122)
plt.imshow(x_train_canny[plot_idx, :, :, :])
plt.show()
if USE_CORNERHARRIS:
mms = MinMaxScaler()
for i in range(n_images):
for c in range(CHANNELS):
img = x_train[i, :, :, c].astype(np.float32)
dst = cornerHarris(img, 2, 3, 0.04)
dst = dilate(dst,None)
img[dst > 0.01*dst.max()] = 1.0
x_train_cornerharris[i, :, :, c] = img
X2 = x_train.copy()
for c in range(CHANNELS):
X2[plot_idx, :, :, c] = mms.fit_transform(X2[plot_idx, :, :, c])
print(np.shape(X2))
plt.subplot(121)
plt.imshow(X2[plot_idx, :, :, :])
plt.subplot(122)
plt.imshow(x_train_cornerharris[plot_idx, :, :, c])
plt.show()
if TRAIN:
# Define model:
# Add a single fully connected layer on top of the conv layers of Inception
# Freeze Inception layers
print('Define and fit model...')
input = Input(shape=(INPUT_SIZE,INPUT_SIZE,3))
# pre-trained network options:
# resnet50 = resnet50.ResNet50(weights='imagenet', include_top=False, input_shape=(INPUT_SIZE,INPUT_SIZE,3), input_tensor=input)
# r = resnet50.output
# densenet201 = densenet.DenseNet201(weights='imagenet', include_top=False, input_shape=(INPUT_SIZE,INPUT_SIZE,3), input_tensor=input)
# r = densenet201.output
#inception_resnet = inception_resnet_v2.InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(INPUT_SIZE,INPUT_SIZE,3), input_tensor=input)
# r = inception_resnet.output
inception_v3 = inception_v3.InceptionV3(weights='imagenet', include_top=False, input_tensor=input)
x = inception_v3.output
# print(K.shape(x))
# print(K.shape(r))
x = BatchNormalization()(x)
x = GlobalAveragePooling2D()(x)
# r = BatchNormalization()(r)
# r = GlobalAveragePooling2D()(r)
# x = Concatenate(axis=1)([x,r])
x = Dense(1024, activation='relu')(x)
x = Dropout(.3)(x)
predictions = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(inputs=input, outputs=predictions)
for layer in inception_v3.layers:
layer.trainable = False
# for layer in inception_resnet.layers:
# layer.trainable = False
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
metrics=['accuracy'])
# Fit model on data, with callbacks to save best model and run TensorBoard
cp = ModelCheckpoint(fname, monitor='val_loss', save_best_only=True)
tb = TensorBoard(log_dir=log_dir)
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size,
subset='training'),
validation_data=datagen.flow(x_train, y_train,
batch_size=batch_size,
subset='validation'),
steps_per_epoch=x_train.shape[0] / batch_size,
epochs=n_pre_epochs,
callbacks=[cp, tb])
# Now we will fine-tune the top inception block
print('Fine-tuning model')
for layer in inception_v3.layers[:249]:
layer.trainable = False
for layer in inception_v3.layers[249:]:
layer.trainable = True
# no fine-tuning of the second pre-trained network
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9),
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size,
subset='training'),
validation_data=datagen.flow(x_train, y_train,
batch_size=batch_size,
subset='validation'),
steps_per_epoch=x_train.shape[0] / batch_size,
epochs=n_pre_epochs + n_epochs,
initial_epoch=n_pre_epochs,
callbacks=[cp, tb])