-
Notifications
You must be signed in to change notification settings - Fork 0
/
incep-resn_v2_train_whole_net.py
149 lines (129 loc) · 5.75 KB
/
incep-resn_v2_train_whole_net.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
#
# Modified from Keras doc: Fine-tune InceptionV3 on a new set of classes
#Keras blog
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.preprocessing import image
from keras.models import Model, Sequential
from keras.layers import Dense, GlobalAveragePooling2D,Dropout, Flatten
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.metrics import top_k_categorical_accuracy, sparse_top_k_categorical_accuracy
from keras import applications
import keras
import numpy as np
import tensorflow as tf
# dimensions of our images.
img_width, img_height = 299, 299
top_model_weights_path = 'resnet50.h5'
train_data_dir = '../data/scene_classification/scene_train_images_20170904'
validation_data_dir = '../data/scene_classification/scene_validation_images_20170908'
train_labels = np.load('training_labels.npy')
validation_labels = np.load('validation_labels.npy')
nb_train_samples = len(train_labels)
nb_validation_samples = len(validation_labels)
epochs = 10
batch_size = 20
def to_one_hot(array):
array=array.astype(np.int)
n_values = np.max(array) + 1
return np.eye(n_values)[array]
def show_layers(model):
for i, layer in enumerate(model.layers):
print(i, layer.name)
def top_3_categorical_accuracy(y_true, y_pred, k=3):
return K.mean(K.in_top_k(y_pred, K.argmax(y_true, axis=-1), k))
def save_features():
datagen = ImageDataGenerator(rescale=1. / 255)
# build the network
model = keras.applications.inception_resnet_v2.InceptionResNetV2(weights=None, include_top=False, pooling='avg')
generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
bottleneck_features_train = model.predict_generator(
generator, 1 + nb_train_samples // batch_size, verbose=1)
#+1 in order not to lose the last batch
# !!! labels need to be modified accordingly
np.save('bottleneck_features_train.npy', bottleneck_features_train)
#the array will have size (no. of samples, 1536)
#save_validation_features:
generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode=None,
shuffle=False)
bottleneck_features_validation = model.predict_generator(
generator, 1 + nb_validation_samples // batch_size)
np.save('bottleneck_features_validation.npy', bottleneck_features_validation)
def train_top_model():
np.random.seed(1)
global train_labels
global validation_labels
train_data = np.load('bottleneck_features_train.npy')
#train_labels = np.array([0] * (nb_train_samples / 2) + [1] * (nb_train_samples / 2))
#train_labels have been defined globally
validation_data = np.load('bottleneck_features_validation.npy')
#validation_labels = np.array([0] * (nb_validation_samples / 2) + [1] * (nb_validation_samples / 2))
# array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
model = Sequential()
#model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dropout(0.3, input_shape=(1536,)))
model.add(Dense(1000, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(80, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy',top_3_categorical_accuracy])
#keras.optimizers.RMSprop(lr=0.0005)
train_labels=train_labels.reshape((len(train_labels),1))
trainmerge=np.append(train_data,train_labels,axis=1)
validation_labels=validation_labels.reshape((len(validation_labels),1))
validation_data=validation_data[:-20,:]
validationmerge=np.append(validation_data,validation_labels,axis=1)
np.random.shuffle(trainmerge)
np.random.shuffle(validationmerge)
train_data=trainmerge[:,:-1]
train_labels=trainmerge[:,-1]
validation_data=validationmerge[:,:-1]
validation_labels=validationmerge[:,-1]
#validation_labels=K.one_hot(validation_labels,80)
validation_labels=to_one_hot(validation_labels.flatten())
train_labels=to_one_hot(train_labels.flatten())
model.load_weights(top_model_weights_path)
model.fit(train_data, train_labels,
epochs=epochs,
batch_size=batch_size,
validation_data=(validation_data, validation_labels))
model.save_weights(top_model_weights_path)
def train_whole_net():
base_model= keras.applications.inception_resnet_v2.InceptionResNetV2(weights=None, include_top=False, pooling='avg')
top_model = Sequential()
top_model.add(Dropout(0.3, input_shape=(1536,)))
#top_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy',top_3_categorical_accuracy])
#model.add(top_model)
model = Model(inputs= base_model.input, outputs= top_model(base_model.output))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy',top_3_categorical_accuracy])
datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='sparse',
shuffle=True)
validation_generator = datagen.flow_from_directory(
validation_data_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='sparse',
shuffle=True)
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
epochs=epochs,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)
keras.models.save_model(model,top_model_weights_path)
#save_features()
train_whole_net()
#train_top_model()