示例#1
0
def write_summary(value, tag, summary_writer, global_step):
    """Write a single summary value to tensorboard"""
    summary = tf.Summary()
    summary.value.add(tag=tag, simple_value=value)
    summary_writer.add_summary(summary, global_step)
示例#2
0
                    _gradients = []  # clean list of gradients
                    # acumulate loss
                    train_loss += gradIterationLoss
                    iterationLossCounter += gradIterationLossCounter
                    gradIterationLoss, gradIterationLossCounter = 0, 0
            if iteration % 10 == 0:
                print('Iteration ', iteration, ' out of ', train_iterations, ' finished.')
        # ----------------------------------------------------------------------
        # -----------------------------------------------------------------------
        # avg accumulators
        with open('Trainings-outputs.txt', 'a') as f:
            f.write('acumulated accuracy: ' + str(train_acc) + ' iterations: ' + str(accuracyIterationsCounter))
            f.write('\n')
        train_loss /= iterationLossCounter  # average loss
        train_acc /= accuracyIterationsCounter  # average accuracy
        train_summary = tf.Summary(value=[tf.Summary.Value(tag='train_loss', simple_value=train_loss),
                                          tf.Summary.Value(tag='train_accuracy', simple_value=train_acc)])
        # -----------------------------------------------------------------------
        # VALIDATION
        if settings['validImagesDir'] != '':
            validBatchReader = BatchReader(settings['validImagesDir'], 0.0, 1.0, 0.0)
        else:
            validBatchReader = batchReader

        if validBatchReader.getNOfIterations('Valid') > 0:
            valid_acc, valid_loss, valid_summary = performValidation(sess, validBatchReader, valid_iterator)
            # summary_writer.add_summary(summary=valid_summary, global_step=epoch)
            # summary_writer.add_summary(summary=train_summary, global_step=epoch)
            # summary_writer.flush()
            line = "epoch: %d/%d, train_loss: %.4f, train_acc: %.4f, valid_loss: %.4f, valid_acc: %.4f \n" % (
                epoch, settings['total_epochs'], train_loss, train_acc, valid_loss, valid_acc)
            print(line)
示例#3
0
def eval_once(saver, summary_writer, label_predict, summary_op, labels, images,
              filenames, logits):
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            saver.restore(sess, ckpt.model_checkpoint_path)
            # Assuming model_checkpoint_path looks something like:
            #   /my-favorite-path/cifar10_train/model.ckpt-0,
            # extract global_step from it.
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
        else:
            print('No checkpoint file found')
            return

        # Start the queue runners.
        coord = tf.train.Coordinator()
        try:
            threads = []
            for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
                threads.extend(
                    qr.create_threads(sess,
                                      coord=coord,
                                      daemon=True,
                                      start=True))

            num_iter = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size))
            true_count = 0  # Counts the number of correct predictions.
            total_sample_count = num_iter * FLAGS.batch_size
            step = 0
            while step < num_iter and not coord.should_stop():
                #predictions = sess.run([top_k_op])
                predictions, image, label, filename, logit = sess.run(
                    [label_predict, images, labels, filenames, logits])
                #image = sess.run(images)
                #label = sess.run(labels)
                saveLabelResults(image, label, predictions, filename, logit)
                true_count += np.sum(predictions)
                step += 1

            # Compute precision @ 1.
            precision = true_count / total_sample_count
            print('%s: precision @ 1 = %.3f' % (datetime.now(), precision))

            summary = tf.Summary()
            summary.ParseFromString(sess.run(summary_op))
            summary.value.add(tag='Precision @ 1', simple_value=precision)
            summary_writer.add_summary(summary, global_step)
        except Exception as e:  # pylint: disable=broad-except
            coord.request_stop(e)

        coord.request_stop()
        coord.join(threads, stop_grace_period_secs=10)
def write_summary(value, tag, summary_writer, global_step):
    summary = tf.Summary()
    summary.value.add(tag=tag, simple_value=value)
    summary_writer.add_summary(summary, global_step)
示例#5
0
def run_training(hypes, modules, tv_graph, tv_sess, start_step=0):
    """Run one iteration of training."""
    # Unpack operations for later use
    summary = tf.Summary()
    sess = tv_sess['sess']
    summary_writer = tv_sess['writer']

    solver = modules['solver']

    display_iter = hypes['logging']['display_iter']
    write_iter = hypes['logging'].get('write_iter', 5 * display_iter)
    eval_iter = hypes['logging']['eval_iter']
    save_iter = hypes['logging']['save_iter']
    image_iter = hypes['logging'].get('image_iter', 5 * save_iter)
    dict_smoother = ExpoSmoother(0.95)
    py_smoother = MedianSmoother(20)
    n = 0

    eval_names, eval_ops = zip(*tv_graph['eval_list'])
    # Run the training Step
    start_time = time.time()
    for step in range(start_step, hypes['solver']['max_steps']):
        regression_weights = solver.get_regression_weights(step, 1.0)
        lr = solver.get_learning_rate(hypes, step)
        feed_dict = {
            tv_graph['learning_rate']: lr,
            hypes['solver']['regression_weights']: regression_weights
        }
        if step % display_iter:
            sess.run([tv_graph['train_op']], feed_dict=feed_dict)

        # Write the summaries and print an overview fairly often.
        elif step % display_iter == 0:
            # Print status to stdout.
            _, loss_value, training_loss, eval_results = sess.run(
                [
                    tv_graph['train_op'], tv_graph['losses']['total_loss'],
                    tv_graph['losses'], eval_ops
                ],
                feed_dict=feed_dict)
            _print_training_status(hypes, step, loss_value, start_time, lr)
            _print_eval_dict(eval_names, eval_results, prefix='   (raw)')

            dict_smoother.update_weights(eval_results)
            smoothed_results = dict_smoother.get_weights()

            _print_eval_dict(eval_names, smoothed_results, prefix='(smooth)')

            #logging.info('Regression Weights: Depth: %.2f, Location: %.2f, Corner: %.2f'%(regression_weights[0], \
            #              regression_weights[1], regression_weights[2]))
            # Reset timer
            start_time = time.time()

        if step % write_iter == 0:
            # write values to summary
            if FLAGS.summary:
                summary_str = sess.run(tv_sess['summary_op'],
                                       feed_dict=feed_dict)
                summary_writer.add_summary(summary_str, global_step=step)
            summary.value.add(tag='training/total_loss',
                              simple_value=float(loss_value))
            summary.value.add(tag='training/learning_rate', simple_value=lr)
            summary_writer.add_summary(summary, step)
            # Convert numpy types to simple types.
            eval_results = np.array(eval_results)
            eval_results = eval_results.tolist()
            eval_dict = zip(eval_names, eval_results)
            _write_eval_dict_to_summary(eval_dict, 'Eval/raw', summary_writer,
                                        step)
            eval_dict = zip(eval_names, smoothed_results)
            _write_eval_dict_to_summary(eval_dict, 'Eval/smooth',
                                        summary_writer, step)

        # Do a evaluation and print the current state
        if (step) % eval_iter == 0 \
           or (step + 1) == hypes['solver']['max_steps']:
            # write checkpoint to disk

            logging.info('Running Evaluation Script.')

            eval_dict, images = modules['eval'].evaluate(
                hypes, sess, tv_graph['image_pl'], tv_graph['calib_pl'],
                tv_graph['xy_scale_pl'], tv_graph['inf_out'])

            _write_images_to_summary(images, summary_writer, step)
            logging.info("Evaluation Finished. All results will be saved to:")
            logging.info(hypes['dirs']['output_dir'])

            if images is not None and len(images) > 0:

                name = str(n % 10) + '_' + images[0][0]
                image_file = os.path.join(hypes['dirs']['image_dir'], name)
                imageio.imsave(image_file, images[0][1])
                n = n + 1

            logging.info('Raw Results:')
            utils.print_eval_dict(eval_dict, prefix='(raw)   ')
            _write_eval_dict_to_summary(eval_dict, 'Evaluation/raw',
                                        summary_writer, step)

            logging.info('Smooth Results:')
            names, res = zip(*eval_dict)
            smoothed = py_smoother.update_weights(res)
            eval_dict = zip(names, smoothed)
            utils.print_eval_dict(eval_dict, prefix='(smooth)')
            _write_eval_dict_to_summary(eval_dict, 'Evaluation/smoothed',
                                        summary_writer, step)

            # Reset timer
            start_time = time.time()

        # Save a checkpoint periodically.

        if (step) % save_iter == 0 and step > 0 or \
           (step + 1) == hypes['solver']['max_steps']:
            # write checkpoint to disk
            checkpoint_path = os.path.join(hypes['dirs']['output_dir'],
                                           'model.ckpt')
            tv_sess['saver'].save(sess, checkpoint_path, global_step=step)
            # Reset timer
            start_time = time.time()

        if step % image_iter == 0 and step > 0 or \
           (step + 1) == hypes['solver']['max_steps']:
            _write_images_to_disk(hypes, images, step)
def main():
    args = parser.parse_args()

    # We store all arguments in a json file. This has two advantages:
    # 1. We can always get back and see what exactly that experiment was
    # 2. We can resume an experiment as-is without needing to remember all flags.
    args_file = os.path.join(args.experiment_root, 'args.json')
    if args.resume:
        if not os.path.isfile(args_file):
            raise IOError('`args.json` not found in {}'.format(args_file))

        print('Loading args from {}.'.format(args_file))
        with open(args_file, 'r') as f:
            args_resumed = json.load(f)
        args_resumed['resume'] = True  # This would be overwritten.

        # When resuming, we not only want to populate the args object with the
        # values from the file, but we also want to check for some possible
        # conflicts between loaded and given arguments.
        for key, value in args.__dict__.items():
            if key in args_resumed:
                resumed_value = args_resumed[key]
                if resumed_value != value:
                    print('Warning: For the argument `{}` we are using the'
                          ' loaded value `{}`. The provided value was `{}`'
                          '.'.format(key, resumed_value, value))
                    comand = input('Would you like to restore it?(yes/no)')
                    if comand == 'yes':
                        args.__dict__[key] = resumed_value
                        print('For the argument `{}` we are using the loaded value `{}`.'.format(key,
                                                                                                 args.__dict__[key]))
                    else:
                        print('For the argument `{}` we are using the provided value `{}`.'.format(key,
                                                                                                   args.__dict__[key]))
            else:
                print('Warning: A new argument was added since the last run:'
                      ' `{}`. Using the new value: `{}`.'.format(key, value))
        os.remove(args_file)
        with open(args_file, 'w') as f:
            json.dump(vars(args), f, ensure_ascii=False, indent=2, sort_keys=True)

    else:
        # If the experiment directory exists already, we bail in fear.
        if os.path.exists(args.experiment_root):
            if os.listdir(args.experiment_root):
                print('The directory {} already exists and is not empty.'
                      ' If you want to resume training, append --resume to'
                      ' your call.'.format(args.experiment_root))
                exit(1)
        else:
            os.makedirs(args.experiment_root)

        # Store the passed arguments for later resuming and grepping in a nice
        # and readable format.
        with open(args_file, 'w') as f:
            json.dump(vars(args), f, ensure_ascii=False, indent=2, sort_keys=True)

    log_file = os.path.join(args.experiment_root, "train")
    logging.config.dictConfig(common.get_logging_dict(log_file))
    log = logging.getLogger('train')

    # Also show all parameter values at the start, for ease of reading logs.
    log.info('Training using the following parameters:')
    for key, value in sorted(vars(args).items()):
        log.info('{}: {}'.format(key, value))

    # Check them here, so they are not required when --resume-ing.
    if not args.train_set:
        parser.print_help()
        log.error("You did not specify the `train_set` argument!")
        sys.exit(1)
    if not args.image_root:
        parser.print_help()
        log.error("You did not specify the required `image_root` argument!")
        sys.exit(1)


######################################################################################
#prepare the training dataset
    # Load the data from the TxT file. see Common.load_dataset function for details
    pids_train, fids_train = common.load_dataset(args.train_set, args.image_root)
    max_fid_len = max(map(len, fids_train))  # We'll need this later for logfiles.

    # Setup a tf.Dataset where one "epoch" loops over all PIDS.
    # PIDS are shuffled after every epoch and continue indefinitely.
    unique_pids = np.unique(pids_train)
    dataset = tf.data.Dataset.from_tensor_slices(unique_pids)
    dataset = dataset.shuffle(len(unique_pids))

    # Constrain the dataset size to a multiple of the batch-size, so that
    # we don't get overlap at the end of each epoch.
    dataset = dataset.take((len(unique_pids) // args.batch_p) * args.batch_p)
    dataset = dataset.repeat(None)  # Repeat forever. Funny way of stating it.

    # For every PID, get K images.
    dataset = dataset.map(lambda pid: sample_k_fids_for_pid(
        pid, all_fids=fids_train, all_pids=pids_train, batch_k=args.batch_k))  # now the dataset has been modified as [selected_fids
    # , pid] due to the return of the function 'sample_k_fids_for_pid'

    # Ungroup/flatten the batches for easy loading of the files.
    dataset = dataset.apply(tf.contrib.data.unbatch())

    # Convert filenames to actual image tensors.
    net_input_size = (args.net_input_height, args.net_input_width)
    pre_crop_size = (args.pre_crop_height, args.pre_crop_width)
    dataset = dataset.map(
        lambda fid, pid: common.fid_to_image(
            fid, pid, image_root=args.image_root,
            image_size=pre_crop_size if args.crop_augment else net_input_size),
        num_parallel_calls=args.loading_threads)  # now the dataset has been modified as [selected_images
    # , fid, pid] due to the return of the function 'fid_to_image'


    # Augment the data if specified by the arguments.
    if args.flip_augment:
        dataset = dataset.map(
            lambda im, fid, pid: (tf.image.random_flip_left_right(im), fid, pid))
    if args.crop_augment:
        dataset = dataset.map(
            lambda im, fid, pid: (tf.random_crop(im, net_input_size + (3,)), fid, pid))

    # Group it back into PK batches.
    batch_size = args.batch_p * args.batch_k
    dataset = dataset.batch(batch_size)

    # Overlap producing and consuming for parallelism.
    dataset = dataset.prefetch(1)

    # Since we repeat the data infinitely, we only need a one-shot iterator.
    images_train, fids_train, pids_train = dataset.make_one_shot_iterator().get_next()
########################################################################################################################
    #prepare the validation set
    pids_val, fids_val = common.load_dataset(args.validation_set, args.validation_image_root)
    # Setup a tf.Dataset where one "epoch" loops over all PIDS.
    # PIDS are shuffled after every epoch and continue indefinitely.
    unique_pids_val = np.unique(pids_val)
    dataset_val = tf.data.Dataset.from_tensor_slices(unique_pids_val)
    dataset_val = dataset_val.shuffle(len(unique_pids_val))

    # Constrain the dataset size to a multiple of the batch-size, so that
    # we don't get overlap at the end of each epoch.
    dataset_val = dataset_val.take((len(unique_pids_val) // args.batch_p) * args.batch_p)
    dataset_val = dataset_val.repeat(None)  # Repeat forever. Funny way of stating it.

    # For every PID, get K images.
    dataset_val = dataset_val.map(lambda pid: sample_k_fids_for_pid(
        pid, all_fids=fids_val, all_pids=pids_val, batch_k=args.batch_k))  # now the dataset has been modified as [selected_fids
    # , pid] due to the return of the function 'sample_k_fids_for_pid'

    # Ungroup/flatten the batches for easy loading of the files.
    dataset_val = dataset_val.apply(tf.contrib.data.unbatch())

    # Convert filenames to actual image tensors.
    net_input_size = (args.net_input_height, args.net_input_width)
    pre_crop_size = (args.pre_crop_height, args.pre_crop_width)
    dataset_val = dataset_val.map(
        lambda fid, pid: common.fid_to_image(
            fid, pid, image_root=args.validation_image_root,
            image_size=pre_crop_size if args.crop_augment else net_input_size),
        num_parallel_calls=args.loading_threads)  # now the dataset has been modified as [selected_images
    # , fid, pid] due to the return of the function 'fid_to_image'

    # Augment the data if specified by the arguments.
    if args.flip_augment:
        dataset_val = dataset_val.map(
            lambda im, fid, pid: (tf.image.random_flip_left_right(im), fid, pid))
    if args.crop_augment:
        dataset_val = dataset_val.map(
            lambda im, fid, pid: (tf.random_crop(im, net_input_size + (3,)), fid, pid))

    # Group it back into PK batches.
    dataset_val = dataset_val.batch(batch_size)

    # Overlap producing and consuming for parallelism.
    dataset_val = dataset_val.prefetch(1)

    # Since we repeat the data infinitely, we only need a one-shot iterator.
    images_val, fids_val, pids_val = dataset_val.make_one_shot_iterator().get_next()
####################################################################################################################
    # Create the model and an embedding head.
    model = import_module('nets.' + args.model_name)
    head = import_module('heads.' + args.head_name)

    # Feed the image through the model. The returned `body_prefix` will be used
    # further down to load the pre-trained weights for all variables with this
    # prefix.
    input_images = tf.placeholder(dtype=tf.float32, shape=[None,args.net_input_height,args.net_input_width,3],name='input')
    pids = tf.placeholder(dtype=tf.string, shape=[None,],name='pids')
    fids = tf.placeholder(dtype=tf.string, shape=[None, ], name='fids')

    endpoints, body_prefix = model.endpoints(input_images, is_training=True)
    with tf.name_scope('head'):
        endpoints = head.head(endpoints, args.embedding_dim, is_training=True)

    # Create the loss in two steps:
    # 1. Compute all pairwise distances according to the specified metric.
    # 2. For each anchor along the first dimension, compute its loss.
    # dists = loss.cdist(endpoints['emb'], endpoints['emb'], metric=args.metric)
    # losses, train_top1, prec_at_k, _, neg_dists, pos_dists = loss.LOSS_CHOICES[args.loss](
    #     dists, pids, args.margin, batch_precision_at_k=args.batch_k-1)
    # # '_' stands for the boolean matrix shows topK where the correct match of the identities occurs
    # shape=(batch_size,K)

# 更改
    # loss1
    dists1 = loss.cdist(endpoints['feature1'], endpoints['feature1'], metric=args.metric)
    losses1,_,_,_,_,_ =loss.LOSS_CHOICES[args.loss](
        dists1, pids, args.margin, batch_precision_at_k=args.batch_k - 1)
    dists2 = loss.cdist(endpoints['feature2'], endpoints['feature2'], metric=args.metric)
    losses2, _, _, _, _, _ = loss.LOSS_CHOICES[args.loss](
        dists2, pids, args.margin, batch_precision_at_k=args.batch_k - 1)
    dists3 = loss.cdist(endpoints['feature3'], endpoints['feature3'], metric=args.metric)
    losses3, _, _, _, _, _ = loss.LOSS_CHOICES[args.loss](
        dists3, pids, args.margin, batch_precision_at_k=args.batch_k - 1)
    dists4 = loss.cdist(endpoints['feature4'], endpoints['feature4'], metric=args.metric)
    losses4, _, _, _, _, _ = loss.LOSS_CHOICES[args.loss](
        dists4, pids, args.margin, batch_precision_at_k=args.batch_k - 1)
    dists_fu = loss.cdist(endpoints['fusion_layer'], endpoints['fusion_layer'], metric=args.metric)
    losses_fu, train_top1, prec_at_k, _, neg_dists, pos_dists = loss.LOSS_CHOICES[args.loss](
        dists_fu, pids, args.margin, batch_precision_at_k=args.batch_k - 1)

    losses = losses1+losses2+losses3+losses4+losses_fu


# 更改
    #loss
    # losses_fu, train_top1, prec_at_k, _, neg_dists, pos_dists = loss.LOSS_CHOICES[args.loss](
    #     endpoints,pids, model_type=args.model_name, metric=args.metric, batch_precision_at_k=args.batch_k - 1
    # )

    # Count the number of active entries, and compute the total batch loss.
    num_active = tf.reduce_sum(tf.cast(tf.greater(losses, 1e-5), tf.float32))

    # 此处losses即为 pospair 比 negpair+margin 还大的部分
    loss_mean = tf.reduce_mean(losses)

    # Some logging for tensorboard.
    tf.summary.histogram('loss_distribution', losses)
    tf.summary.scalar('loss', loss_mean)
    tf.summary.scalar('batch_top1', train_top1)
    tf.summary.scalar('batch_prec_at_{}'.format(args.batch_k-1), prec_at_k)
    tf.summary.scalar('active_count', num_active)
    #tf.summary.histogram('embedding_dists', dists)
    tf.summary.histogram('embedding_pos_dists', pos_dists)
    tf.summary.histogram('embedding_neg_dists', neg_dists)
    tf.summary.histogram('embedding_lengths',
                         tf.norm(endpoints['emb_raw'], axis=1))

    # Create the mem-mapped arrays in which we'll log all training detail in
    # addition to tensorboard, because tensorboard is annoying for detailed
    # inspection and actually discards data in histogram summaries.
    if args.detailed_logs:
        log_embs = lb.create_or_resize_dat(
            os.path.join(args.experiment_root, 'embeddings'),
            dtype=np.float32, shape=(args.train_iterations, batch_size, args.embedding_dim))
        log_loss = lb.create_or_resize_dat(
            os.path.join(args.experiment_root, 'losses'),
            dtype=np.float32, shape=(args.train_iterations, batch_size))
        log_fids = lb.create_or_resize_dat(
            os.path.join(args.experiment_root, 'fids'),
            dtype='S' + str(max_fid_len), shape=(args.train_iterations, batch_size))

    # These are collected here before we add the optimizer, because depending
    # on the optimizer, it might add extra slots, which are also global
    # variables, with the exact same prefix.
    model_variables = tf.get_collection(
        tf.GraphKeys.GLOBAL_VARIABLES, body_prefix)

    # Define the optimizer and the learning-rate schedule.
    # Unfortunately, we get NaNs if we don't handle no-decay separately.
    global_step = tf.Variable(0, name='global_step', trainable=False)  # 'global_step' means the number of batches seen
                                                                       #  by graph
    if 0 <= args.decay_start_iteration < args.train_iterations:
        learning_rate = tf.train.exponential_decay(
            args.learning_rate,
            tf.maximum(0, global_step - args.decay_start_iteration),  # decay every 'lr_decay_steps' after the
                                                                      # 'decay_start_iteration'
            # args.train_iterations - args.decay_start_iteration, args.weight_decay_factor)
            args.lr_decay_steps, args.lr_decay_factor, staircase=True)
    else:
        learning_rate = args.learning_rate  # the case when we set 'decay_start_iteration' as -1
    tf.summary.scalar('learning_rate', learning_rate)
    optimizer = tf.train.AdamOptimizer(learning_rate,epsilon=1e-3)
    # Feel free to try others!
    # optimizer = tf.train.AdadeltaOptimizer(learning_rate)

    # Update_ops are used to update batchnorm stats.
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        train_op = optimizer.minimize(loss_mean, global_step=global_step)

    # Define a saver for the complete model.
    checkpoint_saver = tf.train.Saver(max_to_keep=0)

    with tf.Session(config=config) as sess:
        if args.resume:
            # In case we're resuming, simply load the full checkpoint to init.
            if args.checkpoint is None:
                last_checkpoint = tf.train.latest_checkpoint(args.experiment_root)
                log.info('Restoring from checkpoint: {}'.format(last_checkpoint))
                checkpoint_saver.restore(sess, last_checkpoint)
            else:
                ckpt_path = os.path.join(args.experiment_root, args.checkpoint)
                log.info('Restoring from checkpoint: {}'.format(args.checkpoint))
                checkpoint_saver.restore(sess, ckpt_path)
        else:
            # But if we're starting from scratch, we may need to load some
            # variables from the pre-trained weights, and random init others.
            sess.run(tf.global_variables_initializer())
            if args.initial_checkpoint is not None:
                saver = tf.train.Saver(model_variables)
                saver.restore(sess, args.initial_checkpoint)  # restore the pre-trained parameter from online model

            # In any case, we also store this initialization as a checkpoint,
            # such that we could run exactly reproduceable experiments.
            checkpoint_saver.save(sess, os.path.join(
                args.experiment_root, 'checkpoint'), global_step=0)

        merged_summary = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter(args.experiment_root, sess.graph)

        start_step = sess.run(global_step)
        log.info('Starting training from iteration {}.'.format(start_step))

        # Finally, here comes the main-loop. This `Uninterrupt` is a handy
        # utility such that an iteration still finishes on Ctrl+C and we can
        # stop the training cleanly.
        with lb.Uninterrupt(sigs=[SIGINT, SIGTERM], verbose=True) as u:
            for i in range(start_step, args.train_iterations):

                # Compute gradients, update weights, store logs!
                start_time = time.time()
                _, summary, step, b_prec_at_k, b_embs, b_loss, b_fids = \
                    sess.run([train_op, merged_summary, global_step,
                              prec_at_k, endpoints['emb'], losses, fids], feed_dict={input_images:images_train.eval(),
                                                                                     pids:pids_train.eval(),
                                                                                     fids:fids_train.eval()})
                elapsed_time = time.time() - start_time

                # Compute the iteration speed and add it to the summary.
                # We did observe some weird spikes that we couldn't track down.
                summary2 = tf.Summary()
                summary2.value.add(tag='secs_per_iter', simple_value=elapsed_time)
                summary_writer.add_summary(summary2, step)
                summary_writer.add_summary(summary, step)

                if args.detailed_logs:
                    log_embs[i], log_loss[i], log_fids[i] = b_embs, b_loss, b_fids

                # Do a huge print out of the current progress.
                seconds_todo = (args.train_iterations - step) * elapsed_time
                log.info('iter:{:6d}, loss min|avg|max: {:.3f}|{:.3f}|{:6.3f}, '
                         'batch-p@{}: {:.2%}, ETA: {} ({:.2f}s/it)'.format(
                             step,
                             float(np.min(b_loss)),
                             float(np.mean(b_loss)),
                             float(np.max(b_loss)),
                             args.batch_k-1, float(b_prec_at_k),
                             timedelta(seconds=int(seconds_todo)),
                             elapsed_time))
                sys.stdout.flush()
                sys.stderr.flush()

                # Save a checkpoint of training every so often.
                if (args.checkpoint_frequency > 0 and
                        step % args.checkpoint_frequency == 0):
                    checkpoint_saver.save(sess, os.path.join(
                        args.experiment_root, 'checkpoint'), global_step=step)

                #get validation results
                if (args.validation_frequency > 0 and
                        step % args.validation_frequency == 0):
                     b_prec_at_k_val, b_loss, b_fids = \
                        sess.run([prec_at_k, losses, fids], feed_dict={input_images : images_val.eval(),
                                                                       pids:pids_val.eval(),
                                                                       fids:fids_val.eval()})
                     log.info('Validation @:{:6d} iteration, loss min|avg|max: {:.3f}|{:.3f}|{:6.3f}, '
                         'batch-p@{}: {:.2%}'.format(
                          step,
                          float(np.min(b_loss)),
                          float(np.mean(b_loss)),
                          float(np.max(b_loss)),
                          args.batch_k - 1, float(b_prec_at_k_val)
                          ))
                     sys.stdout.flush()
                     sys.stderr.flush()
                     summary3 = tf.Summary()
                     summary3.value.add(tag='secs_per_iter', simple_value=float(np.mean(b_loss)))
                     summary_writer.add_summary(summary3, step)
                     summary_writer.add_summary(summary3, step)

                # Stop the main-loop at the end of the step, if requested.
                if u.interrupted:
                    log.info("Interrupted on request!")
                    break

        # Store one final checkpoint. This might be redundant, but it is crucial
        # in case intermediate storing was disabled and it saves a checkpoint
        # when the process was interrupted.
        checkpoint_saver.save(sess, os.path.join(
            args.experiment_root, 'checkpoint'), global_step=step)
示例#7
0
    def train(self, trainImagesLabelsTup, valImagesLabelsTup=None):

        #prep data
        windowsArr, labelAnsArr = self.extractWindowsAndLabels(trainImagesLabelsTup)
        #prep val data (done at test time now)
        # if valImagesLabelsTup:
        #     valImages, valLabels = valImagesLabelsTup
        #     valImages, valLabels = np.repeat(valImages, int(c.BATCH_SZ/c.NUM_TEST_IMGS), axis=0), np.repeat(valLabels, int(c.BATCH_SZ/c.NUM_TEST_IMGS)) #hack to fit in batch size
        #     valWindows, valLabel = self.extractWindowsAndLabels(valImagesLabelsTup)

        #train
        step = 0
        num_batches_per_e = int(c.NUM_TRAIN_IMGS/c.BATCH_SZ)
        num_steps = num_batches_per_e*c.NUM_EPOCHS
        for epoch in range(c.NUM_EPOCHS):
            if c.STOCHASTIC_GD:#shuffle data
                permutation = np.random.permutation(len(labelAnsArr))
                windowsArr, labelAnsArr = windowsArr[permutation], labelAnsArr[permutation]
            
            # Begin Epoch Train #
            for b_num in range(num_batches_per_e):
                if step < tf.train.global_step(self.sess, self.glob_step):
                    #catch up to saved state if resuming training. (dont do anything if step/epoch is too early)
                    step += 1
                    continue

                if c.STOCHASTIC_GD:
                    print("\nTrain Step:", step)
                else:
                    print("\nTrain Step (Epoch):", step)
                print(100.0*step/num_steps, "percent done")

                start_index = c.BATCH_SZ*b_num
                stop_index = start_index + c.BATCH_SZ
                data, labels = windowsArr[start_index:stop_index], labelAnsArr[start_index:stop_index]

                feedDict = {self.img_batch_in: data, self.labels: labels}
                if self.saveable:
                    sessArgs = [self.accuracy, self.loss, self.train_loss_summary, self.trainOp]
                    acc, lossReturned, summary, _ = self.sess.run(sessArgs, feed_dict=feedDict)
                else:
                    sessArgs = [self.accuracy, self.loss, self.trainOp]
                    acc, lossReturned, _ = self.sess.run(sessArgs, feed_dict=feedDict)

                print("loss -", lossReturned)
                print("accuracy -", acc)

                step += 1
            # End Epoch Train #

            #save model/summary and validate
            if self.saveable and step >= tf.train.global_step(self.sess, self.glob_step):
                if (epoch%c.SAVE_FREQ == 0) or (epoch==c.NUM_EPOCHS-1):
                    self.save()
                if (epoch%c.SUMMARY_FREQ == 0) or (epoch==c.NUM_EPOCHS-1):
                    #note, this train summary is currently only for the last batch
                    self.summary_writer.add_summary(summary, global_step=step)
                    if valImagesLabelsTup:
                        print("\n\nvalidating...")
                        # feedDict = {self.img_batch_in: valWindows, self.labels: valLabel}
                        # acc, valSummary = self.sess.run([self.accuracy, self.test_loss_summary], feed_dict=feedDict)
                        val_acc = self.test(valImagesLabelsTup, verbose=False)
                        valSummary = tf.Summary(value=[tf.Summary.Value(tag="test_accuracy", simple_value=val_acc)])
                        self.summary_writer.add_summary(valSummary, global_step=step)
                        print("val accuracy::", val_acc, "\n\n")
示例#8
0
文件: main.py 项目: jcnlp/dssm-lstm
            epoch = model.epoch.eval()
            random_idxs = range(len(data_queries))
            random.shuffle(random_idxs)
            data_queries = [data_queries[i] for i in random_idxs]
            data_docs = np.reshape(data_docs, (len(data_queries), -1))
            data_docs = [data_docs[i] for i in random_idxs]
            data_docs = np.reshape(data_docs, len(data_queries) * (FLAGS.neg_num + 1))
            start_time = time.time()
            loss = train(model, sess, data_queries, data_docs)
            epoch_time = time.time() - start_time
            total_train_time += epoch_time

            # test loss
            test_loss = test(model, sess, test_queries, test_docs, ground_truths)

            summary = tf.Summary()
            summary.value.add(tag='loss/train', simple_value=loss)
            summary.value.add(tag='loss/test', simple_value=test_loss)
            cur_lr = model.learning_rate.eval()
            summary.value.add(tag='lr/train', simple_value=cur_lr)
            summary_writer.add_summary(summary, epoch)
            model.saver.save(sess, '%s/checkpoint' % FLAGS.train_dir, global_step=model.global_step)
            print("epoch %d learning rate %.10f epoch-time %.4f loss %.8f test loss %.8f" % (
            epoch, cur_lr, epoch_time, loss, test_loss))
        with open(os.path.join(FLAGS.train_dir, FLAGS.time_log_path), 'a') as fp:
            fp.writelines(['total training time: %f\n' % total_train_time, 'last test loss: %.8f' % test_loss])




示例#9
0
def main():
    """Create the model and start the training."""
    hvd.init()
    args = get_arguments()
    args.snapshot_dir = args.snapshot_dir.replace(
        'DeepDICD/', 'DeepDICD/' + args.model_name + '-' + args.domain + '-')
    print(toMagenta(args.snapshot_dir))
    ss = args.domain.split('-')
    if ss[0] == 'D':
        args.list1 = args.list1.replace('amazon.txt', 'dslr.txt')
    elif ss[0] == 'W':
        args.list1 = args.list1.replace('amazon.txt', 'webcam.txt')

    if ss[1] == 'A':
        args.list2 = args.list2.replace('dslr.txt', 'amazon.txt')
    elif ss[1] == 'W':
        args.list2 = args.list2.replace('dslr.txt', 'webcam.txt')

    print(toMagenta(args.list1))
    print(toMagenta(args.list2))

    start_steps = args.start_steps

    h = args.h
    w = args.w

    # construct data generator
    file1 = open(args.list1)
    num1 = len(file1.readlines())
    file2 = open(args.list2)
    num2 = len(file2.readlines())
    file1.close()
    file2.close()

    steps_per_epoch = int((num1 / (args.batch_size)))
    num_steps = int(steps_per_epoch * args.num_epochs)
    val_num_steps = int(num2 / args.batch_size)

    print(toCyan('src domain: {:d}, tar domain {:d}'.format(num1, num2)))
    print(
        toCyan('steps_per_epoch x num_epochs:{:d} x {:d}'.format(
            steps_per_epoch, args.num_epochs)))

    # Chong
    # split_batch_size=int(args.batch_size/hvd.size())
    myDataloader = Dataloader(args.img_dir, args.list1, args.list2,
                              args.batch_size, args.h, args.w,
                              args.num_threads)

    src_img = myDataloader.simg_batch
    src_label = myDataloader.slabel_batch
    tar_img = myDataloader.timg_batch
    tar_label = myDataloader.tlabel_batch

    coord = tf.train.Coordinator()

    # Learning Startegy Settings
    baseLR1 = tf.constant(args.lr1)
    baseLR2 = tf.constant(args.lr2)
    step_ph = tf.placeholder(dtype=tf.float32, shape=())
    if args.learning_strategy == 0:
        lr1 = baseLR1 / tf.pow(1 + 0.001 * step_ph / steps_per_epoch, 0.75)
        lr2 = baseLR2 / tf.pow(1 + 0.001 * step_ph / steps_per_epoch, 0.75)
    elif args.learning_strategy == 1:
        lr1 = baseLR1 / tf.pow(1 + 0.0005 * step_ph / steps_per_epoch, 0.75)
        lr2 = baseLR2 / tf.pow(1 + 0.0005 * step_ph / steps_per_epoch, 0.75)
    elif args.learning_strategy == 2:
        lr1 = baseLR1 / tf.pow(1 + 0.005 * step_ph / steps_per_epoch, 0.75)
        lr2 = baseLR2 / tf.pow(1 + 0.005 * step_ph / steps_per_epoch, 0.75)
    elif args.learning_strategy == 3:  # consistent with paper
        lr1 = baseLR1 / tf.pow(1 + 0.001 * step_ph, 0.75)
        lr2 = baseLR2 / tf.pow(1 + 0.001 * step_ph, 0.75)
    elif args.learning_strategy == 4:
        lr1 = baseLR1 / tf.pow(1 + 0.005 * step_ph, 0.75)
        lr2 = baseLR2 / tf.pow(1 + 0.005 * step_ph, 0.75)
    elif args.learning_strategy == 5:
        lr1 = baseLR1 / tf.pow(1 + 0.00005 * step_ph, 0.75)
        lr2 = baseLR2 / tf.pow(1 + 0.00005 * step_ph, 0.75)

    keep_prob = tf.placeholder(dtype=tf.float32, shape=())
    # p = (1 - tf.scalar_mul(1., tf.pow((1 - step_ph / num_steps), args.power)))
    p = adaptation_factor(step_ph / MAX_STEP / 6)
    # p = adaptation_factor(step_ph/MAX_STEP)
    # p2 = adaptation_factor(step_ph/(2*MAX_STEP))
    alpha1 = p
    # alpha1 = tf.constant(1.,tf.float32)
    alpha2 = 0.30 * p
    # alpha2 = tf.constant(0.,tf.float32)
    alpha3 = 1 * p
    # alpha2=p2

    # loss_balance=tf.constant(0.001,tf.float32)
    # boundaries = [np.float32(np.int32((1/14) * num_steps))]
    # values = [0., 0.01]
    # loss_balance = tf.train.piecewise_constant(step_ph, boundaries, values)
    #
    model = DeepDICDModel(args, keep_prob, src_img, src_label, tar_img,
                          tar_label)
    centroid_update_ops = model.build_losses(alpha1, alpha2, alpha3)
    model.build_outputs()
    summary_ = model.build_summary()

    all_trainable_var = [v for v in tf.trainable_variables()]
    fine_tune_var = [
        v for v in all_trainable_var if 'fc8' not in v.name
        and 'fc9' not in v.name and 'domain' not in v.name
    ]
    fine_tune_var_weights = [v for v in fine_tune_var if 'weights' in v.name]
    fine_tune_var_bias = [v for v in fine_tune_var if 'bias' in v.name]
    retrain_var = [
        v for v in all_trainable_var if 'fc8' in v.name or 'fc9' in v.name
    ]
    retrain_var_weights = [v for v in retrain_var if 'weights' in v.name]
    retrain_var_bias = [v for v in retrain_var if 'bias' in v.name]
    domain_var = [v for v in all_trainable_var if 'domain' in v.name]
    domain_var_weights = [v for v in domain_var if 'weights' in v.name]
    domain_var_bias = [v for v in domain_var if 'bias' in v.name]

    if hvd.rank() == 0:
        print(toGreen('----fine tune var----'))
        for v in fine_tune_var:
            print(toCyan(v.name))

        print(toGreen('----retrain  var----'))
        for v in retrain_var:
            print(toCyan(v.name))

        print(toGreen('----domain  var----'))
        for v in domain_var:
            print(toCyan(v.name))

    # Chong: broadcast every iter
    bcast = hvd.broadcast_global_variables(0)
    with tf.control_dependencies([centroid_update_ops]):

        opt_1_1 = tf.train.MomentumOptimizer(lr1 * hvd.size(), args.momentum)
        opt_1_1 = hvd.DistributedOptimizer(opt_1_1)
        # Chong: different with tf.gradients(), which just return grads
        # While, compute_gradients() return (gradients,var) tuple
        grads_1_1 = opt_1_1.compute_gradients(model.F_loss,
                                              fine_tune_var_weights)
        train_op_1_1 = opt_1_1.apply_gradients(grads_1_1)

        opt_1_2 = tf.train.MomentumOptimizer(2 * lr1 * hvd.size(),
                                             args.momentum)
        opt_1_2 = hvd.DistributedOptimizer(opt_1_2)
        grads_1_2 = opt_1_2.compute_gradients(model.F_loss, fine_tune_var_bias)
        train_op_1_2 = opt_1_2.apply_gradients(grads_1_2)

        opt_2_1 = tf.train.MomentumOptimizer(lr2 * hvd.size(), args.momentum)
        opt_2_1 = hvd.DistributedOptimizer(opt_2_1)
        grads_2_1 = opt_2_1.compute_gradients(model.F_loss,
                                              retrain_var_weights)
        train_op_2_1 = opt_2_1.apply_gradients(grads_2_1)

        opt_2_2 = tf.train.MomentumOptimizer(2 * lr2 * hvd.size(),
                                             args.momentum)
        opt_2_2 = hvd.DistributedOptimizer(opt_2_2)
        grads_2_2 = opt_2_2.compute_gradients(model.F_loss, retrain_var_bias)
        train_op_2_2 = opt_2_2.apply_gradients(grads_2_2)

        opt_3_1 = tf.train.MomentumOptimizer(lr2 * hvd.size(), args.momentum)
        opt_3_1 = hvd.DistributedOptimizer(opt_3_1)
        grads_3_1 = opt_3_1.compute_gradients(
            model.dregular_loss + model.D_loss, domain_var_weights)
        train_op_3_1 = opt_3_1.apply_gradients(grads_3_1)

        opt_3_2 = tf.train.MomentumOptimizer(2 * lr2 * hvd.size(),
                                             args.momentum)
        opt_3_2 = hvd.DistributedOptimizer(opt_3_2)
        grads_3_2 = opt_3_2.compute_gradients(
            model.dregular_loss + model.D_loss, domain_var_bias)
        train_op_3_2 = opt_3_2.apply_gradients(grads_3_2)

        # Chong: broadcast ever iter
        train_op = tf.group(train_op_1_1, train_op_1_2, train_op_2_1,
                            train_op_2_2, train_op_3_1, train_op_3_2, bcast)

    # Set up tf session and initialize variables.
    #
    config = tf.ConfigProto()  # Chong
    config.gpu_options.allow_growth = True
    config.gpu_options.visible_device_list = str(hvd.local_rank())
    config.gpu_options.per_process_gpu_memory_fraction = 0.4
    sess = tf.Session(config=config)
    init_local = tf.local_variables_initializer()
    init = tf.global_variables_initializer()

    # construct summary
    summary_.append(tf.summary.scalar('train/lr1', lr1))
    summary_.append(tf.summary.scalar('train/lr2', lr2))
    summary_.append(tf.summary.scalar('train/alpha1', alpha1))
    summary_.append(tf.summary.scalar('train/alpha2', alpha2))

    summary_merged = tf.summary.merge(summary_)
    if hvd.rank() == 0:
        FinalSummary = tf.summary.FileWriter(args.snapshot_dir, sess.graph)

    # init
    sess.run([init_local, init])

    sess.run(bcast)

    # Saver for storing checkpoints of the model.
    var = tf.global_variables()
    skip_var = ['fc8', 'fc9']
    saver = tf.train.Saver(var_list=var, max_to_keep=5)

    ckpt = tf.train.get_checkpoint_state(args.resume_from)
    if ckpt and ckpt.model_checkpoint_path and args.resume:
        loader = tf.train.Saver(var_list=var)
        load_step = int(
            os.path.basename(ckpt.model_checkpoint_path).split('-')[1])
        load(loader, sess, ckpt.model_checkpoint_path)
    elif not args.not_load_pretrained:
        print(toRed('Restore from pre-trained model...' + args.restore_from))
        model.load_initial_weights(sess, args.restore_from,
                                   skip_var)  # Chong:0531

    # Start queue threads.
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    # Iterate over training steps.
    acc2_history = 0
    for step in range(start_steps, num_steps):
        start_time = time.time()
        feed_dict = {step_ph: step, keep_prob: 0.5}
        summary, total_loss, _ = sess.run(
            [summary_merged, model.F_loss, train_op], feed_dict=feed_dict)

        if hvd.rank() == 0:
            FinalSummary.add_summary(summary, step)
            duration = time.time() - start_time
            remain_time = duration * (num_steps - step) / 3600
            print('\r', toCyan('{:s}:{:d}-{:d}-{:d} loss = {:.3f},({:.3f} sec/step, ERT: {:.3f})'\
                .format(args.model_name + '-'\
                + args.domain, step % steps_per_epoch,\
                step // steps_per_epoch, args.num_epochs,\
                total_loss, duration, remain_time)), end='')

            if step % args.test_every == 0:
                acc1, acc2 = 0, 0
                for jj in range(val_num_steps):
                    feed_dict = {keep_prob: 1}
                    src_acc, tar_acc = sess.run([model.src_acc, model.tar_acc],
                                                feed_dict=feed_dict)
                    acc1 += np.sum(src_acc)
                    acc2 += np.sum(tar_acc)

                acc1 = acc1 / (val_num_steps * args.batch_size)
                acc2 = acc2 / (val_num_steps * args.batch_size)
                # pdb.set_trace()
                test_summary = tf.Summary()
                test_summary.value.add(tag='test/source_accuracy',
                                       simple_value=acc1)
                test_summary.value.add(tag='test/target_accuracy',
                                       simple_value=acc2)
                FinalSummary.add_summary(test_summary, step)

                if acc2 > acc2_history:
                    save(saver, sess, args.snapshot_dir, step)
                    acc2_history = acc2

    coord.request_stop()
    coord.join(threads)
    sess.close()
示例#10
0
 def __enter__(self):
     self.summary = tf.Summary()
     return self
示例#11
0
def evaluate_localization_thread(global_step, mode, nearest_d_dist,
                                 nearest_d_indices, nearest_latent_indices,
                                 out_name, query_image_info, query_xy,
                                 ref_image_info, ref_xy, writer):
    d_to_nearest_latent = np.empty(nearest_latent_indices.shape)
    for i in range(NUM_EVAL_QUERIES):
        for j in range(nearest_latent_indices.shape[1]):
            other_index = nearest_latent_indices[i][j]
            d_to_nearest_latent[i, j] = np.linalg.norm(query_xy[i, :] -
                                                       ref_xy[other_index, :])
    top_n = np.empty(nearest_latent_indices.shape)
    for i in range(NUM_EVAL_QUERIES):
        for j in range(nearest_latent_indices.shape[1]):
            top_n[i, j] = min(d_to_nearest_latent[i, 0:(j + 1)])
    # Sorting top_n
    x = [[] for n in range(top_n.shape[1] + 1)]
    y = [[] for n in range(top_n.shape[1] + 1)]
    for n in range(top_n.shape[1]):
        x[n] = np.sort(top_n[:, n])
        y[n] = np.array(range(NUM_EVAL_QUERIES)) / float(NUM_EVAL_QUERIES)
    x[-1] = np.sort(np.array(nearest_d_dist).reshape(-1))
    y[-1] = np.array(range(NUM_EVAL_QUERIES)) / float(NUM_EVAL_QUERIES)
    summary = tf.Summary()
    full_auc = sklearn.metrics.auc(x[0], y[0])
    summary.value.add(tag='full_auc@Top1', simple_value=full_auc)
    x_partial = x  # not actually a copy (x will be changed when modifiying x_partial)
    y_partial = y
    for rad in [1.0, 10.0]:
        plt.clf()
        for n in range(top_n.shape[1] + 1):
            x_partial[n] = [p for p in x_partial[n] if p <= rad]
            y_partial[n] = [y_partial[n][k] for k in range(len(x_partial[n]))]
            plt.plot(x_partial[n], y_partial[n])
        plt.legend(['Top-1', 'Top-2', 'Top-3', 'Top-4', 'Top-5', 'Optimum'])
        plt.ylabel('Correctly localized')
        plt.xlabel('Tolerance [m]')
        plt.xlim(0, rad)
        title = os.path.basename(
            os.path.dirname(OUT_DIR)) + '\n' + os.path.basename(
                OUT_DIR) + '\n' + mode + ' ' + out_name
        plt.title(title)

        if len(x_partial[0]) > 1:
            auc = sklearn.metrics.auc(x_partial[0], y_partial[0])
            summary.value.add(tag='{}m-auc@Top1'.format(rad), simple_value=auc)
            plt.text(0.5 * float(rad), 0.08, 'AUC@Top1={:7.2f}'.format(auc))

        correct_fraction = len(x_partial[0]) / len(top_n[:, 0])
        summary.value.add(tag='%<{}m@Top1'.format(rad),
                          simple_value=correct_fraction)
        plt.text(0.5 * float(rad), 0.02,
                 '%<{}m@Top1={:7.2f}'.format(rad, correct_fraction))

        plt.savefig(
            os.path.join(OUT_DIR,
                         mode + '_' + out_name + '_{}.pdf'.format(rad)))
    writer.add_summary(summary, global_step)
    # Save visual examples
    example_img_dir = os.path.join(OUT_DIR, mode + '_' + out_name)
    os.mkdir(example_img_dir)
    for index in np.random.choice(NUM_EVAL_QUERIES, 10, replace=False):
        # Query image
        query_file_path = img_path(query_image_info[index])
        query_image = load_img(query_file_path)
        query_image = put_text('Query', query_image)

        # Retrieved image
        file_path = img_path(ref_image_info[nearest_latent_indices[index][0]])
        retrieved_image = load_img(file_path)
        dist = d_to_nearest_latent[index][0]
        retrieved_image = put_text('Retrieved {}'.format(dist),
                                   retrieved_image)

        # Optimal image
        file_path = img_path(ref_image_info[nearest_d_indices[index][0]])
        optimal_image = load_img(file_path)
        dist = nearest_d_dist[index][0]
        optimal_image = put_text('Optimal {}'.format(dist), optimal_image)
        merged_images = merge_images(query_image, retrieved_image)
        merged_images = merge_images(merged_images, optimal_image)
        save_img(
            merged_images,
            os.path.join(example_img_dir, os.path.basename(query_file_path)))
示例#12
0
 def log_to_tensorboard(self, tag, group, value, index):
     summary = tf.Summary()
     tag = group + '/' + tag
     summary.value.add(tag=tag, simple_value=value)
     self.stat_writer.add_summary(summary, index)
    def eval(self, model):
        """
        Iterate through all mini-batches in a dataset, computing:
        (1) Average predictive loss (i.e. NLL) across all samples in the dataset
        (2) Regularization loss (i.e. negative log prior)
        (3) Total loss (i.e. loss used as a training objective by the model)

        Additionally, the function computes both targets and predictions for all samples in the dataset.

        The method returns a tf.Summary object containing the value values of all three losses, as well as the targets
        and model predictions for all samples in the dataset.

        :param model: An instantiated object of any class inheriting from BasicModel. It is assumed that the
        computational graph of the model has been already built and that all variables are initialised.
        :return: A tf.Summary object containing the value values of the average predictive loss, regularization loss
        and total loss across all samples in the dataset. Two NumPy arrays containing the targets and predictions
        respectively are also returned.
        """
        # Initialize variables
        batches_seen, n_samples = 0, 0
        pred_loss, reg_loss, total_loss = 0, 0, 0
        targets, predictions = [], []
        # Iterate across all model batches, accumulating output along the way
        while batches_seen < model.get_n_batches():
            # Compute loss (predictive, regularization and total), original targets and model predictions for the
            # mini-batch
            tensors = model.get_losses(keys=['prediction', 'regularization', 'total']) + \
                      model.get_inputs(keys=['target']) + \
                      model.get_outputs(keys=['mlp/out'])
            pred_loss_batch, reg_loss_batch, total_loss_batch, targets_batch, predictions_batch = self.sess.run(
                tensors)
            batch_size = targets_batch.shape[
                0]  # Handle potential variable-size mini-batches

            # Update losses
            pred_loss += batch_size * pred_loss_batch
            reg_loss += batch_size * reg_loss_batch  # Not strictly necessary, as it is constant across batches
            total_loss += batch_size * total_loss_batch

            # Update targets and predictions
            targets.append(targets_batch)
            predictions.append(predictions_batch)

            # Update number of examples
            n_samples += batch_size

            # Increase counter of batches processed
            batches_seen += 1

        # Create TensorFlow Summary, adding the three basic model losses
        summary = tf.Summary()
        summary.value.add(tag='Predictive_loss',
                          simple_value=pred_loss / n_samples)
        summary.value.add(tag='Regularization_loss',
                          simple_value=reg_loss / n_samples)
        summary.value.add(tag='Total_loss',
                          simple_value=total_loss / n_samples)

        # Concatenate targets and predictions for each mini-batch into single NumPy arrays
        targets, predictions = np.concatenate(targets, axis=0), np.concatenate(
            predictions, axis=0)

        return summary, targets, predictions
示例#14
0
def log_metric(log, name, value, step=0):
    summary = tf.Summary(value=[tf.Summary.Value(tag=name, simple_value=value)])
    log.add_summary(summary, global_step=step)
def deep_q_learning(sess,
                    env,
                    q_estimator,
                    target_estimator,
                    state_processor,
                    num_episodes,
                    experiment_dir,
                    replay_memory_size=500000,
                    replay_memory_init_size=50000,
                    update_target_estimator_every=10000,
                    discount_factor=0.99,
                    epsilon_start=1.0,
                    epsilon_end=0.1,
                    epsilon_decay_steps=500000,
                    batch_size=32,
                    record_video_every=50):
    """
  Q-Learning algorithm for off-policy TD control using Function Approximation.
  Finds the optimal greedy policy while following an epsilon-greedy policy.

  Args:
      sess: Tensorflow Session object
      env: OpenAI environment
      q_estimator: Estimator object used for the q values
      target_estimator: Estimator object used for the targets
      state_processor: A StateProcessor object
      num_episodes: Number of episodes to run for
      experiment_dir: Directory to save Tensorflow summaries in
      replay_memory_size: Size of the replay memory
      replay_memory_init_size: Number of random experiences to sample when initializing
        the reply memory.
      update_target_estimator_every: Copy parameters from the Q estimator to the
        target estimator every N steps
      discount_factor: Gamma discount factor
      epsilon_start: Chance to sample a random action when taking an action.
        Epsilon is decayed over time and this is the start value
      epsilon_end: The final minimum value of epsilon after decaying is done
      epsilon_decay_steps: Number of steps to decay epsilon over
      batch_size: Size of batches to sample from the replay memory
      record_video_every: Record a video every N episodes

  Returns:
      An EpisodeStats object with two numpy arrays for episode_lengths and episode_rewards.
  """

    Transition = namedtuple(
        "Transition", ["state", "action", "reward", "next_state", "done"])

    # The replay memory
    replay_memory = []

    # Make model copier object
    estimator_copy = ModelParametersCopier(q_estimator, target_estimator)

    # Keeps track of useful statistics
    stats = plotting.EpisodeStats(episode_lengths=np.zeros(num_episodes),
                                  episode_rewards=np.zeros(num_episodes))

    # For 'system/' summaries, usefull to check if currrent process looks healthy
    current_process = psutil.Process()

    # Create directories for checkpoints and summaries
    checkpoint_dir = os.path.join(experiment_dir, "checkpoints")
    checkpoint_path = os.path.join(checkpoint_dir, "model")
    monitor_path = os.path.join(experiment_dir, "monitor")

    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)
    if not os.path.exists(monitor_path):
        os.makedirs(monitor_path)

    saver = tf.train.Saver()
    # Load a previous checkpoint if we find one
    latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
    if latest_checkpoint:
        print("Loading model checkpoint {}...\n".format(latest_checkpoint))
        saver.restore(sess, latest_checkpoint)

    # Get the current time step
    # total_t = sess.run(tf.contrib.framework.get_global_step())
    total_t = sess.run(tf.train.get_global_step())

    # The epsilon decay schedule
    epsilons = np.linspace(epsilon_start, epsilon_end, epsilon_decay_steps)

    # The policy we're following
    policy = make_epsilon_greedy_policy(q_estimator, len(VALID_ACTIONS))

    # Populate the replay memory with initial experience
    print("Populating replay memory...")
    state = env.reset()
    state = state_processor.process(sess, state)
    state = np.stack([state] * 4, axis=2)
    for i in range(replay_memory_init_size):
        action_probs = policy(sess, state,
                              epsilons[min(total_t, epsilon_decay_steps - 1)])
        action = np.random.choice(np.arange(len(action_probs)), p=action_probs)
        next_state, reward, done, _ = env.step(VALID_ACTIONS[action])
        next_state = state_processor.process(sess, next_state)
        next_state = np.append(state[:, :, 1:],
                               np.expand_dims(next_state, 2),
                               axis=2)
        replay_memory.append(
            Transition(state, action, reward, next_state, done))
        if done:
            state = env.reset()
            state = state_processor.process(sess, state)
            state = np.stack([state] * 4, axis=2)
        else:
            state = next_state

    # Record videos
    # Add env Monitor wrapper
    env = Monitor(env,
                  directory=monitor_path,
                  video_callable=lambda count: count % record_video_every == 0,
                  resume=True)

    for i_episode in range(num_episodes):

        # Save the current checkpoint
        saver.save(tf.get_default_session(), checkpoint_path)

        # Reset the environment
        state = env.reset()
        state = state_processor.process(sess, state)
        state = np.stack([state] * 4, axis=2)
        loss = None

        # One step in the environment
        for t in itertools.count():

            # Epsilon for this time step
            epsilon = epsilons[min(total_t, epsilon_decay_steps - 1)]

            # Maybe update the target estimator
            if total_t % update_target_estimator_every == 0:
                estimator_copy.make(sess)
                print("\nCopied model parameters to target network.")

            # Print out which step we're on, useful for debugging.
            print("\rStep {} ({}) @ Episode {}/{}, loss: {}".format(
                t, total_t, i_episode + 1, num_episodes, loss),
                  end="")
            sys.stdout.flush()

            # Take a step
            action_probs = policy(sess, state, epsilon)
            action = np.random.choice(np.arange(len(action_probs)),
                                      p=action_probs)
            next_state, reward, done, _ = env.step(VALID_ACTIONS[action])
            next_state = state_processor.process(sess, next_state)
            next_state = np.append(state[:, :, 1:],
                                   np.expand_dims(next_state, 2),
                                   axis=2)

            # If our replay memory is full, pop the first element
            if len(replay_memory) == replay_memory_size:
                replay_memory.pop(0)

            # Save transition to replay memory
            replay_memory.append(
                Transition(state, action, reward, next_state, done))

            # Update statistics
            stats.episode_rewards[i_episode] += reward
            stats.episode_lengths[i_episode] = t

            # Sample a minibatch from the replay memory
            samples = random.sample(replay_memory, batch_size)
            states_batch, action_batch, reward_batch, next_states_batch, done_batch = map(
                np.array, zip(*samples))

            # Calculate q values and targets
            q_values_next = target_estimator.predict(sess, next_states_batch)
            targets_batch = reward_batch + np.invert(done_batch).astype(
                np.float32) * discount_factor * np.amax(q_values_next, axis=1)

            # Perform gradient descent update
            states_batch = np.array(states_batch)
            loss = q_estimator.update(sess, states_batch, action_batch,
                                      targets_batch)

            if done:
                break

            state = next_state
            total_t += 1

        # Add summaries to tensorboard
        episode_summary = tf.Summary()
        episode_summary.value.add(simple_value=epsilon, tag="episode/epsilon")
        episode_summary.value.add(
            simple_value=stats.episode_rewards[i_episode],
            tag="episode/reward")
        episode_summary.value.add(
            simple_value=stats.episode_lengths[i_episode],
            tag="episode/length")
        episode_summary.value.add(simple_value=current_process.cpu_percent(),
                                  tag="system/cpu_usage_percent")
        episode_summary.value.add(
            simple_value=current_process.memory_percent(memtype="vms"),
            tag="system/v_memeory_usage_percent")
        q_estimator.summary_writer.add_summary(episode_summary, i_episode)
        q_estimator.summary_writer.flush()

        yield total_t, plotting.EpisodeStats(
            episode_lengths=stats.episode_lengths[:i_episode + 1],
            episode_rewards=stats.episode_rewards[:i_episode + 1])

    return stats
def log_scalar(tag, value, step, w):
    summary = tf.Summary(value=[tf.Summary.Value(tag=tag,
                                                 simple_value=value)])
    w.add_summary(summary, step)
示例#17
0
def eval_once(saver, summary_writer, output, labels, summary_op, last_step,
              num_classes, batch_size, checkpoint_dir, restore_step, log_dir,
              num_examples, unlabeled, min_angle, max_angle):
    """Run Eval once.
    Args:
        saver: Saver.
        summary_writer: Summary writer.
        eval_op: Evaluation op.
        summary_op: Summary op.
    """
    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            if not restore_step:
                saver.restore(sess, ckpt.model_checkpoint_path)
                global_step = int(
                    ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
            else:
                saver.restore(
                    sess,
                    os.path.join(checkpoint_dir,
                                 'model.ckpt-{}'.format(restore_step)))
                global_step = restore_step

            #global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
            print('{}: Checkpoint step: {}'.format(datetime.now(),
                                                   global_step))
        else:
            print('{}: No checkpoint found'.format(datetime.now()))
            return last_step

        if global_step == last_step:
            print('{}: Checkpoint already evaluated'.format(datetime.now()))
            return last_step
        else:
            last_step = global_step

        # Start the queue runners.
        coord = tf.train.Coordinator()

        try:
            threads = []
            for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
                threads.extend(
                    qr.create_threads(sess,
                                      coord=coord,
                                      daemon=True,
                                      start=True))

            result = np.array([])
            example_count = 0
            step = 0

            while example_count < num_examples and not coord.should_stop():

                start_time = time.time()

                #label_value, output_value, imgs = sess.run([labels, output, images])
                label_value, output_value = sess.run([labels, output])

                duration = time.time() - start_time

                if num_classes > 1:
                    # back from vectors to angles...
                    output_value = [
                        nvidia_input.vec_to_angle(ov, num_classes, max_angle,
                                                  min_angle)
                        for ov in output_value
                    ]

                    if len(label_value.shape) > 1:
                        label_value = [
                            nvidia_input.vec_to_angle(lv, num_classes,
                                                      max_angle, min_angle)
                            for lv in label_value
                        ]

                else:
                    output_value = [
                        nvidia_input.angle_from_normalized(
                            ov, max_angle, min_angle) for ov in output_value
                    ]

                    if not unlabeled:
                        label_value = [
                            nvidia_input.angle_from_normalized(
                                lv, max_angle, min_angle) for lv in label_value
                        ]

                output_value = np.reshape([output_value], [-1, 1])
                label_value = np.reshape([label_value], [-1, 1])
                rows = np.hstack((label_value, output_value))
                result = np.append(result, rows)
                result = np.reshape(result, [-1, 2])

                example_count += output_value.shape[0]

                if step % 10 == 0:
                    #print(label_value[:3], output_value[:3])
                    examples_per_sec = batch_size / duration
                    sec_per_batch = float(duration)

                    format_str = ('%s: step %d (%.1f examples/sec; %.3f '
                                  'sec/batch)')
                    print(format_str % (datetime.now(), step, examples_per_sec,
                                        sec_per_batch))

                step += 1

            eval_dir = os.path.join(checkpoint_dir, log_dir)
            makedirs(eval_dir)

            if not unlabeled:

                result = pd.DataFrame(result,
                                      columns=['label', 'steering_angle'])
                rmse = np.sqrt(np.mean((output_value - label_value)**2))
                print('Root mean square error: {:.6f}'.format(rmse))
                result.iloc[:num_examples + 1].to_csv(os.path.join(
                    eval_dir, 'eval-{0:010d}.csv'.format(last_step)),
                                                      index=False)
                summary = tf.Summary()
                summary.ParseFromString(sess.run(summary_op))
                summary.value.add(tag='Root mean square error',
                                  simple_value=float(rmse))
                summary_writer.add_summary(summary, last_step)

            else:  # just generate submission file
                result = pd.DataFrame(result,
                                      columns=['frame_id', 'steering_angle'])
                result['frame_id'] = result.frame_id.apply(lambda x: '%.f' %
                                                           (x))
                result.iloc[:num_examples + 1].to_csv(os.path.join(
                    eval_dir, 'test-{0:010d}.csv'.format(last_step)),
                                                      index=False)

        except Exception as e:
            coord.request_stop(e)
            print(e)

        coord.request_stop()
        coord.join(threads, stop_grace_period_secs=10)
        return last_step
def log_scalar1(tag, value, step):
    summary = tf.Summary(value=[tf.Summary.Value(tag=tag,
                                                 simple_value=value)])
    writer1.add_summary(summary, step)
示例#19
0
文件: ppo.py 项目: zhaohaobing/ray
    def _train(self):
        agents = self.remote_evaluators
        config = self.config
        model = self.local_evaluator

        if (config["num_workers"] * config["min_steps_per_task"] >
                config["timesteps_per_batch"]):
            print(
                "WARNING: num_workers * min_steps_per_task > "
                "timesteps_per_batch. This means that the output of some "
                "tasks will be wasted. Consider decreasing "
                "min_steps_per_task or increasing timesteps_per_batch.")

        print("===> iteration", self.iteration)

        iter_start = time.time()
        weights = ray.put(model.get_weights())
        [a.set_weights.remote(weights) for a in agents]
        samples = collect_samples(agents, config, self.local_evaluator)

        def standardized(value):
            # Divide by the maximum of value.std() and 1e-4
            # to guard against the case where all values are equal
            return (value - value.mean()) / max(1e-4, value.std())

        samples.data["advantages"] = standardized(samples["advantages"])

        rollouts_end = time.time()
        print("Computing policy (iterations=" + str(config["num_sgd_iter"]) +
              ", stepsize=" + str(config["sgd_stepsize"]) + "):")
        names = [
            "iter", "total loss", "policy loss", "vf loss", "kl", "entropy"]
        print(("{:>15}" * len(names)).format(*names))
        samples.shuffle()
        shuffle_end = time.time()
        tuples_per_device = model.load_data(
            samples, self.iteration == 0 and config["full_trace_data_load"])
        load_end = time.time()
        rollouts_time = rollouts_end - iter_start
        shuffle_time = shuffle_end - rollouts_end
        load_time = load_end - shuffle_end
        sgd_time = 0
        for i in range(config["num_sgd_iter"]):
            sgd_start = time.time()
            batch_index = 0
            num_batches = (
                int(tuples_per_device) // int(model.per_device_batch_size))
            loss, policy_loss, vf_loss, kl, entropy = [], [], [], [], []
            permutation = np.random.permutation(num_batches)
            # Prepare to drop into the debugger
            if self.iteration == config["tf_debug_iteration"]:
                model.sess = tf_debug.LocalCLIDebugWrapperSession(model.sess)
            while batch_index < num_batches:
                full_trace = (
                    i == 0 and self.iteration == 0 and
                    batch_index == config["full_trace_nth_sgd_batch"])
                batch_loss, batch_policy_loss, batch_vf_loss, batch_kl, \
                    batch_entropy = model.run_sgd_minibatch(
                        permutation[batch_index] * model.per_device_batch_size,
                        self.kl_coeff, full_trace,
                        self.file_writer)
                loss.append(batch_loss)
                policy_loss.append(batch_policy_loss)
                vf_loss.append(batch_vf_loss)
                kl.append(batch_kl)
                entropy.append(batch_entropy)
                batch_index += 1
            loss = np.mean(loss)
            policy_loss = np.mean(policy_loss)
            vf_loss = np.mean(vf_loss)
            kl = np.mean(kl)
            entropy = np.mean(entropy)
            sgd_end = time.time()
            print(
                "{:>15}{:15.5e}{:15.5e}{:15.5e}{:15.5e}{:15.5e}".format(
                    i, loss, policy_loss, vf_loss, kl, entropy))

            values = []
            if i == config["num_sgd_iter"] - 1:
                metric_prefix = "ppo/sgd/final_iter/"
                values.append(tf.Summary.Value(
                    tag=metric_prefix + "kl_coeff",
                    simple_value=self.kl_coeff))
                values.extend([
                    tf.Summary.Value(
                        tag=metric_prefix + "mean_entropy",
                        simple_value=entropy),
                    tf.Summary.Value(
                        tag=metric_prefix + "mean_loss",
                        simple_value=loss),
                    tf.Summary.Value(
                        tag=metric_prefix + "mean_kl",
                        simple_value=kl)])
                if self.file_writer:
                    sgd_stats = tf.Summary(value=values)
                    self.file_writer.add_summary(sgd_stats, self.global_step)
            self.global_step += 1
            sgd_time += sgd_end - sgd_start
        if kl > 2.0 * config["kl_target"]:
            self.kl_coeff *= 1.5
        elif kl < 0.5 * config["kl_target"]:
            self.kl_coeff *= 0.5

        info = {
            "kl_divergence": kl,
            "kl_coefficient": self.kl_coeff,
            "rollouts_time": rollouts_time,
            "shuffle_time": shuffle_time,
            "load_time": load_time,
            "sgd_time": sgd_time,
            "sample_throughput": len(samples["observations"]) / sgd_time
        }

        FilterManager.synchronize(
            self.local_evaluator.filters, self.remote_evaluators)
        res = self._fetch_metrics_from_remote_evaluators()
        res = res._replace(info=info)
        return res
示例#20
0
        if savedir is None:
            # Careful! This will not get cleaned up. Docker spoils the developers.
            savedir = tempfile.TemporaryDirectory().name
    else:
        container = None
    # Create and seed the env.
    env, monitored_env = make_env(args.env)
    if args.seed > 0:
        set_global_seeds(args.seed)
        env.unwrapped.seed(args.seed)

    subdir = (datetime.datetime.now()
              ).strftime("%m-%d-%Y-%H:%M:%S") + " " + args.comment
    tf_writer = tf.summary.FileWriter(os.path.join(args.log_dir, subdir),
                                      tf.get_default_graph())
    value_summary = tf.Summary()
    qec_summary = tf.Summary()
    value_summary.value.add(tag='reward_mean')
    qec_summary.value.add(tag='qec_mean')
    qec_summary.value.add(tag='qec_fount')
    value_summary.value.add(tag='steps')

    with U.make_session(4) as sess:
        # EMDQN

        ec_buffer = []
        buffer_size = 1000000
        latent_dim = 2 * args.latent_dim
        # input_dim = 1024
        for i in range(env.action_space.n):
            ec_buffer.append(LRU_KNN(buffer_size, latent_dim, 'game'))
def main(args):

    #network = importlib.import_module(args.model_def, 'inception_v3')
    network = importlib.import_module(args.model_def)

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')

    log_dir = os.path.join(os.path.expanduser(args.logs_base_dir), subdir)
    if not os.path.isdir(
            log_dir):  # Create the log directory if it doesn't exist
        os.makedirs(log_dir)

    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(
            model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)

    with open(os.path.join(model_dir, 'args.txt'), 'w') as f:
        for arg in vars(args):
            f.write(arg + ' ' + str(getattr(args, arg)) + '\n')

    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, log_dir, ' '.join(sys.argv))

    np.random.seed(seed=args.seed)
    #train_set = facenet.get_dataset(args.data_dir)
    train_set = facenet.get_dataset_with_enhanced(args.data_dir)
    nrof_classes = len(train_set)

    print('Model directory: %s' % model_dir)
    print('Log directory: %s' % log_dir)
    pretrained_model = None
    if args.pretrained_model:
        pretrained_model = os.path.expanduser(args.pretrained_model)
        print('Pre-trained model: %s' % pretrained_model)

    if args.lfw_dir:
        print('LFW directory: %s' % args.lfw_dir)
        # Read the file containing the pairs used for testing
        pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))
        # Get the paths for the corresponding images
        lfw_paths, actual_issame = lfw.get_paths(
            os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)
        # Get a list of image paths and their labels
        image_list, label_list = facenet.get_image_paths_and_labels(train_set)
        #image_list, label_list, dataset_ind_flat, dataset_paths = facenet.get_image_paths_and_labels_and_dataset_ind(args.data_dir)
        del train_set

        # Read data and apply label preserving distortions
        image_batch, label_batch = facenet.read_and_augument_data(
            image_list,
            label_list,
            args.image_size,
            args.batch_size,
            args.max_nrof_epochs,
            random_rotate=args.random_rotate,
            random_crop=args.random_crop,
            random_flip=args.random_flip,
            nrof_preprocess_threads=args.nrof_preprocess_threads,
            padding_size=args.padding_size,
            patch_type=args.patch_type)
        #image_batch, label_batch = facenet.read_and_augument_data_reduced(image_list, label_list, dataset_ind_flat, dataset_paths, args.image_size,
        #    args.batch_size, args.max_nrof_epochs, args.random_rotate,args.random_crop, args.random_flip, args.nrof_preprocess_threads,args.padding_size,args.patch_type)
        #nrof_classes = np.max(label_list)+1
        print('Total number of classes: %d' % nrof_classes)
        print('Total number of examples: %d' % len(image_list))

        # Node for input images
        image_batch = tf.identity(image_batch, name='input')

        # Placeholder for the learning rate
        learning_rate_placeholder = tf.placeholder(tf.float32,
                                                   name='learning_rate')

        # Placeholder for phase_train
        phase_train_placeholder = tf.placeholder(tf.bool, name='phase_train')

        # Placeholder for keep probability
        keep_probability_placeholder = tf.placeholder(tf.float32,
                                                      name='keep_prob')

        # Build the inference graph
        # prelogits = network.inference(image_batch, keep_probability_placeholder,
        #    phase_train=phase_train_placeholder, weight_decay=args.weight_decay)
        batch_norm_params = {
            # Decay for the moving averages
            'decay': 0.995,
            # epsilon to prevent 0s in variance
            'epsilon': 0.001,
            # force in-place updates of mean and variance estimates
            'updates_collections': None,
            # Moving averages ends up in the trainable variables collection
            'variables_collections': [tf.GraphKeys.TRAINABLE_VARIABLES],
            # Only update statistics during training mode
            'is_training': phase_train_placeholder
        }

        #prelogits, _ = network.inception_v3(image_batch, num_classes=len(train_set),is_training=True)
        prelogits, _ = network.inference(image_batch,
                                         args.keep_probability,
                                         phase_train=phase_train_placeholder,
                                         weight_decay=args.weight_decay)
        #prelogits = tf.identity(prelogits, name="prelogits")
        bottleneck = _fully_connected(prelogits,
                                      args.embedding_size,
                                      name='pre_embedding')
        #bottleneck = tf.nn.l2_normalize(bottleneck, dim=1,name='embedding')
        logits = _fully_connected_classifier(bottleneck,
                                             nrof_classes,
                                             name='logits')
        """
        bottleneck = slim.fully_connected(prelogits, args.embedding_size, activation_fn=None, 
                weights_initializer=tf.truncated_normal_initializer(stddev=0.1), 
                weights_regularizer=slim.l2_regularizer(args.weight_decay),
                normalizer_fn=slim.batch_norm,
                normalizer_params=batch_norm_params,
                scope='Bottleneck', reuse=False)

        logits = slim.fully_connected(bottleneck, len(train_set), activation_fn=None,
                weights_initializer=tf.truncated_normal_initializer(stddev=0.1), 
                weights_regularizer=slim.l2_regularizer(args.weight_decay),
                scope='Logits', reuse=False)

        logits = tf.identity(logits, name="logits")
        """
        # Add DeCov regularization loss
        if args.decov_loss_factor > 0.0:
            logits_decov_loss = facenet.decov_loss(
                logits) * args.decov_loss_factor
            tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES,
                                 logits_decov_loss)

        # Add center loss
        update_centers = tf.no_op('update_centers')
        if args.center_loss_factor > 0.0:
            prelogits_center_loss, update_centers = facenet.center_loss(
                bottleneck, label_batch, args.center_loss_alfa, nrof_classes)
            tf.add_to_collection(
                tf.GraphKeys.REGULARIZATION_LOSSES,
                prelogits_center_loss * args.center_loss_factor)

        #embeddings = tf.nn.l2_normalize(bottleneck, 1, 1e-10, name='embeddings')

        learning_rate = tf.train.exponential_decay(
            learning_rate_placeholder,
            global_step,
            args.learning_rate_decay_epochs * args.epoch_size,
            args.learning_rate_decay_factor,
            staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=label_batch,
            logits=logits,
            name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy,
                                            name='cross_entropy')
        """
        # Multi-label loss: sigmoid loss
        sigmoid_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=label_batch, logits=logits, name='sigmoid_loss_per_example')
        sigmoid_loss_mean = tf.reduce_mean(sigmoid_loss, name='sigmoid_loss')
        """
        tf.add_to_collection('losses', cross_entropy_mean)

        # Calculate the total losses
        regularization_losses = tf.get_collection(
            tf.GraphKeys.REGULARIZATION_LOSSES)
        total_loss = tf.add_n([cross_entropy_mean] + regularization_losses,
                              name='total_loss')
        #total_loss = tf.add_n([cross_entropy_mean], name='total_loss')

        # prediction
        prediction = tf.argmax(logits, axis=1, name='prediction')
        acc = slim.metrics.accuracy(predictions=tf.cast(prediction,
                                                        dtype=tf.int32),
                                    labels=tf.cast(label_batch,
                                                   dtype=tf.int32))

        # Build a Graph that trains the model with one batch of examples and updates the model parameters
        train_op = facenet.train(total_loss, global_step, args.optimizer,
                                 learning_rate, args.moving_average_decay,
                                 tf.global_variables())

        # Create a saver
        # save_variables = list(set(tf.all_variables())-set([w])-set([b]))
        save_variables = tf.trainable_variables()
        saver = tf.train.Saver(save_variables, max_to_keep=3)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()

        # Start running operations on the Graph.
        # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        summary_writer = tf.summary.FileWriter(log_dir, sess.graph)
        tf.train.start_queue_runners(sess=sess)

        with sess.as_default():

            if pretrained_model:
                saver.restore(sess, pretrained_model)

            # Training and validation loop
            epoch = 0
            while epoch < args.max_nrof_epochs:
                step = sess.run(global_step, feed_dict=None)
                epoch = step // args.epoch_size
                # Train for one epoch
                train(args, sess, epoch, phase_train_placeholder,
                      learning_rate_placeholder, keep_probability_placeholder,
                      global_step, total_loss, acc, train_op, summary_op,
                      summary_writer, regularization_losses,
                      args.learning_rate_schedule_file, update_centers)

                # Evaluate on LFW
                if args.lfw_dir:
                    start_time = time.time()
                    _, _, accuracy, val, val_std, far = lfw.validate(
                        sess,
                        lfw_paths,
                        actual_issame,
                        args.seed,
                        args.batch_size,
                        image_batch,
                        phase_train_placeholder,
                        keep_probability_placeholder,
                        embeddings,
                        nrof_folds=args.lfw_nrof_folds)
                    print('Accuracy: %1.3f+-%1.3f' %
                          (np.mean(accuracy), np.std(accuracy)))
                    print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' %
                          (val, val_std, far))
                    lfw_time = time.time() - start_time
                    # Add validation loss and accuracy to summary
                    summary = tf.Summary()
                    #pylint: disable=maybe-no-member
                    summary.value.add(tag='lfw/accuracy',
                                      simple_value=np.mean(accuracy))
                    summary.value.add(tag='lfw/val_rate', simple_value=val)
                    summary.value.add(tag='time/lfw', simple_value=lfw_time)
                    summary_writer.add_summary(summary, step)
                    with open(os.path.join(log_dir, 'lfw_result.txt'),
                              'at') as f:
                        f.write('%d\t%.5f\t%.5f\n' %
                                (step, np.mean(accuracy), val))

                # Save variables and the metagraph if it doesn't exist already
                save_variables_and_metagraph(sess, saver, summary_writer,
                                             model_dir, subdir, step)

    return model_dir
示例#22
0
                        [model.cls_loss, model.pred_class], feed_dict=vfeed)

                    vcls_l, vloc_gt_l, vloc_t1_l, vcls_cor, vloc_gt_cor, vloc_t1_cor = \
                        sess.run([model.cls_loss, model.loc_loss_gt, model.loc_loss_t1,
                                  model.corrections, model.correct_and_iou_gt, model.correct_and_iou_t1],
                                 feed_dict=vfeed)

                    cls_loss += vcls_l
                    loc_gt_loss += vloc_gt_l
                    loc_t1_loss += vloc_t1_l
                    cls_corrections.extend(vcls_cor)
                    loc_gt_corrections.extend(vloc_gt_cor)
                    loc_t1_corrections.extend(vloc_t1_cor)

                val_cls_loss_sum = tf.Summary(value=[
                    tf.Summary.Value(tag="val_cls_loss", simple_value=cls_loss)
                ])
                summary_writer.add_summary(val_cls_loss_sum, counter)
                val_loc_gt_loss_sum = tf.Summary(value=[
                    tf.Summary.Value(tag="val_loc_gt_loss",
                                     simple_value=loc_gt_loss)
                ])
                summary_writer.add_summary(val_loc_gt_loss_sum, counter)
                val_loc_t1_loss_sum = tf.Summary(value=[
                    tf.Summary.Value(tag="val_loc_t1_loss",
                                     simple_value=loc_t1_loss)
                ])
                summary_writer.add_summary(val_loc_t1_loss_sum, counter)

                val_cls_acc = sum(cls_corrections) / len(cls_corrections)
                val_cls_acc_sum = tf.Summary(value=[
示例#23
0
def _write_eval_dict_to_summary(eval_dict, tag, summary_writer, global_step):
    summary = tf.Summary()
    for name, result in eval_dict:
        summary.value.add(tag=tag + '/' + name, simple_value=result)
    summary_writer.add_summary(summary, global_step)
    return
    def train(self,
              max_iter,
              train_eval_step,
              validation_eval_step,
              display_step,
              checkpoint_step = None):
        
        # Keep training until reach max iterations
        for _ in range (max_iter):
            if self.global_step % self.num_batch == 0:
                self.data_train ,self.ground_truth_train = self.shuffle(self.data_train, self.ground_truth_train) 
       
            batch_idx = self.global_step % self.num_batch
            data_idx = np.arange (batch_idx * self.batch_size,
                                  min ((batch_idx+1) * self.batch_size,
                                       self.data_train.shape[0])).astype('int32')
            
            batch_x = self.data_train[data_idx]
            batch_y = self.ground_truth_train[data_idx]

            if self.global_step % train_eval_step == 0:
                
                print("Training: " + str(self.global_step))
                eval_result = self.evaluate_model(self.model, self.sess, batch_x, batch_y)                
                
                #summary np.array
                auc_summary_str_train = tf.Summary(value = [
                        tf.Summary.Value(tag = 'train/'+key, simple_value = eval_result[key])
                        for key in eval_result])
                
                self.summary_writer.add_summary(auc_summary_str_train, global_step = self.global_step)
                self.summary_writer.flush()
            
  
            if self.global_step % validation_eval_step == 0:
                
                print("Validation: " + str(self.global_step))
                eval_result = self.evaluate_model(self.model, self.sess, self.data_validation, self.ground_truth_validation)
                                
                #summary np.array
                auc_summary_str_valid = tf.Summary(value = [
                        tf.Summary.Value(tag = 'validation/'+key, simple_value = eval_result[key])
                        for key in eval_result])
                
                self.summary_writer.add_summary(auc_summary_str_valid, global_step = self.global_step)
                self.summary_writer.flush()
            
                
            #optimization part
            _, summary_str, loss, debug_info = self.sess.run((self.model.optimizer, self.summary, self.model.cost, self.model.debug_info),
                                           feed_dict={self.model.x: batch_x,
                                                      self.model.y: batch_y})
            
            #print debug_info
            
            self.summary_writer.add_summary(summary_str, global_step = self.global_step)
            self.summary_writer.flush()
            
            if self.global_step % display_step == 0:
                print "Minibatch Loss= {:.6f}".format(loss)
   
            
            if checkpoint_step is not None and self.global_step % checkpoint_step == 0:
                self.save_model(self.checkpoint_path)

            self.global_step += 1
        print " Optimization Finished!"
示例#25
0
def main():

    experiment_dir = os.path.dirname(os.path.abspath(__file__))

    # generating the dataset
    X_train, X_val, y_train, y_val = gen_dataset(config.op, config.op_sym, config.training_size, config.digits)
    
    # define input placeholders 
    with tf.name_scope('inputs'):
        input_seq = tf.placeholder(tf.float32, [None, config.maxlen, config.numbers_alphabet_size + 2], name='input_seq') # tensor of shape <batch_size, seq_len, embedding size>
        expected_ouput_seq = tf.placeholder(tf.float32, [None, config.maxlen, config.numbers_alphabet_size + 1], name='ouput_seq') # tensor of shape <batch_size, seq_len, numbers_alphabet_size>
        
    # construct layers
    nn_output_probs = construct_layers(input_seq) 
    nn_ouput_probs_rsh = tf.reshape(nn_output_probs, [-1, config.numbers_alphabet_size + 1])
   
    expected_ouput_seq_rsh = tf.reshape(expected_ouput_seq, [-1, config.numbers_alphabet_size + 1])

    # define loss 
    with tf.name_scope('loss'):    
        loss =  tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=nn_ouput_probs_rsh, labels=expected_ouput_seq_rsh))
        #tf.losses.softmax_cross_entropy(onehot_labels=expected_ouput_seq_rsh, logits=nn_ouput_probs_rsh)
    loss_summary = tf.summary.scalar('loss', loss)

    # define accuracy
    with tf.name_scope('accuracy'):
        correct_prediction = tf.equal(tf.argmax(expected_ouput_seq_rsh, 1), tf.argmax(nn_ouput_probs_rsh, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    accuracy_summary = tf.summary.scalar('accuracy', accuracy)

    # optimization
    optimizer = tf.train.AdamOptimizer(config.lr)
    global global_step
    global_step = tf.train.get_or_create_global_step()
    train_op = slim.learning.create_train_op(loss, optimizer, global_step=global_step)

    # config magic
    tf_config = tf.ConfigProto(log_device_placement = False)
    tf_config.gpu_options.allow_growth = True
    tf_config.gpu_options.allocator_type = 'BFC'
    
    try:
        with tf.train.MonitoredTrainingSession(checkpoint_dir=experiment_dir,
                                               save_checkpoint_secs=300,
                                               save_summaries_steps=200, config=tf_config) as sess:

            global_step_init = get_global_step_init(experiment_dir) #TODO
            if global_step_init != 0:
                print('Last run was aborted on global_step_init :{}'.format(global_step_init))
            else:
                print('Previous runs not found (You are begining this experiment).')


            for inputs, targets in utils.iterate_minibatches_global(X_train, y_train, batchsize=config.batch_size, start_it=global_step_init, iters=config.iters):

                train_loss = sess.run([train_op, loss, accuracy], feed_dict={input_seq : inputs, expected_ouput_seq : targets})
                gs = sess.run(global_step)

                if gs % config.print_loss_every == 0:
                    print("Step = {}; Train loss: {}".format(gs, train_loss))

                if gs % config.calc_val_loss_every == 0:
                    val_loss, val_accuracy = calc_validation_loss(sess, loss, accuracy, input_seq, expected_ouput_seq, X_val, y_val)
                    val_loss_summary = tf.Summary()
                    val_loss_summary.value.add(tag="loss/val_loss", simple_value=val_loss)
                    val_loss_summary.value.add(tag="accuracy/val_accuracy", simple_value=val_accuracy)
                    sess._hooks[1]._summary_writer.add_summary(val_loss_summary, gs)
                    print("=========== Step = {}; Val loss: {}; Val accuracy: {}".format(gs, val_loss, val_accuracy))

    except KeyboardInterrupt:
        print('Train process was interrupted')

    print('Done')
示例#26
0
 def scalar_summary(self, tag, value, step):
     summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value)])
     self.writer.add_summary(summary, step)
示例#27
0
def evaluate(evalDataSet, time, model_time, name='train'):

    with tf.Graph().as_default() as g, tf.device('/cpu:0'):

        # Placeholders for input, output and dropout
        input_x = tf.placeholder(tf.int32, [None, OPTION.SEQUENCE_LEN],
                                 name="input_x")
        features_before = tf.placeholder(tf.float32, [None, model_time, None],
                                         name="features_before")

        textcnn = TextCNNpn_model.Model(
            sequence_length=OPTION.SEQUENCE_LEN,
            num_classes=OPTION.NUM_CLASSES,
            vocab_size=None,
            embedding_size=OPTION.EMEBEDDING_DIMENSION,
            filter_sizes=list(map(int, FLAGS.filter_sizes.split(","))),
            num_filters=FLAGS.num_filters,
            Word2vec=True,
            Trainable=False)

        # inference model.
        _, features = textcnn.inference(input_x,
                                        features_before,
                                        OPTION.EVAL_BATCH_SIZE,
                                        eval_data=True)

        # get model paramaters
        # paramaters_list_reshape = textcnn.get_paramaters_list_reshape()

        # Restore the moving average version of the learned variables for eval. # ?????????????????????????
        variable_averages = tf.train.ExponentialMovingAverage(
            OPTION.MOVING_AVERAGE_DECAY)
        variables_to_restore = variable_averages.variables_to_restore()
        saver = tf.train.Saver(variables_to_restore)

        # Build the summary operation based on the TF collection of Summaries.
        summary_op = tf.summary.merge_all()
        summary_writer = tf.summary.FileWriter(OPTION.EVAL_DIR, g)

        # Start running operations on the Graph. allow_soft_placement must be set to
        # True to build towers on GPU, as some of the ops do not have GPU implementations.
        with tf.Session(config=tf.ConfigProto(
                allow_soft_placement=FLAGS.allow_soft_placement,
                log_device_placement=FLAGS.log_device_placement)) as sess:

            if os.path.exists(
                    os.path.join(OPTION.EVAL_DIR, 'model.ckpt-best.index')):
                # new_saver = tf.train.import_meta_graph(
                #     os.path.join(OPTION.TRAIN_DIR, 'model.ckpt-' + checkpoint + '.meta'))
                saver.restore(sess,
                              os.path.join(OPTION.EVAL_DIR, 'model.ckpt-best'))
            else:
                print('No checkpoint file found')
                return

            max_steps_per_epoch = int(
                math.ceil(evalDataSet.get_dataset_size() /
                          float(OPTION.EVAL_BATCH_SIZE)))
            total_predicted_value = []
            for step in range(max_steps_per_epoch):
                test_data, test_features = evalDataSet.next_batch(
                    OPTION.EVAL_BATCH_SIZE)
                predicted_value = sess.run(features,
                                           feed_dict={
                                               input_x: test_data,
                                               features_before: test_features
                                           })
                total_predicted_value.append(predicted_value)

            # test_data, test_label = evalDataSet.next_batch(OPTION.EVAL_BATCH_SIZE)
            summary = tf.Summary()
            # summary.ParseFromString(sess.run(summary_op, feed_dict={input_x: test_data, input_y: test_label}))

            total_predicted_value = np.concatenate(total_predicted_value,
                                                   axis=0)

            total_predicted_value = total_predicted_value[0:evalDataSet.
                                                          get_dataset_size()]

            assert evalDataSet.get_dataset_size(
            ) == total_predicted_value.shape[0], 'sample_count error!'

            detail_filename = os.path.join(
                OPTION.MODELPARA_DIR,
                'features_%s_%d_%d' % (name, time, model_time))
            if os.path.exists(detail_filename):
                os.remove(detail_filename)
            np.savetxt(detail_filename, total_predicted_value, fmt='%.4f')
示例#28
0
    def attack(self, X: np.ndarray, Y: np.ndarray, target_class: list = None, threshold: float = 0.,
               verbose: bool = False, print_every: int = 100, log_every: int = 100) \
            -> Tuple[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
        """
        Find a counterfactual (CF) for instance X using a fast iterative shrinkage-thresholding algorithm (FISTA).

        Parameters
        ----------
        X
            Instance to attack
        Y
            Labels for X as one-hot-encoding
        target_class
            List with target classes used to find closest prototype. If None, the nearest prototype
            except for the predict class on the instance is used.
        threshold
            Threshold level for the ratio between the distance of the counterfactual to the prototype of the
            predicted class for the original instance over the distance to the prototype of the predicted class
            for the counterfactual. If the trust score is below the threshold, the proposed counterfactual does
            not meet the requirements.
        verbose
            Print intermediate results of optimization if True
        print_every
            Print frequency if verbose is True
        log_every
            Tensorboard log frequency if write directory is specified

        Returns
        -------
        Overall best attack and gradients for that attack.
        """

        # make sure nb of instances in X equals batch size
        assert self.batch_size == X.shape[0]

        def compare(x: Union[float, int, np.ndarray], y: int) -> bool:
            """
            Compare predictions with target labels and return whether counterfactual conditions hold.

            Parameters
            ----------
            x
                Predicted class probabilities or labels
            y
                Target or predicted labels

            Returns
            -------
            Bool whether counterfactual conditions hold.
            """
            if not isinstance(x, (float, int, np.int64)):
                x = np.copy(x)
                x[y] += self.kappa
                x = np.argmax(x)
            return x != y

        # define target classes for prototype if not specified yet
        if target_class is None and self.enc_or_kdtree:
            target_class = list(range(self.classes))
            target_class.remove(np.argmax(Y, axis=1))
            if verbose:
                print('Predicted class: {}'.format(np.argmax(Y, axis=1)))
                print('Target classes: {}'.format(target_class))

        # find closest prototype in the target class list
        dist_proto = {}
        if self.enc_model:
            for k, v in self.class_proto.items():
                if k not in target_class:
                    continue
                dist_proto[k] = np.linalg.norm(self.enc.predict(X) - v)
        elif self.use_kdtree:
            self.class_proto = {}
            for c in range(self.classes):
                if c not in target_class:
                    continue
                dist_c, idx_c = self.kdtrees[c].query(X, k=1)
                dist_proto[c] = dist_c[0]
                self.class_proto[c] = self.X_by_class[c][idx_c[0]]

        if self.enc_or_kdtree:
            self.id_proto = min(dist_proto, key=dist_proto.get)
            proto_val = self.class_proto[self.id_proto]
            if verbose:
                print('Prototype class: {}'.format(self.id_proto))
        else:  # no prototype loss term used
            proto_val = np.zeros(self.shape_enc)

        # set the lower and upper bounds for the constant 'c' to scale the attack loss term
        # these bounds are updated for each c_step iteration
        const_lb = np.zeros(self.batch_size)
        const = np.ones(self.batch_size) * self.c_init
        const_ub = np.ones(self.batch_size) * 1e10

        # init values for the best attack instances for each instance in the batch
        overall_best_dist = [1e10] * self.batch_size
        overall_best_attack = [np.zeros(self.shape[1:])] * self.batch_size
        overall_best_grad = (np.zeros(self.shape), np.zeros(self.shape))

        # keep track of counterfactual evolution
        self.cf_global = {i: [] for i in range(self.c_steps)}  # type: dict

        # iterate over nb of updates for 'c'
        for _ in range(self.c_steps):

            # init variables
            self.sess.run(self.init)

            # reset current best distances and scores
            current_best_dist = [1e10] * self.batch_size
            current_best_proba = [-1] * self.batch_size

            # assign variables for the current iteration
            self.sess.run(
                self.setup, {
                    self.assign_orig: X,
                    self.assign_target: Y,
                    self.assign_const: const,
                    self.assign_adv: X,
                    self.assign_adv_s: X,
                    self.assign_target_proto: proto_val
                })

            X_der_batch, X_der_batch_s = [], []

            for i in range(self.max_iterations):

                # numerical gradients
                grads_num = np.zeros(self.shape)
                grads_num_s = np.zeros(self.shape)

                # check if numerical gradient computation is needed
                if not self.model and (self.c_init != 0. or self.c_steps > 1):
                    X_der = self.adv.eval(session=self.sess)
                    X_der_s = self.adv_s.eval(session=self.sess)
                    X_der_batch.append(X_der)
                    X_der_batch_s.append(X_der_s)

                    if i % self.update_num_grad == 0 and i > 0:  # compute numerical gradients
                        c = self.const.eval(session=self.sess)
                        X_der_batch = np.concatenate(X_der_batch)
                        X_der_batch_s = np.concatenate(X_der_batch_s)
                        grads_num = self.get_gradients(X_der_batch, Y) * c
                        grads_num_s = self.get_gradients(X_der_batch_s, Y) * c
                        # clip gradients
                        grads_num = np.clip(grads_num, self.clip[0],
                                            self.clip[1])
                        grads_num_s = np.clip(grads_num_s, self.clip[0],
                                              self.clip[1])
                        X_der_batch, X_der_batch_s = [], []

                # compute and clip gradients defined in graph
                grads_vars_graph = self.sess.run(self.compute_grads)
                grads_graph = [g for g, _ in grads_vars_graph][0]
                grads_graph = np.clip(grads_graph, self.clip[0], self.clip[1])

                # apply gradients
                grads = grads_graph + grads_num_s
                self.sess.run(self.apply_grads,
                              feed_dict={self.grad_ph: grads})

                # update adv and adv_s with perturbed instances
                self.sess.run([
                    self.adv_updater, self.adv_updater_s, self.delta,
                    self.delta_s
                ])

                # compute overall and attack loss, L1+L2 loss, prediction probabilities
                # on perturbed instances and new adv
                # L1+L2 and prediction probabilities used to see if adv is better than the current best adv under FISTA
                if self.model:
                    loss_tot, loss_attack, loss_l1_l2, pred_proba, adv = \
                        self.sess.run([self.loss_total, self.loss_attack, self.l1_l2, self.pred_proba, self.adv])
                else:
                    X_der = self.adv.eval(
                        session=self.sess)  # get updated perturbed instances
                    pred_proba = self.predict(X_der)

                    # compute attack, total and L1+L2 losses as well as new perturbed instance
                    loss_attack = self.loss_fn(pred_proba, Y)
                    feed_dict = {self.loss_attack: loss_attack}
                    loss_tot, loss_l1_l2, adv = self.sess.run(
                        [self.loss_total, self.l1_l2, self.adv],
                        feed_dict=feed_dict)

                if i % log_every == 0 or i % print_every == 0:
                    loss_l2, loss_l1, loss_ae, loss_proto = \
                        self.sess.run([self.loss_l2, self.loss_l1, self.loss_ae, self.loss_proto])
                    target_proba = np.sum(pred_proba * Y)
                    nontarget_proba_max = np.max((1 - Y) * pred_proba)
                    loss_opt = loss_l1_l2 + loss_attack + loss_ae + loss_proto

                if i % log_every == 0 and self.writer is not None:
                    lr, zt, gs = self.sess.run(
                        [self.learning_rate, self.zt, self.global_step])

                    # add values and images to tensorboard
                    summary = tf.Summary()
                    summary.value.add(tag='loss/Optimized',
                                      simple_value=loss_opt)
                    summary.value.add(tag='loss/Total', simple_value=loss_tot)
                    summary.value.add(tag='loss/L1', simple_value=loss_l1)
                    summary.value.add(tag='loss/L2', simple_value=loss_l2)
                    summary.value.add(tag='loss/AutoEncoder',
                                      simple_value=loss_ae)
                    summary.value.add(tag='loss/ClassPrototype',
                                      simple_value=loss_proto)
                    summary.value.add(tag='loss/PredScale',
                                      simple_value=const[0])
                    summary.value.add(tag='loss/PredLoss',
                                      simple_value=loss_attack)
                    summary.value.add(tag='training/lr', simple_value=lr)
                    summary.value.add(tag='training/z', simple_value=zt)
                    summary.value.add(tag='training/GlobalStep',
                                      simple_value=gs)
                    self.writer.add_summary(summary)
                    self.writer.flush()

                if verbose and i % print_every == 0:
                    print('\nIteration: {}; Const: {}'.format(i, const[0]))
                    print('Loss total: {:.3f}, loss attack: {:.3f}'.format(
                        loss_tot, loss_attack))
                    print('L2: {:.3f}, L1: {:.3f}, loss AE: {:.3f}'.format(
                        loss_l2, loss_l1, loss_ae))
                    print('Loss proto: {:.3f}'.format(loss_proto))
                    print('Target proba: {:.2f}, max non target proba: {:.2f}'.
                          format(target_proba, nontarget_proba_max))
                    print('Gradient graph min/max: {:.3f}/{:.3f}'.format(
                        grads_graph.min(), grads_graph.max()))
                    print('Gradient graph mean/abs mean: {:.3f}/{:.3f}'.format(
                        np.mean(grads_graph), np.mean(np.abs(grads_graph))))
                    if not self.model:
                        print(
                            'Gradient numerical attack min/max: {:.3f}/{:.3f}'.
                            format(grads_num.min(), grads_num.max()))
                        print(
                            'Gradient numerical mean/abs mean: {:.3f}/{:.3f}'.
                            format(np.mean(grads_num),
                                   np.mean(np.abs(grads_num))))
                    sys.stdout.flush()

                # update best perturbation (distance) and class probabilities
                # if beta * L1 + L2 < current best and predicted label is different from the initial label:
                # update best current step or global perturbations
                for batch_idx, (dist, proba, adv_idx) in enumerate(
                        zip(loss_l1_l2, pred_proba, adv)):
                    Y_class = np.argmax(Y[batch_idx])

                    # calculate trust score
                    if threshold > 0.:
                        score = self.score(np.expand_dims(adv_idx, axis=0),
                                           np.argmax(pred_proba), Y_class)
                        above_threshold = score > threshold
                    else:
                        above_threshold = True

                    # current step
                    if dist < current_best_dist[batch_idx] and compare(
                            proba, Y_class) and above_threshold:
                        current_best_dist[batch_idx] = dist
                        current_best_proba[batch_idx] = np.argmax(proba)

                    # global
                    if dist < overall_best_dist[batch_idx] and compare(
                            proba, Y_class) and above_threshold:
                        if verbose:
                            print('\nNew best counterfactual found!')
                        overall_best_dist[batch_idx] = dist
                        overall_best_attack[batch_idx] = adv_idx
                        overall_best_grad = (grads_graph, grads_num)
                        self.best_attack = True
                        self.cf_global[_].append(adv_idx)

            # adjust the 'c' constant for the first loss term
            for batch_idx in range(self.batch_size):
                if (compare(current_best_proba[batch_idx],
                            np.argmax(Y[batch_idx]))
                        and current_best_proba[batch_idx] != -1):
                    # want to refine the current best solution by putting more emphasis on the regularization terms
                    # of the loss by reducing 'c'; aiming to find a perturbation closer to the original instance
                    const_ub[batch_idx] = min(const_ub[batch_idx],
                                              const[batch_idx])
                    if const_ub[batch_idx] < 1e9:
                        const[batch_idx] = (const_lb[batch_idx] +
                                            const_ub[batch_idx]) / 2
                else:
                    # no valid current solution; put more weight on the first loss term to try and meet the
                    # prediction constraint before finetuning the solution with the regularization terms
                    const_lb[batch_idx] = max(
                        const_lb[batch_idx],
                        const[batch_idx])  # update lower bound to constant
                    if const_ub[batch_idx] < 1e9:
                        const[batch_idx] = (const_lb[batch_idx] +
                                            const_ub[batch_idx]) / 2
                    else:
                        const[batch_idx] *= 10

        # return best overall attack
        best_attack = np.concatenate(overall_best_attack, axis=0)
        if best_attack.shape != self.shape:
            best_attack = np.expand_dims(best_attack, axis=0)

        return best_attack, overall_best_grad
示例#29
0
def train(args, sess, dataset, epoch, image_paths_placeholder, labels_placeholder, labels_batch,
          batch_size_placeholder, learning_rate_placeholder, phase_train_placeholder, enqueue_op, input_queue, global_step, 
          embeddings, loss, train_op, summary_op, summary_writer, learning_rate_schedule_file,
          embedding_size, anchor, positive, negative, triplet_loss):
    batch_number = 0
    
    if args.learning_rate>0.0:
        lr = args.learning_rate
    else:
        lr = facenet.get_learning_rate_from_file(learning_rate_schedule_file, epoch)
    while batch_number < args.epoch_size:
        # Sample people randomly from the dataset
        image_paths, num_per_class = sample_people(dataset, args.people_per_batch, args.images_per_person)
        
        print('Running forward pass on sampled images: ', end='')
        start_time = time.time()
        nrof_examples = args.people_per_batch * args.images_per_person
        labels_array = np.reshape(np.arange(nrof_examples),(-1,3))
        image_paths_array = np.reshape(np.expand_dims(np.array(image_paths),1), (-1,3))
        sess.run(enqueue_op, {image_paths_placeholder: image_paths_array, labels_placeholder: labels_array})
        emb_array = np.zeros((nrof_examples, embedding_size))
        nrof_batches = int(np.ceil(nrof_examples / args.batch_size))
        for i in range(nrof_batches):
            batch_size = min(nrof_examples-i*args.batch_size, args.batch_size)
            emb, lab = sess.run([embeddings, labels_batch], feed_dict={batch_size_placeholder: batch_size, 
                learning_rate_placeholder: lr, phase_train_placeholder: True})
            emb_array[lab,:] = emb
        print('%.3f' % (time.time()-start_time))

        # Select triplets based on the embeddings
        print('Selecting suitable triplets for training')
        triplets, nrof_random_negs, nrof_triplets = select_triplets(emb_array, num_per_class, 
            image_paths, args.people_per_batch, args.alpha)
        selection_time = time.time() - start_time
        print('(nrof_random_negs, nrof_triplets) = (%d, %d): time=%.3f seconds' % 
            (nrof_random_negs, nrof_triplets, selection_time))

        # Perform training on the selected triplets
        nrof_batches = int(np.ceil(nrof_triplets*3/args.batch_size))
        triplet_paths = list(itertools.chain(*triplets))
        labels_array = np.reshape(np.arange(len(triplet_paths)),(-1,3))
        triplet_paths_array = np.reshape(np.expand_dims(np.array(triplet_paths),1), (-1,3))
        sess.run(enqueue_op, {image_paths_placeholder: triplet_paths_array, labels_placeholder: labels_array})
        nrof_examples = len(triplet_paths)
        train_time = 0
        i = 0
        emb_array = np.zeros((nrof_examples, embedding_size))
        loss_array = np.zeros((nrof_triplets,))
        summary = tf.Summary()
        step = 0
        while i < nrof_batches:
            start_time = time.time()
            batch_size = min(nrof_examples-i*args.batch_size, args.batch_size)
            feed_dict = {batch_size_placeholder: batch_size, learning_rate_placeholder: lr, phase_train_placeholder: True}
            err, _, step, emb, lab = sess.run([loss, train_op, global_step, embeddings, labels_batch], feed_dict=feed_dict)
            emb_array[lab,:] = emb
            loss_array[i] = err
            duration = time.time() - start_time
            print('Epoch: [%d][%d/%d]\tTime %.3f\tLoss %2.3f' %
                  (epoch, batch_number+1, args.epoch_size, duration, err))
            batch_number += 1
            i += 1
            train_time += duration
            summary.value.add(tag='loss', simple_value=err)
            
        # Add validation loss and accuracy to summary
        #pylint: disable=maybe-no-member
        summary.value.add(tag='time/selection', simple_value=selection_time)
        summary_writer.add_summary(summary, step)
    return step
示例#30
0
def q_learning(env,
               sess,
               qlearn_estimator,
               target_estimator,
               num_episodes,
               num_epoches,
               replay_memory_size=500000,
               replay_memory_init_size=50000,
               experiment_dir='./log/',
               update_target_estimator_every=10000,
               discount_factor=0.99,
               epsilon_start=1.0,
               epsilon_end=0.1,
               epsilon_decay_steps=500000,
               batch_size=512,
               num_label_propagation=20,
               num_active_learning=5,
               test=0):
    """
    Q-Learning algorithm for off-policy TD control using Function Approximation.
    Finds the optimal greedy policy while following an epsilon-greedy policy.

    Args:
        env: The environment.
        estimator: Action-Value function estimator
        num_episodes: Number of episodes to run for.
        discount_factor: Lambda time discount factor.
        epsilon: Chance the sample a random action. Float betwen 0 and 1.
        epsilon_decay: Each episode, epsilon is decayed by this factor

    Returns:
        NULL
    """
    # 1. Define some useful variable
    # To memory
    Transition = namedtuple("Transition",
                            ["state", "reward", "next_state", "done"])

    # The replay memory
    replay_memory = []

    # Create directories for checkpoints and summaries
    checkpoint_dir = os.path.join(experiment_dir, "checkpoints")

    checkpoint_path = os.path.join(checkpoint_dir, "model")

    if not os.path.exists(checkpoint_dir):
        os.makedirs(checkpoint_dir)

    # Load a previous checkpoint if we find one
    saver = tf.train.Saver()

    latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir)
    if latest_checkpoint:
        print("Loading model checkpoint {}...\n".format(latest_checkpoint))
        saver.restore(sess, latest_checkpoint)
        if test:
            return

    # Get the time step
    total_t = sess.run(tf.contrib.framework.get_global_step())

    # The epsilon decay schedule
    epsilons = np.linspace(epsilon_start, epsilon_end, epsilon_decay_steps)

    # The policy we're following
    policy = make_epsilon_greedy_policy(qlearn_estimator, env.action_space_n)

    num_label = 0

    # 2. Populate the replay memory with initial experience by SVM
    popu_time = time.time()

    # warm up with active learning
    print('Warm up starting...')

    outliers_fraction = 0.01
    data_train = []
    for num in range(env.datasetsize):
        env.reset()
        # remove time window
        data_train.extend(env.states_list)
    # Isolation Forest model
    model = WarmUp().warm_up_isolation_forest(outliers_fraction, data_train)

    # label propagation model
    lp_model = label_propagation.LabelSpreading()

    for t in itertools.count():
        env.reset()
        data = np.array(env.states_list).transpose(2, 0, 1).reshape(
            2, -1)[0].reshape(-1, n_steps)[:, -1].reshape(-1, 1)
        anomaly_score = model.decision_function(data)  # [-0.5, 0.5]
        pred_score = [-1 * s + 0.5 for s in anomaly_score]  # [0, 0.5]
        #threshold = stats.scoreatpercentile(pred_score, 100 * outliers_fraction)
        warm_samples = np.argsort(pred_score)[:5]
        warm_samples = np.append(warm_samples, np.argsort(pred_score)[-5:])

        # al.label(warm_samples)

        # retrieve input for label propagation
        state_list = np.array(env.states_list).transpose(2, 0, 1)[0]
        label_list = [-1] * len(state_list)  # remove labels

        for sample in warm_samples:
            # pick up a state from warm_up samples
            state = env.states_list[sample]
            # update the cursor
            env.timeseries_curser = sample + n_steps
            action_probs = policy(
                state, epsilons[min(total_t, epsilon_decay_steps - 1)])
            action = np.random.choice(np.arange(len(action_probs)),
                                      p=action_probs)

            # mark the sample to labeled
            env.timeseries['label'][env.timeseries_curser] = env.timeseries[
                'anomaly'][env.timeseries_curser]
            num_label += 1

            # retrieve label for propagation
            label_list[sample] = int(
                env.timeseries['anomaly'][env.timeseries_curser])

            next_state, reward, done, _ = env.step(action)

            replay_memory.append(Transition(state, reward, next_state, done))

        # label propagation main process:

        unlabeled_indices = [i for i, e in enumerate(label_list) if e == -1]
        label_list = np.array(label_list)
        lp_model.fit(state_list, label_list)
        pred_entropies = stats.distributions.entropy(
            lp_model.label_distributions_.T)
        # select up to N samples that is most certain about
        certainty_index = np.argsort(pred_entropies)
        certainty_index = certainty_index[np.in1d(
            certainty_index, unlabeled_indices)][:num_label_propagation]
        # give them pseudo labels
        for index in certainty_index:
            pseudo_label = lp_model.transduction_[index]
            env.timeseries['label'][index + n_steps] = pseudo_label

        if len(replay_memory) >= replay_memory_init_size:
            break
    '''
    # warm up without active learning
    state = env.reset()
    while env.datasetidx > env.datasetrng * validation_separate_ratio:
        env.reset()
        print('double reset')
        
    for i in range(replay_memory_init_size):
        action_probs = policy(state, epsilons[min(total_t, epsilon_decay_steps - 1)])
        action = np.random.choice(np.arange(len(action_probs)), p=action_probs)

        # mark the sample to labeled
        env.timeseries['labeled'][env.timeseries_curser] = 1
        num_label += 1
        
        next_state, reward, done, _ = env.step(action)
        replay_memory.append(Transition(state, reward, next_state, done))

        if done:
            state = env.reset()
            while env.datasetidx > env.datasetrng * validation_separate_ratio:
                env.reset()
                print('double reset')
        else:
            state = next_state[action]
    '''
    popu_time = time.time() - popu_time
    print("Populating replay memory with time {}".format(popu_time))

    # 3. Start the main loop2
    dict = {}
    for i_episode in range(num_episodes):
        # Save the current checkpoint
        if i_episode % 50 == 49:
            print("Save checkpoint in episode {}/{}".format(
                i_episode + 1, num_episodes))
            saver.save(tf.get_default_session(), checkpoint_path)

        per_loop_time1 = time.time()

        # Reset the environment
        state = env.reset()
        while env.datasetidx > env.datasetrng * validation_separate_ratio:
            env.reset()
            print('double reset')
        # Active learning:
        # if a AL is needed

        # index of already labeled samples of this TS
        labeled_index = [
            i for i, e in enumerate(env.timeseries['label']) if e != -1
        ]
        # transform to match state_list
        labeled_index = [item for item in labeled_index if item >= 25]
        labeled_index = [item - n_steps for item in labeled_index]

        al = active_learning(env=env,
                             N=num_active_learning,
                             strategy='margin_sampling',
                             estimator=qlearn_estimator,
                             already_selected=labeled_index)
        # find the samples need to be labeled by human
        al_samples = al.get_samples()
        print('labeling samples: ' + str(al_samples) + 'in env' +
              str(env.datasetidx))
        # al.label(al_samples)
        # add the new labeled samples
        labeled_index.extend(al_samples)
        num_label += len(al_samples)

        # retrieve input for label propagation
        state_list = np.array(env.states_list).transpose(2, 0, 1)[0]
        label_list = np.array(env.timeseries['label'][n_steps:])

        for new_sample in al_samples:
            label_list[new_sample] = env.timeseries['anomaly'][n_steps +
                                                               new_sample]
            env.timeseries['label'][
                n_steps + new_sample] = env.timeseries['anomaly'][n_steps +
                                                                  new_sample]

        for samples in labeled_index:
            env.timeseries_curser = samples + n_steps
            # 3.1 Some Preprocess
            # Epsilon for this time step
            epsilon = epsilons[min(total_t, epsilon_decay_steps - 1)]

            state = env.states_list[samples]

            # 3.2 The main work
            # Choose an action to take
            action_probs = policy(state, epsilon)
            action = np.random.choice(np.arange(len(action_probs)),
                                      p=action_probs)

            # Take a step
            next_state, reward, done, _ = env.step(action)

            # 3.3 Control replay memory
            if len(replay_memory) == replay_memory_size:
                replay_memory.pop(0)

            replay_memory.append(Transition(state, reward, next_state, done))

        # label propagation main process:
        unlabeled_indices = [i for i, e in enumerate(label_list) if e == -1]
        label_list = np.array(label_list)
        lp_model.fit(state_list, label_list)
        pred_entropies = stats.distributions.entropy(
            lp_model.label_distributions_.T)
        # select up to N samples that is most certain about
        certainty_index = np.argsort(pred_entropies)
        certainty_index = certainty_index[np.in1d(
            certainty_index, unlabeled_indices)][:num_label_propagation]
        # give them pseudo labels
        for index in certainty_index:
            pseudo_label = lp_model.transduction_[index]
            env.timeseries['label'][index + n_steps] = pseudo_label
        '''
        ori_idx = env.datasetidx
        if env.datasetidx >= 0:
            if dict.has_key(env.datasetidx) is False:
                already_selected = []
                dict[env.datasetidx] = already_selected
            al = active_learning(env=env, N=28, strategy='margin_sampling',
                                 estimator=qlearn_estimator, already_selected=dict[env.datasetidx])
            active_samples = al.get_samples()
            #active_samples = al.get_samples_by_score(threshold=1.5)

            # Label the samples
            # al.label(active_samples)

            # Add new-labeled samples

            dict[env.datasetidx].extend(active_samples)
            print 'add labeled samples: ' + str(active_samples) + 'in env ' + str(env.datasetidx)

            # One step in the environment
            for ids in dict.keys():
                env.reset_to(ids)
                print 'Training on ' + str(ids)
                for samples in dict[ids]:
                    env.timeseries_curser = samples + n_steps
                    # 3.1 Some Preprocess
                    # Epsilon for this time step
                    epsilon = epsilons[min(total_t, epsilon_decay_steps - 1)]

                    state = env.states_list[samples]

                    # 3.2 The main work
                    # Choose an action to take
                    action_probs = policy(state, epsilon)
                    action = np.random.choice(np.arange(len(action_probs)), p=action_probs)

                    # Take a step
                    if env.timeseries['labeled'][env.timeseries_curser] == 0:
                        env.timeseries['labeled'][env.timeseries_curser] = 1
                        num_label += 1

                    next_state, reward, done, _ = env.step(action)

                    # 3.3 Control replay memory
                    if len(replay_memory) == replay_memory_size:
                        replay_memory.pop(0)

                    replay_memory.append(Transition(state, reward, next_state, done))
            print 'num_label: ' + str(num_label)

            env.datasetidx = ori_idx
        else:
            # One step in the environment
            for t in itertools.count():
                # 3.1 Some Preprocess
                # Epsilon for this time step
                epsilon = epsilons[min(total_t, epsilon_decay_steps - 1)]

                # 3.2 The main work
                # Choose an action to take

                action_probs = policy(state, epsilon)
                action = np.random.choice(np.arange(len(action_probs)), p=action_probs)

                # Take a step
                next_state, reward, done, _ = env.step(action)

                # 3.3 Control replay memory
                if len(replay_memory) == replay_memory_size:
                    replay_memory.pop(0)

                # if the sample for this step is unlabeled, skip
                if env.timeseries['labeled'][env.timeseries_curser] != 0:
                    replay_memory.append(Transition(state, reward, next_state, done))
                    print 'memory stored'
                else:
                    print 'unlabeled -> skip'

                if done:
                    break

                state = next_state[action]
        '''
        per_loop_time2 = time.time()

        # Update the model
        for i_epoch in range(num_epoches):
            # Add epsilon to Tensorboard
            if qlearn_estimator.summary_writer:
                episode_summary = tf.Summary()
                qlearn_estimator.summary_writer.add_summary(
                    episode_summary, total_t)

            # Update the target estimator
            if total_t % update_target_estimator_every == 0:
                copy_model_parameters(sess, qlearn_estimator, target_estimator)
                print("\nCopied model parameters to target network.\n")

            # Sample a minibatch from the replay memory
            samples = random.sample(replay_memory, batch_size)
            states_batch, reward_batch, next_states_batch, done_batch = map(
                np.array, zip(*samples))

            if discount_factor > 0:
                # Calculate q values and targets
                next_states_batch = np.squeeze(
                    np.split(next_states_batch, 2, axis=1))
                next_states_batch0 = next_states_batch[0]
                next_states_batch1 = next_states_batch[1]

                q_values_next0 = target_estimator.predict(
                    state=next_states_batch0)
                q_values_next1 = target_estimator.predict(
                    state=next_states_batch1)

                targets_batch = reward_batch + (discount_factor * np.stack(
                    (np.amax(q_values_next0,
                             axis=1), np.amax(q_values_next1, axis=1)),
                    axis=-1))
            else:
                targets_batch = reward_batch

            # Perform gradient descent update
            qlearn_estimator.update(state=states_batch,
                                    target=targets_batch.astype(np.float32))

            total_t += 1

        # Print out which step we're on, useful for debugging.
        per_loop_time_popu = per_loop_time2 - per_loop_time1
        per_loop_time_updt = time.time() - per_loop_time2
        print("Global step {} @ Episode {}/{}, time: {} + {}".format(
            total_t, i_episode + 1, num_episodes, per_loop_time_popu,
            per_loop_time_updt))
    return