def main(_):
    if not FLAGS.dataset_dir:
        raise ValueError('You must supply the dataset directory with --dataset_dir')

    tf.logging.set_verbosity(tf.logging.INFO)
    with tf.Graph().as_default():
        tf_global_step = slim.get_or_create_global_step()

        ######################
        # Select the dataset #
        ######################
        dataset = dataset_factory.get_dataset(
            FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir)

        num_classes = dataset.num_classes

        ####################
        # Select the model #
        ####################
        network_fn = nets_factory.get_network_fn(
            FLAGS.model_name,
            num_classes=num_classes,
            is_training=False)

        ##############################################################
        # Create a dataset provider that loads data from the dataset #
        ##############################################################
        provider = slim.dataset_data_provider.DatasetDataProvider(
            dataset,
            shuffle=False,
            common_queue_capacity=2 * FLAGS.batch_size,
            common_queue_min=FLAGS.batch_size)
        [image, label] = provider.get(['image', 'label'])

        #####################################
        # Select the preprocessing function #
        #####################################
        preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
        image_preprocessing_fn = preprocessing_factory.get_preprocessing(
            preprocessing_name,
            is_training=False)

        eval_image_size = FLAGS.eval_image_size or network_fn.default_image_size

        image, label = image_preprocessing_fn(image, eval_image_size, eval_image_size,
                                              label=label)

        images, labels = tf.train.batch(
            [image, label],
            batch_size=FLAGS.batch_size,
            num_threads=FLAGS.num_preprocessing_threads,
            capacity=5 * FLAGS.batch_size)

        labels_one_hot = slim.one_hot_encoding(labels, num_classes)

        ####################
        # Define the model #
        ####################
        logits, _, _ = network_fn(images)
        logits = tf.image.resize_bilinear(logits,
                                          size=(eval_image_size, eval_image_size), align_corners=True)

        labels_one_hot = tf.reshape(labels_one_hot, logits.get_shape())

        my_loss = tf.losses.softmax_cross_entropy(
            labels_one_hot, logits, label_smoothing=0.0)

        variables_to_restore = slim.get_variables_to_restore()

        predictions = tf.argmax(logits, 3)
        labels = tf.squeeze(labels)
        predictions = tf.squeeze(predictions)

        predictions = tf.reshape(predictions, [-1, ])
        labels = tf.reshape(labels, [-1, ])

        indices = tf.squeeze(tf.where(tf.not_equal(labels, 0)), 1)
        labels = tf.cast(tf.gather(labels, indices), tf.int32)
        predictions = tf.gather(predictions, indices)
        predictions = tf.add(predictions, tf.constant(1, dtype=tf.int64))

        # Define the metrics:
        pixel_acc = tf.contrib.metrics.streaming_accuracy(predictions, labels)
        if FLAGS.dataset_name == 'ade20k':
            mean_iou = tf.contrib.metrics.streaming_mean_iou(predictions, labels, num_classes + 1)
        else:
            mean_iou = tf.contrib.metrics.streaming_mean_iou(predictions, labels, num_classes)

        names_to_values, names_to_updates = \
            tf.contrib.metrics.aggregate_metric_map({
                'Pixel_ACC': pixel_acc,
                'IOU': mean_iou
            })

        # Print the summaries to screen.
        for name, value in names_to_values.items():
            summary_name = 'eval/%s' % name
            op = tf.summary.scalar(summary_name, value, collections=[])
            op = tf.Print(op, [value], summary_name)
            tf.add_to_collection(tf.GraphKeys.SUMMARIES, op)

        num_batches = math.ceil(dataset.num_samples / float(FLAGS.batch_size))

        if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
            checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
        else:
            checkpoint_path = FLAGS.checkpoint_path

        tf.logging.info('Evaluating %s' % checkpoint_path)

        session_config = tf.ConfigProto()
        session_config.gpu_options.allow_growth = True

        my_loss_t = slim.evaluation.evaluate_once(
            session_config=session_config,
            master='',
            checkpoint_path=checkpoint_path,
            logdir=FLAGS.eval_dir,
            num_evals=num_batches,
            eval_op=list(names_to_updates.values()),
            variables_to_restore=variables_to_restore,
            final_op=[my_loss])

        print(my_loss_t)
Example #2
0
def main(_):
    if not FLAGS.dataset_dir:
        raise ValueError('You must supply the dataset directory with --dataset_dir')

    tf.logging.set_verbosity(tf.logging.DEBUG)

    with tf.Graph().as_default():
        deploy_config = model_deploy.DeploymentConfig(
            num_clones=FLAGS.num_clones,
            clone_on_cpu=FLAGS.clone_on_cpu,
            replica_id=FLAGS.task,
            num_replicas=FLAGS.worker_replicas,
            num_ps_tasks=FLAGS.num_ps_tasks)

        # Create global_step
        with tf.device(deploy_config.variables_device()):
            global_step = slim.create_global_step()
        
        net = model_cmc.Model()

        with tf.device(deploy_config.inputs_device()):
            if (FLAGS.model == 'unet'):
                batch_queue = \
                load_batch.get_batch(FLAGS.dataset_dir,
                                        FLAGS.num_readers,
                                        FLAGS.batch_size,
                                        None,
                                        FLAGS,
                                        file_pattern = FLAGS.file_pattern,
                                        is_training = True,
                                        shuffe = FLAGS.shuffle_data)
            elif (FLAGS.model == 'patch'):
                batch_queue = \
                load_batch_patch.get_batch(FLAGS.dataset_dir,
                                        FLAGS.num_readers,
                                        FLAGS.batch_size,
                                        None,
                                        FLAGS,
                                        file_pattern = FLAGS.file_pattern,
                                        is_training = True,
                                        shuffe = FLAGS.shuffle_data)
            elif (FLAGS.model == 'cmc'):
                batch_queue = \
                load_batch_cmc.get_batch(FLAGS.dataset_dir,
                                        FLAGS.num_readers,
                                        FLAGS.batch_size,
                                        None,
                                        FLAGS,
                                        file_pattern = FLAGS.file_pattern,
                                        is_training = True,
                                        shuffe = FLAGS.shuffle_data)

        # =================================================================== #
        # Define the model running on every GPU.
        # =================================================================== #
        def clone_fn(batch_queue):
            batch_shape = [1]*3
            b_image, label = batch_queue
            
            logits, end_points = net.net(b_image)

            # Add loss function.
            loss, mean_iou = net.weighted_losses(logits, label)
            return end_points, mean_iou


        summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES))

        clones = model_deploy.create_clones(deploy_config, clone_fn, [batch_queue])
        first_clone_scope = deploy_config.clone_scope(0)

        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope)


        end_points, mean_iou = clones[0].outputs
        update_ops.append(mean_iou[1])
        #for end_point in end_points:
        #	x = end_points[end_point]
        #	summaries.add(tf.summary.histogram('activations/' + end_point, x))


        for loss in tf.get_collection('EXTRA_LOSSES',first_clone_scope):
            summaries.add(tf.summary.scalar(loss.op.name, loss))

        #
        #for variable in slim.get_model_variables():
        #	summaries.add(tf.summary.histogram(variable.op.name, variable))

        #################################
        # Configure the moving averages #
        #################################
        if FLAGS.moving_average_decay:
            moving_average_variables = slim.get_model_variables()
            variable_averages = tf.train.ExponentialMovingAverage(
                                FLAGS.moving_average_decay, global_step)
        else:
            moving_average_variables, variable_averages = None, None

        #########################################
        # Configure the optimization procedure. #
        #########################################
        with tf.device(deploy_config.optimizer_device()):
            learning_rate = tf_utils.configure_learning_rate(FLAGS,
                                                                FLAGS.num_samples,
                                                                global_step)
            optimizer = tf_utils.configure_optimizer(FLAGS, learning_rate)
            summaries.add(tf.summary.scalar('learning_rate', learning_rate))

        if FLAGS.fine_tune:
            gradient_multipliers = pickle.load(open('nets/multiplier_300.pkl','rb'))
        else:
            gradient_multipliers = None
            

        if FLAGS.moving_average_decay:
            # Update ops executed locally by trainer.
            update_ops.append(variable_averages.apply(moving_average_variables))

        # Variables to train.
        variables_to_train = tf_utils.get_variables_to_train(FLAGS)

        #  and returns a train_tensor and summary_op
        total_loss, clones_gradients = model_deploy.optimize_clones(
            clones,
            optimizer,
            var_list=variables_to_train)
        # Add total_loss to summary.
        summaries.add(tf.summary.scalar('total_loss', total_loss))
        if gradient_multipliers:
            with ops.name_scope('multiply_grads'):
                clones_gradients = slim.learning.multiply_gradients(clones_gradients, gradient_multipliers)

        if FLAGS.clip_gradient_norm > 0:
            with ops.name_scope('clip_grads'):
                clones_gradients = slim.learning.clip_gradient_norms(clones_gradients, FLAGS.clip_gradient_norm)
        # Create gradient updates.
        grad_updates = optimizer.apply_gradients(clones_gradients,
                                                    global_step=global_step)
        update_ops.append(grad_updates)

        update_op = tf.group(*update_ops)
        train_tensor = control_flow_ops.with_dependencies([update_op], total_loss,
                                                            name='train_op')

        #train_tensor = slim.learning.create_train_op(total_loss, optimizer, gradient_multipliers=gradient_multipliers)
        # Add the summaries from the first clone. These contain the summaries
        # created by model_fn and either optimize_clones() or _gather_clone_loss().
        summaries |= set(tf.get_collection(tf.GraphKeys.SUMMARIES,
                                            first_clone_scope))

        # Merge all summaries together.
        summary_op = tf.summary.merge(list(summaries), name='summary_op')

        # =================================================================== #
        # Kicks off the training.
        # =================================================================== #
        
        def train_step_fn(session, *args, **kwargs):
            # visualizer = Beholder(session=session, logdir=FLAGS.train_dir)
            total_loss, should_stop = train_step(session, *args, **kwargs)

            if train_step_fn.step % FLAGS.validation_check == 0:
                _mean_iou = session.run(train_step_fn.mean_iou)
                print('evaluation step %d - loss = %.4f mean_iou = %.2f%%' %\
                 (train_step_fn.step, total_loss, _mean_iou ))
            # evaluated_tensors = session.run([end_points['conv4'], end_points['up1']])
            # example_frame = session.run(end_points['up2'])
            # visualizer.update(arrays=evaluated_tensors, frame=example_frame)

            train_step_fn.step += 1
            return [total_loss, should_stop]

        train_step_fn.step = 0
        train_step_fn.end_points = end_points
        train_step_fn.mean_iou = mean_iou[0]


        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_memory_fraction,
                                    allocator_type="BFC")
        config = tf.ConfigProto(gpu_options=gpu_options,
                                log_device_placement=False,
                                allow_soft_placement = True,								
                                inter_op_parallelism_threads = 0,
                                intra_op_parallelism_threads = 1,)
        saver = tf.train.Saver(max_to_keep=5,
                                keep_checkpoint_every_n_hours=1.0,
                                write_version=2,
                                pad_step_number=False)


        slim.learning.train(
            train_tensor,
            logdir=FLAGS.train_dir,
            master='',
            is_chief=True,
            train_step_fn=train_step_fn,
            init_fn=tf_utils.get_init_fn(FLAGS),
            summary_op=summary_op,
            number_of_steps=FLAGS.max_number_of_steps,
            log_every_n_steps=FLAGS.log_every_n_steps,
            save_summaries_secs=FLAGS.save_summaries_secs,
            saver=saver,
            save_interval_secs=FLAGS.save_interval_secs,
            session_config=config,
            sync_optimizer=None)
import sys
import os
import glob
import shutil

import numpy as np
import PIL.Image as Image
from PIL import ImageDraw
import tensorflow as tf
import matplotlib.pyplot as plt

from cityscapesscripts.helpers.labels import trainId2label

from object_detection.data_decoders.tf_example_decoder import TfExampleDecoder

with tf.Graph().as_default():
    file_pattern = 'object_detection/data/records/cityscapes_mini/00000-of-00004.record'
    tfrecords = glob.glob(file_pattern)

    with tf.device('/cpu:0'):
        filename_queue = tf.train.string_input_producer(
            tfrecords, capacity=len(tfrecords))
        reader = tf.TFRecordReader()
        _, serialized_example = reader.read(filename_queue)
        example = TfExampleDecoder().decode(serialized_example)
        print(example.keys())

    sess = tf.Session()
    init_op = tf.group(tf.global_variables_initializer(),
                       tf.local_variables_initializer())
Example #4
0
def main():

    cfg = TrainConfig().parse()
    print (cfg.name)
    result_dir = os.path.join(cfg.result_root, 
            cfg.name+'_'+datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S'))
    if not os.path.isdir(result_dir):
        os.makedirs(result_dir)
    utils.write_configure_to_file(cfg, result_dir)
    np.random.seed(seed=cfg.seed)

    # prepare dataset
    train_session = cfg.train_session
    train_set = prepare_multimodal_dataset(cfg.feature_root, train_session, cfg.feat, cfg.label_root)
    train_set = train_set[:cfg.label_num]
    batch_per_epoch = len(train_set)//cfg.sess_per_batch

    val_session = cfg.val_session
    val_set = prepare_multimodal_dataset(cfg.feature_root, val_session, cfg.feat, cfg.label_root)


    # construct the graph
    with tf.Graph().as_default():
        tf.set_random_seed(cfg.seed)
        global_step = tf.Variable(0, trainable=False)
        lr_ph = tf.placeholder(tf.float32, name='learning_rate')

        
        ####################### Load models here ########################

        with tf.variable_scope("modality_core"):
            # load backbone model
            if cfg.network == "convtsn":
                model_emb = networks.ConvTSN(n_seg=cfg.num_seg, emb_dim=cfg.emb_dim)
            elif cfg.network == "convrtsn":
                model_emb = networks.ConvRTSN(n_seg=cfg.num_seg, emb_dim=cfg.emb_dim)
            else:
                raise NotImplementedError

            input_ph = tf.placeholder(tf.float32, shape=[None, cfg.num_seg, None, None, None])
            dropout_ph = tf.placeholder(tf.float32, shape=[])
            model_emb.forward(input_ph, dropout_ph)    # for lstm has variable scope

        with tf.variable_scope("modality_sensors"):
            sensors_emb_dim = 32
            model_emb_sensors = networks.RTSN(n_seg=cfg.num_seg, emb_dim=sensors_emb_dim)

            input_sensors_ph = tf.placeholder(tf.float32, shape=[None, cfg.num_seg, 8])
            model_emb_sensors.forward(input_sensors_ph, dropout_ph)

            var_list = {}
            for v in tf.global_variables():
                if v.op.name.startswith("modality_sensors"):
                    var_list[v.op.name.replace("modality_sensors/","")] = v
            restore_saver_sensors = tf.train.Saver(var_list)


        with tf.variable_scope("modality_segment"):
            segment_emb_dim = 32
            model_emb_segment = networks.RTSN(n_seg=cfg.num_seg, emb_dim=segment_emb_dim, n_input=357)

            input_segment_ph = tf.placeholder(tf.float32, shape=[None, cfg.num_seg, 357])
            model_emb_segment.forward(input_segment_ph, dropout_ph)

            var_list = {}
            for v in tf.global_variables():
                if v.op.name.startswith("modality_segment"):
                    var_list[v.op.name.replace("modality_segment/","")] = v
            restore_saver_segment = tf.train.Saver(var_list)

        ############################# Forward Pass #############################


        # Core branch
        if cfg.normalized:
            embedding = tf.nn.l2_normalize(model_emb.hidden, axis=-1, epsilon=1e-10)
            embedding_sensors = tf.nn.l2_normalize(model_emb_sensors.hidden, axis=-1, epsilon=1e-10)
            embedding_hal_sensors = tf.nn.l2_normalize(hal_emb_sensors.hidden, axis=-1, epsilon=1e-10)
            embedding_segment = tf.nn.l2_normalize(model_emb_segment.hidden, axis=-1, epsilon=1e-10)
            embedding_hal_segment = tf.nn.l2_normalize(hal_emb_segment.hidden, axis=-1, epsilon=1e-10)
        else:
            embedding = model_emb.hidden
            embedding_sensors = model_emb_sensors.hidden
            embedding_hal_sensors = hal_emb_sensors.hidden
            embedding_segment = model_emb_segment.hidden
            embedding_hal_segment = hal_emb_segment.hidden

        # variable for visualizing the embeddings
        emb_var = tf.Variable([0.0], name='embeddings')
        set_emb = tf.assign(emb_var, embedding, validate_shape=False)

        # calculated for monitoring all-pair embedding distance
        diffs = utils.all_diffs_tf(embedding, embedding)
        all_dist = utils.cdist_tf(diffs)
        tf.summary.histogram('embedding_dists', all_dist)

        # split embedding into anchor, positive and negative and calculate triplet loss
        anchor, positive, negative = tf.unstack(tf.reshape(embedding, [-1,3,cfg.emb_dim]), 3, 1)
        anc_sensors, pos_sensors, neg_sensors = tf.unstack(tf.reshape(embedding_sensors, [-1,3,sensors_emb_dim]), 3, 1)
        anc_hal_sensors, pos_hal_sensors, neg_hal_sensors = tf.unstack(tf.reshape(embedding_hal_sensors, [-1,3,sensors_emb_dim]), 3, 1)
        anc_segment, pos_segment, neg_segment = tf.unstack(tf.reshape(embedding_segment, [-1,3,segment_emb_dim]), 3, 1)
        anc_hal_segment, pos_hal_segment, neg_hal_segment = tf.unstack(tf.reshape(embedding_hal_segment, [-1,3,segment_emb_dim]), 3, 1)

        # a fusion embedding
        anc_fused = tf.concat((anchor, anc_hal_sensors, anc_hal_segment), axis=1)
        pos_fused = tf.concat((positive, pos_hal_sensors, anc_hal_segment), axis=1)
        neg_fused = tf.concat((negative, neg_hal_sensors, anc_hal_segment), axis=1)

        ############################# Calculate loss #############################

        # triplet loss
        metric_loss = networks.triplet_loss(anchor, positive, negative, cfg.alpha) + \
                      networks.triplet_loss(anc_sensors, pos_sensors, neg_sensors, cfg.alpha) + \
                      networks.triplet_loss(anc_hal_sensors, pos_hal_sensors, neg_hal_sensors, cfg.alpha) + \
                      networks.triplet_loss(anc_segment, pos_segment, neg_segment, cfg.alpha) + \
                      networks.triplet_loss(anc_hal_segment, pos_hal_segment, neg_hal_segment, cfg.alpha) + \
                      networks.triplet_loss(anc_fused, pos_fused, neg_fused, cfg.alpha)

        # hallucination loss (regression loss)
        hal_loss_sensors = tf.nn.l2_loss(embedding_sensors - embedding_hal_sensors)
        hal_loss_segment = tf.nn.l2_loss(embedding_segment - embedding_hal_segment)
        hal_loss = hal_loss_sensors + hal_loss_segment

        regularization_loss = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
        # use lambda_multimodal for hal_loss
        total_loss = metric_loss + cfg.lambda_multimodal * hal_loss + regularization_loss * cfg.lambda_l2

        tf.summary.scalar('learning_rate', lr_ph)
        train_op = utils.optimize(total_loss, global_step, cfg.optimizer,
                                           lr_ph, tf.global_variables())

        saver = tf.train.Saver(max_to_keep=10)
        summary_op = tf.summary.merge_all()    # not logging histogram of variables because it will cause problem when only unimodal_train_op is called

        #########################################################################

        # session iterator for session sampling
        feat_paths_ph = tf.placeholder(tf.string, shape=[None, cfg.sess_per_batch])
        feat2_paths_ph = tf.placeholder(tf.string, shape=[None, cfg.sess_per_batch])
        feat3_paths_ph = tf.placeholder(tf.string, shape=[None, cfg.sess_per_batch])
        label_paths_ph = tf.placeholder(tf.string, shape=[None, cfg.sess_per_batch])
        train_data = multimodal_session_generator(feat_paths_ph, feat2_paths_ph, feat3_paths_ph, label_paths_ph, sess_per_batch=cfg.sess_per_batch, num_threads=2, shuffled=False, preprocess_func=[model_emb.prepare_input, model_emb_sensors.prepare_input, model_emb_segment.prepare_input])
        train_sess_iterator = train_data.make_initializable_iterator()
        next_train = train_sess_iterator.get_next()

        # prepare validation data
        val_sess = []
        val_feats = []
        val_feats2 = []
        val_feats3 = []
        val_labels = []
        val_boundaries = []
        for session in val_set:
            session_id = os.path.basename(session[1]).split('_')[0]
            eve_batch, lab_batch, boundary = load_data_and_label(session[0], session[-1], model_emb.prepare_input_test)    # use prepare_input_test for testing time
            val_feats.append(eve_batch)
            val_labels.append(lab_batch)
            val_sess.extend([session_id]*eve_batch.shape[0])
            val_boundaries.extend(boundary)

            eve2_batch, _,_ = load_data_and_label(session[1], session[-1], model_emb_sensors.prepare_input_test)
            val_feats2.append(eve2_batch)

            eve3_batch, _,_ = load_data_and_label(session[2], session[-1], model_emb_segment.prepare_input_test)
            val_feats3.append(eve3_batch)
        val_feats = np.concatenate(val_feats, axis=0)
        val_feats2 = np.concatenate(val_feats2, axis=0)
        val_feats3 = np.concatenate(val_feats3, axis=0)
        val_labels = np.concatenate(val_labels, axis=0)
        print ("Shape of val_feats: ", val_feats.shape)

        # generate metadata.tsv for visualize embedding
        with open(os.path.join(result_dir, 'metadata_val.tsv'), 'w') as fout:
            fout.write('id\tlabel\tsession_id\tstart\tend\n')
            for i in range(len(val_sess)):
                fout.write('{0}\t{1}\t{2}\t{3}\t{4}\n'.format(i, val_labels[i,0], val_sess[i],
                                            val_boundaries[i][0], val_boundaries[i][1]))

        #########################################################################


        # Start running the graph
        if cfg.gpu:
            os.environ['CUDA_VISIBLE_DEVICES'] = cfg.gpu

        gpu_options = tf.GPUOptions(allow_growth=True)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

        summary_writer = tf.summary.FileWriter(result_dir, sess.graph)

        with sess.as_default():

            sess.run(tf.global_variables_initializer())

            print ("Restoring sensors model: %s" % cfg.sensors_path)
            restore_saver_sensors.restore(sess, cfg.sensors_path)
            print ("Restoring segment model: %s" % cfg.segment_path)
            restore_saver_segment.restore(sess, cfg.segment_path)

            # load pretrain model, if needed
            if cfg.model_path:
                print ("Restoring pretrained model: %s" % cfg.model_path)
                saver.restore(sess, cfg.model_path)


            ################## Training loop ##################
            epoch = -1
            while epoch < cfg.max_epochs-1:
                step = sess.run(global_step, feed_dict=None)
                epoch = step // batch_per_epoch

                # learning rate schedule, reference: "In defense of Triplet Loss"
                if epoch < cfg.static_epochs:
                    learning_rate = cfg.learning_rate
                else:
                    learning_rate = cfg.learning_rate * \
                            0.01**((epoch-cfg.static_epochs)/(cfg.max_epochs-cfg.static_epochs))

                # prepare data for this epoch
                random.shuffle(train_set)

                paths = list(zip(*[iter(train_set)]*cfg.sess_per_batch))

                feat_paths = [[p[0] for p in path] for path in paths]
                feat2_paths = [[p[1] for p in path] for path in paths]
                feat3_paths = [[p[2] for p in path] for path in paths]
                label_paths = [[p[-1] for p in path] for path in paths]

                sess.run(train_sess_iterator.initializer, feed_dict={feat_paths_ph: feat_paths,
                  feat2_paths_ph: feat2_paths,
                  feat3_paths_ph: feat3_paths,
                  label_paths_ph: label_paths})

                # for each epoch
                batch_count = 1
                while True:
                    try:
                        ##################### Data loading ########################
                        start_time = time.time()
                        eve, eve_sensors, eve_segment, lab, batch_sess = sess.run(next_train)
                        load_time = time.time() - start_time
    
                        ##################### Triplet selection #####################
                        start_time = time.time()
                        # Get the embeddings of all events
                        eve_embedding = np.zeros((eve.shape[0], cfg.emb_dim), dtype='float32')
                        for start, end in zip(range(0, eve.shape[0], cfg.batch_size),
                                            range(cfg.batch_size, eve.shape[0]+cfg.batch_size, cfg.batch_size)):
                            end = min(end, eve.shape[0])
                            emb = sess.run(embedding, feed_dict={input_ph: eve[start:end], dropout_ph: 1.0})
                            eve_embedding[start:end] = np.copy(emb)
    
                        # sample triplets within sampled sessions
                        all_diff = utils.all_diffs(eve_embedding, eve_embedding)
                        triplet_input_idx, active_count = utils.select_triplets_facenet(lab,utils.cdist(all_diff,metric=cfg.metric),cfg.triplet_per_batch,cfg.alpha,num_negative=cfg.num_negative)
                        if triplet_input_idx is None:
                            continue
                        
                        triplet_input = eve[triplet_input_idx]
                        sensors_input = eve_sensors[triplet_input_idx]
                        segment_input = eve_segment[triplet_input_idx]

                        select_time = time.time() - start_time

                        if len(triplet_input.shape) > 5:    # debugging
                            pdb.set_trace()
    
                        ##################### Start training  ########################
    
                        err, metric_err, hal_err, _, step, summ = sess.run(
                                [total_loss, metric_loss, hal_loss, train_op, global_step, summary_op],
                                feed_dict = {input_ph: triplet_input,
                                             input_sensors_ph: sensors_input,
                                             input_segment_ph: segment_input,
                                             dropout_ph: cfg.keep_prob,
                                             lr_ph: learning_rate})
    
                        print ("%s\tEpoch: [%d][%d/%d]\tEvent num: %d\tTriplet num: %d\tLoad time: %.3f\tSelect time: %.3f\tMetric Loss %.4f\tHal Loss %.4f" % \
                                (cfg.name, epoch+1, batch_count, batch_per_epoch, eve.shape[0], triplet_input.shape[0]//3, load_time, select_time, metric_err, hal_err))
    
                        summary = tf.Summary(value=[tf.Summary.Value(tag="train_loss", simple_value=err),
                                    tf.Summary.Value(tag="active_count", simple_value=active_count),
                                    tf.Summary.Value(tag="metric_loss", simple_value=metric_err),
                                    tf.Summary.Value(tag="hallucination_loss", simple_value=hal_err)])
    
                        summary_writer.add_summary(summary, step)
                        summary_writer.add_summary(summ, step)

                        batch_count += 1
                    
                    except tf.errors.OutOfRangeError:
                        print ("Epoch %d done!" % (epoch+1))
                        break

                # validation on val_set
                print ("Evaluating on validation set...")
                val_embeddings, hal_err, _ = sess.run([embedding, hal_loss, set_emb],
                                                feed_dict = {input_ph: val_feats,
                                                             input_sensors_ph: val_feats2,
                                                             input_segment_ph: val_feats3,
                                                             dropout_ph: 1.0})
                mAP, mPrec = utils.evaluate_simple(val_embeddings, val_labels)

                summary = tf.Summary(value=[tf.Summary.Value(tag="Valiation mAP", simple_value=mAP),
                                            tf.Summary.Value(tag="Validation [email protected]", simple_value=mPrec),
                                            tf.Summary.Value(tag="Validation hal loss", simple_value=hal_err)])
                summary_writer.add_summary(summary, step)
                print ("Epoch: [%d]\tmAP: %.4f\tmPrec: %.4f" % (epoch+1,mAP,mPrec))

                # config for embedding visualization
                config = projector.ProjectorConfig()
                visual_embedding = config.embeddings.add()
                visual_embedding.tensor_name = emb_var.name
                visual_embedding.metadata_path = os.path.join(result_dir, 'metadata_val.tsv')
                projector.visualize_embeddings(summary_writer, config)

                # save model
                saver.save(sess, os.path.join(result_dir, cfg.name+'.ckpt'), global_step=step)
def create_image(network_type):
  # set parameters
  if network_type in ("model_conv_num_balls_1_beta_0.1", "model_conv_num_balls_1_beta_0.5", "model_conv_num_balls_1_beta_1.0"):
    FLAGS.model="conv"
    FLAGS.num_balls=1
  if network_type in ("model_conv_num_balls_2_beta_0.1", "model_conv_num_balls_2_beta_0.5", "model_conv_num_balls_2_beta_1.0"):
    FLAGS.model="conv"
    FLAGS.num_balls=2
  elif network_type in ("model_fully_connected_num_balls_1_beta_0.1", "model_fully_connected_num_balls_1_beta_0.5", "model_fully_connected_num_balls_1_beta_1.0"):
    FLAGS.model="fully_connected"
    FLAGS.num_balls=1
    FLAGS.hidden_size=10
  elif network_type in ("model_fully_connected_num_balls_2_beta_0.1", "model_fully_connected_num_balls_2_beta_0.5", "model_fully_connected_num_balls_2_beta_1.0"):
    FLAGS.model="fully_connected"
    FLAGS.num_balls=2
    FLAGS.hidden_size=10
  elif network_type in ("model_all_conv_num_balls_1_beta_0.1", "model_all_conv_num_balls_1_beta_0.5", "model_all_conv_num_balls_1_beta_1.0"):
    FLAGS.model="all_conv"
    FLAGS.num_balls=1
    FLAGS.hidden_size=1
  elif network_type in ("model_all_conv_num_balls_2_beta_0.1", "model_all_conv_num_balls_2_beta_0.5", "model_all_conv_num_balls_2_beta_1.0"):
    FLAGS.model="all_conv"
    FLAGS.num_balls=2
    FLAGS.hidden_size=1

  """Eval net to get stddev."""
  with tf.Graph().as_default():
    # make inputs
    x = tf.placeholder(tf.float32, [FLAGS.batch_size, 32, 32, 1])
 
    # no dropout on testing
    keep_prob = 1.0

    # make model
    if FLAGS.model=="fully_connected":
      mean, stddev, y_sampled, x_prime = arc.fully_connected_model(x, keep_prob)
    elif FLAGS.model=="conv":
      mean, stddev, y_sampled, x_prime = arc.conv_model(x, keep_prob)
    elif FLAGS.model=="all_conv":
      mean, stddev, y_sampled, x_prime = arc.all_conv_model(x, keep_prob)
    else:
      print("model requested not found, now some errors!")

    # List of all Variables
    variables = tf.all_variables()

    # Load weights operator
    print('save file is ./checkpoints/train_store_' + network_type)
    ckpt = tf.train.get_checkpoint_state('./checkpoints/train_store_' + network_type)
    weight_saver = tf.train.Saver(variables)

    # Summary op
    summary_op = tf.merge_all_summaries()
 
    # Start running operations on the Graph.
    sess = tf.Session()

    # init if this is the very time training
    weight_saver.restore(sess, ckpt.model_checkpoint_path)
    print("restored from" + ckpt.model_checkpoint_path)

    # Summary op
    graph_def = sess.graph.as_graph_def(add_shapes=True)

    dat = b.bounce_vec(32, FLAGS.num_balls, FLAGS.batch_size)
    stddev_r = np.sum(sess.run([stddev],feed_dict={x:dat})[0], axis=0)/FLAGS.batch_size
    sample_y = sess.run([y_sampled],feed_dict={x:dat})[0]
    print(sample_y[0])

    # create grid
    y_p, x_p = np.mgrid[0:1:32j, 0:1:32j]
    
    for i in xrange(10):
      index = np.argmin(stddev_r)
      for j in xrange(5):
        z_f = np.copy(sample_y)
        z_f[0,index] = 1.5*j - 3.0
        print(sample_y[0])
        print(z_f[0])
        plt.subplot(10,5,j+5*i + 1)
        plt.pcolor(x_p, y_p, sess.run([x_prime],feed_dict={y_sampled:z_f})[0][0,:,:,0])
        if i == 9:
          plt.xlabel(str(1.5*j - 3.0))
        if j == 0:
          plt.ylabel("{0:.2f}".format(stddev_r[index]))
        plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off')
        #plt.axis(frameon=False)
      # just make it big to get out of the way
      stddev_r[index] = 2.0
    plt.savefig("figures/heat_map_" + network_type + ".png")
def from_pipe(opts):
    command = ["ffprobe",
               '-v', "quiet",
               '-print_format', 'json',
               '-show_streams', opts.in_path]

    info = json.loads(str(subprocess.check_output(command), encoding="utf8"))
    width = int(info["streams"][0]["width"])
    height = int(info["streams"][0]["height"])
    fps = round(eval(info["streams"][0]["r_frame_rate"]))

    command = ["ffmpeg",
               '-loglevel', "quiet",
               '-i', opts.in_path,
               '-f', 'image2pipe',
               '-pix_fmt', 'rgb24',
               '-vcodec', 'rawvideo', '-']

    pipe_in = subprocess.Popen(command, stdout=subprocess.PIPE, bufsize=10 ** 9, stdin=None, stderr=None)

    command = ["ffmpeg",
               '-loglevel', "info",
               '-y',  # (optional) overwrite output file if it exists
               '-f', 'rawvideo',
               '-vcodec', 'rawvideo',
               '-s', str(width) + 'x' + str(height),  # size of one frame
               '-pix_fmt', 'rgb24',
               '-r', str(fps),  # frames per second
               '-i', '-',  # The imput comes from a pipe
               '-an',  # Tells FFMPEG not to expect any audio
               '-c:v', 'libx264',
               '-preset', 'slow',
               '-crf', '18',
               opts.out]

    pipe_out = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=None, stderr=None)
    g = tf.Graph()
    soft_config = tf.ConfigProto(allow_soft_placement=True)
    soft_config.gpu_options.allow_growth = True

    with g.as_default(), g.device(opts.device), \
         tf.Session(config=soft_config) as sess:
        batch_shape = (opts.batch_size, height, width, 3)
        img_placeholder = tf.placeholder(tf.float32, shape=batch_shape,
                                         name='img_placeholder')
        preds = transform.net(img_placeholder)
        saver = tf.train.Saver()
        if os.path.isdir(opts.checkpoint):
            ckpt = tf.train.get_checkpoint_state(opts.checkpoint)
            if ckpt and ckpt.model_checkpoint_path:
                saver.restore(sess, ckpt.model_checkpoint_path)
            else:
                raise Exception("No checkpoint found...")
        else:
            saver.restore(sess, opts.checkpoint)

        X = np.zeros(batch_shape, dtype=np.float32)
        nbytes = 3 * width * height
        read_input = True
        last = False

        while read_input:
            count = 0
            while count < opts.batch_size:
                raw_image = pipe_in.stdout.read(width * height * 3)

                if len(raw_image) != nbytes:
                    if count == 0:
                        read_input = False
                    else:
                        last = True
                        X = X[:count]
                        batch_shape = (count, height, width, 3)
                        img_placeholder = tf.placeholder(tf.float32, shape=batch_shape,
                                                     name='img_placeholder')
                        preds = transform.net(img_placeholder)
                    break

                image = numpy.fromstring(raw_image, dtype='uint8')
                image = image.reshape((height, width, 3))
                X[count] = image
                count += 1

            if read_input:
                if last:
                    read_input = False
                _preds = sess.run(preds, feed_dict={img_placeholder: X})

                for i in range(0, batch_shape[0]):
                    img = np.clip(_preds[i], 0, 255).astype(np.uint8)
                    try:
                        pipe_out.stdin.write(img)
                    except IOError as err:
                        ffmpeg_error = pipe_out.stderr.read()
                        error = (str(err) + ("\n\nFFMPEG encountered"
                                             "the following error while writing file:"
                                             "\n\n %s" % ffmpeg_error))
                        read_input = False
                        print(error)
        pipe_out.terminate()
        pipe_in.terminate()
        pipe_out.stdin.close()
        pipe_in.stdout.close()
        del pipe_in
        del pipe_out
Example #7
0
#           ...
#
# * Then you can run the operations on this graph as many times as you want by calling `session.run()`, providing it outputs to fetch from the graph that get returned. This runtime operation is all contained in the block below:
#
#       with tf.Session(graph=graph) as session:
#           ...
#
# Let's load all the data into TensorFlow and build the computation graph corresponding to our training:

# In[7]:

# With gradient descent training, even this much data is prohibitive.
# Subset the training data for faster turnaround.
train_subset = 10000

graph = tf.Graph()
with graph.as_default():

    # Input data.
    # Load the training, validation and test data into constants that are
    # attached to the graph.
    tf_train_dataset = tf.constant(train_dataset[:train_subset, :])
    tf_train_labels = tf.constant(train_labels[:train_subset])
    tf_valid_dataset = tf.constant(valid_dataset)
    tf_test_dataset = tf.constant(test_dataset)

    # Variables.
    # These are the parameters that we are going to be training. The weight
    # matrix will be initialized using random values following a (truncated)
    # normal distribution. The biases get initialized to zero.
    weights = tf.Variable(
Example #8
0
def main(_):
  pipeline_proto = _load_pipeline_proto(FLAGS.pipeline_proto)
  tf.logging.info("Pipeline configure: %s", '=' * 128)
  tf.logging.info(pipeline_proto)

  g = tf.Graph()
  with g.as_default():

    # Infer saliency.

    model = builder.build(pipeline_proto.model, is_training=False)
    predictions = model.build_prediction(
        examples={}, prediction_task=GAPPredictionTasks.word_saliency)

    saver = tf.train.Saver()
    invalid_variable_names = tf.report_uninitialized_variables()

  with tf.Session(graph=g) as sess:

    sess.run(tf.tables_initializer())

    # Load the latest checkpoint.

    checkpoint_path = tf.train.latest_checkpoint(pipeline_proto.model_dir)
    assert checkpoint_path is not None

    saver.restore(sess, checkpoint_path)
    assert len(sess.run(invalid_variable_names)) == 0

    predictions = sess.run(predictions)

  # Process kNN retrieval.

  name_to_class_id = {}
  with open(FLAGS.name_to_class_id_file, 'r') as fid:
    for line in fid.readlines():
      name, class_id = line.strip('\n').split('\t')
      name_to_class_id[name] = class_id

  (vocabulary, word_saliency,
   word_embedding) = (predictions[GAPPredictions.vocabulary],
                      predictions[GAPPredictions.word_saliency],
                      predictions[GAPPredictions.word_embedding])

  queries = list(name_to_class_id)
  synonyms_list = _knn_retrieval(queries, vocabulary, word_embedding,
                                 word_saliency)

  # Print to the terminal.

  expanded_name_to_class_id = []
  for query, synonyms in zip(queries, synonyms_list):
    elems = []
    for synonym in synonyms:
      if synonym['saliency'] < FLAGS.saliency_threshold:
        continue
      if synonym['similarity'] < FLAGS.similarity_threshold:
        continue
      elems.append(synonym['word'])
      expanded_name_to_class_id.append((synonym['word'],
                                        name_to_class_id[query]))
    print('%s\t%s' % (query, ','.join(elems)))

  # Write to output file.

  with open(FLAGS.expanded_name_to_class_id_file, 'w') as fid:
    for word, class_id in expanded_name_to_class_id:
      fid.write('%s\t%s\n' % (word, class_id))

  tf.logging.info('Done')
def main(_):
    os.environ["CUDA_VISIBLE_DEVICES"] = FLAGS.cuda_visible_devices
    gm = GPUManager()
    with gm.auto_choice():
        tf.logging.set_verbosity(tf.logging.INFO)
        tf.reset_default_graph()
        graph = tf.Graph()
        with graph.as_default() as g:
            ####################
            # Select the model #
            ####################
            network_fn = nets_factory.get_network_fn(
                FLAGS.model_name,
                num_classes=(FLAGS.num_classes - FLAGS.labels_offset),
                is_training=False)
            #####################################
            # Select the preprocessing function #
            #####################################
            preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
            image_preprocessing_fn = preprocessing_factory.get_preprocessing(
                preprocessing_name, is_training=False)
            test_image_size = FLAGS.test_image_size or network_fn.default_image_size
            if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
                checkpoint_path = tf.train.latest_checkpoint(
                    FLAGS.checkpoint_path)
            else:
                checkpoint_path = FLAGS.checkpoint_path

            batch_size = FLAGS.batch_size
            # tensor_input = tf.placeholder(tf.float32, [None, test_image_size, test_image_size, 3])
            tensor_input = tf.placeholder(
                tf.float32, [batch_size, test_image_size, test_image_size, 3])
            logits, _ = network_fn(tensor_input)
            #logits = tf.nn.top_k(logits, 5)
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            print(FLAGS.test_dir)
            jpg_list = ListFile(FLAGS.test_dir)
            tot = len(jpg_list)
            with tf.Session(config=config, graph=g) as sess:
                sess.run(tf.global_variables_initializer())
                saver = tf.train.Saver()
                saver.restore(sess, checkpoint_path)
                time_start = time.time()
                with open(os.path.join(FLAGS.test_dir, 'test.txt'), 'w') as f:
                    for idx in range(0, tot, batch_size):
                        images = list()
                        idx_end = min(tot, idx + batch_size)
                        print(idx)
                        for i in range(idx, idx_end):
                            image_id = jpg_list[i]
                            test_path = os.path.join(FLAGS.test_dir,
                                                     'jpg_data', image_id)
                            image = open(test_path, 'rb').read()
                            image = tf.image.decode_jpeg(image, channels=3)
                            processed_image = image_preprocessing_fn(
                                image, test_image_size, test_image_size)
                            processed_image = sess.run(processed_image)
                            images.append(processed_image)
                        images = np.array(images)
                        predictions = sess.run(
                            logits, feed_dict={tensor_input: images})
                        for i in range(idx, idx_end):
                            # if predictions[i-idx][0]-predictions[i-idx][1] <= 0.8:
                            #     continue
                            f.write('{}:{}\n'.format(
                                jpg_list[i], np.argmax(predictions[i - idx])))
                            #print(np.argmax(predictions[i - idx]))
                            #print('{}:{}\n'.format(jpg_list[i], sess.run(tf.argmax(predictions[i - idx]))))
                            #print('{} {}'.format(jpg_list[i], predictions[i - idx]))

                time_total = time.time() - time_start
                print('total time: {}, total images: {}, average time: {}'.
                      format(time_total, tot, time_total / tot))
Example #10
0
def main():
    pnet, rnet, onet = create_network_face_detection(gpu_memory_fraction)
    timer = str(datetime.now().time())
    fileCount = 0

    with tf.Graph().as_default():
        with tf.Session() as sess:
            facenet.load_model(model_dir)

            while True:
                args = parse_args()
                time.sleep(0.1)

                newFileCount = len(os.listdir(input_dir))
                start_time = datetime.strptime(timer, '%H:%M:%S.%f')
                end_time = datetime.strptime(str(datetime.now().time()),
                                             '%H:%M:%S.%f')
                diff = end_time - start_time
                elapsed_time = int((diff.seconds * 1000) +
                                   (diff.microseconds / 1000))

                if newFileCount > fileCount and elapsed_time > args.cluster_update:

                    print('Updating cluster...')

                    fileCount = newFileCount
                    timer = str(datetime.now().time())

                    root = etree.Element("root")

                    blacklist = {}
                    try:
                        tree = etree.parse(cluster_filename)
                        for i in tree.iter():
                            if i.tag == 'root':
                                continue
                            if len(i):
                                blacklist.update({i.tag: []})
                                if 'grp-' in i.tag:
                                    for j in i.iter():
                                        if not len(j):
                                            blacklist[i.tag].append(
                                                [j.tag, j.text])
                    except:
                        pass

                    HumanNames = os.listdir(pre_img)
                    HumanNames.sort()

                    classifier_filename_exp = os.path.expanduser(
                        classifier_filename)
                    with open(classifier_filename_exp, 'rb') as infile:
                        (model, class_names) = pickle.load(infile)

                    image_list = load_images_from_folder(input_dir)
                    images = align_data(image_list, image_size, args.margin,
                                        args.bb_area, pnet, rnet, onet)
                    images_placeholder = sess.graph.get_tensor_by_name(
                        "input:0")
                    embeddings = sess.graph.get_tensor_by_name("embeddings:0")
                    phase_train_placeholder = sess.graph.get_tensor_by_name(
                        "phase_train:0")
                    feed_dict = {
                        images_placeholder: images,
                        phase_train_placeholder: False
                    }
                    emb = sess.run(embeddings, feed_dict=feed_dict)
                    embedding_size = embeddings.get_shape()[1]
                    nrof_images = len(images)
                    matrix = np.zeros((nrof_images, nrof_images))

                    tagged = {}
                    for i in range(nrof_images):
                        emb_array = np.zeros((1, embedding_size))
                        emb_array[0, :] = emb[i]
                        predictions = model.predict_proba(emb_array)
                        best_class_indices = np.argmax(predictions, axis=1)
                        best_class_probabilities = predictions[
                            np.arange(len(best_class_indices)),
                            best_class_indices]
                        if best_class_probabilities > args.class_probability:
                            for H_i in HumanNames:
                                if HumanNames[best_class_indices[0]] == H_i:
                                    tagged.update(
                                        {i: HumanNames[best_class_indices[0]]})

                    # get euclidean distance matrices
                    for i in range(nrof_images):
                        for j in range(nrof_images):
                            dist = np.sqrt(
                                np.sum(
                                    np.square(np.subtract(
                                        emb[i, :], emb[j, :]))))
                            matrix[i][j] = dist

                    # DBSCAN is the only algorithm that doesn't require the number of clusters to be defined.
                    db = DBSCAN(eps=cluster_threshold,
                                min_samples=min_cluster_size,
                                metric='precomputed')
                    db.fit(matrix)
                    labels = db.labels_

                    # get number of clusters
                    no_clusters = len(set(labels)) - (1 if -1 in labels else 0)

                    print('No of clusters:', no_clusters)

                    if no_clusters > 0:
                        for i in range(no_clusters):
                            for j in np.nonzero(labels == i)[0]:
                                # path = os.path.join(output_dir, (tagged[j] if j in tagged else str(i)))
                                # if not os.path.exists(path):
                                #     os.makedirs(path)
                                # misc.imsave(os.path.join(path, image_list[j][0]), image_list[j][1])
                                tag = ("grp-" +
                                       tagged[j] if j in tagged else "unk-" +
                                       str(i))
                                group = root.find(tag)

                                exclude = False
                                for x in blacklist:
                                    for y in blacklist[x]:
                                        if y[1] == image_list[j][0]:
                                            exclude = True
                                            break
                                    if exclude:
                                        break

                                if not exclude:
                                    if group is None:
                                        group = etree.SubElement(root, tag)
                                    etree.SubElement(
                                        group, "face" +
                                        str(j)).text = image_list[j][0]

                    for x in blacklist:
                        for y in blacklist[x]:
                            group = root.find(x)
                            if group is None:
                                group = etree.SubElement(root, x)
                            etree.SubElement(group, y[0]).text = y[1]

                    tree = etree.ElementTree(root)
                    tree.write(cluster_filename)
def main(model_config, train_config, track_config):
    os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu()

    # Create training directory which will be used to save: configurations, model files, TensorBoard logs
    train_dir = train_config['train_dir']
    if not osp.isdir(train_dir):
        logging.info('Creating training directory: %s', train_dir)
        mkdir_p(train_dir)

    g = tf.Graph()
    with g.as_default():
        # Set fixed seed for reproducible experiments
        random.seed(train_config['seed'])
        np.random.seed(train_config['seed'])
        tf.set_random_seed(train_config['seed'])

        # Build the training and validation model
        model = siamese_model.SiameseModel(model_config,
                                           train_config,
                                           mode='train')
        model.build()
        model_va = siamese_model.SiameseModel(model_config,
                                              train_config,
                                              mode='validation')
        model_va.build(reuse=True)

        # Save configurations for future reference
        save_cfgs(train_dir, model_config, train_config, track_config)

        learning_rate = _configure_learning_rate(train_config,
                                                 model.global_step)
        optimizer = _configure_optimizer(train_config, learning_rate)
        tf.summary.scalar('learning_rate', learning_rate)

        # Set up the training ops
        opt_op = tf.contrib.layers.optimize_loss(
            loss=model.total_loss,
            global_step=model.global_step,
            learning_rate=learning_rate,
            optimizer=optimizer,
            clip_gradients=train_config['clip_gradients'],
            learning_rate_decay_fn=None,
            summaries=['learning_rate'])

        with tf.control_dependencies([opt_op]):
            train_op = tf.no_op(name='train')

        saver = tf.train.Saver(
            tf.global_variables(),
            max_to_keep=train_config['max_checkpoints_to_keep'])

        summary_writer = tf.summary.FileWriter(train_dir, g)
        summary_op = tf.summary.merge_all()

        global_variables_init_op = tf.global_variables_initializer()
        local_variables_init_op = tf.local_variables_initializer()
        g.finalize()  # Finalize graph to avoid adding ops by mistake

        # Dynamically allocate GPU memory
        gpu_options = tf.GPUOptions(allow_growth=True)
        sess_config = tf.ConfigProto(gpu_options=gpu_options)

        sess = tf.Session(config=sess_config)
        model_path = tf.train.latest_checkpoint(train_config['train_dir'])

        if not model_path:
            sess.run(global_variables_init_op)
            sess.run(local_variables_init_op)
            start_step = 0

            if model_config['embed_config']['embedding_checkpoint_file']:
                model.init_fn(sess)
        else:
            logging.info('Restore from last checkpoint: {}'.format(model_path))
            sess.run(local_variables_init_op)
            saver.restore(sess, model_path)
            start_step = tf.train.global_step(sess, model.global_step.name) + 1

        # Training loop
        data_config = train_config['train_data_config']
        total_steps = int(data_config['epoch'] *
                          data_config['num_examples_per_epoch'] /
                          data_config['batch_size'])
        logging.info('Train for {} steps'.format(total_steps))
        for step in range(start_step, total_steps):
            start_time = time.time()
            _, loss, batch_loss = sess.run(
                [train_op, model.total_loss, model.batch_loss])
            duration = time.time() - start_time

            if step % 10 == 0:
                examples_per_sec = data_config['batch_size'] / float(duration)
                time_remain = data_config['batch_size'] * (
                    total_steps - step) / examples_per_sec
                m, s = divmod(time_remain, 60)
                h, m = divmod(m, 60)
                format_str = (
                    '%s: step %d, total loss = %.2f, batch loss = %.2f (%.1f examples/sec; %.3f '
                    'sec/batch; %dh:%02dm:%02ds remains)')
                logging.info(format_str %
                             (datetime.now(), step, loss, batch_loss,
                              examples_per_sec, duration, h, m, s))

            if step % 100 == 0:
                summary_str = sess.run(summary_op)
                summary_writer.add_summary(summary_str, step)

            if step % train_config['save_model_every_n_step'] == 0 or (
                    step + 1) == total_steps:
                checkpoint_path = osp.join(train_config['train_dir'],
                                           'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
Example #12
0
def evaluate():
    """Evaluate."""
    assert FLAGS.dataset == 'KITTI', \
        'Currently only supports KITTI dataset'

    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    with tf.Graph().as_default() as g:

        assert FLAGS.net == 'vgg16' or FLAGS.net == 'resnet50' \
            or FLAGS.net == 'squeezeDet' or FLAGS.net == 'squeezeDet+', \
            'Selected neural net architecture not supported: {}'.format(FLAGS.net)
        if FLAGS.net == 'vgg16':
            mc = kitti_vgg16_config()
            mc.BATCH_SIZE = 1  # TODO(bichen): allow batch size > 1
            mc.LOAD_PRETRAINED_MODEL = False
            model = VGG16ConvDet(mc)
        elif FLAGS.net == 'resnet50':
            mc = kitti_res50_config()
            mc.BATCH_SIZE = 1  # TODO(bichen): allow batch size > 1
            mc.LOAD_PRETRAINED_MODEL = False
            model = ResNet50ConvDet(mc)
        elif FLAGS.net == 'squeezeDet':
            mc = kitti_squeezeDet_config()
            mc.BATCH_SIZE = 1  # TODO(bichen): allow batch size > 1
            mc.LOAD_PRETRAINED_MODEL = False
            model = SqueezeDet(mc)
        elif FLAGS.net == 'squeezeDet+':
            mc = kitti_squeezeDetPlus_config()
            mc.BATCH_SIZE = 1  # TODO(bichen): allow batch size > 1
            mc.LOAD_PRETRAINED_MODEL = False
            model = SqueezeDetPlus(mc)

        imdb = kitti(FLAGS.image_set, FLAGS.data_path, mc)

        # add summary ops and placeholders
        ap_names = []
        for cls in imdb.classes:
            ap_names.append(cls + '_easy')
            ap_names.append(cls + '_medium')
            ap_names.append(cls + '_hard')

        eval_summary_ops = []
        eval_summary_phs = {}
        for ap_name in ap_names:
            ph = tf.placeholder(tf.float32)
            eval_summary_phs['APs/' + ap_name] = ph
            eval_summary_ops.append(tf.summary.scalar('APs/' + ap_name, ph))

        ph = tf.placeholder(tf.float32)
        eval_summary_phs['APs/mAP'] = ph
        eval_summary_ops.append(tf.summary.scalar('APs/mAP', ph))

        ph = tf.placeholder(tf.float32)
        eval_summary_phs['timing/im_detect'] = ph
        eval_summary_ops.append(tf.summary.scalar('timing/im_detect', ph))

        ph = tf.placeholder(tf.float32)
        eval_summary_phs['timing/im_read'] = ph
        eval_summary_ops.append(tf.summary.scalar('timing/im_read', ph))

        ph = tf.placeholder(tf.float32)
        eval_summary_phs['timing/post_proc'] = ph
        eval_summary_ops.append(tf.summary.scalar('timing/post_proc', ph))

        ph = tf.placeholder(tf.float32)
        eval_summary_phs['num_det_per_image'] = ph
        eval_summary_ops.append(tf.summary.scalar('num_det_per_image', ph))

        saver = tf.train.Saver(model.model_params)

        summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g)

        ckpts = set()
        while True:
            if FLAGS.run_once:
                # When run_once is true, checkpoint_path should point to the exact
                # checkpoint file.
                eval_once(saver, FLAGS.checkpoint_path, summary_writer,
                          eval_summary_ops, eval_summary_phs, imdb, model)
                return
            else:
                # When run_once is false, checkpoint_path should point to the directory
                # that stores checkpoint files.
                ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_path)
                if ckpt and ckpt.model_checkpoint_path:
                    if ckpt.model_checkpoint_path in ckpts:
                        # Do not evaluate on the same checkpoint
                        print(
                            'Wait {:d}s for new checkpoints to be saved ... '.
                            format(FLAGS.eval_interval_secs))
                        time.sleep(FLAGS.eval_interval_secs)
                    else:
                        ckpts.add(ckpt.model_checkpoint_path)
                        print('Evaluating {}...'.format(
                            ckpt.model_checkpoint_path))
                        eval_once(saver, ckpt.model_checkpoint_path,
                                  summary_writer, eval_summary_ops,
                                  eval_summary_phs, imdb, model)
                else:
                    print('No checkpoint file found')
                    if not FLAGS.run_once:
                        print(
                            'Wait {:d}s for new checkpoints to be saved ... '.
                            format(FLAGS.eval_interval_secs))
                        time.sleep(FLAGS.eval_interval_secs)
def main(_):
  if not FLAGS.dataset_dir:
    raise ValueError('You must supply the dataset directory with --dataset_dir')

  tf.logging.set_verbosity(tf.logging.INFO)
  with tf.Graph().as_default():
    tf_global_step = slim.get_or_create_global_step()

    ######################
    # Select the dataset #
    ######################
    dataset = dataset_factory.get_dataset(
        FLAGS.dataset_name, FLAGS.dataset_split_name, FLAGS.dataset_dir)

    ####################
    # Select the model #
    ####################
    network_fn = nets_factory.get_network_fn(
        FLAGS.model_name,
        num_classes=(dataset.num_classes - FLAGS.labels_offset),
        is_training=False)

    ##############################################################
    # Create a dataset provider that loads data from the dataset #
    ##############################################################
    provider = slim.dataset_data_provider.DatasetDataProvider(
        dataset,
        shuffle=False,
        common_queue_capacity=2 * FLAGS.batch_size,
        common_queue_min=FLAGS.batch_size,

        )

    [image, label] = provider.get(['image', 'label'])
    label -= FLAGS.labels_offset

    #####################################
    # Select the preprocessing function #
    #####################################
    preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
    image_preprocessing_fn = preprocessing_factory.get_preprocessing(
        preprocessing_name,
        is_training=False)

    eval_image_size = FLAGS.eval_image_size or network_fn.default_image_size

    image = image_preprocessing_fn(image, eval_image_size, eval_image_size)

    images, labels = tf.train.batch(
        [image, label],
        batch_size=FLAGS.batch_size,
        num_threads=FLAGS.num_preprocessing_threads,
        capacity=5 * FLAGS.batch_size,
        )

    ####################
    # Define the model #
    ####################
    logits, _ = network_fn(images)

    if FLAGS.moving_average_decay:
      variable_averages = tf.train.ExponentialMovingAverage(
          FLAGS.moving_average_decay, tf_global_step)
      variables_to_restore = variable_averages.variables_to_restore(
          slim.get_model_variables())
      variables_to_restore[tf_global_step.op.name] = tf_global_step
    else:
      variables_to_restore = slim.get_variables_to_restore()

    predictions = tf.argmax(logits, 1)
    labels = tf.squeeze(labels)

    # Define the metrics:
    names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
        'Accuracy': slim.metrics.streaming_accuracy(predictions, labels),
        # 'Recall_5': slim.metrics.streaming_recall_at_k(
        #     logits, labels, 5),
    })

    # Print the summaries to screen.
    for name, value in names_to_values.items():
      summary_name = 'eval/%s' % name
      op = tf.summary.scalar(summary_name, value, collections=[])
      op = tf.Print(op, [value], summary_name)
      tf.add_to_collection(tf.GraphKeys.SUMMARIES, op)

    # TODO(sguada) use num_epochs=1
    if FLAGS.max_num_batches:
      num_batches = FLAGS.max_num_batches
    else:
      # This ensures that we make a single pass over all of the data.
      num_batches = math.ceil(dataset.num_samples / float(FLAGS.batch_size))

    if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
      checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
    else:
      checkpoint_path = FLAGS.checkpoint_path


    # # checkpoint_path_list = ['/home/cp/git/tensorflow/models/research/slim/logs/train/vgg16/model.ckpt-330','/home/cp/git/tensorflow/models/research/slim/logs/train/vgg16/model.ckpt-382']
    #
    # for checkpoint_path in checkpoint_path_list:


    tf.logging.info('Evaluating %s' % checkpoint_path)

    slim.evaluation.evaluate_once(
        master=FLAGS.master,
        checkpoint_path=checkpoint_path,
        logdir=FLAGS.eval_dir,
        num_evals=num_batches,
        eval_op=list(names_to_updates.values()),
        variables_to_restore=variables_to_restore)
Example #14
0
def train(word2vec_path):
    """Training TextCNN-one-label model."""

    # Load sentences, labels, and training parameters
    logger.info("✔︎ Loading data...")

    logger.info("✔︎ Training data processing...")
    train_data = feed.load_data_and_labels_one_label(
        FLAGS.training_data_file,
        FLAGS.num_classes,
        FLAGS.embedding_dim,
        word2vec_path=word2vec_path,
        data_aug_flag=False)

    logger.info("✔︎ Validation data processing...")
    val_data = feed.load_data_and_labels_one_label(FLAGS.validation_data_file,
                                                   FLAGS.num_classes,
                                                   FLAGS.embedding_dim,
                                                   word2vec_path=word2vec_path,
                                                   data_aug_flag=False)

    logger.info("Recommended padding Sequence length for GOV_MEASURE is: "
                "{}".format(FLAGS.pad_seq_len_gov))

    logger.info("Recommended padding Sequence length for Article is: "
                "{}".format(FLAGS.pad_seq_len_art))

    logger.info("✔︎ GOV MEASURE padding...")
    x_train_gov, x_train_art, y_train = feed.pad_data_one_label(
        train_data, FLAGS.pad_seq_len_gov, FLAGS.pad_seq_len_art)
    print("x_train_gov: ", len(x_train_gov))
    print("x_train_art: ", len(x_train_art))

    logger.info("✔︎ Validation data padding...")
    x_val_gov, x_val_art, y_val = feed.pad_data_one_label(
        val_data, FLAGS.pad_seq_len_gov, FLAGS.pad_seq_len_art)
    print("x_val_gov: ", len(x_val_gov))
    print("x_val_art: ", len(x_val_art))

    # Build vocabulary

    VOCAB_SIZE = feed.load_vocab_size(FLAGS.embedding_dim,
                                      word2vec_path=word2vec_path)

    # Use pretrained W2V
    pretrained_word2vec_matrix = feed.load_word2vec_matrix(
        VOCAB_SIZE, FLAGS.embedding_dim, word2vec_path=word2vec_path)

    # Build a graph and cnn object
    with tf.Graph().as_default():

        session_conf = tf.ConfigProto(
            allow_soft_placement=FLAGS.allow_soft_placement,
            log_device_placement=FLAGS.log_device_placement)

        session_conf.gpu_options.allow_growth = \
            FLAGS.gpu_options_allow_growth

        sess = tf.Session(config=session_conf)

        with sess.as_default():
            cnn = OneLabelTextCNN(
                sequence_length_gov=FLAGS.pad_seq_len_gov,
                sequence_length_art=FLAGS.pad_seq_len_art,
                vocab_size=VOCAB_SIZE,
                fc_hidden_size=FLAGS.fc_hidden_size,
                embedding_size=FLAGS.embedding_dim,
                embedding_type=FLAGS.embedding_type,
                filter_sizes=list(map(int, FLAGS.filter_sizes.split(','))),
                num_filters=FLAGS.num_filters,
                l2_reg_lambda=FLAGS.l2_reg_lambda,
                pretrained_embedding=pretrained_word2vec_matrix)

            # Define training procedure
            with tf.control_dependencies(
                    tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
                learning_rate = tf.train.exponential_decay(
                    learning_rate=FLAGS.learning_rate,
                    global_step=cnn.global_step,
                    decay_steps=FLAGS.decay_steps,
                    decay_rate=FLAGS.decay_rate,
                    staircase=True)
                optimizer = tf.train.AdamOptimizer(learning_rate)
                grads, variables = zip(*optimizer.compute_gradients(cnn.loss))
                grads, _ = tf.clip_by_global_norm(grads,
                                                  clip_norm=FLAGS.norm_ratio)
                train_op = optimizer.apply_gradients(
                    zip(grads, variables),
                    global_step=cnn.global_step,
                    name="train_op")

            # Keep track of gradient values and sparsity (optional)
            grad_summaries = []
            for g, v in zip(grads, variables):
                if g is not None:
                    grad_hist_summary = tf.summary.histogram(
                        "{0}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.summary.scalar(
                        "{0}/grad/sparsity".format(v.name),
                        tf.nn.zero_fraction(g))
                    grad_summaries.append(grad_hist_summary)
                    grad_summaries.append(sparsity_summary)
            grad_summaries_merged = tf.summary.merge(grad_summaries)

            # Output directory for models and summaries
            if FLAGS.train_or_restore == 'R':
                MODEL = input("☛ Please input the checkpoints model you want "
                              "to restore, it should be like(1490175368): ")
                # The model you want to restore

                while not (MODEL.isdigit() and len(MODEL) == 10):
                    MODEL = input("✘ The format of your input is illegal, "
                                  "please re-input: ")
                logger.info("✔︎ The format of your input is legal, "
                            "now loading to next step...")
                out_dir = os.path.abspath(
                    os.path.join(
                        "/home/zachary/hdd/OneLabelTextCNN",  # os.path.curdir
                        "runs",
                        MODEL))
                logger.info("✔︎ Writing to {0}\n".format(out_dir))
            else:
                timestamp = str(int(time.time()))
                out_dir = os.path.abspath(
                    os.path.join("/home/zachary/hdd/OneLabelTextCNN", "runs",
                                 timestamp))
                logger.info("✔︎ Writing to {0}\n".format(out_dir))

            checkpoint_dir = os.path.abspath(
                os.path.join(out_dir, "checkpoints"))
            best_checkpoint_dir = os.path.abspath(
                os.path.join(out_dir, "bestcheckpoints"))

            # Summaries for loss
            loss_summary = tf.summary.scalar("loss", cnn.loss)

            # Train summaries
            train_summary_op = tf.summary.merge(
                [loss_summary, grad_summaries_merged])
            train_summary_dir = os.path.join(out_dir, "summaries", "train")
            train_summary_writer = tf.summary.FileWriter(
                train_summary_dir, sess.graph)

            # Validation summaries
            validation_summary_op = tf.summary.merge([loss_summary])
            validation_summary_dir = os.path.join(out_dir, "summaries",
                                                  "validation")
            validation_summary_writer = tf.summary.FileWriter(
                validation_summary_dir, sess.graph)

            saver = tf.train.Saver(tf.global_variables(),
                                   max_to_keep=FLAGS.num_checkpoints)
            # best_saver = checkpoints.BestCheckpointSaver(
            #     save_dir=best_checkpoint_dir,
            #     num_to_keep=3,
            #     maximize=True)

            if FLAGS.train_or_restore == 'R':
                # Load cnn model
                logger.info("✔︎ Loading model...")
                checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)
                logger.info(checkpoint_file)

                # Load the saved meta graph and restore variables
                saver = tf.train.import_meta_graph(
                    "{0}.meta".format(checkpoint_file))
                saver.restore(sess, checkpoint_file)
            else:
                if not os.path.exists(checkpoint_dir):
                    os.makedirs(checkpoint_dir)
                sess.run(tf.global_variables_initializer())
                sess.run(tf.local_variables_initializer())

                # Embedding visualization config
                config = projector.ProjectorConfig()
                embedding_conf = config.embeddings.add()
                embedding_conf.tensor_name = "embedding"
                embedding_conf.metadata_path = FLAGS.metadata_file

                projector.visualize_embeddings(train_summary_writer, config)
                projector.visualize_embeddings(validation_summary_writer,
                                               config)

                # Save the embedding visualization
                saver.save(
                    sess, os.path.join(out_dir, "embedding", "embedding.ckpt"))

            current_step = sess.run(cnn.global_step)
            print("current_step: ", current_step)

            def train_step(x_batch_gov, x_batch_art, y_batch):
                """A single training step"""
                feed_dict = {
                    cnn.input_x_gov: x_batch_gov,
                    cnn.input_x_art: x_batch_art,
                    cnn.input_y: y_batch,
                    cnn.dropout_keep_prob: FLAGS.dropout_keep_prob,
                    cnn.is_training: True
                }

                _, step, \
                summaries, \
                loss, \
                scores, \
                input_y = sess.run(
                    [train_op,
                     cnn.global_step,
                     train_summary_op,
                     cnn.loss,
                     cnn.scores,
                     cnn.input_y],
                    feed_dict)

                count_label_one, \
                count_label_zero,\
                count_correct_one, \
                count_correct_zero = count_correct_pred(scores, input_y)
                print("'[TRAIN] num_correct_one is {} out of {}".format(
                    count_correct_one, count_label_one))
                print("'[TRAIN] num_correct_zero is {} out of {}".format(
                    count_correct_zero, count_label_zero))

                logger.info("step {0}: loss {1:g}".format(step, loss))
                train_summary_writer.add_summary(summaries, step)
                return loss

            def validation_step(_x_val_gov, _x_val_art, _y_val, writer=None):
                print("_x_val_gov: ", len(_x_val_gov))
                print("_x_val_art: ", len(_x_val_art))
                """Evaluates model on a validation set"""
                batches_validation = \
                    feed.batch_iter(
                    list(zip(_x_val_gov,
                             _x_val_art,
                             _y_val)),
                    FLAGS.batch_size,
                    num_epochs=1,
                    shuffle=False)

                _eval_counter, _eval_loss = 0, 0.0

                _eval_pre_tk = [0.0] * FLAGS.top_num
                _eval_rec_tk = [0.0] * FLAGS.top_num
                _eval_F_tk = [0.0] * FLAGS.top_num

                true_onehot_labels = []
                predicted_onehot_scores = []
                predicted_onehot_labels_ts = []
                predicted_onehot_labels_tk = [[] for _ in range(FLAGS.top_num)]

                valid_count_correct_one = 0
                valid_count_label_one = 0
                valid_count_correct_zero = 0
                valid_count_label_zero = 0

                valid_step_count = 0
                for batch_validation in batches_validation:
                    valid_step_count += 1
                    x_batch_val_gov, x_batch_val_art, y_batch_val = \
                        zip(*batch_validation)
                    feed_dict = {
                        cnn.input_x_gov: x_batch_val_gov,
                        cnn.input_x_art: x_batch_val_art,
                        cnn.input_y: y_batch_val,
                        cnn.dropout_keep_prob: 1.0,
                        cnn.is_training: False
                    }
                    step, \
                    summaries, \
                    scores, \
                    cur_loss, \
                    input_y = sess.run(
                        [cnn.global_step,
                         validation_summary_op,
                         cnn.scores,
                         cnn.loss,
                         cnn.input_y],
                        feed_dict)

                    count_label_one, \
                    count_label_zero, \
                    count_correct_one, \
                    count_correct_zero = count_correct_pred(scores,
                                                            input_y)
                    valid_count_correct_one += count_correct_one
                    valid_count_label_one += count_label_one

                    valid_count_correct_zero += count_correct_zero
                    valid_count_label_zero += count_label_zero

                    print("[VALID] num_correct_answer is {} out of {}".format(
                        count_correct_one, count_label_one))
                    print("[VALID] num_correct_answer is {} out of {}".format(
                        count_correct_zero, count_label_zero))

                    # Prepare for calculating metrics
                    for i in y_batch_val:
                        true_onehot_labels.append(i)
                    for j in scores:
                        predicted_onehot_scores.append(j)

                    # Predict by threshold
                    batch_predicted_onehot_labels_ts = \
                        feed.get_onehot_label_threshold(scores=scores,
                                                        threshold=FLAGS.
                                                        threshold)

                    for k in batch_predicted_onehot_labels_ts:
                        predicted_onehot_labels_ts.append(k)

                    # Predict by topK
                    for _top_num in range(FLAGS.top_num):
                        batch_predicted_onehot_labels_tk = feed.\
                            get_onehot_label_topk(scores=scores,
                                                  top_num=_top_num + 1)

                        for i in batch_predicted_onehot_labels_tk:
                            predicted_onehot_labels_tk[_top_num].append(i)

                    _eval_loss = _eval_loss + cur_loss
                    _eval_counter = _eval_counter + 1

                    if writer:
                        writer.add_summary(summaries, step)

                logger.info("[VALID_FINAL] Total Correct One Answer is {} out "
                            "of {}".format(valid_count_correct_one,
                                           valid_count_label_one))
                logger.info("[VALID_FINAL] Total Correct Zero Answer is {} "
                            "out of {}".format(valid_count_correct_zero,
                                               valid_count_label_zero))

                _eval_loss = float(_eval_loss / _eval_counter)

                # Calculate Precision & Recall & F1 (threshold & topK)
                _eval_pre_ts = precision_score(
                    y_true=np.array(true_onehot_labels),
                    y_pred=np.array(predicted_onehot_labels_ts),
                    average='micro')
                _eval_rec_ts = recall_score(
                    y_true=np.array(true_onehot_labels),
                    y_pred=np.array(predicted_onehot_labels_ts),
                    average='micro')
                _eval_F_ts = f1_score(
                    y_true=np.array(true_onehot_labels),
                    y_pred=np.array(predicted_onehot_labels_ts),
                    average='micro')

                for _top_num in range(FLAGS.top_num):
                    _eval_pre_tk[_top_num] = precision_score(
                        y_true=np.array(true_onehot_labels),
                        y_pred=np.array(predicted_onehot_labels_tk[_top_num]),
                        average='micro')
                    _eval_rec_tk[_top_num] = recall_score(
                        y_true=np.array(true_onehot_labels),
                        y_pred=np.array(predicted_onehot_labels_tk[_top_num]),
                        average='micro')
                    _eval_F_tk[_top_num] = f1_score(
                        y_true=np.array(true_onehot_labels),
                        y_pred=np.array(predicted_onehot_labels_tk[_top_num]),
                        average='micro')

                # Calculate the average AUC
                _eval_auc = roc_auc_score(
                    y_true=np.array(true_onehot_labels),
                    y_score=np.array(predicted_onehot_scores),
                    average='micro')
                # Calculate the average PR
                _eval_prc = average_precision_score(
                    y_true=np.array(true_onehot_labels),
                    y_score=np.array(predicted_onehot_scores),
                    average='micro')

                return _eval_loss, _eval_auc, _eval_prc, _eval_rec_ts, \
                       _eval_pre_ts, _eval_F_ts, _eval_rec_tk, _eval_pre_tk, \
                       _eval_F_tk

            # Generate batches
            batches_train = feed.batch_iter(
                list(zip(x_train_gov, x_train_art, y_train)), FLAGS.batch_size,
                FLAGS.num_epochs)

            num_batches_per_epoch = \
                int((len(x_train_gov) - 1) / FLAGS.batch_size) + 1

            # Training loop. For each batch...
            train_loss_tracker = 0
            for batch_train in batches_train:
                x_batch_train_gov, \
                x_batch_train_art, \
                y_batch_train = zip(*batch_train)

                # print(x_batch_train_gov[0][100:150])
                # print("x_batch_train_gov.shape", len(list(
                #     x_batch_train_gov)))
                #
                # print(x_batch_train_art[0][100:150])
                # print("x_batch_train_art.shape", len(list(
                #     x_batch_train_art)))

                one_batch_loss = train_step(x_batch_train_gov,
                                            x_batch_train_art, y_batch_train)
                train_loss_tracker += one_batch_loss

                current_step = tf.train.global_step(sess, cnn.global_step)

                if current_step % FLAGS.evaluate_every == 0:
                    logger.info("\nEvaluation:")
                    eval_loss, \
                    eval_auc, \
                    eval_prc, \
                    eval_rec_ts, \
                    eval_pre_ts, \
                    eval_F_ts, \
                    eval_rec_tk, \
                    eval_pre_tk, \
                    eval_F_tk = \
                        validation_step(x_val_gov,
                                        x_val_art,
                                        y_val,
                                        writer=validation_summary_writer)

                    logger.info(
                        "All Validation set: Loss {0:g} | AUC {1:g} | AUPRC {2:g}"
                        .format(eval_loss, eval_auc, eval_prc))

                    logger.info("Train Loss: {}".format(train_loss_tracker /
                                                        FLAGS.evaluate_every))
                    train_loss_tracker = 0

                    # Predict by threshold
                    logger.info(
                        "☛ Predict by threshold: Precision {0:g}, Recall {1:g}, "
                        "F {2:g}".format(eval_pre_ts, eval_rec_ts, eval_F_ts))

                    # Predict by topK
                    logger.info("☛ Predict by topK:")
                    for top_num in range(FLAGS.top_num):
                        logger.info(
                            "Top{0}: Precision {1:g}, Recall {2:g}, F {3:g}".
                            format(top_num + 1, eval_pre_tk[top_num],
                                   eval_rec_tk[top_num], eval_F_tk[top_num]))
                    # best_saver.handle(eval_prc, sess, current_step)

                if current_step % FLAGS.checkpoint_every == 0:
                    checkpoint_prefix = os.path.join(checkpoint_dir, "model")
                    path = saver.save(sess,
                                      checkpoint_prefix,
                                      global_step=current_step)
                    logger.info(
                        "✔︎ Saved model checkpoint to {0}\n".format(path))

                if current_step % num_batches_per_epoch == 0:
                    current_epoch = current_step // num_batches_per_epoch
                    logger.info(
                        "✔︎ Epoch {0} has finished!".format(current_epoch))

    logger.info("✔︎ Done.")
Example #15
0
def adjective_embeddings(data_file, embeddings_file_name, num_steps,
                         embedding_dim):

    with open(data_file) as f:
        f_list = f.read().split()
    vocabulary_size = 5000
    batch_size = 64
    embedding_size = embedding_dim
    skip_window = 3
    num_samples = 4
    learning_rate = 0.002  # How many times to reuse an input to generate a label.
    num_sampled = 64  # Sample size for negative examples.

    global data_index

    logs_path = './log/'

    data, count, dictionary, reverse_dictionary = build_dataset(
        f_list, vocabulary_size)

    # Specification of test Sample:
    sample_size = 20  # Random sample of words to evaluate similarity.
    sample_window = 100  # Only pick samples in the head of the distribution.

    sample_examples = np.random.choice(
        sample_window, sample_size,
        replace=False)  # Randomly pick a sample of size 16

    graph = tf.Graph()

    #Constructing the graph...
    with graph.as_default():
        with tf.device('/cpu:0'):
            # Placeholders to read input data.
            with tf.name_scope('Inputs'):
                train_inputs = tf.placeholder(tf.int32, shape=[batch_size])
                train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])

            # Look up embeddings for inputs.
            with tf.name_scope('Embeddings'):
                sample_dataset = tf.constant(sample_examples, dtype=tf.int32)
                embeddings = tf.Variable(
                    tf.random_uniform([vocabulary_size, embedding_size], -1.0,
                                      1.0))
                embed = tf.nn.embedding_lookup(embeddings, train_inputs)

                # Construct the variables for the NCE loss
                nce_weights = tf.Variable(
                    tf.truncated_normal([vocabulary_size, embedding_size],
                                        stddev=1.0 /
                                        math.sqrt(embedding_size)))
                nce_biases = tf.Variable(tf.zeros([vocabulary_size]))

            # Compute the average NCE loss for the batch.
            # tf.nce_loss automatically draws a new sample of the negative labels each
            # time we evaluate the loss.
            with tf.name_scope('Loss'):
                loss = tf.reduce_mean(
                    tf.nn.sampled_softmax_loss(weights=nce_weights,
                                               biases=nce_biases,
                                               labels=train_labels,
                                               inputs=embed,
                                               num_sampled=num_sampled,
                                               num_classes=vocabulary_size))

            # Construct the Gradient Descent optimizer using a learning rate of 0.01.
            with tf.name_scope('Gradient_Descent'):
                optimizer = tf.train.AdamOptimizer(learning_rate).minimize(
                    loss)

            # Normalize the embeddings to avoid overfitting.
            with tf.name_scope('Normalization'):
                norm = tf.sqrt(
                    tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
                normalized_embeddings = embeddings / norm

            sample_embeddings = tf.nn.embedding_lookup(normalized_embeddings,
                                                       sample_dataset)
            similarity = tf.matmul(sample_embeddings,
                                   normalized_embeddings,
                                   transpose_b=True)

            # Add variable initializer.
            init = tf.global_variables_initializer()

            # Create a summary to monitor cost tensor
            tf.summary.scalar("cost", loss)
            # Merge all summary variables.
            merged_summary_op = tf.summary.merge_all()

    with tf.Session(graph=graph) as session:
        # We must initialize all variables before we use them.
        session.run(init)
        summary_writer = tf.summary.FileWriter(logs_path,
                                               graph=tf.get_default_graph())

        print('Initializing the model')

        average_loss = 0
        for step in range(num_steps):
            batch_inputs, batch_labels = generate_batch(
                batch_size, num_samples, skip_window, data)
            feed_dict = {
                train_inputs: batch_inputs,
                train_labels: batch_labels
            }

            # We perform one update step by evaluating the optimizer op using session.run()
            _, loss_val, summary = session.run(
                [optimizer, loss, merged_summary_op], feed_dict=feed_dict)

            summary_writer.add_summary(summary, step)
            average_loss += loss_val

            if step % 5000 == 0:
                if step > 0:
                    average_loss /= 5000
                    # The average loss is an estimate of the loss over the last 5000 batches.
                    print('Average loss at step ', step, ': ', average_loss)
                    average_loss = 0

            # Evaluate similarity after every 10000 iterations.
            if step % 10000 == 0:
                nlp = spacy.load('en')
                sim = similarity.eval()  #
                for i in range(sample_size):
                    sample_word = reverse_dictionary[sample_examples[i]]

                    doc = nlp(sample_word)
                    for token in doc:
                        if token.pos_ == "ADJ":
                            top_k = 10  # Look for top-10 neighbours for words in sample set.

                            nearest = (-sim[i, :]).argsort()[1:top_k + 1]
                            log_str = 'Nearest to %s:' % sample_word
                            for k in range(top_k):
                                close_word = reverse_dictionary[nearest[k]]
                                log_str = '%s %s,' % (log_str, close_word)
                            print(log_str)
                print()

        final_embeddings = normalized_embeddings.eval()

        with open(embeddings_file_name, 'w', encoding="utf-8") as file:
            file.write(str(len(dictionary)))
            file.write(' ')
            file.write(str(embedding_dim))
            file.write('\n')
            i = 0
            for key in dictionary:
                file.write(key)
                file.write(' ')
                for j in final_embeddings[i]:
                    file.write('{:.6f}'.format(j))
                    #                    file.write(str(round(int(j),6)))
                    file.write(' ')
                file.write('\n')
                i += 1
Example #16
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    # Get dataset-dependent information.
    dataset = segmentation_dataset.get_dataset(FLAGS.dataset,
                                               FLAGS.eval_split,
                                               dataset_dir=FLAGS.dataset_dir)

    tf.gfile.MakeDirs(FLAGS.eval_logdir)
    tf.logging.info('Evaluating on %s set', FLAGS.eval_split)

    with tf.Graph().as_default():
        samples = input_generator.get(dataset,
                                      FLAGS.eval_crop_size,
                                      FLAGS.eval_batch_size,
                                      min_resize_value=FLAGS.min_resize_value,
                                      max_resize_value=FLAGS.max_resize_value,
                                      resize_factor=FLAGS.resize_factor,
                                      dataset_split=FLAGS.eval_split,
                                      is_training=False,
                                      model_variant=FLAGS.model_variant)

        model_options = common.ModelOptions(
            outputs_to_num_classes={common.OUTPUT_TYPE: dataset.num_classes},
            crop_size=FLAGS.eval_crop_size,
            atrous_rates=FLAGS.atrous_rates,
            output_stride=FLAGS.output_stride)

        if tuple(FLAGS.eval_scales) == (1.0, ):
            tf.logging.info('Performing single-scale test.')
            predictions = model.predict_labels(
                samples[common.IMAGE],
                model_options,
                image_pyramid=FLAGS.image_pyramid)
        else:
            tf.logging.info('Performing multi-scale test.')
            predictions = model.predict_labels_multi_scale(
                samples[common.IMAGE],
                model_options=model_options,
                eval_scales=FLAGS.eval_scales,
                add_flipped_images=FLAGS.add_flipped_images)
        predictions = predictions[common.OUTPUT_TYPE]
        predictions = tf.reshape(predictions, shape=[-1])
        labels = tf.reshape(samples[common.LABEL], shape=[-1])
        weights = tf.to_float(tf.not_equal(labels, dataset.ignore_label))

        # Set ignore_label regions to label 0, because metrics.mean_iou requires
        # range of labels = [0, dataset.num_classes). Note the ignore_label regions
        # are not evaluated since the corresponding regions contain weights = 0.
        labels = tf.where(tf.equal(labels, dataset.ignore_label),
                          tf.zeros_like(labels), labels)

        predictions_tag = 'miou'
        for eval_scale in FLAGS.eval_scales:
            predictions_tag += '_' + str(eval_scale)
        if FLAGS.add_flipped_images:
            predictions_tag += '_flipped'

        # Define the evaluation metric.
        metric_map = {}
        metric_map[predictions_tag] = tf.metrics.mean_iou(predictions,
                                                          labels,
                                                          dataset.num_classes,
                                                          weights=weights)

        metrics_to_values, metrics_to_updates = (
            tf.contrib.metrics.aggregate_metric_map(metric_map))

        for metric_name, metric_value in six.iteritems(metrics_to_values):
            slim.summaries.add_scalar_summary(metric_value,
                                              metric_name,
                                              print_summary=True)

        num_batches = int(
            math.ceil(dataset.num_samples / float(FLAGS.eval_batch_size)))

        tf.logging.info('Eval num images %d', dataset.num_samples)
        tf.logging.info('Eval batch size %d and num batch %d',
                        FLAGS.eval_batch_size, num_batches)

        num_eval_iters = None
        if FLAGS.max_number_of_evaluations > 0:
            num_eval_iters = FLAGS.max_number_of_evaluations
        slim.evaluation.evaluation_loop(
            master=FLAGS.master,
            checkpoint_dir=FLAGS.checkpoint_dir,
            logdir=FLAGS.eval_logdir,
            num_evals=num_batches,
            eval_op=list(metrics_to_updates.values()),
            max_number_of_evaluations=num_eval_iters,
            eval_interval_secs=FLAGS.eval_interval_secs)
Example #17
0
def optimize(content_targets,
             style_target,
             content_weight,
             style_weight,
             tv_weight,
             vgg_path,
             epochs=2,
             print_iterations=1000,
             batch_size=4,
             save_path='saver/fns.ckpt',
             slow=False,
             learning_rate=1e-3,
             debug=True):
    if slow:
        batch_size = 1
    mod = len(content_targets) % batch_size
    if mod > 0:
        print("Train set has been trimmed slightly..")
        content_targets = content_targets[:-mod]

    style_features = {}

    batch_shape = (batch_size, 256, 256, 3)
    style_shape = (1, ) + style_target.shape
    print(style_shape)

    # precompute style features
    with tf.Graph().as_default(), tf.device('/cpu:0'), tf.Session() as sess:
        style_image = tf.placeholder(tf.float32,
                                     shape=style_shape,
                                     name='style_image')
        style_image_pre = vgg.preprocess(style_image)
        net = vgg.net(vgg_path, style_image_pre)
        style_pre = np.array([style_target])
        for layer in STYLE_LAYERS:
            features = net[layer].eval(feed_dict={style_image: style_pre})
            features = np.reshape(features, (-1, features.shape[3]))
            gram = np.matmul(features.T, features) / features.size
            style_features[layer] = gram

    with tf.Graph().as_default(), tf.Session() as sess:
        X_content = tf.placeholder(tf.float32,
                                   shape=batch_shape,
                                   name="X_content")
        X_pre = vgg.preprocess(X_content)

        # precompute content features
        content_features = {}
        content_net = vgg.net(vgg_path, X_pre)
        content_features[CONTENT_LAYER] = content_net[CONTENT_LAYER]

        if slow:
            preds = tf.Variable(
                tf.random_normal(X_content.get_shape()) * 0.256)
            preds_pre = preds
        else:
            preds = transform.net(X_content / 255.0)
            preds_pre = vgg.preprocess(preds)

        net = vgg.net(vgg_path, preds_pre)

        content_size = _tensor_size(
            content_features[CONTENT_LAYER]) * batch_size
        assert _tensor_size(content_features[CONTENT_LAYER]) == _tensor_size(
            net[CONTENT_LAYER])
        content_loss = content_weight * (
            2 * tf.nn.l2_loss(net[CONTENT_LAYER] -
                              content_features[CONTENT_LAYER]) / content_size)

        style_losses = []
        for style_layer in STYLE_LAYERS:
            layer = net[style_layer]
            bs, height, width, filters = map(lambda i: i.value,
                                             layer.get_shape())
            size = height * width * filters
            feats = tf.reshape(layer, (bs, height * width, filters))
            feats_T = tf.transpose(feats, perm=[0, 2, 1])
            grams = tf.matmul(feats_T, feats) / size
            style_gram = style_features[style_layer]
            style_losses.append(2 * tf.nn.l2_loss(grams - style_gram) /
                                style_gram.size)

        style_loss = style_weight * functools.reduce(tf.add,
                                                     style_losses) / batch_size

        # total variation denoising
        tv_y_size = _tensor_size(preds[:, 1:, :, :])
        tv_x_size = _tensor_size(preds[:, :, 1:, :])
        y_tv = tf.nn.l2_loss(preds[:, 1:, :, :] -
                             preds[:, :batch_shape[1] - 1, :, :])
        x_tv = tf.nn.l2_loss(preds[:, :, 1:, :] -
                             preds[:, :, :batch_shape[2] - 1, :])
        tv_loss = tv_weight * 2 * (x_tv / tv_x_size +
                                   y_tv / tv_y_size) / batch_size

        loss = content_loss + style_loss + tv_loss

        # overall loss
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)
        sess.run(tf.global_variables_initializer())
        import random
        uid = random.randint(1, 100)
        print("UID: %s" % uid)
        for epoch in range(epochs):
            num_examples = len(content_targets)
            iterations = 0
            while iterations * batch_size < num_examples:
                start_time = time.time()
                curr = iterations * batch_size
                step = curr + batch_size
                X_batch = np.zeros(batch_shape, dtype=np.float32)
                for j, img_p in enumerate(content_targets[curr:step]):
                    X_batch[j] = get_img(img_p,
                                         (256, 256, 3)).astype(np.float32)

                iterations += 1
                assert X_batch.shape[0] == batch_size

                feed_dict = {X_content: X_batch}
                print("start training")
                train_step.run(feed_dict=feed_dict)
                end_time = time.time()
                delta_time = end_time - start_time
                if debug:
                    print("UID: %s, batch time: %s" % (uid, delta_time))
                is_print_iter = int(iterations) % print_iterations == 0
                if slow:
                    is_print_iter = epoch % print_iterations == 0
                is_last = epoch == epochs - 1 and iterations * batch_size >= num_examples
                should_print = is_print_iter or is_last
                if should_print:
                    to_get = [style_loss, content_loss, tv_loss, loss, preds]
                    test_feed_dict = {X_content: X_batch}

                    tup = sess.run(to_get, feed_dict=test_feed_dict)
                    _style_loss, _content_loss, _tv_loss, _loss, _preds = tup
                    losses = (_style_loss, _content_loss, _tv_loss, _loss)
                    if slow:
                        _preds = vgg.unprocess(_preds)
                    else:
                        saver = tf.train.Saver()
                        res = saver.save(sess, save_path)
                    yield (_preds, losses, iterations, epoch)
Example #18
0
    def __init__(self,
                 dim_x1,
                 dim_x2,
                 dim_z,
                 dim_y,
                 num_examples,
                 num_lab,
                 num_batches,
                 p_x1='gaussian',
                 p_x2='gaussian',
                 q_z='gaussian_mixture_marg',
                 p_z='gaussian_marg',
                 hidden_layers_px1=[250],
                 hidden_layers_px2=[250],
                 hidden_layers_qz=[250],
                 hidden_layers_qy=[250],
                 nonlin_px1=tf.nn.softplus,
                 nonlin_px2=tf.nn.softplus,
                 nonlin_qz=tf.nn.softplus,
                 nonlin_qy=tf.nn.softplus,
                 beta=0.1,
                 l2_loss=0.0,
                 zeta_reg=1e6):

        self.dim_x1, self.dim_x2, self.dim_z, self.dim_y = dim_x1, dim_x2, dim_z, dim_y

        self.zeta_reg = zeta_reg
        self.distributions = {
            'p_x1': p_x1,
            'p_x2': p_x2,
            'q_z': q_z,
            'p_z': p_z,
            'p_y': 'uniform'
        }

        self.num_examples = num_examples
        self.num_batches = num_batches
        self.num_lab = num_lab
        self.num_ulab = self.num_examples - num_lab

        #        assert self.num_lab % self.num_batches == 0, '#Labelled % #Batches != 0'
        #        assert self.num_ulab % self.num_batches == 0, '#Unlabelled % #Batches != 0'
        #        assert self.num_examples % self.num_batches == 0, '#Examples % #Batches != 0'

        self.num_lab_batch = self.num_lab // self.num_batches
        self.num_ulab_batch = self.num_ulab // self.num_batches
        self.batch_size = self.num_examples // self.num_batches

        #        self.alpha = beta * ( float(self.batch_size) / self.num_lab_batch )
        self.alpha = beta * float(self.batch_size)
        ''' Create Graph '''

        self.G = tf.Graph()

        with self.G.as_default():

            def weight_variable(shape):
                initial = tf.truncated_normal(shape, stddev=0.01)
                return tf.Variable(initial)
#            zeta = weight_variable([2])

            init_value = (np.sqrt(1.0 / 2)).astype('float32')
            zeta = tf.Variable(tf.constant(init_value, shape=[2]))
            #            zeta = tf.constant(init_value, shape=[2])
            zeta = zeta * zeta
            self.zeta1 = zeta[0]
            self.zeta2 = zeta[1]

            self.x1_labelled_mu = tf.placeholder(tf.float32,
                                                 [None, self.dim_x1])
            self.x1_labelled_lsgms = tf.placeholder(tf.float32,
                                                    [None, self.dim_x1])
            self.x1_unlabelled_mu = tf.placeholder(tf.float32,
                                                   [None, self.dim_x1])
            self.x1_unlabelled_lsgms = tf.placeholder(tf.float32,
                                                      [None, self.dim_x1])
            self.x2_labelled_mu = tf.placeholder(tf.float32,
                                                 [None, self.dim_x2])
            self.x2_labelled_lsgms = tf.placeholder(tf.float32,
                                                    [None, self.dim_x2])
            self.x2_unlabelled_mu = tf.placeholder(tf.float32,
                                                   [None, self.dim_x2])
            self.x2_unlabelled_lsgms = tf.placeholder(tf.float32,
                                                      [None, self.dim_x2])
            self.y_lab = tf.placeholder(tf.float32, [None, self.dim_y])

            self.classifier = FullyConnected(dim_output=self.dim_y,
                                             hidden_layers=hidden_layers_qy,
                                             nonlinearity=nonlin_qy,
                                             l2loss=l2_loss)

            self.encoder_1 = FullyConnected(dim_output=2 * self.dim_z,
                                            hidden_layers=hidden_layers_qz,
                                            nonlinearity=nonlin_qz,
                                            l2loss=l2_loss)

            self.encoder_2 = FullyConnected(dim_output=2 * self.dim_z,
                                            hidden_layers=hidden_layers_qz,
                                            nonlinearity=nonlin_qz,
                                            l2loss=l2_loss)

            self.decoder_1 = FullyConnected(dim_output=2 * self.dim_x1,
                                            hidden_layers=hidden_layers_px1,
                                            nonlinearity=nonlin_px1,
                                            l2loss=l2_loss)

            self.decoder_2 = FullyConnected(dim_output=2 * self.dim_x2,
                                            hidden_layers=hidden_layers_px2,
                                            nonlinearity=nonlin_px2,
                                            l2loss=l2_loss)

            self._objective()
            self.saver = tf.train.Saver(write_version=tf.train.SaverDef.V1)
            self.session = tf.Session()
Example #19
0
#         After that, load the testing set.                                                                  #
# ---------------------------------------------------------------------------------------------------------- #
X_train, y_train, classes_train = load_multiclass_dataset(TRAIN_FOLDER, IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS)
X_train = X_train/255.#.reshape(-1, IMAGE_HEIGHT*IMAGE_WIDTH*NUM_CHANNELS)/255.
X_train, y_train = shuffle(X_train, y_train, seed=42)
X_train, y_train, X_val, y_val = split(X_train, y_train, SPLIT_RATE)

print(X_train.shape, y_train.shape, X_val.shape, y_val.shape)

# ---------------------------------------------------------------------------------------------------------- #
# Description:                                                                                               #
#         Create a training graph that receives a batch of images and their respective labels and run a      #
#         training iteration or an inference job. Train the last FC layer using fine_tuning_op or the entire #
#         network using full_backprop_op. A weight decay of 1e-4 is used for full_backprop_op only.          #
# ---------------------------------------------------------------------------------------------------------- #
graph = tf.Graph()
with graph.as_default():
	X = tf.placeholder(tf.float32, shape = (None, IMAGE_HEIGHT, IMAGE_WIDTH, NUM_CHANNELS))
	y = tf.placeholder(tf.int64, shape = (None,))
	y_one_hot = tf.one_hot(y, len(classes_train))
	learning_rate = tf.placeholder(tf.float32)
	is_training = tf.placeholder(tf.bool)
	print(X.shape)
	out = tf.layers.conv2d(X, 64, (3, 3), (2, 2), padding='valid', activation=tf.nn.relu)
	print(out.shape)
	out = tf.layers.max_pooling2d(out, (2, 2), (2, 2), padding='valid')
	print(out.shape)
	out = tf.layers.conv2d(out, 128, (3, 3), (2, 2), padding='valid', activation=tf.nn.relu)
	print(out.shape)
	out = tf.layers.max_pooling2d(out, (2, 2), (2, 2), padding='valid')
	print(out.shape)
Example #20
0
def train():
    """Train CIFAR-10 for a number of steps."""
    with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)

        # Get images and labels for CIFAR-10.
        images, labels = cifar10.distorted_inputs()

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = cifar10.inference(images)

        # Calculate loss.
        loss = cifar10.loss(logits, labels)

        # Build a Graph that trains the model with one batch of examples and
        # updates the model parameters.
        train_op = cifar10.train(loss, global_step)

        # Create a saver.
        saver = tf.train.Saver(tf.all_variables())

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

        # Build an initialization operation to run below.
        init = tf.global_variables_initializer()

        # Start running operations on the Graph.
        sess = tf.Session(config=tf.ConfigProto(
            log_device_placement=FLAGS.log_device_placement))
        sess.run(init)

        # Start the queue runners.
        tf.train.start_queue_runners(sess=sess)

        summary_writer = tf.summary.FileWriter(FLAGS.train_dir, sess.graph)

        for step in xrange(FLAGS.max_steps):
            start_time = time.time()
            _, loss_value = sess.run([train_op, loss])
            duration = time.time() - start_time

            assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

            if step % 10 == 0:
                num_examples_per_step = FLAGS.batch_size
                examples_per_sec = num_examples_per_step / duration
                sec_per_batch = float(duration)

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

            if step % 100 == 0:
                summary_str = sess.run(summary_op)
                summary_writer.add_summary(summary_str, step)

            # Save the model checkpoint periodically.
            if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step=step)
def main(args):
    sleep(random.random())
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # 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, output_dir, ' '.join(sys.argv))
    dataset = facenet.get_dataset(args.input_dir)

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    # Add a random key to the filename to allow alignment using multiple processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)

    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if args.random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if args.random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir, filename + '.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = imageio.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim < 2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:, :, 0:3]

                        bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces > 0:
                            det = bounding_boxes[:, 0:4]
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces > 1:
                                bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1])
                                img_center = img_size / 2
                                offsets = np.vstack([(det[:, 0] + det[:, 2]) / 2 - img_center[1],
                                                     (det[:, 1] + det[:, 3]) / 2 - img_center[0]])
                                offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
                                index = np.argmax(
                                    bounding_box_size - offset_dist_squared * 2.0)  # some extra weight on the centering
                                det = det[index, :]
                            det = np.squeeze(det)
                            bb = np.zeros(4, dtype=np.int32)
                            # print(det[0], det[1], det[2], det[3])
                            bb[0] = np.maximum(det[0] - args.margin / 2, 0)
                            bb[1] = np.maximum(det[1] - args.margin / 2, 0)
                            bb[2] = np.minimum(det[2] + args.margin / 2, img_size[1])
                            bb[3] = np.minimum(det[3] + args.margin / 2, img_size[0])
                            # print(bb)
                            cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                            # scaled = misc.imresize(cropped, (args.image_size, args.image_size), interp='bilinear')
                            scaled = np.array(Image.fromarray(cropped).resize((args.image_size, args.image_size)))
                            '''
                            img = scipy.misc.imresize(myImage, size=(num_px, num_px))
                            img = np.array(Image.fromarray(myImage).resize((num_px, num_px)))'''
                            nrof_successfully_aligned += 1
                            imageio.imsave(output_filename, scaled)
                            text_file.write('%s %d %d %d %d\n' % (output_filename, bb[0], bb[1], bb[2], bb[3]))
                        else:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))

    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
Example #22
0
def start_training(datas):
    if not tf.gfile.Exists(train_log_dir):
        tf.gfile.MakeDirs(train_log_dir)

    with tf.Graph().as_default():
        # Set up the data loading:
        feature = tf.placeholder(dtype=tf.float32, shape=[None, 10])
        power = tf.placeholder(dtype=tf.float32, shape=[None, 1])

        # Define the model:
        predictions = inference(feature)

        # Specify the loss function:
        absloss = tf.reduce_mean(tf.abs(power-predictions))
        sqloss = tf.reduce_mean(tf.square(power - predictions))
        slim.losses.add_loss(absloss)

        total_loss = slim.losses.get_total_loss()  # tf.losses.get_total_loss()  #

        # Specify the optimization scheme:
        global_step = tf.Variable(0, trainable=False)
        learning_rate = tf.train.exponential_decay(0.07, global_step, 100, 0.999)
        ema = tf.train.ExponentialMovingAverage(0.99, global_step)
        average_op = ema.apply(tf.trainable_variables())
        train_step = tf.train.AdamOptimizer(learning_rate=learning_rate) \
            .minimize(total_loss, global_step=global_step)
        train_op = tf.group(train_step, average_op)

        # initialize var in graph`
        sess = tf.Session()
        init_op = tf.group(tf.global_variables_initializer(),
                           tf.local_variables_initializer())  # the local var is for update_op
        sess.run(init_op)

        # train and test data
        train_indices = np.random.choice(datas.shape[0], round(datas.shape[0] * 0.9), replace=False)
        test_indices = np.array(list(set(range(datas.shape[0])) - set(train_indices)))
        data = datas[train_indices, :]
        datat = datas[test_indices, :]

        # Main Loop
        restart = 0
        num = data.shape[0]
        shuffled_data = 0.0
        start = 0
        end = BATCH

        for step in range(1, STEPS+1):
            if step == 1 or restart:
                # print('Step:%d: Start Again!!!' % sess.run(global_step))
                restart = 0
                start = 0
                end = BATCH
                permutation = np.random.permutation(num)
                shuffled_data = data[permutation, :]
                # print(shuffled_data[start:end, 0].reshape((end-start, 1)).shape)
            _, loss_, mean_absolute_loss_ = sess.run([train_op, sqloss, absloss], feed_dict={
                feature: shuffled_data[start:end, 1:11],
                power: shuffled_data[start:end, 0].reshape((end-start, 1))
            })
            start += BATCH
            end += BATCH
            if end >= num:
                restart = 1
                end = num
            if step % 1000 == 0:
                # Apply the Moving Average Variables
                saver = tf.train.Saver()
                saver.save(sess, train_log_dir + '\model.ckpt')
                saver = tf.train.Saver(ema.variables_to_restore())
                saver.restore(sess, train_log_dir + '\model.ckpt')
                # Test the model
                loss_t, mean_absolute_loss_t = sess.run([sqloss, absloss], feed_dict={
                    feature: datat[:, 1:11],
                    power: datat[:, 0].reshape((datat.shape[0], 1))
                })
                print('STEP:%d'
                      '\nTrain:   SQLOSS:%.6f, Mean_absolute_loss:%.6f'
                      '\nTest:   SQLOSS:%.6f, Mean_absolute_loss:%.6f'
                      % (step, loss_, mean_absolute_loss_, loss_t, mean_absolute_loss_t))
        sess.close()
Example #23
0
import tensorflow as tf
from preprocessing import process


batch = None
mini_batch = 9
learning_rate = 0.00000001
beta1 = 0.09
beta2 = 0.099
linear = tf.Graph()

with linear.as_default():

	train_data = tf.placeholder(tf.float32, [batch, 30])
	label_data = tf.placeholder(tf.float32, [batch])
	batch_size = tf.placeholder(tf.float32, [])
	#keep_prob = tf.placeholder(tf.float32, [])

	layer_1 = tf.contrib.layers.fully_connected(train_data, 2, weights_initializer=tf.zeros_initializer())
	#layer_2 = tf.nn.dropout(tf.contrib.layers.fully_connected(layer_1, 20), keep_prob=keep_prob)
	#layer_3 = tf.contrib.layers.fully_connected(layer_2, 2)
	#layer_4 = tf.contrib.layers.fully_connected(layer_3, 2)
	final = tf.nn.softmax(layer_1)

	probability = tf.reduce_max(final, -1)

	prediction = tf.argmax(final, -1)

	train_accuracy = tf.scalar_mul(batch_size, tf.to_float(tf.count_nonzero(tf.cast(tf.equal(tf.to_int64(label_data), prediction), dtype=tf.int64))))

Example #24
0
def train():
    with tf.Graph().as_default():
        with tf.device('/gpu:' + str(GPU_INDEX)):
            pointclouds_pl, labels_pl = MODEL.placeholder_inputs(
                BATCH_SIZE, NUM_POINT)
            is_training_pl = tf.placeholder(tf.bool, shape=())

            # Note the global_step=batch parameter to minimize.
            # That tells the optimizer to helpfully increment the 'batch' parameter
            # for you every time it trains.
            batch = tf.get_variable('batch', [],
                                    initializer=tf.constant_initializer(0),
                                    trainable=False)
            bn_decay = get_bn_decay(batch)
            tf.summary.scalar('bn_decay', bn_decay)

            # Get model and loss
            pred, end_points = MODEL.get_model(pointclouds_pl,
                                               is_training_pl,
                                               bn_decay=bn_decay,
                                               num_class=NUM_CLASSES)
            MODEL.get_loss(pred, labels_pl, end_points)
            losses = tf.get_collection('losses')
            total_loss = tf.add_n(losses, name='total_loss')
            tf.summary.scalar('total_loss', total_loss)
            for l in losses + [total_loss]:
                tf.summary.scalar(l.op.name, l)

            correct = tf.equal(tf.argmax(pred, 1), tf.to_int64(labels_pl))
            accuracy = tf.reduce_sum(tf.cast(correct,
                                             tf.float32)) / float(BATCH_SIZE)
            tf.summary.scalar('accuracy', accuracy)

            print("--- Get training operator")
            # Get training operator
            learning_rate = get_learning_rate(batch)
            tf.summary.scalar('learning_rate', learning_rate)
            if OPTIMIZER == 'momentum':
                optimizer = tf.train.MomentumOptimizer(learning_rate,
                                                       momentum=MOMENTUM)
            elif OPTIMIZER == 'adam':
                optimizer = tf.train.AdamOptimizer(learning_rate)
            train_op = optimizer.minimize(total_loss, global_step=batch)

            # Add ops to save and restore all the variables.
            saver = tf.train.Saver()

        # Create a session
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        config.allow_soft_placement = True
        config.log_device_placement = False
        sess = tf.Session(config=config)

        # Add summary writers
        merged = tf.summary.merge_all()
        train_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'train'),
                                             sess.graph)
        test_writer = tf.summary.FileWriter(os.path.join(LOG_DIR, 'test'),
                                            sess.graph)

        # Init variables
        init = tf.global_variables_initializer()
        sess.run(init)
        # saver.restore(sess, os.path.join(LOG_DIR,'model.ckpt'))
        # log_string("Model restored.")

        ops = {
            'pointclouds_pl': pointclouds_pl,
            'labels_pl': labels_pl,
            'is_training_pl': is_training_pl,
            'pred': pred,
            'loss': total_loss,
            'train_op': train_op,
            'merged': merged,
            'step': batch,
            'end_points': end_points
        }

        best_acc = -1
        for epoch in range(MAX_EPOCH):
            log_string('**** EPOCH %03d ****' % (epoch))
            sys.stdout.flush()

            train_one_epoch(sess, ops, train_writer)
            eval_one_epoch(sess, ops, test_writer)

            # Save the variables to disk.
            # if epoch % 10 == 0:
            save_path = saver.save(sess, os.path.join(LOG_DIR, "model.ckpt"))
            log_string("Model saved in file: %s" % save_path)
    def test_model(self, path, model_name, type_to_restore="SavedModel", show_incorrect=True):
        """

        :param path:
        :param model_name:
        :param type_to_restore: String, "SavedModel" or "SavedSession"
        :param show_incorrect:  show incorrectly guessed images
        :return:
        """

        print("\n{}".format("#" * 50))

        # self.test_data = self.create_test_data("dataset/test", "test_data", typ="np")

        test_imgs, test_lbls = self.test_data[0], self.test_data[1]
        test_batch = 5

        length = len(test_lbls)

        n_correct = 0
        total_n = length

        with tf.Session(graph=tf.Graph()) as sess:

            graph = tf.get_default_graph()

            if type_to_restore == "SavedModel":
                tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.TRAINING], path + model_name)
            else:
                saver = tf.train.import_meta_graph(path + model_name)
                saver.restore(sess, tf.train.latest_checkpoint(path))

            x_img = graph.get_tensor_by_name("x_img:0")
            y_lbl = graph.get_tensor_by_name("y_lbl:0")
            predictor = graph.get_tensor_by_name("accuracy/prediction:0")
            keep_prob = graph.get_tensor_by_name("dropout/keep_prob:0")

            i = 0
            lngth = len(test_lbls)

            while i < lngth:
                # ensure that all images in test set are tested, even if final set has size less than `test_batch`
                if (lngth - test_batch) >= i:
                    imgs = test_imgs[i: (i + test_batch)]
                    lbls = test_lbls[i: (i + test_batch)]
                else:
                    imgs = test_imgs[i:lngth]
                    lbls = test_lbls[i:lngth]

                print("{}\nTest batch {}".format("-" * 15, i / test_batch))

                preds = sess.run(predictor, feed_dict={x_img: imgs, keep_prob: 1.0})

                truths = np.asarray([t[1] for t in lbls])               # TODO:  These indices should match

                print("predictions: \t", preds)
                print("truths: \t\t", truths)

                correct = np.asarray([1 if p == t else 0 for p, t in zip(preds, truths)])
                n_correct += sum(correct)

                acc = float(sum(correct)) / len(correct)
                print("accuracy: {:.3f}".format(acc))

                acc_thresh = 0.8
                if show_incorrect and acc < acc_thresh:

                    print("Accuracy less than {}%, displaying incorrect guesses...".format(acc_thresh))

                    for idx in range(len(correct)):
                        if correct[idx]:
                            continue

                        incorrect_img = imgs[idx]
                        guess = preds[idx]

                        # im =
                        plt.imshow(np.squeeze(incorrect_img, axis=2), cmap='gray')

                        plt.title(
                            (TadpoleConvNet.CLASS_ONE, TadpoleConvNet.CLASS_TWO)[guess] + " (false)"
                        )
                        plt.show()

                i += test_batch     # increment i with test_batch step size

            accuracy = graph.get_tensor_by_name("accuracy/accuracy:0")
            print("test final accuracy: ", sess.run(accuracy, feed_dict={x_img: test_imgs, y_lbl: test_lbls, keep_prob: 1.0}))

            print("-" * 30)
            print('Final Test Accuracy: {:.6f}'.format(
                float(n_correct) / total_n)
            )
            print("-" * 30)

        return float(n_correct) / total_n
Example #26
0
def main(args):
    with tf.Graph().as_default():

        with tf.Session() as sess:

            # Read the file containing the pairs used for testing
            # 1. 读入之前的pairs.txt文件
            # 读入后如[['Abel_Pacheco','1','4']]
            pairs = lfw.read_pairs(os.path.expanduser(args.lfw_pairs))  # 剪裁好了的图片

            # Get the paths for the corresponding images
            # 获取文件路径和是否匹配关系对
            paths, actual_issame = lfw.get_paths(os.path.expanduser(args.lfw_dir), pairs, args.lfw_file_ext)
            print("paths shape =", len(paths))  # 12000
            print("paths=", paths[0:200])
            print('actual_issame shape=', len(actual_issame))  # 6000
            print('actual_issame', actual_issame[0:200])

            # Load the model
            facenet.load_model(args.model)

            # Get input and output tensors
            images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
            embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
            phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")

            # image_size = images_placeholder.get_shape()[1]  # For some reason this doesn't work for frozen graphs
            image_size = args.image_size
            embedding_size = embeddings.get_shape()[1]  # 128

            # Run forward pass to calculate embeddings
            print('Runnning forward pass on LFW images')
            batch_size = args.lfw_batch_size
            nrof_images = len(paths)  # 12000
            nrof_batches = int(math.ceil(1.0 * nrof_images / batch_size))
            emb_array = np.zeros((nrof_images, embedding_size))  # 12000* 128
            for i in range(nrof_batches):
                start_index = i * batch_size
                end_index = min((i + 1) * batch_size, nrof_images)
                paths_batch = paths[start_index:end_index]
                images = facenet.load_data(paths_batch, False, False, image_size)
                feed_dict = {images_placeholder: images, phase_train_placeholder: False}
                emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict)

            # 输出调试信息
            embeddings1 = emb_array[0::2]  # 6000张图片 是每一个Paris中的第一张
            embeddings2 = emb_array[1::2]  # 6000张图片 是每一个Paris中的第2张
            diff = np.subtract(embeddings1, embeddings2)  # 每一个Paris中的两个向量相减
            dist = np.sum(np.square(diff), 1)  # 向量二范数的平方,两个向量之间距离的平方。 每一个Pari之间的欧几里得距离的平方
            # #也可以说是两张图片之间特征向量的相似度
            print('------------------------------------------------------------')
            print('dist.len=', len(dist))

            # 把特征向量保存到文件中去
            f = open(r'E:\MyPythonProjects\HWDB1\train1\pairs/embeddingsOfCluster.txt', 'w')
            for embedding in emb_array:
                for data in embedding:
                    f.write(str(data) + ' ')
                f.write('\n')
            f.close()

            # 把误差保存到文件中去
            f = open(r'E:\MyPythonProjects\HWDB1\train1\pairs/distForCluster.txt', 'w')
            for d in dist:
                f.write(str(d) + '\n')
            f.close()

            # 对数据做聚类 k-means 10个类别
            from sklearn.cluster import KMeans
            kmeans=KMeans(n_clusters=10,random_state=0).fit(emb_array)
            #cluster_centers_   cluster_centers_kmeans
            f=open(r'E:\MyPythonProjects\HWDB1\train1\pairs/cluster_centers_kmeans.txt','w')
            for centers in kmeans.cluster_centers_:
                for i in centers:
                    f.write(str(i)+' ')
                f.write('\n')
            f.close()

            f=open(r'E:\MyPythonProjects\HWDB1\train1\pairs/labelsForKmeansCluster.txt','w')
            for label in kmeans.labels_:
                f.write(str(label)+'\n') # 从数据中可以看出来,是有一定的聚类的作用。不过效果不是特别好
            f.close()

            print("Done")

            # # 绘制三维图像
            #
            # import matplotlib.pyplot as plt
            # from mpl_toolkits.mplot3d import Axes3D
            # data =emb_array[:,0:3]
            #
            # x, y, z = data[:, 0], data[:, 1], data[:, 2]
            #
            # fig = plt.figure()
            # ax = fig.add_subplot(111, projection='3d')  # 创建一个三维的绘图工程
            # #  将数据点分成三部分画,在颜色上有区分度
            # # b: blue
            # # g: green
            # # r: red
            # # c: cyan
            # # m: magenta
            # # y: yellow
            # # k: black
            # # w: white
            #
            # ax.scatter(x[0:200], y[0:200], z[0:200], c='b')  # 绘制数据点
            # ax.scatter(x[200:400], y[200:400], z[200:400], c='r')
            # ax.scatter(x[400:600], y[400:600], z[400:600], c='g')
            # ax.scatter(x[600:800], y[600:800], z[600:800], c='k')
            #
            # ax.set_zlabel('Z')  # 坐标轴
            # ax.set_ylabel('Y')
            # ax.set_xlabel('X')
            # plt.show()

            '''
Example #27
0
def main():
    #Step 1 - download google's pre-trained neural network
# =============================================================================
#     url = 'https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip'
#     data_dir = '../data/'
#     model_name = os.path.split(url)[-1]
#     local_zip_file = os.path.join(data_dir, model_name)
#     if not os.path.exists(local_zip_file):
#         # Download
#         model_url = urllib.request.urlopen(url)
#         with open(local_zip_file, 'wb') as output:
#             output.write(model_url.read())
#         # Extract
#         with zipfile.ZipFile(local_zip_file, 'r') as zip_ref:
#             zip_ref.extractall(data_dir)
# =============================================================================
    data_dir = 'F:/Deep_Dream/inception5h'
    # start with a gray image with a little noise   
    img_noise = np.random.uniform(size=(224,224,3)) + 100.0
  
    model_fn = 'tensorflow_inception_graph.pb'
    
    #Step 2 - Creating Tensorflow session and loading the model
    graph = tf.Graph()
    sess = tf.InteractiveSession(graph=graph)
    with tf.gfile.FastGFile(os.path.join(data_dir, model_fn), 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    t_input = tf.placeholder(np.float32, name='input') # define the input tensor
    imagenet_mean = 117.0
    t_preprocessed = tf.expand_dims(t_input-imagenet_mean, 0)
    tf.import_graph_def(graph_def, {'input':t_preprocessed})
    
    layers = [op.name for op in graph.get_operations() if op.type=='Conv2D' and 'import/' in op.name]
    feature_nums = [int(graph.get_tensor_by_name(name+':0').get_shape()[-1]) for name in layers]
    
    print('Number of layers', len(layers))
    print('Total number of feature channels:', sum(feature_nums))
  
    # Helper functions for TF Graph visualization
    #pylint: disable=unused-variable
    def strip_consts(graph_def, max_const_size=32):
        """Strip large constant values from graph_def."""
        strip_def = tf.GraphDef()
        for n0 in graph_def.node:
            n = strip_def.node.add() #pylint: disable=maybe-no-member
            n.MergeFrom(n0)
            if n.op == 'Const':
                tensor = n.attr['value'].tensor
                size = len(tensor.tensor_content)
                if size > max_const_size:
                    tensor.tensor_content = "<stripped %d bytes>"%size
        return strip_def
      
    def rename_nodes(graph_def, rename_func):
        res_def = tf.GraphDef()
        for n0 in graph_def.node:
            n = res_def.node.add() #pylint: disable=maybe-no-member
            n.MergeFrom(n0)
            n.name = rename_func(n.name)
            for i, s in enumerate(n.input):
                n.input[i] = rename_func(s) if s[0]!='^' else '^'+rename_func(s[1:])
        return res_def
      
    def showarray(a):
        a = np.uint8(np.clip(a, 0, 1)*255)
        plt.axis("off")
        plt.imshow(a)
        plt.show()
        a=cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
        cv2.imshow("output", a)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        
    def visstd(a, s=0.1):
        '''Normalize the image range for visualization'''
        return (a-a.mean())/max(a.std(), 1e-4)*s + 0.5
    
    def T(layer):
        '''Helper for getting layer output tensor'''
        return graph.get_tensor_by_name("import/%s:0"%layer)
    
    def render_naive(t_obj, img0=img_noise, iter_n=20, step=1.0):
        t_score = tf.reduce_mean(t_obj) # defining the optimization objective
        t_grad = tf.gradients(t_score, t_input)[0] # behold the power of automatic differentiation!
        
        img = img0.copy()
        for _ in range(iter_n):
            g, _ = sess.run([t_grad, t_score], {t_input:img})
            # normalizing the gradient, so the same step size should work 
            g /= g.std()+1e-8         # for different layers and networks
            img += g*step
        showarray(visstd(img))
        
    def tffunc(*argtypes):
        '''Helper that transforms TF-graph generating function into a regular one.
        See "resize" function below.
        '''
        placeholders = list(map(tf.placeholder, argtypes))
        def wrap(f):
            out = f(*placeholders)
            def wrapper(*args, **kw):
                return out.eval(dict(zip(placeholders, args)), session=kw.get('session'))
            return wrapper
        return wrap
    
    def resize(img, size):
        img = tf.expand_dims(img, 0)
        return tf.image.resize_bilinear(img, size)[0,:,:,:]
    resize = tffunc(np.float32, np.int32)(resize)
    
    def calc_grad_tiled(img, t_grad, tile_size=512):
        '''Compute the value of tensor t_grad over the image in a tiled way.
        Random shifts are applied to the image to blur tile boundaries over 
        multiple iterations.'''
        sz = tile_size
        h, w = img.shape[:2]
        sx, sy = np.random.randint(sz, size=2)
        img_shift = np.roll(np.roll(img, sx, 1), sy, 0)
        grad = np.zeros_like(img)
        for y in range(0, max(h-sz//2, sz),sz):
            for x in range(0, max(w-sz//2, sz),sz):
                sub = img_shift[y:y+sz,x:x+sz]
                g = sess.run(t_grad, {t_input:sub})
                grad[y:y+sz,x:x+sz] = g
        return np.roll(np.roll(grad, -sx, 1), -sy, 0)    
        
        
    def render_deepdream(t_obj, img0=img_noise,
                         iter_n=10, step=1.5, octave_n=4, octave_scale=1.4):
        t_score = tf.reduce_mean(t_obj) # defining the optimization objective
        t_grad = tf.gradients(t_score, t_input)[0] # behold the power of automatic differentiation!
    
        # split the image into a number of octaves
        img = img0
        octaves = []
        for _ in range(octave_n-1):
            hw = img.shape[:2]
            lo = resize(img, np.int32(np.float32(hw)/octave_scale))
            hi = img-resize(lo, hw)
            img = lo
            octaves.append(hi)
        
        # generate details octave by octave
        for octave in range(octave_n):
            if octave>0:
                hi = octaves[-octave]
                img = resize(img, hi.shape[:2])+hi
            for _ in range(iter_n):
                g = calc_grad_tiled(img, t_grad)
                img += g*(step / (np.abs(g).mean()+1e-7))
            
            #this will usually be like 3 or 4 octaves
            #Step 5 output deep dream image via matplotlib
            showarray(img/255.0)
  
   	#Step 3 - Pick a layer to enhance our image
    layer = 'mixed4d_3x3_bottleneck_pre_relu'
    channel = 139 # picking some feature channel to visualize
    
    #open image
    img0 = PIL.Image.open('test.png')
    img0 = np.float32(img0)
     
    #Step 4 - Apply gradient ascent to that layer
    render_deepdream(tf.square(T('mixed4c')), img0)
def main(args):


    network = importlib.import_module(args.model_def, 'inference')
    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)

    # 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)
    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)

        # 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, args.random_crop, args.random_flip, args.random_rotate, 
            args.nrof_preprocess_threads)
        print('Total number of classes: %d' % nrof_classes)
        print('Total number of examples: %d' % len(image_list))
        
        print('Building training graph')
        
        # Placeholder for the learning rate
        learning_rate_placeholder = tf.placeholder(tf.float32, name='learning_rate')
        
        # Build the inference graph
        prelogits, _ = network.inference(image_batch, args.keep_probability, 
            phase_train=True, weight_decay=args.weight_decay)
        logits = slim.fully_connected(prelogits, 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)

        # 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
        if args.center_loss_factor>0.0:
            prelogits_center_loss, _ = facenet.center_loss(prelogits, label_batch, args.center_loss_alfa, nrof_classes)
            tf.add_to_collection(tf.GraphKeys.REGULARIZATION_LOSSES, prelogits_center_loss * args.center_loss_factor)

        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.scalar_summary('learning_rate', learning_rate)

        # Calculate the average cross entropy loss across the batch
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits, label_batch, name='cross_entropy_per_example')
        cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
        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')

        # 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.all_variables(), args.log_histograms)
        
        # Evaluation
        print('Building evaluation graph')
        lfw_label_list = range(0,len(lfw_paths))
        assert (len(lfw_paths) % args.lfw_batch_size == 0), "The number of images in the LFW test set need to be divisible by the lfw_batch_size"
        eval_image_batch, eval_label_batch = facenet.read_and_augument_data(lfw_paths, lfw_label_list, args.image_size,
            args.lfw_batch_size, None, False, False, False, args.nrof_preprocess_threads, shuffle=False)
        # Node for input images
        eval_image_batch.set_shape((None, args.image_size, args.image_size, 3))
        eval_image_batch = tf.identity(eval_image_batch, name='input')
        eval_prelogits, _ = network.inference(eval_image_batch, 1.0, 
            phase_train=False, weight_decay=0.0, reuse=True)
        eval_embeddings = tf.nn.l2_normalize(eval_prelogits, 1, 1e-10, name='embeddings')

        # Create a saver
        saver = tf.train.Saver(tf.all_variables(), max_to_keep=3)

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

        # 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))
        sess.run(tf.initialize_all_variables())
        sess.run(tf.initialize_local_variables())
        summary_writer = tf.train.SummaryWriter(log_dir, sess.graph)
        tf.train.start_queue_runners(sess=sess)

        with sess.as_default():

            if pretrained_model:
                print('Restoring pretrained model: %s' % pretrained_model)
                saver.restore(sess, pretrained_model)

            # Training and validation loop
            print('Running training')
            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, learning_rate_placeholder, global_step, 
                    total_loss, train_op, summary_op, summary_writer, regularization_losses, args.learning_rate_schedule_file)

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

                # Evaluate on LFW

                '''
                if args.lfw_dir:
                    evaluate(sess, eval_embeddings, eval_label_batch, actual_issame, args.lfw_batch_size, args.seed,
                        args.lfw_nrof_folds, log_dir, step, summary_writer)
                '''

                
    return model_dir
Example #29
0
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')

NUM_CLASSES = 90

opener = urllib.request.URLopener()
opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
tar_file = tarfile.open(MODEL_FILE)
for file in tar_file.getmembers():
  file_name = os.path.basename(file.name)
  if 'frozen_inference_graph.pb' in file_name:
    tar_file.extract(file, os.getcwd())

detection_graph = tf.Graph()
with detection_graph.as_default():
  od_graph_def = tf.GraphDef()
  with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
    serialized_graph = fid.read()
    od_graph_def.ParseFromString(serialized_graph)
    tf.import_graph_def(od_graph_def, name='')

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
category_index = label_map_util.create_category_index(categories)

import cv2
cap=cv2.VideoCapture(0) # 0 stands for very first webcam attach
filename="F:\\latest v\\open_cv_images\\output\\output11.avi"#place were i stored my output 
codec=cv2.VideoWriter_fourcc('m','p','4','v')#fourcc stands for four character code
def split_graph(path_input_pb,output_folder,name_pb1,name_pb2,scopes_pb2=None,optypes_pb2=None,opnames_pb2=None,savepbtxt=False): 
    
    savepbtxt=False # now always be false
    
    if scopes_pb2 is None and optypes_pb2 is None and opnames_pb2 is None:
        print('No split!!!')
        return None
    
    ops1 = []
    ops2 = []
    
######################################################################################################
    print('Load graph: ',path_input_pb)
    
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        with tf.gfile.GFile(path_input_pb, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
        ops = detection_graph.get_operations()
        
        
        # all unimplementing op in ops2, others in ops1
        for op in ops:
            if is_in_these_scopes(op,scopes=scopes_pb2,optypes=optypes_pb2,names=opnames_pb2):
                ops2.append(op)
            else:
                ops1.append(op)
        #################################################################################################
        print('ops1 and ops2 initialization done.')
        
        # move op in ops1 to ops2 which has inputs(dont need control dependencies) in ops2
        to_remove_in_ops1 = []
        new_add = True
        while(new_add):
            new_add = False
            for op in ops1:
                if (is_op_input_in_ops(op, ops2)):
                    ops2.append(op)
                    to_remove_in_ops1.append(op)
                    print(op.name)
                    new_add = True
            for op in to_remove_in_ops1:
                ops1.remove(op)
            to_remove_in_ops1 = []
    
        #ensure ops2's inputs ops which are in ops1 are all with len(op.outputs)==1
        need_add = True
        while(need_add):
            need_add = False
            to_add_ops = set()
            for oo in ops2:
                for ooi in oo.inputs:
                    has,num,outdtype,_3,_4 = is_opname_in_ops(ooi.op.name,ops1)
                    if has and num > 1:
                        to_add_ops.add(ooi.op.name)
                        print('Ensure step: '+ooi.op.name+' to move to ops2')
                        need_add = True
            to_remove = []
            for oo in ops1:
                for name in to_add_ops:
                    if name == oo.name:
                        ops2.append(oo)
                        to_remove.append(oo)
                        break
            for oo in to_remove:
                ops1.remove(oo)
                print('Ensure step: remove '+oo.name+' from ops1')
        #####################################################################################################
        print('ops1 and ops2 update done.')
        
        #for all op in ops2
        #if its inputs not in ops2,
        #  check it in ops1, and add to placeholders
        #if its control_inputs not in ops2,
        #  add to consts
        # 3 kind of types
        # not const, need a placeholder
        # a const ,can create a new same const
        # control like assert use a random const replace
        ops_placeholder_name = []
        ops_placeholder_dtype = []
        ops_const_name = []
        
        ops_true_const_node_def = []
        
        name_inputs = set()
        name_control_inputs = set()
        
        for op in ops2:
            for ci in op.control_inputs:
                name_control_inputs.add(ci.name)
            for t in op.inputs:
                name_inputs.add(t.op.name)
        
        ##########################################################################################
        print('Adding control_input as constant in pb2.')
        
        for opname in name_control_inputs:
            _0,_1,_2,_3,_4 = is_opname_in_ops(opname,ops2)
            if _0:
                continue
            else:
                ops_const_name.append(opname)
        
        ############################################################################################
        
        print('Adding inputs for pb2: copy constants for true const inputs and create pleceholders for none-const inputs.')
        
        for opname in name_inputs:
            _0,_1,_2,_3,_4 = is_opname_in_ops(opname,ops2)
    
            if _0:
                continue
            else:
                ophas,outnum,outdtype,optype,op_def = is_opname_in_ops(opname,ops1)
                # perhaps a const output to both ops1 and ops2, so dont't move ops2' const inputs(in ops1) to ops2
                if outnum != 1:
                    print(opname+' in ops1, outnum > 1!')
                    print('outnum: '+str(outnum))
                    import sys
                    sys.exit(-1)   
                if ophas:
                    if optype == 'Const':
                        ops_true_const_node_def.append(op_def)
                        print('Add true const: '+opname)
                    else:
                        ops_placeholder_name.append(opname)
                        ops_placeholder_dtype.append(outdtype)
                        print('Add placeholder: '+opname)
                else:
                    print(opname+' not in ops1!')
                    import sys
                    sys.exit(-1)
        #########################################################################
    
        #for all op in ops1
        #if its inputs not in ops1,
        #  exit(-1)
        #if its control_inputs not in ops1,
        #  add to consts

        ops_const_name_1 = []
        
        name_inputs_1 = set()
        name_control_inputs_1 = set()
        
        for op in ops1:
            for ci in op.control_inputs:
                name_control_inputs_1.add(ci.name)
            for t in op.inputs:
                name_inputs_1.add(t.op.name)
        for opname in name_inputs_1:
            _0,_1,_2,_3,_4 = is_opname_in_ops(opname,ops1)
            if _0 is False:
                print("wrong: " + str(opname) + " is not in ops1.\nexit.")
                sys.exit(-1)
        ##########################################################################################
        print('Adding control_input as constant in pb1.')
        
        for opname in name_control_inputs_1:
            _0,_1,_2,_3,_4 = is_opname_in_ops(opname,ops1)
            if _0:
                continue
            else:
                ops_const_name_1.append(opname)
        
############################################################################################
    
    # add consts for ops1
    # control like assert use a random const replace
    with tf.Graph().as_default():
        for cs in ops_const_name_1:
            #controls
            ops1.append(tf.constant(1.0,dtype=tf.float32,name=cs).op)
            print('constant added for ops1: ')
            print('\tscope name: '+cs)
    
    # add placeholders and consts for ops2
    # 3 kind of types
    # not const, need a placeholder
    # a const ,can create a new same const
    # control like assert use a random const replace
    with tf.Graph().as_default():
        for tmptype,tmpname in zip(ops_placeholder_dtype,ops_placeholder_name):
            ops2.append(tf.placeholder(tmptype, shape=None, name=tmpname).op)
            print('placeholder added: ')
            print('\tdata type: '+str(tmptype))
            print('\tscope name: '+tmpname)
        for cs in ops_const_name:
            #controls
            has = False
            for nn in ops_placeholder_name:
                  if cs == nn:
                      has = True
            if has:
                print('In add const: opname '+cs+' already in placeholders.')
                continue
            else:
                ops2.append(tf.constant(1.0,dtype=tf.float32,name=cs).op)
                print('constant added for ops2: ')
                print('\tscope name: '+cs)
    
    
    
    #########################################################################
    
    
    print('split done, follwed with saving.')
    '''
    with open(PATH_PLACEHOLDERS, 'w+') as plhfile:
        for name in ops_placeholder_name:
            plhfile.write(name+'\n')
    
    
    with open(PATH_OPS1, 'w+') as opsfile1:
        for o1 in ops1:
            #print(o1.name)
            sss = node_def_add_node(str(o1.node_def))
            opsfile1.write(sss)
    
    with open(PATH_OPS2, 'w+') as opsfile2:
        for true_const_def in ops_true_const_node_def:
            sss = node_def_add_node(str(true_const_def))
            opsfile2.write(sss)
        for o2 in ops2:
            sss = node_def_add_node(str(o2.node_def))
            opsfile2.write(sss)
    '''
    sss1 = ''
    for o1 in ops1:
        sss = node_def_add_node(str(o1.node_def))
        sss1 = sss1 + sss
    sss2 = ''
    for true_const_def in ops_true_const_node_def:
        sss = node_def_add_node(str(true_const_def))
        sss2 = sss2 + sss
    for o2 in ops2:
        sss = node_def_add_node(str(o2.node_def))
        sss2 = sss2 + sss
    
    if savepbtxt:
        import os
        name_pbtxt1 = name_pb1 + 'txt'
        name_pbtxt2 = name_pb2 + 'txt'
        with open(os.path.join(output_folder+name_pbtxt1), 'w+') as opsfile1:
            opsfile1.write(sss1)
            print('save pbtxt1: ', os.path.join(output_folder+name_pbtxt1))
        with open(os.path.join(output_folder+name_pbtxt2), 'w+') as opsfile2:
            opsfile2.write(sss2)
            print('save pbtxt2: ', os.path.join(output_folder+name_pbtxt2))
    ##########################################################################
    print('start saving pb1 and pb2.')
    
    convert_pbtxt_str_to_pb(sss1,output_folder,name_pb1)
    convert_pbtxt_str_to_pb(sss2,output_folder,name_pb2)
    
    print('saving pb1 and pb2 done.')
    
    return ops_placeholder_name