Beispiel #1
0
def main(unused_argv):
  tf.logging.set_verbosity(tf.logging.INFO)
  # Get dataset-dependent information.
  dataset = segmentation_dataset.get_dataset(
      FLAGS.dataset, FLAGS.vis_split, dataset_dir=FLAGS.dataset_dir)
  train_id_to_eval_id = None
  if dataset.name == segmentation_dataset.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)
  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)

  g = tf.Graph()
  with g.as_default():
    samples = input_generator.get(dataset,
                                  FLAGS.vis_crop_size,
                                  FLAGS.vis_batch_size,
                                  min_resize_value=FLAGS.min_resize_value,
                                  max_resize_value=FLAGS.max_resize_value,
                                  resize_factor=FLAGS.resize_factor,
                                  dataset_split=FLAGS.vis_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.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.')
      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]

    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 reisze the predictions back.
      original_image = tf.squeeze(samples[common.ORIGINAL_IMAGE])
      original_image_shape = tf.shape(original_image)
      predictions = tf.slice(
          predictions,
          [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 = tf.squeeze(
          tf.image.resize_images(tf.expand_dims(predictions, 3),
                                 resized_shape,
                                 method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
                                 align_corners=True), 3)

    tf.train.get_or_create_global_step()
    saver = tf.train.Saver(slim.get_variables_to_restore())
    sv = tf.train.Supervisor(graph=g,
                             logdir=FLAGS.vis_logdir,
                             init_op=tf.global_variables_initializer(),
                             summary_op=None,
                             summary_writer=None,
                             global_step=None,
                             saver=saver)
    num_batches = int(math.ceil(
        dataset.num_samples / float(FLAGS.vis_batch_size)))
    last_checkpoint = None

    # Loop to visualize the results when new checkpoint is created.
    num_iters = 0
    while (FLAGS.max_number_of_iterations <= 0 or
           num_iters < FLAGS.max_number_of_iterations):
      num_iters += 1
      last_checkpoint = slim.evaluation.wait_for_new_checkpoint(
          FLAGS.checkpoint_dir, last_checkpoint)
      start = time.time()
      tf.logging.info(
          'Starting visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S',
                                                       time.gmtime()))
      tf.logging.info('Visualizing with model %s', last_checkpoint)

      with sv.managed_session(FLAGS.master,
                              start_standard_services=False) as sess:
        sv.start_queue_runners(sess)
        sv.saver.restore(sess, last_checkpoint)

        image_id_offset = 0
        for batch in range(num_batches):
          tf.logging.info('Visualizing batch %d / %d', batch + 1, num_batches)
          _process_batch(sess=sess,
                         original_images=samples[common.ORIGINAL_IMAGE],
                         semantic_predictions=predictions,
                         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,
                         raw_save_dir=raw_save_dir,
                         train_id_to_eval_id=train_id_to_eval_id)
          image_id_offset += FLAGS.vis_batch_size

      tf.logging.info(
          'Finished visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S',
                                                       time.gmtime()))
      time_to_next_eval = start + FLAGS.eval_interval_secs - time.time()
      if time_to_next_eval > 0:
        time.sleep(time_to_next_eval)
Beispiel #2
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    # Get dataset-dependent information.
    dataset = segmentation_dataset.get_dataset(FLAGS.dataset,
                                               FLAGS.vis_split,
                                               dataset_dir=FLAGS.dataset_dir)
    train_id_to_eval_id = None
    if dataset.name == segmentation_dataset.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)
    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)

    g = tf.Graph()
    with g.as_default():
        samples = input_generator.get(dataset,
                                      FLAGS.vis_crop_size,
                                      FLAGS.vis_batch_size,
                                      min_resize_value=FLAGS.min_resize_value,
                                      max_resize_value=FLAGS.max_resize_value,
                                      resize_factor=FLAGS.resize_factor,
                                      dataset_split=FLAGS.vis_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.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.')
            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]

        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 reisze the predictions back.
            original_image = tf.squeeze(samples[common.ORIGINAL_IMAGE])
            original_image_shape = tf.shape(original_image)
            predictions = tf.slice(
                predictions, [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 = tf.squeeze(
                tf.image.resize_images(
                    tf.expand_dims(predictions, 3),
                    resized_shape,
                    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
                    align_corners=True), 3)

        if FLAGS.is_quant:
            tf.contrib.quantize.experimental_create_eval_graph(
                weight_bits=FLAGS.weight_bits,
                activation_bits=FLAGS.activation_bits)

        tf.train.get_or_create_global_step()
        saver = tf.train.Saver(slim.get_variables_to_restore())
        sv = tf.train.Supervisor(graph=g,
                                 logdir=FLAGS.vis_logdir,
                                 init_op=tf.global_variables_initializer(),
                                 summary_op=None,
                                 summary_writer=None,
                                 global_step=None,
                                 saver=saver)
        num_batches = int(
            math.ceil(dataset.num_samples / float(FLAGS.vis_batch_size)))
        last_checkpoint = None

        # Loop to visualize the results when new checkpoint is created.
        num_iters = 0
        while (FLAGS.max_number_of_iterations <= 0
               or num_iters < FLAGS.max_number_of_iterations):
            num_iters += 1
            last_checkpoint = slim.evaluation.wait_for_new_checkpoint(
                FLAGS.checkpoint_dir, last_checkpoint)
            start = time.time()
            tf.logging.info('Starting visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))
            tf.logging.info('Visualizing with model %s', last_checkpoint)

            with sv.managed_session(FLAGS.master,
                                    start_standard_services=False) as sess:
                sv.start_queue_runners(sess)
                sv.saver.restore(sess, last_checkpoint)

                image_id_offset = 0
                for batch in range(num_batches):
                    tf.logging.info('Visualizing batch %d / %d', batch + 1,
                                    num_batches)
                    _process_batch(
                        sess=sess,
                        original_images=samples[common.ORIGINAL_IMAGE],
                        semantic_predictions=predictions,
                        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,
                        raw_save_dir=raw_save_dir,
                        train_id_to_eval_id=train_id_to_eval_id)
                    image_id_offset += FLAGS.vis_batch_size

            tf.logging.info('Finished visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))
            time_to_next_eval = start + FLAGS.eval_interval_secs - time.time()
            if time_to_next_eval > 0:
                time.sleep(time_to_next_eval)
Beispiel #3
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    # Get dataset-dependent information.
    dataset = segmentation_dataset.get_dataset(
        FLAGS.dataset,
        FLAGS.vis_split,
        dataset_dir=FLAGS.dataset_dir,
        use_input_hints=FLAGS.input_hints,
        hint_types=FLAGS.hint_types)

    train_id_to_eval_id = None
    if dataset.name == segmentation_dataset.get_cityscapes_dataset_name(
    ) and FLAGS.convert_to_eval_id:
        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)
    raw_save_dir = os.path.join(FLAGS.vis_logdir,
                                _RAW_SEMANTIC_PREDICTION_SAVE_FOLDER)
    tf.gfile.MakeDirs(raw_save_dir)

    logit_save_dir = os.path.join(FLAGS.vis_logdir, 'logits')
    tf.gfile.MakeDirs(logit_save_dir)

    uncertainty_save_dir = os.path.join(FLAGS.vis_logdir, 'uncertainties')
    tf.gfile.MakeDirs(uncertainty_save_dir)

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

    g = tf.Graph()
    with g.as_default():
        # Running samples_orig will grab a new batch
        samples_orig = input_generator.get(
            dataset,
            FLAGS.vis_crop_size,
            FLAGS.vis_batch_size,
            min_resize_value=FLAGS.min_resize_value,
            max_resize_value=FLAGS.max_resize_value,
            resize_factor=FLAGS.resize_factor,
            dataset_split=FLAGS.vis_split,
            is_training=False,
            model_variant=FLAGS.model_variant)
        # samples_placeholders will represent a batch of data for the network. The values will be filled
        # by samples_orig. Decoupled so that same batch can be run through network multiple times.
        # See _process_batch.
        samples_placeholders = {}
        for k, v in samples_orig.items():
            samples_placeholders[k] = tf.placeholder(
                dtype=v.dtype, shape=v.shape, name='samples_{}'.format(k))

        # Since original code was written with 'samples' variable, leave original code alone and initialize samples dictionary here
        # The reason we don't use samples = samples_placeholders is because samples is overwritten several times
        # and we need to keep samples_placeholders in its original state in order to fill it with values from samples_orig.
        samples = {k: v for k, v in samples_placeholders.items()}

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

        if FLAGS.input_hints:  # or if common.HINT in samples.keys():
            if 'dynamic_class_partial_boundary_hint' in FLAGS.hint_types:
                assert len(
                    FLAGS.hint_types
                ) == 1, 'When using dynamic partial boundary class hints, do not use other hint types!'
                print("----")
                print(
                    "eval.py: Partial boundary hints with grid {}x{}.".format(
                        FLAGS.dynamic_class_partial_boundary_hint_B,
                        FLAGS.dynamic_class_partial_boundary_hint_B))
                print("eval.py: Drawing blocks with p {}.".format(
                    FLAGS.dynamic_class_partial_boundary_hint_p))
                if FLAGS.dynamic_class_partial_boundary_full_block:
                    print(
                        "eval.py: Keeping entire block instead of masking boundaries."
                        .format(FLAGS.boundary_threshold))
                else:
                    print(
                        "eval.py: Masking with boundary threshold {}.".format(
                            FLAGS.boundary_threshold))

                print("----")

                if FLAGS.dynamic_class_partial_boundary_full_block:
                    boundary_mask = tf.cast(
                        tf.ones_like(samples[common.LABEL]), tf.uint8)
                else:
                    boundary_mask = tf.cast(
                        tf.less(samples[common.BOUNDARY_DMAP],
                                FLAGS.boundary_threshold), tf.uint8)

                class_hints, hinted = tf.py_func(
                    func=train_utils.generate_class_partial_boundaries_helper(
                        B=FLAGS.dynamic_class_partial_boundary_hint_B,
                        p=FLAGS.dynamic_class_partial_boundary_hint_p),
                    inp=[samples[common.LABEL], boundary_mask],
                    Tout=[tf.uint8, tf.bool])
                samples[common.HINT] = class_hints
                samples[common.HINT].set_shape(
                    samples[common.LABEL].get_shape().as_list())
                # Now preprocess this. Set the flag so that  the rest of the work will be done as usual.
                FLAGS.hint_types = ['class_hint']
            ###

            if 'dynamic_class_hint' in FLAGS.hint_types:
                assert len(
                    FLAGS.hint_types
                ) == 1, 'When using dynamic class hints, do not use other hint types!'
                print("----")
                print(
                    "WARNING: Do not use dynamic class hints when simulating crowdsourced points as the points should not change between runs."
                )
                print("vis.py: Drawing hints with geo mean {}.".format(
                    FLAGS.dynamic_class_hint_geo_mean))
                print("vis.py: Masking with boundary threshold {}.".format(
                    FLAGS.boundary_threshold))
                print("----")
                boundary_mask = tf.cast(
                    tf.less(samples[common.BOUNDARY_DMAP],
                            FLAGS.boundary_threshold), tf.uint8)
                class_hints, hinted = tf.py_func(
                    func=train_utils.generate_class_clicks_helper(
                        geo_mean=FLAGS.dynamic_class_hint_geo_mean),
                    inp=[samples[common.LABEL], boundary_mask],
                    Tout=[tf.uint8, tf.bool])
                samples[common.HINT] = class_hints
                samples[common.HINT].set_shape(
                    samples[common.LABEL].get_shape().as_list())
                # Now preprocess this. Set the flag so that  the rest of the work will be done as usual.
                FLAGS.hint_types = ['class_hint']

            # If using class hints, preprocess into num_class binary mask channels
            if 'class_hint' in FLAGS.hint_types:
                assert len(
                    FLAGS.hint_types
                ) == 1, 'When using class hints, do not use other hint types!'
                num_classes = dataset.num_classes
                print('vis.py: num classes is {}'.format(num_classes))
                class_hint_channels_list = []
                for label in range(num_classes):
                    # Multiply by 255 is to bring into same range as image pixels...,
                    # and so feature_extractor mean subtraction will reduce it back to 0,1 range
                    class_hint_channel = tf.to_float(
                        tf.equal(samples[common.HINT], label)) * 255
                    class_hint_channels_list.append(class_hint_channel)
                class_hint_channels = tf.concat(class_hint_channels_list,
                                                axis=-1)
                samples[common.HINT] = class_hint_channels

            # Get hints and concat to image as input into network
            samples[common.HINT] = tf.identity(samples[common.HINT],
                                               name=common.HINT)
            model_inputs = tf.concat(
                [samples[common.IMAGE],
                 tf.to_float(samples[common.HINT])],
                axis=-1)
        else:
            # Just image is input into network
            model_inputs = samples[common.IMAGE]

        outputs_to_scales_to_logits = None
        logits = None
        predictions = None
        fixed_features = None
        extra_to_run = {}
        if tuple(FLAGS.eval_scales) == (1.0, ):
            tf.logging.info('Performing single-scale test.')
            if FLAGS.compute_uncertainty and FLAGS.force_dropout_only_branch:
                fixed_features = model._get_features_after_decoder(
                    images=model_inputs,
                    model_options=model_options,
                    reuse=None,
                    is_training=False,
                    fine_tune_batch_norm=False,
                    force_dropout=True,
                    force_dropout_only_branch=FLAGS.force_dropout_only_branch,
                    keep_prob=FLAGS.keep_prob)

                samples_placeholders['fixed_features'] = tf.placeholder(
                    dtype=fixed_features.dtype, shape=fixed_features.shape)
                logits_from_fixed_features = model._get_branch_logits(
                    samples_placeholders['fixed_features'],
                    model_options.outputs_to_num_classes[common.OUTPUT_TYPE],
                    model_options.atrous_rates,
                    aspp_with_batch_norm=model_options.aspp_with_batch_norm,
                    kernel_size=model_options.logits_kernel_size,
                    reuse=None,
                    scope_suffix=common.OUTPUT_TYPE,
                    keep_prob=FLAGS.keep_prob,
                    force_dropout=True)
                logits_from_fixed_features = tf.image.resize_bilinear(
                    logits_from_fixed_features,
                    size=tf.shape(samples[common.IMAGE])[1:3],
                    align_corners=True)

                softmax_from_fixed_features = tf.nn.softmax(
                    logits_from_fixed_features)

                samples_placeholders['accumulated_softmax'] = tf.placeholder(
                    dtype=softmax_from_fixed_features.dtype,
                    shape=FLAGS.vis_placeholder_size)
                #shape=[1, 1025, 2049, 19])
                #shape=[1, 513, 513, 23])
                samples_placeholders[
                    'accumulated_softmax_sq'] = tf.placeholder(
                        dtype=softmax_from_fixed_features.dtype,
                        shape=FLAGS.vis_placeholder_size)
                #shape=[1, 1025, 2049, 19])
                #shape=[1, 513, 513, 23])

                accumulated_softmax = samples_placeholders[
                    'accumulated_softmax'] + softmax_from_fixed_features
                accumulated_softmax_sq = samples_placeholders[
                    'accumulated_softmax_sq'] + tf.square(
                        softmax_from_fixed_features)
                extra_to_run['accumulated_softmax'] = accumulated_softmax
                extra_to_run['accumulated_softmax_sq'] = accumulated_softmax_sq

            elif FLAGS.save_logits or FLAGS.compute_uncertainty:
                predictions, outputs_to_scales_to_logits = model.predict_labels(
                    # samples[common.IMAGE],
                    model_inputs,
                    model_options=model_options,
                    image_pyramid=FLAGS.image_pyramid,
                    also_return_logits=True,
                    force_dropout=(FLAGS.compute_uncertainty
                                   or FLAGS.force_dropout),
                    force_dropout_only_branch=FLAGS.force_dropout_only_branch,
                    keep_prob=FLAGS.keep_prob)

                assert tuple(FLAGS.eval_scales) == (1.0, )
                assert len(outputs_to_scales_to_logits) == 1
                for output in sorted(outputs_to_scales_to_logits):
                    scales_to_logits = outputs_to_scales_to_logits[output]
                    logits = scales_to_logits[model._MERGED_LOGITS_SCOPE]

                if FLAGS.compute_uncertainty:
                    assert not FLAGS.save_logits
                    # We need full size logits to compute final predition and uncertainty.
                    logits = tf.image.resize_bilinear(
                        logits,
                        size=tf.shape(model_inputs)[1:3],
                        align_corners=True)

                    softmax_logits = tf.nn.softmax(logits)

                    samples_placeholders[
                        'accumulated_softmax'] = tf.placeholder(
                            dtype=softmax_logits.dtype,
                            shape=FLAGS.vis_placeholder_size)
                    #shape=[1, 1025, 2049, 19])
                    #shape=[1, 513, 513, 23])
                    samples_placeholders[
                        'accumulated_softmax_sq'] = tf.placeholder(
                            dtype=softmax_logits.dtype,
                            shape=FLAGS.vis_placeholder_size)
                    #shape=[1, 1025, 2049, 19])
                    #shape=[1, 513, 513, 23])

                    accumulated_softmax = samples_placeholders[
                        'accumulated_softmax'] + softmax_logits
                    accumulated_softmax_sq = samples_placeholders[
                        'accumulated_softmax_sq'] + tf.square(softmax_logits)
                    extra_to_run['accumulated_softmax'] = accumulated_softmax
                    extra_to_run[
                        'accumulated_softmax_sq'] = accumulated_softmax_sq
            else:
                predictions = model.predict_labels(
                    # samples[common.IMAGE],
                    model_inputs,
                    model_options=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_inputs,
                model_options=model_options,
                eval_scales=FLAGS.eval_scales,
                add_flipped_images=FLAGS.add_flipped_images)
            if FLAGS.save_logits:
                raise NotImplementedError("Multiscale logits aren't saved")

        if predictions is not None:
            predictions = predictions[common.OUTPUT_TYPE]

        if FLAGS.min_resize_value and FLAGS.max_resize_value:
            if FLAGS.input_hints:
                #raise Exception("***Unclear if this will work with hints. Look over the code.")
                print(
                    "***Unclear if this will work with hints. Look over the code."
                )
            # 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 reisze the predictions back.
            original_image = tf.squeeze(samples[common.ORIGINAL_IMAGE])
            original_image_shape = tf.shape(original_image)
            predictions = tf.slice(
                predictions, [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 = tf.squeeze(
                tf.image.resize_images(
                    tf.expand_dims(predictions, 3),
                    resized_shape,
                    method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
                    align_corners=True), 3)

        tf.train.get_or_create_global_step()
        saver = tf.train.Saver(slim.get_variables_to_restore())
        sv = tf.train.Supervisor(graph=g,
                                 logdir=FLAGS.vis_logdir,
                                 init_op=tf.global_variables_initializer(),
                                 summary_op=None,
                                 summary_writer=None,
                                 global_step=None,
                                 saver=saver)

        if FLAGS.vis_num_batches <= 0:
            num_batches = int(
                math.ceil(dataset.num_samples / float(FLAGS.vis_batch_size)))
        else:
            num_batches = FLAGS.vis_num_batches

            if FLAGS.shuffle:
                shuffled_idxs = range(dataset.num_samples)
                np.random.seed(FLAGS.shuffle_seed)
                np.random.shuffle(shuffled_idxs)
            else:
                shuffled_idxs = range(dataset.num_samples)
            idxs_to_keep = shuffled_idxs[FLAGS.start_idx:FLAGS.start_idx +
                                         FLAGS.vis_num_batches]

            if FLAGS.also_vis_first_N > 0:
                idxs_to_keep.extend(shuffled_idxs[0:FLAGS.also_vis_first_N])

            print(sorted(idxs_to_keep)[:10])
            print("There are {} indices to keep.".format(len(idxs_to_keep)))

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

        last_checkpoint = None

        # Loop to visualize the results when new checkpoint is created.
        num_iters = 0
        while (FLAGS.max_number_of_iterations <= 0
               or num_iters < FLAGS.max_number_of_iterations):
            num_iters += 1
            last_checkpoint = slim.evaluation.wait_for_new_checkpoint(
                FLAGS.checkpoint_dir, last_checkpoint)
            start = time.time()
            tf.logging.info('Starting visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))
            tf.logging.info('Visualizing with model %s', last_checkpoint)

            with sv.managed_session(FLAGS.master,
                                    start_standard_services=False) as sess:
                sv.start_queue_runners(sess)
                sv.saver.restore(sess, last_checkpoint)

                image_id_offset = 0
                for batch in range(num_batches):

                    if batch in idxs_to_keep:
                        tf.logging.info('Visualizing batch %d / %d', batch + 1,
                                        num_batches)
                        _process_batch(
                            sess=sess,
                            original_images=samples[common.ORIGINAL_IMAGE],
                            semantic_predictions=predictions,
                            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,
                            raw_save_dir=raw_save_dir,
                            save_logits=FLAGS.save_logits,
                            logits=logits,
                            fixed_features=fixed_features,
                            extra_to_run=extra_to_run,
                            logit_save_dir=logit_save_dir,
                            uncertainty_save_dir=uncertainty_save_dir,
                            train_id_to_eval_id=train_id_to_eval_id,
                            samples_orig=samples_orig,
                            samples_placeholders=samples_placeholders,
                            compute_uncertainty=FLAGS.compute_uncertainty,
                            num_forward_passes=FLAGS.
                            compute_uncertainty_iterations)
                    else:
                        # Run batch generator to skip this batch
                        sess.run([samples_orig])
                    image_id_offset += FLAGS.vis_batch_size

            tf.logging.info('Finished visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))
            time_to_next_eval = start + FLAGS.eval_interval_secs - time.time()
            if time_to_next_eval > 0 and num_iters < FLAGS.max_number_of_iterations:
                time.sleep(time_to_next_eval)
Beispiel #4
0
def main(unused_argv):
  tf.logging.set_verbosity(tf.logging.INFO)
  # Get dataset-dependent information.
  dataset = segmentation_dataset.get_dataset(
      FLAGS.dataset, FLAGS.vis_split, dataset_dir=FLAGS.dataset_dir)
  train_id_to_eval_id = None

  if dataset.name == segmentation_dataset.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

  global filename_list
  global perf_all
  filename_list = open(os.path.join(FLAGS.dataset_dir, '../ReOrg/filename-val.txt'), encoding='utf-8').readlines()
  filename_list = [x.strip() for x in filename_list]
  perf_all = np.empty((len(filename_list), 3))

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

  g = tf.Graph()
  with g.as_default():
    samples = input_generator.get(dataset,
                                  FLAGS.vis_crop_size,
                                  FLAGS.vis_batch_size,
                                  min_resize_value=FLAGS.min_resize_value,
                                  max_resize_value=FLAGS.max_resize_value,
                                  resize_factor=FLAGS.resize_factor,
                                  dataset_split=FLAGS.vis_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.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.')
      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]
    pred_compute = tf.reshape(predictions, shape=[-1])
    label_compute = tf.reshape(samples[common.LABEL], shape=[-1])
    weights = tf.to_float(tf.not_equal(label_compute, 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.
    label_compute = tf.where(
            tf.equal(label_compute, dataset.ignore_label), tf.zeros_like(label_compute), label_compute)

    m_iou, m_accu = compute_m_iou_accu(
            pred_compute, label_compute, dataset.num_classes, weights=weights)

    # m_iou = compute_miou(
    #         pred_compute, label_compute, dataset.num_classes, weights=weights)

    # m_accu = compute_maccu(
    #         pred_compute, label_compute, dataset.num_classes, weights=weights)

    accu = compute_accu(
            pred_compute, label_compute, weights=weights)

    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 reisze the predictions back.
      original_image = tf.squeeze(samples[common.ORIGINAL_IMAGE])
      original_image_shape = tf.shape(original_image)
      predictions = tf.slice(
          predictions,
          [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 = tf.squeeze(
          tf.image.resize_images(tf.expand_dims(predictions, 3),
                                 resized_shape,
                                 method=tf.image.ResizeMethod.NEAREST_NEIGHBOR,
                                 align_corners=True), 3)

    tf.train.get_or_create_global_step()
    saver = tf.train.Saver(slim.get_variables_to_restore())
    sv = tf.train.Supervisor(graph=g,
                             logdir=FLAGS.vis_logdir,
                             init_op=tf.global_variables_initializer(),
                             summary_op=None,
                             summary_writer=None,
                             global_step=None,
                             saver=saver)
    num_batches = int(math.ceil(
        dataset.num_samples / float(FLAGS.vis_batch_size)))

    last_checkpoint = FLAGS.checkpoint_dir

    start = time.time()
    tf.logging.info(
        'Starting visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S',
                                                    time.gmtime()))
    tf.logging.info('Visualizing with model %s', last_checkpoint)

    with sv.managed_session(FLAGS.master,
                            start_standard_services=False) as sess:
        sv.start_queue_runners(sess)
        sv.saver.restore(sess, last_checkpoint)

        image_id_offset = 1
        for batch in range(num_batches):
            tf.logging.info('Visualizing batch %d / %d', batch + 1, num_batches)
            _process_batch(sess=sess,
                            original_images=samples[common.ORIGINAL_IMAGE],
                            labels=samples[common.LABEL],
                            semantic_predictions=predictions,
                            image_names=samples[common.IMAGE_NAME],
                            image_heights=samples[common.HEIGHT],
                            image_widths=samples[common.WIDTH],
                            perf_metrics=[m_iou, m_accu, accu],
                            image_id_offset=image_id_offset,
                            save_dir=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

        np.savetxt(os.path.join(FLAGS.vis_logdir, 'per-image-metrics.txt'), perf_all)

        tf.logging.info(
            'Finished visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S',
                                                        time.gmtime()))
Beispiel #5
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    # Get dataset-dependent information.
    dataset = segmentation_dataset.get_dataset(FLAGS.dataset,
                                               FLAGS.vis_split,
                                               dataset_dir=FLAGS.dataset_dir)
    train_id_to_eval_id = None
    if dataset.name == segmentation_dataset.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)
    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)

    g = tf.Graph()
    with g.as_default():
        image, label, image_name, height, width = input_generator.get_(
            dataset,
            FLAGS.vis_crop_size,
            FLAGS.vis_batch_size,
            min_resize_value=FLAGS.min_resize_value,
            max_resize_value=FLAGS.max_resize_value,
            resize_factor=FLAGS.resize_factor,
            dataset_split=FLAGS.vis_split,
            is_training=False,
            model_variant="mobilenet_v2")

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

        mask = tf.squeeze(label)
        #     original_images=tf.squeeze(samples[common.ORIGINAL_IMAGE])
        #     images=tf.squeeze(samples[common.IMAGE])

        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

        tf.train.get_or_create_global_step()
        saver = tf.train.Saver(slim.get_variables_to_restore())
        sv = tf.train.Supervisor(graph=g,
                                 logdir=FLAGS.vis_logdir,
                                 init_op=tf.global_variables_initializer(),
                                 summary_op=None,
                                 summary_writer=None,
                                 global_step=None,
                                 saver=saver)
        num_batches = int(
            math.ceil(dataset.num_samples / float(FLAGS.vis_batch_size)))

        # Loop to visualize the results when new checkpoint is created.
        num_iters = 0
        while (FLAGS.max_number_of_iterations <= 0
               or num_iters < FLAGS.max_number_of_iterations):
            num_iters += 1

            start = time.time()
            tf.logging.info('Starting visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))

            with sv.managed_session(FLAGS.master,
                                    start_standard_services=False) as sess:
                sv.start_queue_runners(sess)

                image_id_offset = 0
                for batch in range(num_batches):
                    tf.logging.info('Visualizing batch %d / %d', batch + 1,
                                    num_batches)
                    _process_batch(sess=sess,
                                   resize_images=image,
                                   image_names=image_name,
                                   mask=mask,
                                   image_heights=height,
                                   image_widths=width,
                                   image_id_offset=image_id_offset,
                                   save_dir=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

            tf.logging.info('Finished visualization at ' +
                            time.strftime('%Y-%m-%d-%H:%M:%S', time.gmtime()))
            time_to_next_eval = start + FLAGS.eval_interval_secs - time.time()
            if time_to_next_eval > 0:
                time.sleep(time_to_next_eval)