def transfer_learn(): test_actions = [ 'CleanAndJerk', 'MoppingFloor', 'FrontCrawl', # 'Surfing', 'Bowling', 'SoccerPenalty', 'SumoWrestling', 'Shotput', 'PlayingSitar', 'FloorGymnastics', # 'Typing', 'JumpingJack', 'ShavingBeard', 'FrisbeeCatch', 'WritingOnBoard', 'JavelinThrow', 'Fencing', # 'FieldHockeyPenalty', # 'BaseballPitch', 'CuttingInKitchen', # 'Kayaking', ] train_dataset, test_dataset = get_traditional_dataset( base_address=BASE_ADDRESS, class_sample_size=CLASS_SAMPLE_SIZE, test_actions=test_actions) with tf.variable_scope('train_data'): input_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) input_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, 85]) tf.summary.image('train', input_data_ph[:, 0, :, :, :], max_outputs=25) with tf.variable_scope('validation_data'): val_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) val_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, 85]) tf.summary.image('validation', val_data_ph[:, 0, :, :, :], max_outputs=25) gpu_devices = ['/gpu:{}'.format(gpu_id) for gpu_id in range(NUM_GPUS)] maml = ModelAgnosticMetaLearning(C3DNetwork, input_data_ph, input_labels_ph, val_data_ph, val_labels_ph, log_dir=LOG_DIR, gpu_devices=gpu_devices, meta_learn_rate=0.00001, learning_rate=0.001, train=TRAIN, log_device_placement=False) maml.load_model(path='MAML/sports1m_pretrained.model', load_last_layer=False) for it in range(TRANSFER_LEARNING_ITERATIONS): print(it) data = train_dataset.next_batch(num_classes=85, real_labels=True) batch_test_data, batch_test_labels = data['train'] batch_test_val_data, batch_test_val_labels = data['validation'] batch_split_size = int(NUM_CLASSES / BATCH_SPLIT_NUM) for batch_split_index in range(BATCH_SPLIT_NUM): start = batch_split_index * batch_split_size end = batch_split_index * batch_split_size + batch_split_size test_data = batch_test_data[start:end, :, :, :, :] test_labels = batch_test_labels[start:end, :] test_val_data = batch_test_val_data[start:end, :, :, :, :] test_val_labels = batch_test_val_labels[start:end, :] if it % 50 == 0: if batch_split_index == 0: val_accs = [] if it % 100 == 0: maml.save_model( 'saved_models/transfer_learning_85/model', step=it) run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() merged_summary = maml.sess.run(maml.merged, feed_dict={ input_data_ph: test_data, input_labels_ph: test_labels, val_data_ph: test_val_data, val_labels_ph: test_val_labels, }, options=run_options, run_metadata=run_metadata) maml.file_writer.add_summary(merged_summary, global_step=it + batch_split_index) outputs = maml.sess.run(maml.inner_model_out, feed_dict={ maml.input_data: test_val_data, maml.input_labels: test_val_labels, }) val_accs.append(print_accuracy(outputs, test_val_labels))\ if batch_split_index == BATCH_SPLIT_NUM - 1: print('iteration: {}'.format(it)) print('Validation accuracy on all batches: ') print(np.mean(val_accs)) maml.sess.run(maml.inner_train_ops, feed_dict={ input_data_ph: test_data, input_labels_ph: test_labels, }) maml.save_model('saved_models/transfer_learning_85/model', step=it)
def transfer_learn(): test_actions = [ 'CleanAndJerk', 'MoppingFloor', 'FrontCrawl', 'Surfing', # 0 'Bowling', 'SoccerPenalty', 'SumoWrestling', 'Shotput', 'PlayingSitar', 'FloorGymnastics', 'Typing', # 1 'JumpingJack', 'ShavingBeard', 'FrisbeeCatch', 'WritingOnBoard', 'JavelinThrow', 'Fencing', 'FieldHockeyPenalty', # 3 'BaseballPitch', # 4 'CuttingInKitchen', 'Kayaking', # 2 ] train_dataset, test_dataset = get_traditional_dataset( base_address=BASE_ADDRESS, class_sample_size=CLASS_SAMPLE_SIZE, test_actions=test_actions) with tf.variable_scope('train_data'): input_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) input_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, 20]) tf.summary.image('train', input_data_ph[:, 0, :, :, :], max_outputs=25) with tf.variable_scope('validation_data'): val_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) val_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, 20]) tf.summary.image('validation', val_data_ph[:, 0, :, :, :], max_outputs=25) gpu_devices = ['/gpu:{}'.format(gpu_id) for gpu_id in range(NUM_GPUS)] maml = ModelAgnosticMetaLearning(C3DNetwork, input_data_ph, input_labels_ph, val_data_ph, val_labels_ph, log_dir=LOG_DIR, gpu_devices=gpu_devices, meta_learn_rate=0.00001, learning_rate=0.001, train=TRAIN, log_device_placement=False) maml.load_model(path='saved_models/transfer_learning/model-400', load_last_layer=False) data = test_dataset.next_batch(num_classes=20) test_data, test_labels = data['train'] test_val_data, test_val_labels = data['validation'] print(test_dataset.actions) for it in range(TRANSFER_LEARNING_ITERATIONS): print(it) if it % 50 == 0: if it % 100 == 0: maml.save_model( 'saved_models/transfer_learning_to_20_classes/model', step=it) merged_summary = maml.sess.run(maml.merged, feed_dict={ input_data_ph: test_data, input_labels_ph: test_labels, val_data_ph: test_val_data, val_labels_ph: test_val_labels, }) maml.file_writer.add_summary(merged_summary, global_step=it) outputs = maml.sess.run(maml.inner_model_out, feed_dict={ maml.input_data: test_val_data, maml.input_labels: test_val_labels, }) val_acc = print_accuracy(outputs, test_val_labels) print('iteration: {}'.format(it)) print('Validation accuracy on all batches: ') print(val_acc) maml.sess.run(maml.inner_train_ops, feed_dict={ input_data_ph: test_data, input_labels_ph: test_labels, })
def train_maml(): with tf.variable_scope('train_data'): input_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) input_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, NUM_CLASSES]) tf.summary.image('train', input_data_ph[:, 0, :, :, :], max_outputs=NUM_CLASSES) with tf.variable_scope('validation_data'): val_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) val_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, NUM_CLASSES]) tf.summary.image('validation', val_data_ph[:, 0, :, :, :], max_outputs=NUM_CLASSES) gpu_devices = ['/gpu:{}'.format(gpu_id) for gpu_id in range(NUM_GPUS)] maml = ModelAgnosticMetaLearning( C3DNetwork, input_data_ph, input_labels_ph, val_data_ph, val_labels_ph, log_dir=LOG_DIR, gpu_devices=gpu_devices, meta_learn_rate=0.0001, learning_rate=0.001, train=TRAIN, log_device_placement=False, num_classes=101 ) if TRAIN: train_dataset, test_dataset = get_traditional_dataset( num_train_actions=600, base_address=BASE_ADDRESS, class_sample_size=CLASS_SAMPLE_SIZE, ) maml.load_model(path='MAML/sports1m_pretrained.model', load_last_layer=False) print('start meta training.') it = 0 for it in range(10001): train_dataset.sample_k_samples() data = train_dataset.next_batch(num_classes=NUM_CLASSES) tr_data, tr_labels = data['train'] val_data, val_labels = data['validation'] if it % 50 == 0: merged_summary = maml.sess.run(maml.merged, feed_dict={ input_data_ph: tr_data, input_labels_ph: tr_labels, val_data_ph: val_data, val_labels_ph: val_labels, }) maml.file_writer.add_summary(merged_summary, global_step=it) print(it) maml.sess.run(maml.train_op, feed_dict={ input_data_ph: tr_data, input_labels_ph: tr_labels, val_data_ph: val_data, val_labels_ph: val_labels, }) if it % 200 == 0: maml.save_model(path='saved_models/kinetics400/model', step=it) if it != 0: maml.save_model(path='saved_models/kinetics400/model', step=it) else: # test_actions = [ # 'CleanAndJerk', # 'MoppingFloor', # 'FrontCrawl', # 'Surfing', # 'Bowling', # 'SoccerPenalty', # 'SumoWrestling', # 'Shotput', # 'PlayingSitar', # 'FloorGymnastics', # 'Typing', # 'JumpingJack', # 'ShavingBeard', # 'FrisbeeCatch', # 'WritingOnBoard', # 'JavelinThrow', # 'Fencing', # 'FieldHockeyPenalty', # 'BaseballPitch', # 'CuttingInKitchen', # 'Kayaking', # ] test_actions = [ 'ApplyEyeMakeup', 'Archery', 'BabyCrawling', 'BandMarching', 'Bowling', 'Basketball', 'Biking', 'Billiards', 'BlowDryHair', 'FloorGymnastics', 'Typing', 'CliffDiving', 'ShavingBeard', 'FrisbeeCatch', 'WritingOnBoard', 'JavelinThrow', 'Fencing', 'FieldHockeyPenalty', 'PlayingPiano', 'CuttingInKitchen', ] train_dataset, test_dataset = get_traditional_dataset( base_address='/home/siavash/UCF-101/', class_sample_size=CLASS_SAMPLE_SIZE, test_actions=test_actions ) maml.load_model(path='saved_models/kinetics400/model-8000') print('Start testing the network') data = test_dataset.next_batch(num_classes=NUM_CLASSES) test_data, test_labels = data['train'] test_val_data, test_val_labels = data['validation'] print(test_dataset.actions) for it in range(5): maml.sess.run(maml.inner_train_ops, feed_dict={ input_data_ph: test_data, input_labels_ph: test_labels, }) if it % 1 == 0: merged_summary = maml.sess.run(maml.merged, feed_dict={ input_data_ph: test_data, input_labels_ph: test_labels, val_data_ph: test_val_data, val_labels_ph: test_val_labels, }) maml.file_writer.add_summary(merged_summary, global_step=it) print('gradient step: ') print(it) outputs = maml.sess.run(maml.inner_model_out, feed_dict={ maml.input_data: test_val_data, maml.input_labels: test_val_labels, }) print_accuracy(outputs, test_val_labels) maml.save_model('saved_models/ucf101-fit/model-kinetics-trained', step=it)
def transfer_learn(): train_dataset, test_dataset = get_traditional_dataset( base_address=BASE_ADDRESS, class_sample_size=CLASS_SAMPLE_SIZE, num_train_actions=400) with tf.variable_scope('train_data'): input_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) input_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, 400]) tf.summary.image('train', input_data_ph[:, 0, :, :, :], max_outputs=25) with tf.variable_scope('validation_data'): val_data_ph = tf.placeholder(dtype=tf.float32, shape=[None, 16, 112, 112, 3]) val_labels_ph = tf.placeholder(dtype=tf.float32, shape=[None, 400]) tf.summary.image('validation', val_data_ph[:, 0, :, :, :], max_outputs=25) gpu_devices = ['/gpu:{}'.format(gpu_id) for gpu_id in range(NUM_GPUS)] maml = ModelAgnosticMetaLearning(C3DNetwork, input_data_ph, input_labels_ph, val_data_ph, val_labels_ph, log_dir=LOG_DIR, gpu_devices=gpu_devices, meta_learn_rate=0.00001, learning_rate=0.001, train=TRAIN, log_device_placement=False) maml.load_model(path='MAML/sports1m_pretrained.model', load_last_layer=False) for it in range(TRANSFER_LEARNING_ITERATIONS): print(it) data, labels = train_dataset.next_simple_batch(batch_size=BATCH_SIZE) if it % 100 == 0: maml.save_model('saved_models/transfer_learning/model', step=it) if it % 20 == 0: merged_summary = maml.sess.run(maml.merged, feed_dict={ input_data_ph: data, input_labels_ph: labels, val_data_ph: data, val_labels_ph: labels, }) maml.file_writer.add_summary(merged_summary, global_step=it) outputs = maml.sess.run(maml.inner_model_out, feed_dict={ maml.input_data: data, maml.input_labels: labels, }) print_accuracy(outputs, labels) maml.sess.run(maml.inner_train_ops, feed_dict={ input_data_ph: data, input_labels_ph: labels, })