Ejemplo n.º 1
0
def loop_through_by_using_every_dataset_as_holdout_dataset(main_dir):

    hold_out_list = [
        'indoor-1-5-way-3000', 'indoor-1-my1-5-way-3000',
        'indoor-1-my2-5-way-3000'
    ]

    for hold_index, hold_name in enumerate(hold_out_list):

        if hold_index == 0:
            continue

        sub_dir = main_dir + os.sep + hold_name

        if not os.path.exists(sub_dir):
            os.mkdir(sub_dir)
        print_start_of_new_input_pipline_to_all_loggers(
            'Using %s as holdout set' % hold_name)

        dataset_filenames, dataset_sizes = dataset_name_factory.new_get_train_test_data_with_holdout_5_way_half_dataset(
            hold_index)

        with open(sub_dir + os.sep + 'dataset_filenames_and_sizes.txt',
                  'w') as f:
            for k, v in dataset_filenames.items():
                f.write(str(k) + ":" + str(v))
                f.write('\n')

            for k, v in dataset_sizes.items():
                f.write(str(k) + ":" + str(v))
                f.write('\n')

        tf.reset_default_graph()
        configp = tf.ConfigProto(allow_soft_placement=True,
                                 log_device_placement=False)
        sess = tf.InteractiveSession(config=configp)

        with sess.as_default():
            cnn_variable_initializer.set_from_main(sess)
            cnn_variable_initializer.build_tensorflw_variables_multiple_5_way()
            models_utils.set_from_main(sess, logger)

            train_cnn_multiple_epochs(sess,
                                      75,
                                      5,
                                      dataset_filenames,
                                      dataset_sizes,
                                      train_fraction=1,
                                      valid_fraction=1)
            # ============================================================================
            # Persisting data
            # ============================================================================
            logger.info('Saving CNN Model')
            #cnn_model_visualizer.save_cnn_hyperparameters(sub_dir, kernel_size_dict, stride_dict,
            #                                              scope_list, 'hyperparams-final.pickle')
            #cnn_model_visualizer.save_cnn_weights_multiple(sub_dir, sess, 'cnn-model-final.ckpt')

            sess.close()
        accuracyFH = logging.FileHandler(IMG_DIR + os.sep + 'accuracy_'+ str(env_idx) +'_.log', mode='w')
        accuracyFH.setFormatter(logging.Formatter('%(message)s'))
        accuracyFH.setLevel(logging.INFO)
        accuracy_logger.addHandler(accuracyFH)
        accuracy_logger.info('#Train EnvID, Epoch, Test EnvID, Non-collision Accuracy,Non-collision Accuracy(Soft),Non-collision loss,' +
                         'Preci-NC-L,Preci-NC-S,Preci-NC-R,,Rec-NC-L,Rec-NC-S,Rec-NC-R')
        accuracy_loggers.append(accuracy_logger)

    graph = tf.Graph()
    configp = tf.ConfigProto(allow_soft_placement=True,log_device_placement=False)
    sess = tf.InteractiveSession(graph=graph,config=configp)

    with sess.as_default() and graph.as_default():
        cnn_variable_initializer.set_from_main(sess)
        cnn_variable_initializer.build_tensorflw_variables_detached()
        models_utils.set_from_main(sess,logger)

        train_data_gen = data_generator.DataGenerator(
            config.BATCH_SIZE, config.TF_NUM_CLASSES, dataset_sizes['train_dataset'],
            config.TF_INPUT_SIZE, sess, dataset_filenames['train_dataset'], config.TF_INPUT_AFTER_RESIZE,False
        )

        test_data_gen = data_generator.DataGenerator(
            config.BATCH_SIZE, config.TF_NUM_CLASSES, dataset_sizes['test_dataset'],
            config.TF_INPUT_SIZE, sess, dataset_filenames['test_dataset'], config.TF_INPUT_AFTER_RESIZE, True
        )

        tf_train_img_ids, tf_train_images, tf_train_labels = train_data_gen.tf_augment_data_with()
        tf_test_img_ids, tf_test_images, tf_test_labels = test_data_gen.tf_augment_data_with()

        define_tf_ops(tf_train_images, tf_train_labels, tf_test_images, tf_test_labels)
def train_using_all_data():

    dataset_filenames = {
        'train_dataset': ['..' + os.sep + 'data_indoor_1_1000' + os.sep + 'image-direction-shuffled.tfrecords'],
        'train_bump_dataset': [
            '..' + os.sep + 'data_indoor_1_bump_200' + os.sep + 'image-direction-shuffled.tfrecords'],
        'test_dataset': ['..' + os.sep + 'data_grande_salle_1000' + os.sep + 'image-direction-shuffled.tfrecords'],
        'test_bump_dataset': [
            '..' + os.sep + 'data_grande_salle_bump_200' + os.sep + 'image-direction-shuffled.tfrecords']
    }

    dataset_sizes = {'train_dataset': 1000 + 1000,
                     'train_bump_dataset': 400,
                     'test_dataset': 1000,
                     'test_bump_dataset': 200}

    with sess.as_default() and graph.as_default():
        cnn_variable_initializer.set_from_main(sess, graph)
        cnn_variable_initializer.build_tensorflw_variables_naive()
        models_utils.set_from_main(sess, graph, logger)

        all_train_files, all_bump_files = [], []
        all_test_files, all_bump_test_files = [], []

        all_train_files = dataset_filenames['train_dataset']
        all_bump_files = dataset_filenames['train_bump_dataset']
        all_test_files = dataset_filenames['test_dataset']
        all_bump_test_files = dataset_filenames['test_bump_dataset']

        tf_img_ids, tf_images, tf_labels = models_utils.build_input_pipeline(
            all_train_files, config.BATCH_SIZE, shuffle=True,
            training_data=True, use_opposite_label=False, inputs_for_sdae=False)

        tf_bump_img_ids, tf_bump_images, tf_bump_labels = models_utils.build_input_pipeline(
            all_bump_files, config.BATCH_SIZE, shuffle=True,
            training_data=True, use_opposite_label=True, inputs_for_sdae=False)

        tf_test_img_ids, tf_test_images, tf_test_labels = models_utils.build_input_pipeline(all_test_files,
                                                                                            config.BATCH_SIZE,
                                                                                            shuffle=False,
                                                                                            training_data=False,
                                                                                            use_opposite_label=False,
                                                                                            inputs_for_sdae=False)
        tf_bump_test_img_ids, tf_bump_test_images, tf_bump_test_labels = models_utils.build_input_pipeline(
            all_bump_test_files, config.BATCH_SIZE, shuffle=False,
            training_data=False, use_opposite_label=True, inputs_for_sdae=False)

        print('\t\tAverage test accuracy: %.5f ' % np.mean(test_accuracy))
        print('\t\tAverage test accuracy(soft): %.5f' % np.mean(soft_test_accuracy))
        print('\t\tAverage test precision: %s', test_noncol_precision)
        print('\t\tAverage test recall: %s', test_noncol_recall)

        print('\t\tAverage bump test accuracy: %.5f ' % np.mean(bump_test_accuracy))
        print('\t\tAverage bump test (soft) accuracy: %.5f ' % np.mean(bump_soft_accuracy))
        print('\t\tAverage test bump precision: %s', test_col_precision)
        print('\t\tAverage test bump recall: %s', test_col_recall)

        noncol_precision_string = ''.join(['%.3f;' % test_noncol_precision[pi] for pi in range(3)])
        noncol_recall_string = ''.join(['%.3f;' % test_noncol_recall[ri] for ri in range(3)])
        col_precision_string = ''.join(['%.3f;' % test_col_precision[pi] for pi in range(3)])
        col_recall_string = ''.join(['%.3f;' % test_col_recall[ri] for ri in range(3)])

        SummaryLogger.info('%d;%.3f;%.3f;%.3f;%.3f;%.5f;%.5f;%s;%s;%s;%s', epoch, np.mean(test_accuracy),
                             np.mean(soft_test_accuracy),
                             np.mean(bump_test_accuracy), np.mean(bump_soft_accuracy), np.mean(avg_loss),
                             np.mean(avg_bump_loss),
                             noncol_precision_string, noncol_recall_string, col_precision_string, col_recall_string)
def loop_through_by_using_every_dataset_as_holdout_dataset(main_dir,n_epochs):
    global min_thresh,max_thresh

    hold_out_list = ['apartment-my1-2000', 'apartment-my2-2000', 'apartment-my3-2000',
                     'indoor-1-2000', 'indoor-1-my1-2000', 'grande_salle-my1-2000',
                     'grande_salle-my2-2000', 'sandbox-2000']

    for hold_index, hold_name in enumerate(hold_out_list):
        sub_dir = main_dir + os.sep + hold_name

        if not os.path.exists(sub_dir):
            os.mkdir(sub_dir)
        print_start_of_new_input_pipline_to_all_loggers('Using %s as holdout set' % hold_name)

        dataset_filenames, dataset_sizes = dataset_name_factory.new_get_train_test_data_with_holdout(hold_index)

        dsize = dataset_sizes['train_dataset']

        validdsize = dataset_sizes['valid_dataset']
        testdsize = dataset_sizes['test_dataset']
        bumptestdsize = dataset_sizes['test_bump_dataset']

        with open(sub_dir + os.sep + 'dataset_filenames_and_sizes.txt', 'w') as f:
            for k, v in dataset_filenames.items():
                f.write(str(k) + ":" + str(v))
                f.write('\n')

            for k, v in dataset_sizes.items():
                f.write(str(k) + ":" + str(v))
                f.write('\n')

        tf.reset_default_graph()
        configp = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)
        sess = tf.InteractiveSession(config=configp)

        tf_img_ids, tf_images, tf_labels = {},{},{}
        for direct in ['left','straight','right']:
            tf_img_ids[direct], tf_images[direct], tf_labels[direct] = models_utils.build_input_pipeline(
                dataset_filenames['train_dataset'][direct], config.BATCH_SIZE, shuffle=True,
                training_data=True, use_opposite_label=False, inputs_for_sdae=False, rand_valid_direction_for_bump=False)

        tf_valid_img_ids, tf_valid_images, tf_valid_labels = models_utils.build_input_pipeline(
            dataset_filenames['valid_dataset'], config.BATCH_SIZE, shuffle=True,
            training_data=True, use_opposite_label=False, inputs_for_sdae=False, rand_valid_direction_for_bump=False)

        tf_test_img_ids, tf_test_images, tf_test_labels = models_utils.build_input_pipeline(
            dataset_filenames['test_dataset'],
            config.BATCH_SIZE,
            shuffle=False,
            training_data=False,
            use_opposite_label=False,
            inputs_for_sdae=False, rand_valid_direction_for_bump=False)

        tf_bump_test_img_ids, tf_bump_test_images, tf_bump_test_labels = models_utils.build_input_pipeline(
            dataset_filenames['test_bump_dataset'], config.BATCH_SIZE, shuffle=False,
            training_data=False, use_opposite_label=True, inputs_for_sdae=False, rand_valid_direction_for_bump=False)

        with sess.as_default():
            cnn_variable_initializer.set_from_main(sess)
            cnn_variable_initializer.build_tensorflw_variables_naive()
            models_utils.set_from_main(sess, logger)

            output_activation = 'sigmoid'

            if output_activation == 'sigmoid':
                max_thresh = 0.6
                min_thresh = 0.4
            else:
                raise NotImplementedError

            train_results, test_results = train_with_non_collision(
                sess, tf_images, tf_labels, dsize,
                tf_valid_images, tf_valid_labels, validdsize,
                tf_test_images, tf_test_labels, tf_test_img_ids, testdsize,
                tf_bump_test_images, tf_bump_test_labels, tf_bump_test_img_ids, bumptestdsize,
                n_epochs, test_interval, sub_dir,
                include_bump_test_data=True, use_cross_entropy=True, activation=output_activation
            )

            sess.close()