train_cnn_augmented_data.py
Go to the documentation of this file.
1 import argparse
2 parser = argparse.ArgumentParser(description='Run CNN training on patches with a few different hyperparameter sets.')
3 parser.add_argument('-c', '--config', help="JSON with script configuration", default='config.json')
4 parser.add_argument('-o', '--output', help="Output model file name", default='model')
5 parser.add_argument('-g', '--gpu', help="Which GPU index", default='0')
6 args = parser.parse_args()
7 
8 import os
9 os.environ['KERAS_BACKEND'] = "tensorflow"
10 os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
11 
12 import tensorflow as tf
13 import keras
14 if keras.__version__[0] != '2':
15  print 'Please use the newest Keras 2.x.x API with the Tensorflow backend'
16  quit()
17 keras.backend.set_image_data_format('channels_last')
18 keras.backend.set_image_dim_ordering('tf')
19 
20 import numpy as np
21 np.random.seed(2017) # for reproducibility
22 from keras.models import Model
23 from keras.layers import Input
24 from keras.layers.core import Dense, Dropout, Activation, Flatten
25 from keras.layers.convolutional import Conv2D, MaxPooling2D
26 from keras.layers.advanced_activations import LeakyReLU
27 # from keras.layers.normalization import BatchNormalization
28 from keras.preprocessing.image import ImageDataGenerator
29 from keras.optimizers import SGD
30 from keras.utils import np_utils
31 from os.path import exists, isfile, join
32 import json
33 
34 from utils import read_config, get_patch_size, count_events
35 
36 def save_model(model, name):
37  try:
38  with open(name + '_architecture.json', 'w') as f:
39  f.write(model.to_json())
40  model.save_weights(name + '_weights.h5', overwrite=True)
41  return True # Save successful
42  except:
43  return False # Save failed
44 
45 ####################### configuration #############################
46 print 'Reading configuration...'
47 config = read_config(args.config)
48 
49 CNN_INPUT_DIR = config['training_on_patches']['input_dir']
50 # input image dimensions
51 PATCH_SIZE_W, PATCH_SIZE_D = get_patch_size(CNN_INPUT_DIR)
52 img_rows, img_cols = PATCH_SIZE_W, PATCH_SIZE_D
53 
54 batch_size = config['training_on_patches']['batch_size']
55 nb_classes = config['training_on_patches']['nb_classes']
56 nb_epoch = config['training_on_patches']['nb_epoch']
57 
58 nb_pool = 2 # size of pooling area for max pooling
59 
60 cfg_name = 'sgd_lorate'
61 
62 # convolutional layers:
63 nb_filters1 = 48 # number of convolutional filters in the first layer
64 nb_conv1 = 5 # 1st convolution kernel size
65 convactfn1 = 'relu'
66 
67 maxpool = False # max pooling between conv. layers
68 
69 nb_filters2 = 0 # number of convolutional filters in the second layer
70 nb_conv2 = 7 # convolution kernel size
71 convactfn2 = 'relu'
72 
73 drop1 = 0.2
74 
75 # dense layers:
76 densesize1 = 128
77 actfn1 = 'relu'
78 densesize2 = 32
79 actfn2 = 'relu'
80 drop2 = 0.2
81 
82 ####################### CNN definition ############################
83 print 'Compiling CNN model...'
84 with tf.device('/gpu:' + args.gpu):
85  main_input = Input(shape=(img_rows, img_cols, 1), name='main_input')
86 
87  if convactfn1 == 'leaky':
88  x = Conv2D(nb_filters1, (nb_conv1, nb_conv1),
89  padding='valid', data_format='channels_last',
90  activation=LeakyReLU())(main_input)
91  else:
92  x = Conv2D(nb_filters1, (nb_conv1, nb_conv1),
93  padding='valid', data_format='channels_last',
94  activation=convactfn1)(main_input)
95 
96  if nb_filters2 > 0:
97  if maxpool:
98  x = MaxPooling2D(pool_size=(nb_pool, nb_pool))(x)
99  x = Conv2D(nb_filters2, (nb_conv2, nb_conv2))(x)
100  if convactfn2 == 'leaky':
101  x = Conv2D(nb_filters2, (nb_conv2, nb_conv2), activation=LeakyReLU())(x)
102  else:
103  x = Conv2D(nb_filters2, (nb_conv2, nb_conv2), activation=convactfn2)(x)
104 
105  x = Dropout(drop1)(x)
106  x = Flatten()(x)
107  # x = BatchNormalization()(x)
108 
109  # dense layers
110  x = Dense(densesize1, activation=actfn1)(x)
111  x = Dropout(drop2)(x)
112 
113  if densesize2 > 0:
114  x = Dense(densesize2, activation=actfn2)(x)
115  x = Dropout(drop2)(x)
116 
117  # outputs
118  em_trk_none = Dense(3, activation='softmax', name='em_trk_none_netout')(x)
119  michel = Dense(1, activation='sigmoid', name='michel_netout')(x)
120 
121  sgd = SGD(lr=0.01, decay=1e-5, momentum=0.9, nesterov=True)
122  model = Model(inputs=[main_input], outputs=[em_trk_none, michel])
123  model.compile(optimizer=sgd,
124  loss={'em_trk_none_netout': 'categorical_crossentropy', 'michel_netout': 'mean_squared_error'},
125  loss_weights={'em_trk_none_netout': 0.1, 'michel_netout': 1.})
126 
127 ####################### read data sets ############################
128 n_training = count_events(CNN_INPUT_DIR, 'training')
129 X_train = np.zeros((n_training, PATCH_SIZE_W, PATCH_SIZE_D, 1), dtype=np.float32)
130 EmTrkNone_train = np.zeros((n_training, 3), dtype=np.int32)
131 Michel_train = np.zeros((n_training, 1), dtype=np.int32)
132 print 'Training data size:', n_training, 'events; patch size:', PATCH_SIZE_W, 'x', PATCH_SIZE_D
133 
134 ntot = 0
135 subdirs = [f for f in os.listdir(CNN_INPUT_DIR) if 'training' in f]
136 subdirs.sort()
137 for dirname in subdirs:
138  print 'Reading data in', dirname
139  filesX = [f for f in os.listdir(CNN_INPUT_DIR + '/' + dirname) if '_x.npy' in f]
140  for fnameX in filesX:
141  print '...training data', fnameX
142  fnameY = fnameX.replace('_x.npy', '_y.npy')
143  dataX = np.load(CNN_INPUT_DIR + '/' + dirname + '/' + fnameX)
144  if dataX.dtype != np.dtype('float32'):
145  dataX = dataX.astype("float32")
146  dataY = np.load(CNN_INPUT_DIR + '/' + dirname + '/' + fnameY)
147  n = dataY.shape[0]
148  X_train[ntot:ntot+n] = dataX.reshape(n, img_rows, img_cols, 1)
149  EmTrkNone_train[ntot:ntot+n] = dataY[:,[0, 1, 3]]
150  Michel_train[ntot:ntot+n] = dataY[:,[2]]
151  ntot += n
152 print ntot, 'events ready'
153 
154 n_testing = count_events(CNN_INPUT_DIR, 'testing')
155 X_test = np.zeros((n_testing, PATCH_SIZE_W, PATCH_SIZE_D, 1), dtype=np.float32)
156 EmTrkNone_test = np.zeros((n_testing, 3), dtype=np.int32)
157 Michel_test = np.zeros((n_testing, 1), dtype=np.int32)
158 print 'Testing data size:', n_testing, 'events'
159 
160 ntot = 0
161 subdirs = [f for f in os.listdir(CNN_INPUT_DIR) if 'testing' in f]
162 subdirs.sort()
163 for dirname in subdirs:
164  print 'Reading data in', dirname
165  filesX = [f for f in os.listdir(CNN_INPUT_DIR + '/' + dirname) if '_x.npy' in f]
166  for fnameX in filesX:
167  print '...testing data', fnameX
168  fnameY = fnameX.replace('_x.npy', '_y.npy')
169  dataX = np.load(CNN_INPUT_DIR + '/' + dirname + '/' + fnameX)
170  if dataX.dtype != np.dtype('float32'):
171  dataX = dataX.astype("float32")
172  dataY = np.load(CNN_INPUT_DIR + '/' + dirname + '/' + fnameY)
173  n = dataY.shape[0]
174  X_test[ntot:ntot+n] = dataX.reshape(n, img_rows, img_cols, 1)
175  EmTrkNone_test[ntot:ntot+n] = dataY[:,[0, 1, 3]]
176  Michel_test[ntot:ntot+n] = dataY[:,[2]]
177  ntot += n
178 print ntot, 'events ready'
179 
180 dataX = None
181 dataY = None
182 
183 print 'Training', X_train.shape, 'testing', X_test.shape
184 
185 ########################## training ###############################
186 datagen = ImageDataGenerator(
187  featurewise_center=False, samplewise_center=False,
188  featurewise_std_normalization=False,
189  samplewise_std_normalization=False,
190  zca_whitening=False,
191  rotation_range=0, width_shift_range=0, height_shift_range=0,
192  horizontal_flip=True, # randomly flip images
193  vertical_flip=False) # only horizontal flip
194 datagen.fit(X_train)
195 
196 def generate_data_generator(generator, X, Y1, Y2, b):
197  genY1 = generator.flow(X, Y1, batch_size=b, seed=7)
198  genY2 = generator.flow(X, Y2, batch_size=b, seed=7)
199  while True:
200  g1 = genY1.next()
201  g2 = genY2.next()
202  yield {'main_input': g1[0]}, {'em_trk_none_netout': g1[1], 'michel_netout': g2[1]}
203 
204 print 'Fit config:', cfg_name
205 h = model.fit_generator(
206  generate_data_generator(datagen, X_train, EmTrkNone_train, Michel_train, b=batch_size),
207  validation_data=(
208  {'main_input': X_test},
209  {'em_trk_none_netout': EmTrkNone_test, 'michel_netout': Michel_test}),
210  steps_per_epoch=X_train.shape[0]/batch_size, epochs=nb_epoch,
211  verbose=1)
212 
213 X_train = None
214 EmTrkNone_train = None
215 Michel_train = None
216 
217 score = model.evaluate({'main_input': X_test},
218  {'em_trk_none_netout': EmTrkNone_test, 'michel_netout': Michel_test},
219  verbose=0)
220 print('Test score:', score)
221 
222 X_test = None
223 EmTrkNone_test = None
224 Michel_test = None
225 #####################################################################
226 
227 print h.history['loss']
228 print h.history['val_loss']
229 
230 if save_model(model, args.output + cfg_name):
231  print('All done!')
232 else:
233  print('Error: model not saved.')
234 
int open(const char *, int)
Opens a file descriptor.
def count_events(folder, key)
Definition: utils.py:9
def generate_data_generator(generator, X, Y1, Y2, b)
def read_config(cfgname)
Definition: utils.py:192
def get_patch_size(folder)
Definition: utils.py:20