Ejemplo n.º 1
0
def train(model_dir, cnn_model, saved_model=None, 
			learning_rate = 1e-5, decay=1e-6, 
			train_size = 0.8, seq_length=16,
			hidden_units=256, dense_units=256, reg=1e-1, dropout_rate=1e-1,
			num_classes=6, batch_size=16, nb_epoch=100, 
			image_shape=None):


	# ---- CALL BACK FUNCTIONS FOR FIT_GENERATOR() ---- #
	checkpoints_dir = os.path.join(model_dir, 'checkpoints')
	if not os.path.exists(checkpoints_dir):
		os.makedirs(checkpoints_dir)

	checkpointer = ModelCheckpoint(
			filepath=os.path.join(checkpoints_dir, 'lstm_weights.{epoch:004d}-{val_loss:.3f}.hdf5'),
			verbose=1, save_best_only=False, period=50)
	
	# tensorboard info
	tb = TensorBoard(log_dir=model_dir)

	# ------------------------------------------------- # 


	# PREPARE DATASET
	dataset = DataSet(cnn_model, seq_length)
	
	# steps_per_epoch = number of batches in one epoch
	steps_per_epoch = (len(dataset.data) * train_size) // batch_size

	# create train and validation generators
	generator = dataset.frame_generator(batch_size, 'train')
	# val_generator = dataset.frame_generator(batch_size, 'validation') # use all validation data each time?
	(X_val, y_val) = dataset.generate_data('validation')

	# load or create model
	if saved_model:
		rnn_model = load_model(saved_model)
	else:
		rnn_model = lstm(hidden_units=hidden_units, dense_units=dense_units, 
						reg=reg, dropout_rate=dropout_rate,
						seq_length=seq_length, num_classes=num_classes)
	
	# setup optimizer: ADAM algorithm
	optimizer = Adam(lr=learning_rate, decay=decay)
	
	# metrics for judging performance of model
	metrics = ['categorical_accuracy'] # ['accuracy']  # if using 'top_k_categorical_accuracy', must specify k
	
	rnn_model.compile(loss='categorical_crossentropy', optimizer=optimizer,
			metrics=metrics)

	print(rnn_model.summary())

	# use fit generator to generate data on the fly
	history = rnn_model.fit_generator(generator=generator,
									steps_per_epoch=steps_per_epoch,
									epochs=nb_epoch,
									verbose=1,
									callbacks=[tb, checkpointer],
									validation_data=(X_val, y_val),
									validation_steps=1)  # using all validation data for better metrics

	return history
Ejemplo n.º 2
0
    rnn_model.compile(loss='categorical_crossentropy',
                      optimizer=optimizer,
                      metrics=metrics)

    # load saved weights
    # folder_path = 'experiments/base_model/base_model_dropout_rate_0.00e+00/checkpoints/'
    folder_path = 'experiments/base_model/base_model_dropout_rate_3.00e-01/checkpoints/'

    saved_weights = os.path.join(folder_path, 'lstm_weights.0300-0.619.hdf5')
    rnn_model.load_weights(saved_weights)

    # load and prepare test set
    dataset = DataSet(cnn_model)

    X_train, Y_train = dataset.generate_data('train')
    X_val, Y_val = dataset.generate_data('validation')
    X_test, Y_test = dataset.generate_data('test')

    score = rnn_model.evaluate(x=X_train, y=Y_train, verbose=1)
    print("Train Loss: %2.3f" % score[0])
    print("Train Accuracy: %1.3f\n" % score[1])

    score = rnn_model.evaluate(x=X_val, y=Y_val, verbose=1)
    print("Val Loss: %2.3f" % score[0])
    print("Val Accuracy: %1.3f\n" % score[1])

    score = rnn_model.evaluate(x=X_test, y=Y_test, verbose=1)
    print("Test Loss: %2.3f" % score[0])
    print("Test Accuracy: %1.3f\n" % score[1])