Beispiel #1
0
 def testGetNetworkFnSecondHalf(self):
   batch_size = 5
   num_classes = 1000
   for net in list(nets_factory.networks_map.keys())[10:]:
     with tf.Graph().as_default() as g, self.test_session(g):
       net_fn = nets_factory.get_network_fn(net, num_classes)
       # Most networks use 224 as their default_image_size
       image_size = getattr(net_fn, 'default_image_size', 224)
       inputs = tf.random_uniform((batch_size, image_size, image_size, 3))
       logits, end_points = net_fn(inputs)
       self.assertTrue(isinstance(logits, tf.Tensor))
       self.assertTrue(isinstance(end_points, dict))
       self.assertEqual(logits.get_shape().as_list()[0], batch_size)
       self.assertEqual(logits.get_shape().as_list()[-1], num_classes)
def main(_):
    if not FLAGS.output_file:
        raise ValueError(
            'You must supply the path to save to with --output_file')
    tf.logging.set_verbosity(tf.logging.INFO)
    with tf.Graph().as_default() as graph:
        dataset = dataset_factory.get_dataset(FLAGS.dataset_name, 'train',
                                              FLAGS.dataset_dir)
        network_fn = nets_factory.get_network_fn(
            FLAGS.model_name,
            num_classes=(dataset.num_classes - FLAGS.labels_offset),
            is_training=FLAGS.is_training)
        image_size = FLAGS.image_size or network_fn.default_image_size
        placeholder = tf.placeholder(
            name='input',
            dtype=tf.float32,
            shape=[FLAGS.batch_size, image_size, image_size, 3])
        network_fn(placeholder)
        graph_def = graph.as_graph_def()
        with gfile.GFile(FLAGS.output_file, 'wb') as f:
            f.write(graph_def.SerializeToString())
Beispiel #3
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.INFO)
    #os.environ["OMP_NUM_THREADS"] = "54"
    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))

        num_batches = 100

        config = tf.ConfigProto(
            inter_op_parallelism_threads=FLAGS.inter_op_parallelism_threads,
            intra_op_parallelism_threads=FLAGS.intra_op_parallelism_threads)

        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)

        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,
            hooks=[_LoggerHook()],
            session_config=config)
import numpy
import argparse
import os, sys, time
import tensorflow as tf
import _pickle as pickle

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
import nets_factory

batchsize = 1000
N = 50000

model_name = "densenet121"
num_classes = 1000
network_fn = nets_factory.get_network_fn(model_name,
                                         num_classes=num_classes,
                                         is_training=False)

finalActivationsFileName = "floating_point_acc.outp"
argmaxOutputFileName = "floating_point_argmax.outp"

imagesPlaceHolder = tf.placeholder(tf.float32,
                                   shape=(None, 224, 224, 3),
                                   name="input_x")
logits, end_points = network_fn(imagesPlaceHolder)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    modelPath = "../PreTrainedModel/tf-densenet121.ckpt"
    saver = tf.train.Saver()
Beispiel #5
0
    def cnnmodel(self, in_rgb, in_segxyz, in_control=None, is_training=True):
        step = in_control
        step = tflearn.layers.core.fully_connected(step, 16, activation='relu')
        step = tflearn.layers.normalization.batch_normalization(step)
        step = tflearn.layers.core.fully_connected(step, 24, activation='relu')
        step = tflearn.layers.normalization.batch_normalization(step)
        step_low = tflearn.layers.core.fully_connected(step,
                                                       32,
                                                       activation='relu')
        step = step_low
        step = tflearn.layers.normalization.batch_normalization(step)
        step = tflearn.layers.core.fully_connected(step, 64, activation='relu')
        step_high = tflearn.layers.normalization.batch_normalization(step)

        step_low = tf.reshape(step_low, (-1, 1, 1, 32))
        step_low = tf.tile(step_low, [1, 15, 20, 1])

        step_high = tf.reshape(step_high, (-1, 1, 1, 64))
        step_high = tf.tile(step_high, [1, 4, 5, 1])

        xyz_high = self.cnn(in_segxyz)

        network_fn = get_network_fn('resnet_v2_50',
                                    num_classes=None,
                                    weight_decay=1e-5,
                                    is_training=is_training)
        net, end_points = network_fn(in_rgb)

        feat_C4 = end_points['resnet_v2_50/block4']
        feat_C3 = end_points['resnet_v2_50/block3']
        feat_C2 = end_points['resnet_v2_50/block2']
        feat_C1 = end_points['resnet_v2_50/block1']
        print("feat_C4", feat_C4)
        print("feat_C1", feat_C1)

        feat_C4 = tf.concat([feat_C4, step_high, xyz_high], axis=-1)

        feat = feat_C4
        feat = tflearn.layers.conv.conv_2d(feat,
                                           1024, (1, 1),
                                           strides=1,
                                           activation='relu',
                                           weight_decay=1e-5,
                                           regularizer='L2')

        print("feat", feat)
        feat_0 = tflearn.layers.conv.avg_pool_2d(feat, [4, 5], [4, 5],
                                                 padding='VALID')
        feat_0 = tflearn.layers.conv.conv_2d(feat_0,
                                             256, (1, 1),
                                             strides=1,
                                             activation='relu',
                                             weight_decay=1e-5,
                                             regularizer='L2')
        feat_0 = tf.image.resize_bilinear(feat_0, [4, 5], align_corners=True)

        feat_1 = tflearn.layers.conv.conv_2d(feat,
                                             256, (1, 1),
                                             strides=1,
                                             activation='relu',
                                             weight_decay=1e-5,
                                             regularizer='L2')

        feat_2 = tflearn.layers.conv.atrous_conv_2d(feat,
                                                    256, (3, 3),
                                                    rate=2,
                                                    activation='relu',
                                                    weight_decay=1e-5,
                                                    regularizer='L2')

        feat_4 = tflearn.layers.conv.atrous_conv_2d(feat,
                                                    256, (3, 3),
                                                    rate=4,
                                                    activation='relu',
                                                    weight_decay=1e-5,
                                                    regularizer='L2')

        feat_6 = tflearn.layers.conv.atrous_conv_2d(feat,
                                                    256, (3, 3),
                                                    rate=6,
                                                    activation='relu',
                                                    weight_decay=1e-5,
                                                    regularizer='L2')

        feat_ac = tf.concat([feat_0, feat_1, feat_4, feat_6], axis=-1)
        feat_ac = tflearn.layers.conv.conv_2d(feat_ac,
                                              256, (1, 1),
                                              strides=1,
                                              activation='relu',
                                              weight_decay=1e-5,
                                              regularizer='L2')
        feat_ac = tflearn.layers.core.dropout(feat_ac, keep_prob=0.9)

        feat_low = tf.concat([feat_C1, step_low], axis=-1)

        print("feat_low", feat_low)

        feat_high = tf.image.resize_bilinear(feat_ac, [15, 20],
                                             align_corners=True)

        feat_full = tf.concat([feat_high, feat_low], axis=-1)

        x = tflearn.layers.conv.conv_2d_transpose(feat_full,
                                                  128, [5, 5], [30, 40],
                                                  strides=2,
                                                  activation='relu',
                                                  weight_decay=1e-5,
                                                  regularizer='L2')
        x = tflearn.layers.normalization.batch_normalization(x)
        x = tflearn.layers.conv.conv_2d_transpose(x,
                                                  64, [5, 5], [60, 80],
                                                  strides=2,
                                                  activation='relu',
                                                  weight_decay=1e-5,
                                                  regularizer='L2')
        x = tflearn.layers.normalization.batch_normalization(x)
        x = tflearn.layers.conv.conv_2d_transpose(x,
                                                  64, [5, 5], [120, 160],
                                                  strides=2,
                                                  activation='relu',
                                                  weight_decay=1e-5,
                                                  regularizer='L2')
        x = tflearn.layers.normalization.batch_normalization(x)

        score = x
        score = tflearn.layers.conv.conv_2d(score,
                                            2, (3, 3),
                                            strides=1,
                                            activation='linear',
                                            weight_decay=1e-3,
                                            regularizer='L2')
        return score
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():
        #######################
        # Config model_deploy #
        #######################
        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()

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

        ######################
        # Select the network #
        ######################
        network_fn = nets_factory.get_network_fn(
            FLAGS.model_name,
            num_classes=(dataset.num_classes - FLAGS.labels_offset),
            weight_decay=FLAGS.weight_decay,
            is_training=True)

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

        ##############################################################
        # Create a dataset provider that loads data from the dataset #
        ##############################################################
        with tf.device(deploy_config.inputs_device()):
            provider = slim.dataset_data_provider.DatasetDataProvider(
                dataset,
                num_readers=FLAGS.num_readers,
                common_queue_capacity=20 * FLAGS.batch_size,
                common_queue_min=10 * FLAGS.batch_size)
            [image, label] = provider.get(['image', 'label'])
            label -= FLAGS.labels_offset

            train_image_size = FLAGS.train_image_size or network_fn.default_image_size

            image = image_preprocessing_fn(image, train_image_size,
                                           train_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)
            labels = slim.one_hot_encoding(
                labels, dataset.num_classes - FLAGS.labels_offset)
            batch_queue = slim.prefetch_queue.prefetch_queue(
                [images, labels], capacity=2 * deploy_config.num_clones)

        ####################
        # Define the model #
        ####################
        def clone_fn(batch_queue):
            """Allows data parallelism by creating multiple clones of network_fn."""
            images, labels = batch_queue.dequeue()
            logits, end_points = network_fn(images)

            #############################
            # Specify the loss function #
            #############################
            if 'AuxLogits' in end_points:
                slim.losses.softmax_cross_entropy(
                    end_points['AuxLogits'],
                    labels,
                    label_smoothing=FLAGS.label_smoothing,
                    weights=0.4,
                    scope='aux_loss')
            slim.losses.softmax_cross_entropy(
                logits,
                labels,
                label_smoothing=FLAGS.label_smoothing,
                weights=1.0)
            return end_points

        # Gather initial summaries.
        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)
        # Gather update_ops from the first clone. These contain, for example,
        # the updates for the batch_norm variables created by network_fn.
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS,
                                       first_clone_scope)

        # Add summaries for end_points.
        end_points = clones[0].outputs
        for end_point in end_points:
            x = end_points[end_point]
            summaries.add(tf.summary.histogram('activations/' + end_point, x))
            summaries.add(
                tf.summary.scalar('sparsity/' + end_point,
                                  tf.nn.zero_fraction(x)))

        # Add summaries for losses.
        for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope):
            summaries.add(tf.summary.scalar('losses/%s' % loss.op.name, loss))

        # Add summaries for variables.
        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

        if FLAGS.quantize_delay >= 0:
            tf.contrib.quantize.create_training_graph(
                quant_delay=FLAGS.quantize_delay)

        #########################################
        # Configure the optimization procedure. #
        #########################################
        with tf.device(deploy_config.optimizer_device()):
            learning_rate = _configure_learning_rate(dataset.num_samples,
                                                     global_step)
            optimizer = _configure_optimizer(learning_rate)
            summaries.add(tf.summary.scalar('learning_rate', learning_rate))

        if FLAGS.sync_replicas:
            # If sync_replicas is enabled, the averaging will be done in the chief
            # queue runner.
            optimizer = tf.train.SyncReplicasOptimizer(
                opt=optimizer,
                replicas_to_aggregate=FLAGS.replicas_to_aggregate,
                total_num_replicas=FLAGS.worker_replicas,
                variable_averages=variable_averages,
                variables_to_average=moving_average_variables)
        elif 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 = _get_variables_to_train()

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

        # 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)
        with tf.control_dependencies([update_op]):
            train_tensor = tf.identity(total_loss, name='train_op')

        # 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. #
        ###########################
        slim.learning.train(
            train_tensor,
            logdir=FLAGS.train_dir,
            master=FLAGS.master,
            is_chief=(FLAGS.task == 0),
            init_fn=_get_init_fn(),
            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,
            save_interval_secs=FLAGS.save_interval_secs,
            sync_optimizer=optimizer if FLAGS.sync_replicas else None)
    def __init__(self,
                 network_name,
                 checkpoint_path,
                 batch_size,
                 num_classes,
                 image_size=None,
                 preproc_func_name=None,
                 preproc_threads=2):
        '''
        TensorFlow feature extractor using tf.slim and models/slim.
        Core functionalities are loading network architecture, pretrained weights,
        setting up an image pre-processing function, queues for fast input reading.
        The main workflow after initialization is first loading a list of image
        files using the `enqueue_image_files` function and then pushing them
        through the network with `feed_forward_batch`.

        For pre-trained networks and some more explanation, checkout:
          https://github.com/tensorflow/models/tree/master/slim

        :param network_name: str, network name (e.g. resnet_v1_101)
        :param checkpoint_path: str, full path to checkpoint file to load
        :param batch_size: int, batch size
        :param num_classes: int, number of output classes
        :param image_size: int, width and height to overrule default_image_size (default=None)
        :param preproc_func_name: func, optional to overwrite default processing (default=None)
        :param preproc_threads: int, number of input threads (default=1)

        '''

        self._network_name = network_name
        self._checkpoint_path = checkpoint_path
        self._batch_size = batch_size
        self._num_classes = num_classes
        self._image_size = image_size
        self._preproc_func_name = preproc_func_name
        self._num_preproc_threads = preproc_threads

        self._global_step = tf.train.get_or_create_global_step()

        # Retrieve the function that returns logits and endpoints
        self._network_fn = nets_factory.get_network_fn(self._network_name,
                                                       num_classes=num_classes,
                                                       is_training=False)

        # Retrieve the model scope from network factory
        self._model_scope = nets_factory.arg_scopes_map[self._network_name]

        # Fetch the default image size
        self._image_size = self._network_fn.default_image_size

        # Setup the input pipeline with a queue of filenames
        self._filename_queue = tf.FIFOQueue(100000, [tf.string],
                                            shapes=[[]],
                                            name="filename_queue")
        self._pl_image_files = tf.placeholder(tf.string,
                                              shape=[None],
                                              name="image_file_list")
        self._enqueue_op = self._filename_queue.enqueue_many(
            [self._pl_image_files])
        self._num_in_queue = self._filename_queue.size()

        # Image reader and preprocessing
        self._batch_from_queue, self._batch_filenames = \
            self._preproc_image_batch(self._batch_size, num_threads=preproc_threads)

        # Either use the placeholder as inputs or feed from queue
        self._image_batch = tf.placeholder_with_default(
            self._batch_from_queue,
            shape=[None, self._image_size, self._image_size, 3])

        # Retrieve the logits and network endpoints (for extracting activations)
        # Note: endpoints is a dictionary with endpoints[name] = tf.Tensor
        self._logits, self._endpoints = self._network_fn(self._image_batch)

        # Find the checkpoint file
        checkpoint_path = self._checkpoint_path
        if tf.gfile.IsDirectory(self._checkpoint_path):
            checkpoint_path = tf.train.latest_checkpoint(self._checkpoint_path)

        # Load pre-trained weights into the model
        variables_to_restore = slim.get_variables_to_restore()
        restore_fn = slim.assign_from_checkpoint_fn(self._checkpoint_path,
                                                    variables_to_restore)

        # Soft placement allows placing on CPU ops without GPU implementation.
        session_config = tf.ConfigProto(allow_soft_placement=True,
                                        log_device_placement=False)

        session_config.gpu_options.per_process_gpu_memory_fraction = 0.5
        session_config.gpu_options.visible_device_list = "1"

        # Start the session and load the pre-trained weights
        self._sess = tf.Session(config=session_config)
        restore_fn(self._sess)

        # Local variables initializer, needed for queues etc.
        self._sess.run(tf.local_variables_initializer())

        # Managing the queues and threads
        self._coord = tf.train.Coordinator()
        self._threads = tf.train.start_queue_runners(coord=self._coord,
                                                     sess=self._sess)