def train_lstm(self): """ Train LSTM pipeline using pre-generated CNN codes. :param: :return: """ codesTrainData = json.load(open(self.params.files['lstm_training_struct'])) codesStats = json.load(open(self.params.files['cnn_codes_stats'])) metadataStats = json.load(open(self.params.files['dataset_stats'])) model = get_lstm_model(self.params, codesStats) #model = make_parallel(model, 4) model.compile(optimizer=Adam(lr=self.params.lstm_adam_learning_rate), loss='categorical_crossentropy', metrics=['accuracy']) train_datagen = codes_metadata_generator(self.params, codesTrainData, metadataStats, codesStats) print("training") filePath = os.path.join(self.params.directories['lstm_checkpoint_weights'], 'weights.{epoch:02d}.hdf5') checkpoint = ModelCheckpoint(filepath=filePath, monitor='loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1) callbacks_list = [checkpoint] #model.fit_generator(train_datagen, # steps_per_epoch=(len(codesTrainData) / self.params.batch_size_lstm + 1), # epochs=self.params.lstm_epochs, callbacks=callbacks_list, # max_queue_size=20) model.fit_generator(train_datagen, steps_per_epoch=(len(codesTrainData) / self.params.batch_size_lstm + 1), epochs=self.params.lstm_epochs, max_queue_size=20) model.save(self.params.files['lstm_model'])
def train_multi(self): """ Train LSTM pipeline using pre-generated CNN codes. :param: :return: """ allCodesTrainingData = json.load( open(self.params.files['multi_training_struct'])) # add 50% of the /val/ data (which has false_detection instances) to train dataset, leave rest for validation codesTrainData = {} codesValidData = {} for k, v in allCodesTrainingData.iteritems(): if k.startswith('val/'): if np.random.randint(2): codesTrainData[k] = v else: codesValidData[k] = v else: codesTrainData[k] = v assert len( allCodesTrainingData) == len(codesTrainData) + len(codesValidData) codesStats = json.load(open(self.params.files['cnn_codes_stats'])) if self.params.max_temporal != 0: codesStats['max_temporal'] = self.params.max_temporal metadataStats = json.load(open(self.params.files['dataset_stats'])) loaded_filename = None if self.params.args.load_model: model = load_model(self.params.args.load_model) loaded_filename = os.path.basename(self.params.args.load_model) else: model = get_multi_model(self.params, codesStats) if self.params.args.load_weights: model.load_weights(self.params.args.load_weights, by_name=True) loaded_filename = os.path.basename(self.params.args.load_weights) initial_epoch = self.get_initial_epoch(loaded_filename) if self.params.print_model_summary: model.summary() model = multi_gpu_model(model, gpus=self.params.gpus) model.compile(optimizer=RMSprop(lr=self.params.learning_rate), loss='categorical_crossentropy', metrics=['accuracy']) preffix = self.get_preffix() print("training multi-image model: " + preffix) filePath = os.path.join( self.params.directories['multi_checkpoint_weights'], preffix + '-epoch_' + '{epoch:02d}' + '-acc_' + '{acc:.4f}' + '-val_acc_' + '{val_acc:.4f}.hdf5') checkpoint = ModelCheckpoint(filepath=filePath, monitor='loss', verbose=1, save_best_only=False, save_weights_only=False, mode='auto', period=1) reduce_lr = ReduceLROnPlateau(monitor='val_acc', factor=0.1, patience=1, min_lr=1e-7, epsilon=0.01, verbose=1) model.fit_generator(generator=codes_metadata_generator(self.params, \ codesTrainData, metadataStats, codesStats, \ class_aware_sampling = not self.params.leave_unbalanced, \ temporal_dropout = self.params.temporal_dropout), steps_per_epoch=int(math.ceil((len(codesTrainData) / self.params.batch_size))), epochs=self.params.epochs, callbacks=[checkpoint, FMOW_Callback(), reduce_lr], initial_epoch = initial_epoch, validation_data = codes_metadata_generator(self.params, \ codesValidData, metadataStats, codesStats, \ class_aware_sampling = False,\ temporal_dropout = 0.), validation_steps = int(math.ceil((len(codesValidData) / self.params.batch_size))), ) model.save(self.params.files['multi_model'])