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(
'-m',
'--model', help=
"input CNN model name (saved in JSON and h5 files)", default=
'cnn_model')
5 parser.add_argument(
'-o',
'--output', help=
"output CNN model name (saved in JSON and h5 files)", default=
'cnn_model_out')
6 parser.add_argument(
'-g',
'--gpu', help=
"Which GPU index", default=
'0')
7 args = parser.parse_args()
10 os.environ[
'KERAS_BACKEND'] =
"tensorflow" 11 os.environ[
"CUDA_VISIBLE_DEVICES"]=args.gpu
13 import tensorflow
as tf
15 if keras.__version__[0] !=
'2':
16 print 'Please use the newest Keras 2.x.x API with the Tensorflow backend' 18 keras.backend.set_image_data_format(
'channels_last')
19 keras.backend.set_image_dim_ordering(
'tf')
23 from keras.preprocessing.image
import ImageDataGenerator
24 from keras.models
import model_from_json
25 from keras.optimizers
import SGD
26 from keras.utils
import np_utils
27 from os.path
import exists, isfile, join
30 from utils
import read_config, get_patch_size, count_events, shuffle_in_place
33 with
open(name +
'_architecture.json')
as f:
34 model = model_from_json(f.read())
35 model.load_weights(name +
'_weights.h5')
40 with
open(name +
'_architecture.json',
'w')
as f:
41 f.write(model.to_json())
42 model.save_weights(name +
'_weights.h5', overwrite=
True)
48 print 'Reading configuration...' 53 out_name = args.output
55 CNN_INPUT_DIR = config[
'training_on_patches'][
'input_dir']
58 img_rows, img_cols = PATCH_SIZE_W, PATCH_SIZE_D
60 batch_size = config[
'training_on_patches'][
'batch_size']
61 nb_epoch = config[
'training_on_patches'][
'nb_epoch']
62 nb_classes = config[
'training_on_patches'][
'nb_classes']
65 print 'Compiling CNN model...' 66 with tf.device(
'/gpu:' + args.gpu):
69 sgd = SGD(lr=0.002, decay=1e-5, momentum=0.9, nesterov=
True)
70 model.compile(optimizer=sgd,
71 loss={
'em_trk_none_netout':
'categorical_crossentropy',
'michel_netout':
'mean_squared_error'},
72 loss_weights={
'em_trk_none_netout': 0.1,
'michel_netout': 1.0})
76 X_train = np.zeros((n_training, PATCH_SIZE_W, PATCH_SIZE_D, 1), dtype=np.float32)
77 EmTrkNone_train = np.zeros((n_training, 3), dtype=np.int32)
78 Michel_train = np.zeros((n_training, 1), dtype=np.int32)
79 print 'Training data size:', n_training,
'events; patch size:', PATCH_SIZE_W,
'x', PATCH_SIZE_D
82 subdirs = [f
for f
in os.listdir(CNN_INPUT_DIR)
if 'training' in f]
84 for dirname
in subdirs:
85 print 'Reading data in', dirname
86 filesX = [f
for f
in os.listdir(CNN_INPUT_DIR +
'/' + dirname)
if '_x.npy' in f]
88 print '...training data', fnameX
89 fnameY = fnameX.replace(
'_x.npy',
'_y.npy')
90 dataX = np.load(CNN_INPUT_DIR +
'/' + dirname +
'/' + fnameX)
91 if dataX.dtype != np.dtype(
'float32'):
92 dataX = dataX.astype(
"float32")
93 dataY = np.load(CNN_INPUT_DIR +
'/' + dirname +
'/' + fnameY)
95 X_train[ntot:ntot+n] = dataX.reshape(n, img_rows, img_cols, 1)
96 EmTrkNone_train[ntot:ntot+n] = dataY[:,[0, 1, 3]]
97 Michel_train[ntot:ntot+n] = dataY[:,[2]]
99 print ntot,
'events ready' 102 X_test = np.zeros((n_testing, PATCH_SIZE_W, PATCH_SIZE_D, 1), dtype=np.float32)
103 EmTrkNone_test = np.zeros((n_testing, 3), dtype=np.int32)
104 Michel_test = np.zeros((n_testing, 1), dtype=np.int32)
105 print 'Testing data size:', n_testing,
'events' 108 subdirs = [f
for f
in os.listdir(CNN_INPUT_DIR)
if 'testing' in f]
110 for dirname
in subdirs:
111 print 'Reading data in', dirname
112 filesX = [f
for f
in os.listdir(CNN_INPUT_DIR +
'/' + dirname)
if '_x.npy' in f]
113 for fnameX
in filesX:
114 print '...testing data', fnameX
115 fnameY = fnameX.replace(
'_x.npy',
'_y.npy')
116 dataX = np.load(CNN_INPUT_DIR +
'/' + dirname +
'/' + fnameX)
117 if dataX.dtype != np.dtype(
'float32'):
118 dataX = dataX.astype(
"float32")
119 dataY = np.load(CNN_INPUT_DIR +
'/' + dirname +
'/' + fnameY)
121 X_test[ntot:ntot+n] = dataX.reshape(n, img_rows, img_cols, 1)
122 EmTrkNone_test[ntot:ntot+n] = dataY[:,[0, 1, 3]]
123 Michel_test[ntot:ntot+n] = dataY[:,[2]]
125 print ntot,
'events ready' 130 print 'Training', X_train.shape,
'testing', X_test.shape
133 datagen = ImageDataGenerator(
134 featurewise_center=
False, samplewise_center=
False,
135 featurewise_std_normalization=
False,
136 samplewise_std_normalization=
False,
138 rotation_range=0, width_shift_range=0, height_shift_range=0,
139 horizontal_flip=
True,
144 genY1 = generator.flow(X, Y1, batch_size=b, seed=7)
145 genY2 = generator.flow(X, Y2, batch_size=b, seed=7)
149 yield {
'main_input': g1[0]}, {
'em_trk_none_netout': g1[1],
'michel_netout': g2[1]}
151 print 'Fit config:', cfg_name
152 h = model.fit_generator(
155 {
'main_input': X_test},
156 {
'em_trk_none_netout': EmTrkNone_test,
'michel_netout': Michel_test}),
157 steps_per_epoch=X_train.shape[0]/batch_size, epochs=nb_epoch,
161 EmTrkNone_train =
None 164 score = model.evaluate({
'main_input': X_test},
165 {
'em_trk_none_netout': EmTrkNone_test,
'michel_netout': Michel_test},
167 print(
'Test score:', score)
170 EmTrkNone_test =
None 174 print h.history[
'loss']
175 print h.history[
'val_loss']
180 print(
'Error: model not saved.')
int open(const char *, int)
Opens a file descriptor.
def save_model(model, name)
def count_events(folder, key)
def get_patch_size(folder)
def generate_data_generator(generator, X, Y1, Y2, b)