Ejemplo n.º 1
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)

    # Get dataset-dependent information.
    dataset = data_generator.Dataset(
        dataset_name=FLAGS.dataset,
        split_name=FLAGS.vis_split,
        dataset_dir=FLAGS.dataset_dir,
        batch_size=FLAGS.vis_batch_size,
        crop_size=[int(sz) for sz in FLAGS.vis_crop_size],
        min_resize_value=FLAGS.min_resize_value,
        max_resize_value=FLAGS.max_resize_value,
        resize_factor=FLAGS.resize_factor,
        model_variant=FLAGS.model_variant,
        is_training=False,
        should_shuffle=False,
        should_repeat=False)

    train_id_to_eval_id = None
    if dataset.dataset_name == data_generator.get_cityscapes_dataset_name():
        tf.logging.info('Cityscapes requires converting train_id to eval_id.')
        train_id_to_eval_id = _CITYSCAPES_TRAIN_ID_TO_EVAL_ID

    # Prepare for visualization.
    tf.gfile.MakeDirs(FLAGS.vis_logdir)

    save_dir = os.path.join(FLAGS.vis_logdir, _SEMANTIC_PREDICTION_SAVE_FOLDER)
    tf.gfile.MakeDirs(save_dir)

    instance_save_dir = os.path.join(FLAGS.vis_logdir,
                                     _INSTANCE_PREDICTION_SAVE_FOLDER)
    tf.gfile.MakeDirs(instance_save_dir)

    regression_save_dir = os.path.join(FLAGS.vis_logdir,
                                       _OFFSET_PREDICTION_SAVE_FOLDER)
    tf.gfile.MakeDirs(regression_save_dir)

    panoptic_save_dir = os.path.join(FLAGS.vis_logdir,
                                     _PANOPTIC_PREDICTION_SAVE_FOLDER)
    tf.gfile.MakeDirs(panoptic_save_dir)

    raw_save_dir = os.path.join(FLAGS.vis_logdir,
                                _RAW_SEMANTIC_PREDICTION_SAVE_FOLDER)
    tf.gfile.MakeDirs(raw_save_dir)

    tf.logging.info('Visualizing on %s set', FLAGS.vis_split)

    with tf.Graph().as_default():
        samples = dataset.get_one_shot_iterator().get_next()

        model_options = common.ModelOptions(
            outputs_to_num_classes={
                common.OUTPUT_TYPE: dataset.num_of_classes,
                common.INSTANCE: 1,
                common.OFFSET: 2
            },
            crop_size=[int(sz) for sz in FLAGS.vis_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=model_options,
                image_pyramid=FLAGS.image_pyramid)
        else:
            tf.logging.info('Performing multi-scale test.')
            if FLAGS.quantize_delay_step >= 0:
                raise ValueError(
                    'Quantize mode is not supported with 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_semantic = predictions[common.OUTPUT_TYPE]
        predictions_instance = predictions[common.INSTANCE]
        predictions_regression = predictions[common.OFFSET]

        if FLAGS.min_resize_value and FLAGS.max_resize_value:
            # Only support batch_size = 1, since we assume the dimensions of original
            # image after tf.squeeze is [height, width, 3].
            assert FLAGS.vis_batch_size == 1

            # Reverse the resizing and padding operations performed in preprocessing.
            # First, we slice the valid regions (i.e., remove padded region) and then
            # we resize the predictions back.
            original_image = tf.squeeze(samples[common.ORIGINAL_IMAGE])
            original_image_shape = tf.shape(original_image)
            predictions_semantic = tf.slice(
                predictions_semantic, [0, 0, 0],
                [1, original_image_shape[0], original_image_shape[1]])
            resized_shape = tf.to_int32([
                tf.squeeze(samples[common.HEIGHT]),
                tf.squeeze(samples[common.WIDTH])
            ])
            predictions_semantic = tf.squeeze(
                tf.image.resize_images(
                    tf.expand_dims(predictions_semantic, 3),
                    resized_shape,
                    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
                    align_corners=True), 3)
            ############################### POST PROCESSING LOGITS FROM INSTANCE CENTER #####################
            predictions_instance = tf.slice(
                predictions_instance, [0, 0, 0],
                [1, original_image_shape[0], original_image_shape[1]])
            resized_shape = tf.to_int32([
                tf.squeeze(samples[common.HEIGHT]),
                tf.squeeze(samples[common.WIDTH])
            ])
            predictions_instance = tf.squeeze(
                tf.image.resize_images(
                    tf.expand_dims(predictions_instance, 3),
                    resized_shape,
                    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
                    align_corners=True), 3)

            ############################### POST PROCESSING LOGITS FROM INSTANCE REGRESSION #####################
            predictions_regression = tf.slice(
                predictions_regression, [0, 0, 0, 0],
                [1, original_image_shape[0], original_image_shape[1], 1])
            resized_shape = tf.to_int32([
                tf.squeeze(samples[common.HEIGHT]),
                tf.squeeze(samples[common.WIDTH]), 2
            ])
            predictions_regression = tf.image.resize_images(
                predictions_regression,
                resized_shape,
                method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
                align_corners=True)

        tf.train.get_or_create_global_step()
        if FLAGS.quantize_delay_step >= 0:
            contrib_quantize.create_eval_graph()

        num_iteration = 0
        max_num_iteration = FLAGS.max_number_of_iterations

        checkpoints_iterator = contrib_training.checkpoints_iterator(
            FLAGS.checkpoint_dir, min_interval_secs=FLAGS.eval_interval_secs)
        for checkpoint_path in checkpoints_iterator:
            num_iteration += 1
            tf.logging.info('Starting visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))
            tf.logging.info('Visualizing with model %s', checkpoint_path)

            scaffold = tf.train.Scaffold(
                init_op=tf.global_variables_initializer())
            session_creator = tf.train.ChiefSessionCreator(
                scaffold=scaffold,
                master=FLAGS.master,
                checkpoint_filename_with_path=checkpoint_path)
            with tf.train.MonitoredSession(session_creator=session_creator,
                                           hooks=None) as sess:
                batch = 0
                image_id_offset = 0

                while not sess.should_stop():
                    tf.logging.info('Visualizing batch %d', batch + 1)
                    _process_batch(
                        sess=sess,
                        original_images=samples[common.ORIGINAL_IMAGE],
                        semantic_predictions=predictions_semantic,
                        instance_predictions=predictions_instance,
                        regression_predictions=predictions_regression,
                        image_names=samples[common.IMAGE_NAME],
                        image_heights=samples[common.HEIGHT],
                        image_widths=samples[common.WIDTH],
                        image_id_offset=image_id_offset,
                        save_dir=save_dir,
                        instance_save_dir=instance_save_dir,
                        regression_save_dir=regression_save_dir,
                        panoptic_save_dir=panoptic_save_dir,
                        raw_save_dir=raw_save_dir,
                        train_id_to_eval_id=train_id_to_eval_id)
                    image_id_offset += FLAGS.vis_batch_size
                    batch += 1

            tf.logging.info('Finished visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))
            if max_num_iteration > 0 and num_iteration >= max_num_iteration:
                break
Ejemplo n.º 2
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)

    dataset = data_generator.Dataset(
        dataset_name=FLAGS.dataset,
        split_name=FLAGS.eval_split,
        dataset_dir=FLAGS.dataset_dir,
        batch_size=FLAGS.eval_batch_size,
        crop_size=[int(sz) for sz in FLAGS.eval_crop_size],
        min_resize_value=FLAGS.min_resize_value,
        max_resize_value=FLAGS.max_resize_value,
        resize_factor=FLAGS.resize_factor,
        model_variant=FLAGS.model_variant,
        num_readers=2,
        is_training=False,
        should_shuffle=False,
        should_repeat=False)

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

    with tf.Graph().as_default():
        samples = dataset.get_one_shot_iterator().get_next()

        model_options = common.ModelOptions(
            outputs_to_num_classes={
                common.OUTPUT_TYPE: dataset.num_of_classes,
                common.INSTANCE: 1,
                common.OFFSET: 2
            },
            crop_size=[int(sz) for sz in FLAGS.eval_crop_size],
            atrous_rates=FLAGS.atrous_rates,
            output_stride=FLAGS.output_stride)

        # Set shape in order for tf.contrib.tfprof.model_analyzer to work properly.
        samples[common.IMAGE].set_shape([
            FLAGS.eval_batch_size,
            int(FLAGS.eval_crop_size[0]),
            int(FLAGS.eval_crop_size[1]), 3
        ])

        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.')
            if FLAGS.quantize_delay_step >= 0:
                raise ValueError(
                    'Quantize mode is not supported with multi-scale test.')

        predictions_semantic = predictions[common.OUTPUT_TYPE]
        predictions_center_points = predictions[common.INSTANCE]
        predictions_offset_vectors = predictions[common.OFFSET]

        # tf Non-maxima Suppression
        # Pooling based NMS for Pooling Instance Centers
        # Filtering predictions that are less than 0.1
        instance_prediction = generate_instance_segmentation(
            predictions_semantic, predictions_center_points,
            predictions_offset_vectors)

        category_prediction = tf.squeeze(predictions_semantic)

        category_label = tf.squeeze(samples[common.LABEL][0])
        not_ignore_mask = tf.not_equal(category_label, 255)
        category_label = tf.cast(
            category_label * tf.cast(not_ignore_mask, tf.int32), tf.int32)
        instance_label = tf.squeeze(samples[common.LABEL_INSTANCE_IDS][0])
        category_prediction = category_prediction * tf.cast(
            not_ignore_mask, tf.int64)
        instance_prediction = instance_prediction * tf.cast(
            not_ignore_mask, tf.int64)

        # Define the evaluation metric.
        metric_map = {}
        metric_map[
            'panoptic_quality'] = streaming_metrics.streaming_panoptic_quality(
                category_label,
                instance_label,
                category_prediction,
                instance_prediction,
                num_classes=19,
                max_instances_per_category=256,
                ignored_label=255,
                offset=256 * 256)
        metric_map[
            'parsing_covering'] = streaming_metrics.streaming_parsing_covering(
                category_label,
                instance_label,
                category_prediction,
                instance_prediction,
                num_classes=19,
                max_instances_per_category=256,
                ignored_label=255,
                offset=256 * 256,
                normalize_by_image_size=True)
        metrics_to_values, metrics_to_updates = slim.metrics.aggregate_metric_map(
            metric_map)

        summary_ops = []
        for metric_name, metric_value in metrics_to_values.iteritems():
            if metric_name == 'panoptic_quality':
                [pq, sq, rq, total_tp, total_fn,
                 total_fp] = tf.unstack(metric_value, 6, axis=0)
                panoptic_metrics = {
                    # Panoptic quality.
                    'pq': pq,
                    # Segmentation quality.
                    'sq': sq,
                    # Recognition quality.
                    'rq': rq,
                    # Total true positives.
                    'total_tp': total_tp,
                    # Total false negatives.
                    'total_fn': total_fn,
                    # Total false positives.
                    'total_fp': total_fp,
                }
                # Find the valid classes that will be used for evaluation. We will
                # ignore the `ignore_label` class and other classes which have (tp + fn
                # + fp) equal to 0.
                valid_classes = tf.logical_and(
                    tf.not_equal(tf.range(0, dataset.num_of_classes),
                                 dataset.ignore_label),
                    tf.not_equal(total_tp + total_fn + total_fp, 0))
                for target_metric, target_value in panoptic_metrics.iteritems(
                ):
                    output_metric_name = '{}_{}'.format(
                        metric_name, target_metric)
                    op = tf.summary.scalar(
                        output_metric_name,
                        tf.reduce_mean(
                            tf.boolean_mask(target_value, valid_classes)))
                    op = tf.Print(op, [target_value],
                                  output_metric_name + '_classwise: ',
                                  summarize=dataset.num_of_classes)
                    op = tf.Print(op, [
                        tf.reduce_mean(
                            tf.boolean_mask(target_value, valid_classes))
                    ],
                                  output_metric_name + '_mean: ',
                                  summarize=1)
                    summary_ops.append(op)
            elif metric_name == 'parsing_covering':
                [
                    per_class_covering, total_per_class_weighted_ious,
                    total_per_class_gt_areas
                ] = tf.unstack(metric_value, 3, axis=0)
                # Find the valid classes that will be used for evaluation. We will
                # ignore the `void_label` class and other classes which have
                # total_per_class_weighted_ious + total_per_class_gt_areas equal to 0.
                valid_classes = tf.logical_and(
                    tf.not_equal(tf.range(0, dataset.num_of_classes),
                                 dataset.ignore_label),
                    tf.not_equal(
                        total_per_class_weighted_ious +
                        total_per_class_gt_areas, 0))
                op = tf.summary.scalar(
                    metric_name,
                    tf.reduce_mean(
                        tf.boolean_mask(per_class_covering, valid_classes)))
                op = tf.Print(op, [per_class_covering],
                              metric_name + '_classwise: ',
                              summarize=dataset.num_of_classes)
                op = tf.Print(op, [
                    tf.reduce_mean(
                        tf.boolean_mask(per_class_covering, valid_classes))
                ],
                              metric_name + '_mean: ',
                              summarize=1)
                summary_ops.append(op)
            else:
                raise ValueError('The metric_name "%s" is not supported.' %
                                 metric_name)

        num_eval_iters = None
        if FLAGS.max_number_of_evaluations > 0:
            num_eval_iters = FLAGS.max_number_of_evaluations

        if FLAGS.quantize_delay_step >= 0:
            contrib_quantize.create_eval_graph()

        contrib_tfprof.model_analyzer.print_model_analysis(
            tf.get_default_graph(),
            tfprof_options=contrib_tfprof.model_analyzer.
            TRAINABLE_VARS_PARAMS_STAT_OPTIONS)
        contrib_tfprof.model_analyzer.print_model_analysis(
            tf.get_default_graph(),
            tfprof_options=contrib_tfprof.model_analyzer.FLOAT_OPS_OPTIONS)

        metric_values = slim.evaluation.evaluation_loop(
            master=FLAGS.master,
            checkpoint_dir=FLAGS.checkpoint_dir,
            logdir=FLAGS.eval_logdir,
            num_evals=20,
            eval_op=metrics_to_updates.values(),
            final_op=metrics_to_values.values(),
            summary_op=tf.summary.merge(summary_ops),
            max_number_of_evaluations=FLAGS.max_number_of_evaluations,
            eval_interval_secs=FLAGS.eval_interval_secs)
Ejemplo n.º 3
0
def main(unused_argv):
  tf.logging.set_verbosity(tf.logging.INFO)
  tf.logging.info('Prepare to export model to: %s', FLAGS.export_path)

  with tf.Graph().as_default():
    image, image_size, resized_image_size = _create_input_tensors()

    model_options = common.ModelOptions(
        outputs_to_num_classes={common.OUTPUT_TYPE: FLAGS.num_classes,
                                common.INSTANCE: 1, common.OFFSET: 2},
        crop_size=FLAGS.crop_size,
        atrous_rates=FLAGS.atrous_rates,
        output_stride=FLAGS.output_stride)

    if tuple(FLAGS.inference_scales) == (1.0,):
      tf.logging.info('Exported model performs single-scale inference.')
      predictions = model.predict_labels(
          image,
          model_options=model_options,
          image_pyramid=FLAGS.image_pyramid)
    else:
      tf.logging.info('Exported model performs multi-scale inference.')
      if FLAGS.quantize_delay_step >= 0:
        raise ValueError(
            'Quantize mode is not supported with multi-scale test.')
      predictions = model.predict_labels_multi_scale(
          image,
          model_options=model_options,
          eval_scales=FLAGS.inference_scales,
          add_flipped_images=FLAGS.add_flipped_images)

    raw_predictions = tf.identity(
        tf.cast(predictions[common.OUTPUT_TYPE], tf.float32),
        _RAW_OUTPUT_NAME)
    raw_probabilities = tf.identity(
        predictions[common.OUTPUT_TYPE + model.PROB_SUFFIX],
        _RAW_OUTPUT_PROB_NAME)

    # Crop the valid regions from the predictions.
    semantic_predictions = raw_predictions[
        :, :resized_image_size[0], :resized_image_size[1]]
    semantic_probabilities = raw_probabilities[
        :, :resized_image_size[0], :resized_image_size[1]]

    ################### INSTANCE CENTER ########################
    raw_instance_predictions = tf.identity(
        tf.cast(predictions[common.INSTANCE], tf.float32),
        _RAW_INSTANCE_CENTER_OUTPUT_NAME)
    raw_instance_probabilities = tf.identity(
        predictions[common.INSTANCE + model.PROB_SUFFIX],
        _RAW_INSTANCE_OUTPUT_PROB_NAME)


    # Crop the valid regions from the predictions.
    instance_predictions = raw_instance_predictions[
        :, :resized_image_size[0], :resized_image_size[1]]
    instance_probabilities = raw_instance_probabilities[
        :, :resized_image_size[0], :resized_image_size[1]]

    ####################### INSTANCE OFFSET ########################
    raw_offset_predictions = tf.identity(
        tf.cast(predictions[common.OFFSET], tf.float32),
        _RAW_INSTANCE_OFFSET_OUTPUT_NAME)
    raw_offset_probabilities = tf.identity(
        predictions[common.OFFSET + model.PROB_SUFFIX],
        _RAW_OFFSET_OUTPUT_PROB_NAME)

    # Crop the valid regions from the predictions.
    offset_predictions = raw_offset_predictions[
                           :, :resized_image_size[0], :resized_image_size[1], :]
    offset_probabilities = raw_offset_probabilities[
                             :, :resized_image_size[0], :resized_image_size[1], :]

    # Resize back the prediction to the original image size.
    def _resize_label(label, label_size):
      # Expand dimension of label to [1, height, width, 1] for resize operation.
      label = tf.expand_dims(label, 3)
      resized_label = tf.image.resize_images(
          label,
          label_size,
          method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
          align_corners=True)
      return tf.cast(tf.squeeze(resized_label, 3), tf.int32)

    # Resize back the prediction to the original image size.
    def _resize_label_instance(label, label_size):
      # Expand dimension of label to [1, height, width, 1] for resize operation.
      #label = tf.expand_dims(label, 3)
      resized_label = tf.image.resize_images(
          label,
          label_size,
          method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
          align_corners=True)
      return tf.cast(tf.squeeze(resized_label, 3), tf.float32)

    def _resize_label_offset(label, label_size):
      # Expand dimension of label to [1, height, width, 1] for resize operation.
      #label = tf.expand_dims(label, 3)
      resized_label = tf.image.resize_images(
          label,
          label_size,
          method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
          align_corners=True)
      return tf.cast(resized_label, tf.float32)

    ############################## SEMANTIC LABEL RESIZE ###################################


    print(tf.shape(semantic_predictions))
    semantic_predictions = _resize_label(semantic_predictions, image_size)
    semantic_predictions = tf.identity(semantic_predictions, name=_OUTPUT_NAME)

    semantic_probabilities = tf.image.resize_bilinear(
        semantic_probabilities, image_size, align_corners=True,
        name=_OUTPUT_PROB_NAME)

    ############################### INSTANCE CENTER RESIZE ####################################

    instance_predictions = _resize_label_instance(instance_predictions, image_size)
    instance_predictions = tf.identity(instance_predictions, name=_OUTPUT_NAME_INSTANCE_CENTER)

    instance_probabilities = tf.image.resize_bilinear(
        instance_probabilities, image_size, align_corners=True,
        name=_OUTPUT_INSTANCE_PROB_NAME)

    ################################ INSTANCE REGRESSION RESIZE ###################################

    offset_predictions = _resize_label_offset(offset_predictions, image_size)
    offset_predictions = tf.identity(offset_predictions, name=_OUTPUT_NAME_INSTANCE_OFFSET)

    instance_probabilities = tf.image.resize_bilinear(
        instance_probabilities, image_size, align_corners=True,
        name=_OUTPUT_OFFSET_PROB_NAME)




    if FLAGS.quantize_delay_step >= 0:
      contrib_quantize.create_eval_graph()

    saver = tf.train.Saver(tf.all_variables())

    dirname = os.path.dirname(FLAGS.export_path)
    tf.gfile.MakeDirs(dirname)
    graph_def = tf.get_default_graph().as_graph_def(add_shapes=True)
    freeze_graph.freeze_graph_with_def_protos(
        graph_def,
        saver.as_saver_def(),
        FLAGS.checkpoint_path,
        _OUTPUT_NAME + ',' + _OUTPUT_PROB_NAME +
        ',' + _OUTPUT_NAME_INSTANCE_CENTER +
        ',' + _OUTPUT_INSTANCE_PROB_NAME +
        ',' + _OUTPUT_NAME_INSTANCE_OFFSET +
        ',' + _OUTPUT_OFFSET_PROB_NAME,
        restore_op_name=None,
        filename_tensor_name=None,
        output_graph=FLAGS.export_path,
        clear_devices=True,
        initializer_nodes=None)

    if FLAGS.save_inference_graph:
      tf.train.write_graph(graph_def, dirname, 'inference_graph.pbtxt')