def discriminator(output_shared, n_corpus, state=True): ''' classification layer :param output_pub: :return: ''' dr_gan = Dropout(0.5, name='dr_gan')(output_shared) pool_gan = GlobalAveragePooling1D(name='pool_gan')(dr_gan) output = Dense(n_corpus, activation='softmax', name='softmax_gan')(pool_gan) if state==False: dr_gan.trainable = False pool_gan.trainable = False output.trainable = False return output
def train_models(config): # obtain x_dim x_dim = config['x_dim'] if not os.path.exists(os.path.join( config['exp_save_dir'], "models", "vis")): os.makedirs(os.path.join( config['exp_save_dir'], "models", 'vis')) logger = set_logger(os.path.join(config['exp_save_dir'], "models", 'train_model_architecture_' + dt.now().strftime("%Y-%m-%dT%H-%M-%SZ") + '.log')) print_and_log(logger, "x_dim: {}".format(x_dim)) # LTSM model architecture input_sequences = Input(shape=(config['window_size'], x_dim), name='input_sequences') lstm_one = LSTM(250, return_sequences=True, name="lstm_one")( input_sequences) lstm_two = LSTM(150, name="lstm_two")(lstm_one) # branching event type dense_one_layer = Dense(128, activation='relu', name="dense_one") dense_one = dense_one_layer(lstm_two) dropout_one_layer = Dropout(0.5, name="dropout_one") dropout_one = dropout_one_layer(dense_one) dense_two_layer = Dense(64, activation='relu', name="dense_two") dense_two = dense_two_layer(dropout_one) dropout_two_layer = Dropout(0.5, name="dropout_two") dropout_two = dropout_two_layer(dense_two) event_type_output_layer = Dense(len(config['eventtype_2_id']), activation='softmax', name="event_type_output") event_type_output = event_type_output_layer(dropout_two) # branching time delay dense_four_layer = Dense(128, activation='relu', name="dense_four") dense_four = dense_four_layer(lstm_two) dropout_three_layer = Dropout(0.5, name="dropout_three") dropout_three = dropout_three_layer(dense_four) dense_five_layer = Dense(64, activation='relu', name="dense_five") dense_five = dense_five_layer(dropout_three) dropout_four_layer = Dropout(0.5, name="dropout_four") dropout_four = dropout_four_layer(dense_five) time_delay_output_layer = Dense(1, activation='linear', name="time_delay_output") time_delay_output = time_delay_output_layer(dropout_four) model = Model(inputs=input_sequences, outputs=[event_type_output, time_delay_output]) model = multi_gpu_model(model, gpus=config['num_gpu']) model.summary(print_fn=logger.info) print(model.summary()) # get partition and labels with open(os.path.join(config['exp_save_dir'], "dataset", 'partition.json'), 'r') as f: partition = json.load(f) with open(os.path.join(config['exp_save_dir'], "dataset", 'labels.json'), 'r') as f: labels = json.load(f) # train val generator training_generator = DataGenerator_ET_TD( os.path.join(config['exp_save_dir'], "dataset"), partition['train'], labels, batch_size=config['batch_size'], dim=x_dim, window_size=config['window_size'], n_classes=len(config['eventtype_2_id']), shuffle=config['generator_shuffle']) validation_generator = DataGenerator_ET_TD( os.path.join(config['exp_save_dir'], "dataset"), partition['validation'], labels, batch_size=config['batch_size'], dim=x_dim, window_size=config['window_size'], n_classes=len(config['eventtype_2_id']), shuffle=config['generator_shuffle']) # callback if not os.path.exists(os.path.join( config['exp_save_dir'], "models", "vis")): os.makedirs(os.path.join( config['exp_save_dir'], "models", 'vis')) TensorBoard_callback = TensorBoard( log_dir=os.path.join(config['exp_save_dir'], "models", 'vis'), histogram_freq=0, write_graph=True, write_images=True, write_grads=False) """ Keras TensorBoard Reference: https://keras.io/callbacks/#tensorboard launch: tensorboard --logdir=/full_path_to_your_logs """ callbacks = [ModelCheckpoint(os.path.join(config['exp_save_dir'], "models", 'model.hdf5'), monitor='val_loss', verbose=2), TensorBoard_callback] # save train confg in case testing need it with open(os.path.join(config['exp_save_dir'], "models", 'train_config.pickle'), 'wb') as f: pickle.dump(config, f) print("{} saved!".format('train_config.pickle')) # model_history model_history = dict() # start training for epoch in range(config['num_epochs']): if epoch % (6+1) == 0: # freeze time delay for 1 epoch print("\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!! " "Epoch {}/{} {}".format( epoch+1, config['num_epochs'], "---> train event type branch...")) dense_one_layer.trainable = True dropout_one_layer.trainable = True dense_two_layer.trainable = True dropout_two_layer.trainable = True event_type_output_layer.trainable = True dense_four_layer.trainable = False dropout_three_layer.trainable = False dense_five_layer.trainable = False dropout_four_layer.trainable = False time_delay_output_layer.trainable = False else: # freeze event type for 6 epoch print("\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!! " "Epoch {}/{} {}".format( epoch+1, config['num_epochs'], "---> train time delay branch...")) dense_one_layer.trainable = False dropout_one_layer.trainable = False dense_two_layer.trainable = False dropout_two_layer.trainable = False event_type_output_layer.trainable = False dense_four_layer.trainable = True dropout_three_layer.trainable = True dense_five_layer.trainable = True dropout_four_layer.trainable = True time_delay_output_layer.trainable = True trainable_count = int( np.sum([K.count_params(p) for p in set(model.trainable_weights)])) non_trainable_count = int( np.sum([K.count_params(p) for p in set( model.non_trainable_weights)])) print('Total params: {:,}'.format( trainable_count + non_trainable_count)) print('Trainable params: {:,}'.format(trainable_count)) print('Non-trainable params: {:,}'.format(non_trainable_count)) model.compile(optimizer='adam', loss=[categorical_focal_loss( gamma=config['focalloss_gamma'], alpha=config['focalloss_alpha']), logcosh], loss_weights=config['loss_weights'], metrics={'event_type_output': ( metrics.categorical_accuracy), 'time_delay_output': ( losses.mean_squared_error)}) history = model.fit_generator( generator=training_generator, epochs=1, callbacks=callbacks, validation_data=validation_generator, use_multiprocessing=True, workers=config['multiprocessing_cpu'], shuffle=True) """ Whether to shuffle the order of the batches at the beginning of each epoch. Only used with instances of Sequence (keras.utils.Sequence). Has no effect when steps_per_epoch is not None. Basically, no effect here. https://stackoverflow.com/questions/49027174/ what-does-shuffle-do-in-fit-generator-in-keras """ model.save(os.path.join(config['exp_save_dir'], "models", 'model-{}.hdf5'.format(epoch+1))) print("model-{}.hdf5 saved!".format(epoch+1)) if len(model_history) == 0: model_history = history.history.copy() else: for key in history.history: model_history[key] += history.history[key] with open(os.path.join(config['exp_save_dir'], "models", 'history-{}.pickle'.format(epoch+1)), 'wb') as f: pickle.dump(model_history, f) print("history-{}.pickle saved!".format(epoch+1)) with open(os.path.join(config['exp_save_dir'], "models", 'history.pickle'), 'wb') as f: pickle.dump(model_history, f) print("history.pickle saved!") return
video_embed = Dropout(dropout_rate)(Dense(128, input_shape=(maxlen, vecin.shape[1]), activation='softmax', name='video_dense')(video_input)) # kernel_regularizer=regularizers.l2(0.01), activation='softmax', name='video_dense')(video_input)) texts_input = Input(shape=(maxlen, len(word_indices)), name='texts_input') texts_lstm = LSTM(lstm_size, return_sequences=True, input_shape=(maxlen, len(word_indices)), dropout=dropout_rate, recurrent_dropout=dropout_rate, name='LSTM_input')(texts_input) layer_0 = Dropout(dropout_rate)(concatenate( [topic_embed, video_embed, texts_lstm], axis=2, name='merged_input')) layer_1 = LSTM(lstm_size, return_sequences=False, input_shape=(lstm_size,), dropout=dropout_rate, recurrent_dropout=dropout_rate, name='meta-LSTM')(layer_0) layer_2 = Dense(len(word_indices), name='word_selector')(layer_1) output = Activation('softmax', name='output')(layer_2) topic_input.trainable = True topic_embed.trainable = True video_input.trainable = True video_embed.trainable = True texts_input.trainable = True texts_lstm.trainable = True layer_1.trainable = True model_video = Model(inputs=[topic_input, video_input, texts_input], outputs=[output]) model_video.compile(loss='categorical_crossentropy', optimizer=optimiser) params = np.repeat(np.atleast_2d(video_param), maxlen, axis=0) Writing = True while Writing or Loop: model_video.load_weights(brain_file) # glitch_layer = model_video.layers[10].get_weights()