Ejemplo n.º 1
0
  def testForwardpassDeepLabv3plus(self):
    crop_size = [33, 33]
    outputs_to_num_classes = {'semantic': 3}

    model_options = common.ModelOptions(
        outputs_to_num_classes,
        crop_size,
        output_stride=16
    )._replace(
        add_image_level_feature=True,
        aspp_with_batch_norm=True,
        logits_kernel_size=1,
        model_variant='mobilenet_v2')  # Employ MobileNetv2 for fast test.

    g = tf.Graph()
    with g.as_default():
      with self.test_session(graph=g) as sess:
        inputs = tf.random_uniform(
            (1, crop_size[0], crop_size[1], 3))
        outputs_to_scales_to_logits = model.multi_scale_logits(
            inputs,
            model_options,
            image_pyramid=[1.0])

        sess.run(tf.global_variables_initializer())
        outputs_to_scales_to_logits = sess.run(outputs_to_scales_to_logits)

        # Check computed results for each output type.
        for output in outputs_to_num_classes:
          scales_to_logits = outputs_to_scales_to_logits[output]
          # Expect only one output.
          self.assertEquals(len(scales_to_logits), 1)
          for logits in scales_to_logits.values():
            self.assertTrue(logits.any())
Ejemplo n.º 2
0
 def testBuildDeepLabWithDensePredictionCell(self):
   batch_size = 1
   crop_size = [33, 33]
   outputs_to_num_classes = {'semantic': 2}
   expected_endpoints = ['merged_logits']
   dense_prediction_cell_config = [
       {'kernel': 3, 'rate': [1, 6], 'op': 'conv', 'input': -1},
       {'kernel': 3, 'rate': [18, 15], 'op': 'conv', 'input': 0},
   ]
   model_options = common.ModelOptions(
       outputs_to_num_classes,
       crop_size,
       output_stride=16)._replace(
           aspp_with_batch_norm=True,
           model_variant='mobilenet_v2',
           dense_prediction_cell_config=dense_prediction_cell_config)
   g = tf.Graph()
   with g.as_default():
     with self.test_session(graph=g):
       inputs = tf.random_uniform(
           (batch_size, crop_size[0], crop_size[1], 3))
       outputs_to_scales_to_model_results = model.multi_scale_logits(
           inputs,
           model_options,
           image_pyramid=[1.0])
       for output in outputs_to_num_classes:
         scales_to_model_results = outputs_to_scales_to_model_results[output]
         self.assertListEqual(
             list(scales_to_model_results), expected_endpoints)
         self.assertEqual(len(scales_to_model_results), 1)
Ejemplo n.º 3
0
def deeplab(
        num_classes,
        crop_size=[513, 513],
        atrous_rates=[6, 12, 18],
        output_stride=16,
        fine_tune_batch_norm=False,
        pretrained=True,
        pretained_num_classes=21,
        checkpoint_path='./pretrained_models/deeplabv3_pascal_trainval.pth'):
    """DeepLab v3+ for semantic segmentation."""
    outputs_to_num_classes = {'semantic': num_classes}
    model_options = common.ModelOptions(outputs_to_num_classes,
                                        crop_size=crop_size,
                                        atrous_rates=atrous_rates,
                                        output_stride=output_stride)
    feature_extractor = extractor.feature_extractor(
        model_options.model_variant,
        pretrained=False,
        output_stride=model_options.output_stride)
    model = DeepLab(feature_extractor, model_options, fine_tune_batch_norm)

    if pretrained:
        _load_state_dict(model, num_classes, pretained_num_classes,
                         checkpoint_path)
    return model
Ejemplo n.º 4
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},
            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.')
            predictions = model.predict_labels_multi_scale(
                image,
                model_options=model_options,
                eval_scales=FLAGS.inference_scales,
                add_flipped_images=FLAGS.add_flipped_images)

        # Crop the valid regions from the predictions.
        semantic_predictions = tf.slice(
            predictions[common.OUTPUT_TYPE], [0, 0, 0],
            [1, 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.squeeze(resized_label, 3)

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

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

        tf.gfile.MakeDirs(os.path.dirname(FLAGS.export_path))
        freeze_graph.freeze_graph_with_def_protos(
            tf.get_default_graph().as_graph_def(add_shapes=True),
            saver.as_saver_def(),
            FLAGS.checkpoint_path,
            _OUTPUT_NAME,
            restore_op_name=None,
            filename_tensor_name=None,
            output_graph=FLAGS.export_path,
            clear_devices=True,
            initializer_nodes=None)
Ejemplo n.º 5
0
def _build_deeplab(inputs, outputs_to_num_classes, train_crop_size,
                   resize_size, is_training):

    model_options = common.ModelOptions(
        outputs_to_num_classes=outputs_to_num_classes,
        crop_size=train_crop_size,
        atrous_rates=FLAGS.atrous_rates,
        output_stride=FLAGS.output_stride)

    outputs_to_scales_to_logits = model.multi_scale_logits(
        inputs,
        model_options=model_options,
        image_pyramid=FLAGS.image_pyramid,
        weight_decay=FLAGS.weight_decay,
        is_training=True,
        fine_tune_batch_norm=FLAGS.fine_tune_batch_norm)

    outputs_to_scales_to_logits[common.OUTPUT_TYPE][
        model._MERGED_LOGITS_SCOPE] = tf.identity(outputs_to_scales_to_logits[
            common.OUTPUT_TYPE][model._MERGED_LOGITS_SCOPE],
                                                  name=common.OUTPUT_TYPE)

    logits = outputs_to_scales_to_logits[common.OUTPUT_TYPE][
        model._MERGED_LOGITS_SCOPE]

    logits = tf.image.resize_bilinear(logits, (resize_size, 1),
                                      align_corners=True)

    return logits
Ejemplo n.º 6
0
 def testOutputsToNumClasses(self):
     num_classes = 21
     model_options = common.ModelOptions(
         outputs_to_num_classes={common.OUTPUT_TYPE: num_classes})
     self.assertEqual(
         model_options.outputs_to_num_classes[common.OUTPUT_TYPE],
         num_classes)
Ejemplo n.º 7
0
def _build_model(inputs_queue, clone_batch_size):
    """Builds a clone of train model.

  Args:
    inputs_queue: A prefetch queue for images and labels.
  Returns:
    A dictionary of logits names to logits.
  """
    samples = inputs_queue.dequeue()
    batch_size = clone_batch_size * FLAGS.num_classes
    inputs = tf.identity(samples['image'], name='image')
    labels = tf.identity(samples['label'], name='label')
    model_options = common.ModelOptions(output_stride=FLAGS.output_stride)
    net, end_points = model.get_features(
        inputs,
        model_options=model_options,
        weight_decay=FLAGS.weight_decay,
        is_training=True,
        fine_tune_batch_norm=FLAGS.fine_tune_batch_norm)
    logits, _ = model.classification(net,
                                     end_points,
                                     num_classes=FLAGS.num_classes,
                                     is_training=True)
    if FLAGS.multi_label:
        with tf.name_scope('Multilabel_logits'):
            logits = slim.softmax(logits)
            half_batch_size = batch_size / 2
            for i in range(1, FLAGS.num_classes):
                class_logits = tf.identity(logits[:, i],
                                           name='class_logits_%02d' % (i))
                class_labels = tf.identity(labels[:, i],
                                           name='class_labels_%02d' % (i))
                num_positive = tf.reduce_sum(class_labels)
                num_negative = batch_size - num_positive
                weights = tf.where(
                    tf.equal(class_labels, 1.0),
                    tf.tile([half_batch_size / num_positive], [batch_size]),
                    tf.tile([half_batch_size / num_negative], [batch_size]))
                train_utils.focal_loss(class_labels,
                                       class_logits,
                                       weights=weights,
                                       scope='class_loss_%02d' % (i))
    else:
        logits = slim.softmax(logits)
        train_utils.focal_loss(labels, logits, scope='cls_loss')

    if (FLAGS.dataset == 'protein') and FLAGS.add_counts_logits:
        counts = tf.identity(samples['counts'] - 1, name='counts')
        one_hot_counts = slim.one_hot_encoding(counts, 5)
        counts_logits, _ = model.classification(net,
                                                end_points,
                                                num_classes=5,
                                                is_training=True,
                                                scope='Counts_logits')
        counts_logits = slim.softmax(counts_logits)
        train_utils.focal_loss(one_hot_counts,
                               counts_logits,
                               scope='counts_loss')
        return logits, counts_logits
    return logits
Ejemplo n.º 8
0
def _build_deeplab(iterator, outputs_to_num_classes, ignore_label):
    """Builds a clone of DeepLab.
    Args:
      iterator: An iterator of type tf.data.Iterator for images and labels.
      outputs_to_num_classes: A map from output type to the number of classes. For
        example, for the task of semantic segmentation with 21 semantic classes,
        we would have outputs_to_num_classes['semantic'] = 21.
      ignore_label: Ignore label.
    """
    samples = iterator.get_next()

    # Add name to input and label nodes so we can add to summary.
    #samples[common.IMAGE].set_shape([FLAGS.train_batch_size, FLAGS.train_crop_size[0], FLAGS.train_crop_size[1], 3])
    samples[common.IMAGE] = tf.identity(samples[common.IMAGE],
                                        name=common.IMAGE)
    samples[common.LABEL] = tf.identity(samples[common.LABEL],
                                        name=common.LABEL)

    model_options = common.ModelOptions(
        outputs_to_num_classes=outputs_to_num_classes,
        crop_size=[int(sz) for sz in FLAGS.train_crop_size],
        atrous_rates=FLAGS.atrous_rates,
        output_stride=FLAGS.output_stride,
    )

    outputs_to_scales_to_logits = model.multi_scale_logits(
        samples[common.IMAGE],
        model_options=model_options,
        image_pyramid=FLAGS.image_pyramid,
        skips=FLAGS.skips,
        weight_decay=FLAGS.weight_decay,
        is_training=True,
        fine_tune_batch_norm=FLAGS.fine_tune_batch_norm,
        nas_training_hyper_parameters={
            'drop_path_keep_prob': FLAGS.drop_path_keep_prob,
            'total_training_steps': FLAGS.training_number_of_steps,
        })

    # Add name to graph node so we can add to summary.
    output_type_dict = outputs_to_scales_to_logits[common.OUTPUT_TYPE]
    output_type_dict[model.MERGED_LOGITS_SCOPE] = tf.identity(
        output_type_dict[model.MERGED_LOGITS_SCOPE], name=common.OUTPUT_TYPE)

    for output, num_classes in six.iteritems(outputs_to_num_classes):
        train_utils.add_softmax_cross_entropy_loss_for_each_scale(
            outputs_to_scales_to_logits[output],
            samples[common.LABEL],
            num_classes,
            ignore_label,
            loss_weight=1.0,
            upsample_logits=FLAGS.upsample_logits,
            hard_example_mining_step=FLAGS.hard_example_mining_step,
            top_k_percent_pixels=FLAGS.top_k_percent_pixels,
            scope=output)

        # Log the summary
        _log_summaries(samples[common.IMAGE], samples[common.LABEL],
                       num_classes,
                       output_type_dict[model.MERGED_LOGITS_SCOPE])
Ejemplo n.º 9
0
def _build_deeplab(inputs_queue, outputs_to_num_classes, ignore_label):
    """Builds a clone of DeepLab.

    Args:
      inputs_queue: A prefetch queue for images and labels.
      outputs_to_num_classes: A map from output type to the number of classes.
        For example, for the task of semantic segmentation with 21 semantic
        classes, we would have outputs_to_num_classes['semantic'] = 21.
      ignore_label: Ignore label.

    Returns:
      A map of maps from output_type (e.g., semantic prediction) to a
        dictionary of multi-scale logits names to logits. For each output_type,
        the dictionary has keys which correspond to the scales and values which
        correspond to the logits. For example, if `scales` equals [1.0, 1.5],
        then the keys would include 'merged_logits', 'logits_1.00' and
        'logits_1.50'.
    """
    samples = inputs_queue.dequeue()

    # Add name to input and label nodes so we can add to summary.
    samples[common.IMAGE] = tf.identity(samples[common.IMAGE],
                                        name=common.IMAGE)
    samples[common.LABEL] = tf.identity(samples[common.LABEL],
                                        name=common.LABEL)

    model_options = common.ModelOptions(
        outputs_to_num_classes=outputs_to_num_classes,
        crop_size=FLAGS.train_crop_size,
        atrous_rates=FLAGS.atrous_rates,
        output_stride=FLAGS.output_stride)
    outputs_to_scales_to_logits = model.multi_scale_logits(
        samples[common.IMAGE],
        model_options=model_options,
        image_pyramid=FLAGS.image_pyramid,
        weight_decay=FLAGS.weight_decay,
        is_training=True,
        fine_tune_batch_norm=FLAGS.fine_tune_batch_norm)

    # Add name to graph node so we can add to summary.
    output_type_dict = outputs_to_scales_to_logits[common.OUTPUT_TYPE]
    output_type_dict[model.MERGED_LOGITS_SCOPE] = tf.identity(
        output_type_dict[model.MERGED_LOGITS_SCOPE], name=common.OUTPUT_TYPE)

    for output, num_classes in six.iteritems(outputs_to_num_classes):
        loss_func = train_utils.add_softmax_cross_entropy_loss_for_each_scale
        if FLAGS.loss_function in 'lovasz':
            loss_func = train_utils.add_lovasz_softmax_loss_for_each_scale
        loss_func(outputs_to_scales_to_logits[output],
                  samples[common.LABEL],
                  num_classes,
                  ignore_label,
                  loss_weight=1.0,
                  upsample_logits=FLAGS.upsample_logits,
                  scope=output)

    return outputs_to_scales_to_logits
Ejemplo n.º 10
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():
        input_image = tf.placeholder(tf.float32,
                                     FLAGS.input_shape,
                                     name=_INPUT_NAME)
        inputs = data_augmentation.preprocess_image(input_image,
                                                    FLAGS.image_size,
                                                    FLAGS.image_size,
                                                    is_training=False)
        if FLAGS.channel:
            inputs = inputs[:, :, :FLAGS.channel]
            # inputs = inputs[:,:,3:]
        inputs = tf.expand_dims(inputs, 0)
        model_options = common.ModelOptions(output_stride=FLAGS.output_stride)
        net, end_points = model.get_features(inputs,
                                             model_options=model_options,
                                             is_training=False,
                                             fine_tune_batch_norm=False)

        if FLAGS.hierarchical_cls:
            end_points = model.hierarchical_classification(net,
                                                           end_points,
                                                           is_training=False)
        else:
            _, end_points = model.classification(net,
                                                 end_points,
                                                 num_classes=FLAGS.num_classes,
                                                 is_training=False)

        prediction = tf.identity(end_points['Logits_Predictions'],
                                 name=_OUTPUT_NAME)

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

        tf.gfile.MakeDirs(os.path.dirname(FLAGS.export_path))
        freeze_graph.freeze_graph_with_def_protos(
            tf.get_default_graph().as_graph_def(add_shapes=True),
            saver.as_saver_def(),
            FLAGS.checkpoint_path,
            _OUTPUT_NAME,
            restore_op_name=None,
            filename_tensor_name=None,
            output_graph=FLAGS.export_path,
            clear_devices=True,
            initializer_nodes=None)
Ejemplo n.º 11
0
    def testBuildDeepLabv2(self):
        batch_size = 2
        crop_size = [41, 41]

        # Test with two image_pyramids.
        image_pyramids = [[1], [0.5, 1]]

        # Test two model variants.
        model_variants = ['xception_65', 'mobilenet_v2']

        # Test with two output_types.
        outputs_to_num_classes = {'semantic': 3, 'direction': 2}

        expected_endpoints = [['merged_logits'],
                              ['merged_logits', 'logits_0.50', 'logits_1.00']]
        expected_num_logits = [1, 3]

        for model_variant in model_variants:
            model_options = common.ModelOptions(
                outputs_to_num_classes)._replace(
                    add_image_level_feature=False,
                    aspp_with_batch_norm=False,
                    aspp_with_separable_conv=False,
                    model_variant=model_variant)

            for i, image_pyramid in enumerate(image_pyramids):
                g = tf.Graph()
                with g.as_default():
                    with self.test_session(graph=g):
                        inputs = tf.random_uniform(
                            (batch_size, crop_size[0], crop_size[1], 3))
                        outputs_to_scales_to_logits = model.multi_scale_logits(
                            inputs, model_options, image_pyramid=image_pyramid)

                        # Check computed results for each output type.
                        for output in outputs_to_num_classes:
                            scales_to_logits = outputs_to_scales_to_logits[
                                output]
                            self.assertListEqual(
                                sorted(scales_to_logits.keys()),
                                sorted(expected_endpoints[i]))

                            # Expected number of logits = len(image_pyramid) + 1, since the
                            # last logits is merged from all the scales.
                            self.assertEqual(len(scales_to_logits),
                                             expected_num_logits[i])
Ejemplo n.º 12
0
    def testDeepcopy(self):
        num_classes = 21
        model_options = common.ModelOptions(
            outputs_to_num_classes={common.OUTPUT_TYPE: num_classes})
        model_options_new = copy.deepcopy(model_options)
        self.assertEqual(
            (model_options_new.outputs_to_num_classes[common.OUTPUT_TYPE]),
            num_classes)

        num_classes_new = 22
        model_options_new.outputs_to_num_classes[common.OUTPUT_TYPE] = (
            num_classes_new)
        self.assertEqual(
            model_options.outputs_to_num_classes[common.OUTPUT_TYPE],
            num_classes)
        self.assertEqual(
            (model_options_new.outputs_to_num_classes[common.OUTPUT_TYPE]),
            num_classes_new)
Ejemplo n.º 13
0
def _build_deeplab(inputs_queue, outputs_to_num_classes, ignore_label):
    samples = inputs_queue.dequeue()

    samples[common.IMAGE] = tf.identity(samples[common.IMAGE],
                                        name=common.IMAGE)
    samples[common.LABEL] = tf.identity(samples[common.LABEL],
                                        name=common.LABEL)

    model_options = common.ModelOptions(
        outputs_to_num_classes=outputs_to_num_classes,
        crop_size=FLAGS.train_crop_size,
        atrous_rates=FLAGS.atrous_rates,
        output_stride=FLAGS.output_stride)
    outputs_to_scales_to_logits = model.multi_scale_logits(
        samples[common.IMAGE],
        model_options=model_options,
        image_pyramid=FLAGS.image_pyramid,
        weight_decay=FLAGS.weight_decay,
        is_training=True,
        fine_tune_batch_norm=FLAGS.fine_tune_batch_norm)

    output_type_dict = outputs_to_scales_to_logits[common.OUTPUT_TYPE]
    output_type_dict[model.get_merged_logits_scope()] = tf.identity(
        output_type_dict[model.get_merged_logits_scope()],
        name=common.OUTPUT_TYPE)

    for output, num_classes in six.iteritems(outputs_to_num_classes):
        train_utils.add_softmax_cross_entropy_loss_for_each_scale(
            outputs_to_scales_to_logits[output],
            samples[common.LABEL],
            num_classes,
            ignore_label,
            loss_weight=1.0,
            upsample_logits=FLAGS.upsample_logits,
            scope=output)

    return outputs_to_scales_to_logits
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)
  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},
        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 = 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 resize 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()
    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,
                         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
          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.º 15
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)

    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0)

    session_config = tf.ConfigProto(allow_soft_placement=True,
                                    log_device_placement=False,
                                    gpu_options=gpu_options)

    #session_config.gpu_options.allow_growth = True

    with tf.Graph().as_default():
        samples = dataset.get_one_shot_iterator().get_next()
        #print(samples[common.IMAGE_NAME])

        model_options = common.ModelOptions(
            outputs_to_num_classes={
                common.OUTPUT_TYPE: dataset.num_of_classes
            },
            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, logits = model.predict_labels(
                samples[common.IMAGE],
                model_options,
                image_pyramid=FLAGS.image_pyramid,
                skips=FLAGS.skips)

        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,
                skips=FLAGS.skips,
                eval_scales=FLAGS.eval_scales,
                add_flipped_images=FLAGS.add_flipped_images)
        predictions = predictions[common.OUTPUT_TYPE]
        predictions = tf.reshape(predictions, shape=[-1])
        labels = tf.reshape(samples[common.LABEL], shape=[-1])
        weights = tf.to_float(tf.not_equal(labels, dataset.ignore_label))

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

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

        # Define the evaluation metric.

        metric_map = {}

        # to remove "predictions out of bound error"
        indices = tf.squeeze(
            tf.where(tf.less_equal(labels, dataset.num_of_classes - 1)), 1)
        labels_ind = tf.cast(tf.gather(labels, indices), tf.int32)
        predictions_ind = tf.gather(predictions, indices)
        # end of insert

        miou, update_miou = tf.metrics.mean_iou(labels_ind,
                                                predictions_ind,
                                                dataset.num_of_classes,
                                                weights=weights,
                                                name="mean_iou")
        tf.summary.scalar(predictions_tag, miou)

        # Define the evaluation metric IOU for individual classes
        iou_v, update_op = my_metrics.iou(labels_ind,
                                          predictions_ind,
                                          dataset.num_of_classes,
                                          weights=weights)
        for index in range(0, dataset.num_of_classes):
            metric_map['class_' + str(index) + '_iou'] = (iou_v[index],
                                                          update_op[index])
            tf.summary.scalar('class_' + str(index) + '_iou', iou_v[index])

        # Confusion matrix save hook. It updates the confusion matrix on tensorboard at the end of eval loop.
        confusionMatrixSaveHook = confusion_matrix.SaverHook(
            labels=['BG', 'water', 'ice', 'snow', 'clutter'],
            confusion_matrix_tensor_name='mean_iou/total_confusion_matrix',
            summary_writer=tf.summary.FileWriterCache.get(
                str(FLAGS.eval_logdir)))

        summary_op = tf.summary.merge_all()

        summary_hook = tf.contrib.training.SummaryAtEndHook(
            log_dir=FLAGS.eval_logdir, summary_op=summary_op)
        hooks = [summary_hook, confusionMatrixSaveHook]

        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:
            tf.contrib.quantize.create_eval_graph()

        tf.contrib.training.evaluate_repeatedly(
            master=FLAGS.master,
            checkpoint_dir=FLAGS.checkpoint_dir,
            eval_ops=[update_miou, update_op],
            max_number_of_evaluations=num_eval_iters,
            hooks=hooks,
            eval_interval_secs=FLAGS.eval_interval_secs)
Ejemplo n.º 16
0
def main(unused_argv):
    tf.logging.set_verbosity(tf.logging.INFO)
    # Get dataset-dependent information.
    dataset = segmentation_dataset.get_dataset(FLAGS.dataset,
                                               FLAGS.eval_split,
                                               dataset_dir=FLAGS.dataset_dir)

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

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

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

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

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

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

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

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

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

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

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

        num_eval_iters = None
        if FLAGS.max_number_of_evaluations > 0:
            num_eval_iters = FLAGS.max_number_of_evaluations
        slim.evaluation.evaluation_loop(
            master=FLAGS.master,
            checkpoint_dir=FLAGS.checkpoint_dir,
            logdir=FLAGS.eval_logdir,
            num_evals=num_batches,
            eval_op=list(metrics_to_updates.values()),
            max_number_of_evaluations=num_eval_iters,
            eval_interval_secs=FLAGS.eval_interval_secs)
Ejemplo n.º 17
0
def build_model():
  """Builds graph for model to train with rewrites for quantization.
  Returns:
    g: Graph with fake quantization ops and batch norm folding suitable for
    training quantized weights.
    train_tensor: Train op for execution during training.
  """
  g = tf.Graph()
  with g.as_default(), tf.device(
      tf.train.replica_device_setter(FLAGS.ps_tasks)):
    samples, _ = get_dataset.get_dataset(FLAGS.dataset, FLAGS.dataset_dir,
                                         split_name=FLAGS.train_split,
                                         is_training=True,
                                         image_size=[FLAGS.image_size, FLAGS.image_size],
                                         batch_size=FLAGS.batch_size,
                                         channel=FLAGS.input_channel)

    inputs = tf.identity(samples['image'], name='image')
    labels = tf.identity(samples['label'], name='label')
    model_options = common.ModelOptions(output_stride=FLAGS.output_stride)
    net, end_points = model.get_features(
        inputs,
        model_options=model_options,
        weight_decay=FLAGS.weight_decay,
        is_training=True,
        fine_tune_batch_norm=FLAGS.fine_tune_batch_norm)
    logits, _ = model.classification(net, end_points, 
                                     num_classes=FLAGS.num_classes,
                                     is_training=True)
    logits = slim.softmax(logits)
    focal_loss_tensor = train_utils.focal_loss(labels, logits, weights=1.0)
    # f1_loss_tensor = train_utils.f1_loss(labels, logits, weights=1.0)
    # cls_loss = f1_loss_tensor
    cls_loss = focal_loss_tensor

    # Gather update_ops
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    # Gather initial summaries.
    summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES))

    global_step = tf.train.get_or_create_global_step()
    learning_rate = train_utils.get_model_learning_rate(
          FLAGS.learning_policy, FLAGS.base_learning_rate,
          FLAGS.learning_rate_decay_step, FLAGS.learning_rate_decay_factor,
          FLAGS.number_of_steps, FLAGS.learning_power,
          FLAGS.slow_start_step, FLAGS.slow_start_learning_rate)
    opt = tf.train.AdamOptimizer(learning_rate)
    # opt = tf.train.RMSPropOptimizer(learning_rate, momentum=FLAGS.momentum)
    summaries.add(tf.summary.scalar('learning_rate', learning_rate))

    for loss in tf.get_collection(tf.GraphKeys.LOSSES):
      summaries.add(tf.summary.scalar('sub_losses/%s'%(loss.op.name), loss))
    classifation_loss = tf.identity(cls_loss, name='classifation_loss')
    summaries.add(tf.summary.scalar('losses/classifation_loss', classifation_loss))
    regularization_loss = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    regularization_loss = tf.add_n(regularization_loss, name='regularization_loss')
    summaries.add(tf.summary.scalar('losses/regularization_loss', regularization_loss))

    total_loss = tf.add(cls_loss, regularization_loss, name='total_loss')
    grads_and_vars = opt.compute_gradients(total_loss)

    total_loss = tf.check_numerics(total_loss, 'LossTensor is inf or nan.')
    summaries.add(tf.summary.scalar('losses/total_loss', total_loss))

    grad_updates = opt.apply_gradients(grads_and_vars, global_step=global_step)
    update_ops.append(grad_updates)
    update_op = tf.group(*update_ops, name='update_barrier')
    with tf.control_dependencies([update_op]):
      train_tensor = tf.identity(total_loss, name='train_op')

  # Merge all summaries together.
  summary_op = tf.summary.merge(list(summaries))
  return g, train_tensor, summary_op
Ejemplo n.º 18
0
 def testWrongDeepLabVariant(self):
   model_options = common.ModelOptions([])._replace(
       model_variant='no_such_variant')
   with self.assertRaises(ValueError):
     model._get_logits(images=[], model_options=model_options)
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
            },
            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_func.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 = model_func.predict_labels_multi_scale(
                samples[common.IMAGE],
                model_options=model_options,
                eval_scales=FLAGS.eval_scales,
                add_flipped_images=FLAGS.add_flipped_images)
        predictions = predictions[common.OUTPUT_TYPE]
        predictions = tf.reshape(predictions, shape=[-1])
        labels = tf.reshape(samples[common.LABEL], shape=[-1])
        weights = tf.to_float(tf.not_equal(labels, dataset.ignore_label))

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

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

        # Define the evaluation metric.
        metric_map = {}
        num_classes = dataset.num_of_classes
        metric_map['eval/%s_overall' % predictions_tag] = tf.metrics.mean_iou(
            labels=labels,
            predictions=predictions,
            num_classes=num_classes,
            weights=weights)
        # IoU for each class.
        one_hot_predictions = tf.one_hot(predictions, num_classes)
        one_hot_predictions = tf.reshape(one_hot_predictions,
                                         [-1, num_classes])
        one_hot_labels = tf.one_hot(labels, num_classes)
        one_hot_labels = tf.reshape(one_hot_labels, [-1, num_classes])
        for c in range(num_classes):
            predictions_tag_c = '%s_class_%d' % (predictions_tag, c)
            tp, tp_op = tf.metrics.true_positives(
                labels=one_hot_labels[:, c],
                predictions=one_hot_predictions[:, c],
                weights=weights)
            fp, fp_op = tf.metrics.false_positives(
                labels=one_hot_labels[:, c],
                predictions=one_hot_predictions[:, c],
                weights=weights)
            fn, fn_op = tf.metrics.false_negatives(
                labels=one_hot_labels[:, c],
                predictions=one_hot_predictions[:, c],
                weights=weights)
            tp_fp_fn_op = tf.group(tp_op, fp_op, fn_op)
            iou = tf.where(tf.greater(tp + fn, 0.0), tp / (tp + fn + fp),
                           tf.constant(np.NaN))
            metric_map['eval/%s' % predictions_tag_c] = (iou, tp_fp_fn_op)

        (metrics_to_values,
         metrics_to_updates) = contrib_metrics.aggregate_metric_map(metric_map)

        summary_ops = []
        for metric_name, metric_value in six.iteritems(metrics_to_values):
            op = tf.summary.scalar(metric_name, metric_value)
            op = tf.Print(op, [metric_value], metric_name)
            summary_ops.append(op)

        summary_op = tf.summary.merge(summary_ops)
        summary_hook = contrib_training.SummaryAtEndHook(
            log_dir=FLAGS.eval_logdir, summary_op=summary_op)
        hooks = [summary_hook]

        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)
        contrib_training.evaluate_repeatedly(
            checkpoint_dir=FLAGS.checkpoint_dir,
            master=FLAGS.master,
            eval_ops=list(metrics_to_updates.values()),
            max_number_of_evaluations=num_eval_iters,
            hooks=hooks,
            eval_interval_secs=FLAGS.eval_interval_secs)
Ejemplo n.º 20
0
OUT_PATH_TFLITE = CHECKPOINT_PATH + '/tflite_graph.tflite'
OUT_PATH_FROZEN_GRAPH = CHECKPOINT_PATH + '/frozen_graph.pb'

if __name__ == '__main__':
    input_tensor_name = INPUT_TENSOR_NAME
    input_size = INPUT_SIZE
    outputs_to_num_classes = {common.OUTPUT_TYPE: NUMBER_OF_CLASSES}

    FLAGS.model_variant = MODEL_VARIANT
    FLAGS.dense_prediction_cell_json = './core/dense_prediction_cell_branch5_top1_cityscapes.json' if USE_DPC else ''
    chkpt_path = CHECKPOINT_PATH

    model_options = common.ModelOptions(
        outputs_to_num_classes=outputs_to_num_classes,
        crop_size=input_size[1:3],
        atrous_rates=None,
        output_stride=OUTPUT_STRIDE)

    g = tf.Graph()
    with g.as_default():
        with tf.Session(graph=g) as sess:
            inputs = tf.placeholder(
                tf.float32, input_size, name=input_tensor_name)
            outputs_to_scales_to_logits = model.predict_labels(
                inputs,
                model_options=model_options)
            predictions = tf.cast(
                outputs_to_scales_to_logits[common.OUTPUT_TYPE], tf.int32)
            output_tensor_name = predictions.name.split(':')[0]
Ejemplo n.º 21
0
def main(unused_argv):
    # Check model parameters
    check_model_conflict()

    data_inforamtion = data_generator._DATASETS_INFORMATION[FLAGS.dataset_name]
    tf.logging.set_verbosity(tf.logging.INFO)

    tf.gfile.MakeDirs(FLAGS.train_logdir)
    for split in FLAGS.train_split:
        tf.logging.info('Training on %s set', split)

    path = FLAGS.train_logdir
    parameters_dict = vars(FLAGS)
    with open(os.path.join(path, 'json.txt'), 'w', encoding='utf-8') as f:
        json.dump(parameters_dict, f, indent=3)

    with open(os.path.join(path, 'logging.txt'), 'w') as f:
        for key in parameters_dict:
            f.write("{}: {}".format(str(key), str(parameters_dict[key])))
            f.write("\n")
        f.write("\nStart time: {}".format(
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
        f.write("\n")

    graph = tf.Graph()
    with graph.as_default():
        with tf.device(
                tf.train.replica_device_setter(ps_tasks=FLAGS.num_ps_tasks)):
            assert FLAGS.batch_size % FLAGS.num_clones == 0, (
                'Training batch size not divisble by number of clones (GPUs).')
            clone_batch_size = FLAGS.batch_size // FLAGS.num_clones

            if FLAGS.dataset_name == '2019_ISBI_CHAOS_MR_T1' or FLAGS.dataset_name == '2019_ISBI_CHAOS_MR_T2':
                min_resize_value = data_inforamtion.height
                max_resize_value = data_inforamtion.height
            else:
                if FLAGS.min_resize_value is not None:
                    min_resize_value = FLAGS.min_resize_value
                else:
                    min_resize_value = data_inforamtion.height

                if FLAGS.max_resize_value is not None:
                    max_resize_value = FLAGS.max_resize_value
                else:
                    max_resize_value = data_inforamtion.height

            train_generator = data_generator.Dataset(
                dataset_name=FLAGS.dataset_name,
                split_name=FLAGS.train_split,
                guidance_type=FLAGS.guidance_type,
                batch_size=clone_batch_size,
                pre_crop_flag=FLAGS.pre_crop_flag,
                mt_class=FLAGS.mt_output_node,
                crop_size=data_inforamtion.train["train_crop_size"],
                min_resize_value=FLAGS.min_resize_value,
                max_resize_value=FLAGS.max_resize_value,
                resize_factor=FLAGS.resize_factor,
                min_scale_factor=FLAGS.min_scale_factor,
                max_scale_factor=FLAGS.max_scale_factor,
                scale_factor_step_size=FLAGS.scale_factor_step_size,
                num_readers=2,
                is_training=True,
                shuffle_data=True,
                repeat_data=True,
                prior_num_slice=FLAGS.prior_num_slice,
                prior_num_subject=FLAGS.prior_num_subject,
                seq_length=FLAGS.seq_length,
                seq_type="bidirection",
                z_loss_name=FLAGS.z_loss_name,
            )

            if "val" not in FLAGS.train_split:
                val_generator = data_generator.Dataset(
                    dataset_name=FLAGS.dataset_name,
                    split_name=["val"],
                    guidance_type=FLAGS.guidance_type,
                    batch_size=1,
                    mt_class=FLAGS.mt_output_node,
                    crop_size=[
                        data_inforamtion.height, data_inforamtion.width
                    ],
                    min_resize_value=FLAGS.min_resize_value,
                    max_resize_value=FLAGS.max_resize_value,
                    num_readers=2,
                    is_training=False,
                    shuffle_data=False,
                    repeat_data=True,
                    prior_num_slice=FLAGS.prior_num_slice,
                    prior_num_subject=FLAGS.prior_num_subject,
                    seq_length=FLAGS.seq_length,
                    seq_type="bidirection",
                    z_loss_name=FLAGS.z_loss_name,
                )

            model_options = common.ModelOptions(
                outputs_to_num_classes=train_generator.num_of_classes,
                crop_size=data_inforamtion.train["train_crop_size"],
                output_stride=FLAGS.output_stride)

            steps = tf.compat.v1.placeholder(tf.int32, shape=[])

            dataset1 = train_generator.get_dataset()
            iter1 = dataset1.make_one_shot_iterator()
            train_samples = iter1.get_next()

            train_tensor, summary_op = _train_pgn_model(
                train_samples, train_generator.num_of_classes, model_options,
                train_generator.ignore_label)

            if "val" not in FLAGS.train_split:
                dataset2 = val_generator.get_dataset()
                iter2 = dataset2.make_one_shot_iterator()
                val_samples = iter2.get_next()

                val_tensor, _ = _val_pgn_model(val_samples,
                                               val_generator.num_of_classes,
                                               model_options,
                                               val_generator.ignore_label,
                                               steps)

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

            init_fn = None
            if FLAGS.tf_initial_checkpoint:
                init_fn = train_utils.get_model_init_fn(
                    train_logdir=FLAGS.train_logdir,
                    tf_initial_checkpoint=FLAGS.tf_initial_checkpoint,
                    initialize_first_layer=True,
                    initialize_last_layer=FLAGS.initialize_last_layer,
                    ignore_missing_vars=True)

            scaffold = tf.train.Scaffold(
                init_fn=init_fn,
                summary_op=summary_op,
            )

            stop_hook = tf.train.StopAtStepHook(FLAGS.training_number_of_steps)
            saver = tf.train.Saver()
            best_dice = 0
            with tf.train.MonitoredTrainingSession(
                    master=FLAGS.master,
                    is_chief=(FLAGS.task == 0),
                    config=session_config,
                    scaffold=scaffold,
                    checkpoint_dir=FLAGS.train_logdir,
                    log_step_count_steps=FLAGS.log_steps,
                    save_summaries_steps=20,
                    save_checkpoint_steps=FLAGS.save_checkpoint_steps,
                    hooks=[stop_hook]) as sess:

                # step=0
                total_val_loss, total_val_steps = [], []
                best_model_performance = 0.0
                while not sess.should_stop():
                    _, global_step = sess.run(
                        [train_tensor,
                         tf.train.get_global_step()])
                    if "val" not in FLAGS.train_split:
                        if global_step % FLAGS.validation_steps == 0:
                            cm_total = 0
                            for j in range(
                                    val_generator.splits_to_sizes["val"]):
                                cm_total += sess.run(val_tensor,
                                                     feed_dict={steps: j})

                            mean_dice_score, _ = metrics.compute_mean_dsc(
                                total_cm=cm_total)

                            total_val_loss.append(mean_dice_score)
                            total_val_steps.append(global_step)
                            plt.legend(["validation loss"])
                            plt.xlabel("global step")
                            plt.ylabel("loss")
                            plt.plot(total_val_steps, total_val_loss, "bo-")
                            plt.grid(True)
                            plt.savefig(FLAGS.train_logdir + "/losses.png")

                            if mean_dice_score > best_dice:
                                best_dice = mean_dice_score
                                saver.save(
                                    get_session(sess),
                                    os.path.join(FLAGS.train_logdir,
                                                 'model.ckpt-best'))
                                # saver.save(get_session(sess), os.path.join(FLAGS.train_logdir, 'model.ckpt-best-%d' %global_step))
                                txt = 20 * ">" + " saving best mdoel model.ckpt-best-%d with DSC: %f" % (
                                    global_step, best_dice)
                                print(txt)
                                with open(os.path.join(path, 'logging.txt'),
                                          'a') as f:
                                    f.write(txt)
                                    f.write("\n")

            with open(os.path.join(path, 'logging.txt'), 'a') as f:
                f.write("\nEnd time: {}".format(
                    time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
                f.write("\n")
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},
            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]]

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

        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)

        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,
            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')
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
def eval_model():
    """Evaluates model."""
    tf.logging.set_verbosity(tf.logging.INFO)
    tf.gfile.MakeDirs(FLAGS.eval_dir)
    tf.logging.info('Evaluating on %s set', FLAGS.eval_split)
    g = tf.Graph()
    with g.as_default():
        samples, num_samples = get_dataset.get_dataset(
            FLAGS.dataset,
            FLAGS.dataset_dir,
            split_name=FLAGS.eval_split,
            is_training=False,
            image_size=[FLAGS.image_size, FLAGS.image_size],
            batch_size=FLAGS.batch_size,
            channel=FLAGS.input_channel)
        inputs = tf.identity(samples['image'], name='image')
        labels = tf.identity(samples['label'], name='label')
        model_options = common.ModelOptions(output_stride=FLAGS.output_stride)
        net, end_points = model.get_features(inputs,
                                             model_options=model_options,
                                             is_training=False,
                                             fine_tune_batch_norm=False)

        _, end_points = model.classification(net,
                                             end_points,
                                             num_classes=FLAGS.num_classes,
                                             is_training=False)
        eval_ops = metrics(end_points, labels)
        #num_samples = 1000
        num_batches = math.ceil(num_samples / float(FLAGS.batch_size))
        tf.logging.info('Eval num images %d', num_samples)
        tf.logging.info('Eval batch size %d and num batch %d',
                        FLAGS.batch_size, num_batches)
        # session_config = tf.ConfigProto(device_count={'GPU': 0})
        session_config = tf.ConfigProto(allow_soft_placement=True)
        session_config.gpu_options.allow_growth = True
        if FLAGS.use_slim:
            num_eval_iters = None
            if FLAGS.max_number_of_evaluations > 0:
                num_eval_iters = FLAGS.max_number_of_evaluations
            slim.evaluation.evaluation_loop(
                FLAGS.master,
                FLAGS.checkpoint_dir,
                logdir=FLAGS.eval_dir,
                num_evals=num_batches,
                eval_op=eval_ops,
                session_config=session_config,
                max_number_of_evaluations=num_eval_iters,
                eval_interval_secs=FLAGS.eval_interval_secs)
        else:
            with tf.Session(config=session_config) as sess:
                init_op = tf.group(tf.global_variables_initializer(),
                                   tf.local_variables_initializer())
                sess.run(init_op)
                saver_fn = get_checkpoint_init_fn(FLAGS.checkpoint_dir)
                saver_fn(sess)
                coord = tf.train.Coordinator()
                threads = tf.train.start_queue_runners(sess=sess, coord=coord)
                try:
                    i = 0
                    all_pres = []
                    predictions_custom_list = []
                    all_labels = []
                    while not coord.should_stop():
                        logits_np, labels_np = sess.run(
                            [end_points['Logits_Predictions'], labels])
                        logits_np = logits_np[0]
                        labels_np = labels_np[0]
                        all_labels.append(labels_np)
                        labels_id = np.where(labels_np == 1)[0]
                        predictions_id = list(
                            np.where(logits_np > (_THRESHOULD))[0])
                        predictions_np = np.where(logits_np > (_THRESHOULD), 1,
                                                  0)
                        if np.sum(predictions_np) == 0:
                            max_id = np.argmax(logits_np)
                            predictions_np[max_id] = 1
                            predictions_id.append(max_id)
                        predictions_custom_list.append(predictions_np)
                        i += 1
                        sys.stdout.write(
                            'Image[{0}]--> labels:{1}, predictions: {2}\n'.
                            format(i, labels_id, predictions_id))
                        sys.stdout.flush()

                        predictions_image_list = []
                        for thre in range(1, FLAGS.threshould, 1):
                            predictions_id = list(
                                np.where(logits_np > (thre / 100000000))[0])
                            predictions_np = np.where(
                                logits_np > (thre / 100000000), 1, 0)
                            if np.sum(predictions_np) == 0:
                                max_id = np.argmax(logits_np)
                                predictions_np[max_id] = 1
                                predictions_id.append(max_id)
                            predictions_image_list.append(predictions_np)
                        all_pres.append(predictions_image_list)
                except tf.errors.OutOfRangeError:
                    coord.request_stop()
                    coord.join(threads)
                finally:
                    sys.stdout.write('\n')
                    sys.stdout.flush()
                    pred_rows = []
                    all_labels = np.stack(all_labels, 0)
                    pres_custom = np.stack(predictions_custom_list, 0)
                    eval_custom = metric_eval(all_labels, pres_custom)
                    sys.stdout.write(
                        'Eval[f1_score, precision, recall]: {}\n'.format(
                            eval_custom['All']))
                    sys.stdout.flush()
                    pred_rows.append(eval_custom)
                    all_pres = np.transpose(all_pres, (1, 0, 2))
                    for pre, thre in zip(all_pres,
                                         range(1, FLAGS.threshould, 1)):
                        pred_rows.append(metric_eval(all_labels, pre, thre))
                    columns = ['Thre'] + list(
                        PROTEIN_CLASS_NAMES.values()) + ['All']
                    submission_df = pd.DataFrame(pred_rows)[columns]
                    submission_df.to_csv(os.path.join('./result/protein',
                                                      'protein_eval.csv'),
                                         index=False)