Пример #1
0
def preprocess_image_and_label(tf_image,
                               tf_label,
                               FLAGS,
                               ignore_label,
                               is_training=True,
                               tf_edge=None):
    #    print('tf_image shape',tf_image.shape)
    #    print('tf_label shape',tf_label.shape)

    crop_size = FLAGS.train_crop_size
    original_image, image, label, edge = input_preprocess.preprocess_image_and_label(
        tf_image,
        tf_label,
        crop_height=crop_size[0],
        crop_width=crop_size[1],
        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,
        ignore_label=ignore_label,
        is_training=is_training,
        model_variant=FLAGS.model_variant,
        edge=tf_edge)

    if tf_edge is None:
        return image, label
    else:
        #        print('image shape',image.shape)
        #        print('labe shape',label.shape)
        #        print('edge shape',edge.shape)
        return image, label, edge
Пример #2
0
def parse_record(raw_record, is_training):
    example = _parse_example_proto(raw_record)
    image = example['image']
    label = example['labels_class']
    image_name = example['image_name']
    height = example['height']
    width = example['width']

    if label is not None:
        if label.shape.ndims == 2:
            label = tf.expand_dims(label, 2)
        elif label.shape.ndims == 3 and label.shape.dims[2] == 1:
            pass
        else:
            raise ValueError('Input label shape must be [height, width], or '
                             '[height, width, 1].')

        label.set_shape([None, None, 1])
    if is_training:
        original_image, image, label = preprocess_image_and_label(
            image,
            label,
            crop_height=params['crop_size'][0],
            crop_width=params['crop_size'][1],
            min_scale_factor=params['min_scale_factor'],
            max_scale_factor=params['max_scale_factor'],
            scale_factor_step_size=params['scale_factor_step_size'],
            is_training=True,
            model_variant='resnet_v1_50')
    else:
        original_image, image, label = preprocess_image_and_label(
            image,
            label,
            crop_height=params['crop_size'][0],
            crop_width=params['crop_size'][1],
            is_training=False,
            model_variant='resnet_v1_50')
    sample = {
        'image': image,
        'label': label,
        'height': height,
        'width': width,
        'image_name': image_name
    }
    return sample
Пример #3
0
    def _preprocess_image(self, sample):
        """Preprocesses the image and label.

    Args:
      sample: A sample containing image and label.

    Returns:
      sample: Sample with preprocessed image and label.

    Raises:
      ValueError: Ground truth label not provided during training.
    """
        image = sample[common.IMAGE]
        label = sample[common.LABELS_CLASS]

        preprocessed = input_preprocess.preprocess_image_and_label(
            image=image,
            label=label,
            crop_height=self.crop_size[0],
            crop_width=self.crop_size[1],
            min_resize_value=self.min_resize_value,
            max_resize_value=self.max_resize_value,
            resize_factor=self.resize_factor,
            min_scale_factor=self.min_scale_factor,
            max_scale_factor=self.max_scale_factor,
            scale_factor_step_size=self.scale_factor_step_size,
            ignore_label=self.ignore_label,
            is_training=self.is_training,
            model_variant=self.model_variant,
            non_uniform_sampling=self.non_uniform_sampling,
            output_target_sampling=self.output_target_sampling)

        if self.non_uniform_sampling is not None:
            original_image, image, label, locations = preprocessed
            sample[nus.SAMPLING] = locations
        elif self.output_target_sampling:
            original_image, image, label, target_sampling = preprocessed
            sample[nus.TARGET_SAMPLING] = target_sampling
        else:
            original_image, image, label = preprocessed

        sample[common.IMAGE] = image

        if not self.is_training:
            # Original image is only used during visualization.
            sample[common.ORIGINAL_IMAGE] = original_image

        if label is not None:
            sample[common.LABEL] = label

        # Remove common.LABEL_CLASS key in the sample since it is only used to
        # derive label and not used in training and evaluation.
        sample.pop(common.LABELS_CLASS, None)

        return sample
Пример #4
0
def _create_input_tensors():
    """Creates and prepares input tensors for DeepLab model.

  This method creates a 4-D uint8 image tensor 'ImageTensor' with shape
  [1, None, None, 3]. The actual input tensor name to use during inference is
  'ImageTensor:0'.

  Returns:
    image: Preprocessed 4-D float32 tensor with shape [1, crop_height,
      crop_width, 3].
    original_image_size: Original image shape tensor [height, width].
    resized_image_size: Resized image shape tensor [height, width].
  """
    # input_preprocess takes 4-D image tensor as input.
    input_image = tf.placeholder(tf.uint8,
                                 [FLAGS.inf_batch_size, None, None, 3],
                                 name=_INPUT_NAME)
    original_image_size = tf.shape(input_image)[1:3]

    # Squeeze the dimension in axis=0 since `preprocess_image_and_label` assumes
    # image to be 3-D.
    # image = tf.squeeze(input_image, axis=0)
    # resize image one by one, the input images must have the same shape  hlc 018-11-5
    multi_images = []
    resized_image_size = None
    for one_image in tf.unstack(input_image):
        resized_image, one_image_out, _ = input_preprocess.preprocess_image_and_label(
            one_image,
            label=None,
            crop_height=FLAGS.crop_size[0],
            crop_width=FLAGS.crop_size[1],
            min_resize_value=FLAGS.min_resize_value,
            max_resize_value=FLAGS.max_resize_value,
            resize_factor=FLAGS.resize_factor,
            is_training=False,
            model_variant=FLAGS.model_variant)
        new_size = tf.shape(resized_image)[:2]
        if resized_image_size is None:
            resized_image_size = new_size
        # if resized_image_size[0] != new_size[0] or resized_image_size[1] != new_size[1]:
        #     print(resized_image_size, "!=",new_size)
        #     raise ValueError("resized_image_size not equal to new_size")

        multi_images.append(one_image_out)
    # stack them
    image = tf.stack(multi_images, axis=0)
    print(image)

    # Expand the dimension in axis=0, since the following operations assume the
    # image to be 4-D.
    # image = tf.expand_dims(image, 0)

    return image, original_image_size, resized_image_size
Пример #5
0
  def _preprocess_image(self, sample):
    """Preprocesses the image and label.

    Args:
      sample: A sample containing image and label.

    Returns:
      sample: Sample with preprocessed image and label.

    Raises:
      ValueError: Ground truth label not provided during training.
    """
    image = sample[common.IMAGE]
    ###########################################
    # There aren't test true masks
    if self.split_name == 'test':
      label = None  
    else:
      label = sample[common.LABELS_CLASS]
    ###########################################

    original_image, image, label = input_preprocess.preprocess_image_and_label(
        image=image,
        label=label,
        crop_height=self.crop_size[0],
        crop_width=self.crop_size[1],
        min_resize_value=self.min_resize_value,
        max_resize_value=self.max_resize_value,
        resize_factor=self.resize_factor,
        min_scale_factor=self.min_scale_factor,
        max_scale_factor=self.max_scale_factor,
        scale_factor_step_size=self.scale_factor_step_size,
        ignore_label=self.ignore_label,
        is_training=self.is_training,
        model_variant=self.model_variant)

    sample[common.IMAGE] = image

    if not self.is_training:
      # Original image is only used during visualization.
      sample[common.ORIGINAL_IMAGE] = original_image

    if label is not None:
      sample[common.LABEL] = label

    # Remove common.LABEL_CLASS key in the sample since it is only used to
    # derive label and not used in training and evaluation.
    sample.pop(common.LABELS_CLASS, None)

    return sample
Пример #6
0
    def _preprocess_image(self, sample):
        """Preprocesses the image and label.

    Args:
      sample: A sample containing image and label.

    Returns:
      sample: Sample with preprocessed image and label.

    Raises:
      ValueError: Ground truth label not provided during training.
    """
        # import pdb; pdb.set_trace()
        image = sample[common.IMAGE]
        label = sample[common.LABELS_CLASS]

        # import matplotlib.pyplot as plt
        # print("shape of image: ", image.shape)
        # import pdb; pdb.set_trace()

        original_image, image, label = input_preprocess.preprocess_image_and_label(
            image=image,
            label=label,
            crop_height=self.crop_size[0],
            crop_width=self.crop_size[1],
            min_resize_value=self.min_resize_value,
            max_resize_value=self.max_resize_value,
            resize_factor=self.resize_factor,
            min_scale_factor=self.min_scale_factor,
            max_scale_factor=self.max_scale_factor,
            scale_factor_step_size=self.scale_factor_step_size,
            ignore_label=self.ignore_label,
            is_training=self.is_training,
            model_variant=self.model_variant)

        sample[common.IMAGE] = image

        if not self.is_training:
            # Original image is only used during visualization.
            sample[common.ORIGINAL_IMAGE] = original_image

        if label is not None:
            sample[common.LABEL] = label

        # Remove common.LABEL_CLASS key in the sample since it is only used to
        # derive label and not used in training and evaluation.
        sample.pop(common.LABELS_CLASS, None)

        return sample
def preprocess_image(image_buffer):
    decode = tf.image.decode_png(image_buffer, channels=3)
    original_image, image, _ = input_preprocess.preprocess_image_and_label(
        decode,
        label=None,
        crop_height=CROP_SIZE[0],
        crop_width=CROP_SIZE[1],
        min_resize_value=None,
        max_resize_value=None,
        resize_factor=None,
        ignore_label=255,
        is_training=False,
        model_variant="xception_65")

    #image = tf.expand_dims(image, 0)

    return image
Пример #8
0
  def _preprocess_image(self, sample):
    """Preprocesses the image and label.

    Args:
      sample: A sample containing image and label.

    Returns:
      sample: Sample with preprocessed image and label.

    Raises:
      ValueError: Ground truth label not provided during training.
    """
    image = sample[common.IMAGE]
    label = sample[common.LABELS_CLASS]

    original_image, image, label = input_preprocess.preprocess_image_and_label(
        image=image,
        label=label,
        crop_height=self.crop_size[0],
        crop_width=self.crop_size[1],
        min_resize_value=self.min_resize_value,
        max_resize_value=self.max_resize_value,
        resize_factor=self.resize_factor,
        min_scale_factor=self.min_scale_factor,
        max_scale_factor=self.max_scale_factor,
        scale_factor_step_size=self.scale_factor_step_size,
        ignore_label=self.ignore_label,
        is_training=self.is_training,
        model_variant=self.model_variant)

    sample[common.IMAGE] = image

    if not self.is_training:
      # Original image is only used during visualization.
      sample[common.ORIGINAL_IMAGE] = original_image

    if label is not None:
      sample[common.LABEL] = label

    # Remove common.LABEL_CLASS key in the sample since it is only used to
    # derive label and not used in training and evaluation.
    sample.pop(common.LABELS_CLASS, None)

    return sample
Пример #9
0
    def _construct_and_fill_model(self):
        self.device_ids = sly.remap_gpu_devices(self.config['gpu_devices'])
        initialized_model = namedtuple('Model',
                                       ['input_images', 'predictions', 'sess'])

        with tf.get_default_graph().as_default():
            img = tf.placeholder(tf.float32, shape=(None, None, 3))
            input_w, input_h = self.input_size_wh
            img_re = tf.image.resize_images(img, (input_h, input_w))
            original_image, image, label = input_preprocess.preprocess_image_and_label(
                img_re,
                None,
                crop_height=input_h,
                crop_width=input_w,
                is_training=False,
                model_variant="xception_65")

            image = tf.expand_dims(image, 0)

            model_options = ModelOptions(
                outputs_to_num_classes={'semantic': len(self.train_classes)},
                crop_size=(input_h, input_w),
                atrous_rates=[6, 12, 18],
                output_stride=16)

            predictions = model.predict_logits(tf.shape(img)[0:2],
                                               image,
                                               model_options=model_options,
                                               image_pyramid=None)

            predictions = predictions['semantic']
            saver = tf.train.Saver(slim.get_variables_to_restore())
            sess = tf.train.MonitoredTrainingSession(master='')
            saver.restore(
                sess,
                self.helper.paths.model_dir + '/model_weights/model.ckpt')

            initialized_model.input_images = img
            initialized_model.predictions = predictions
            initialized_model.sess = sess
        self.initialized_model = initialized_model
        logger.info('Weights are loaded.')
Пример #10
0
def _create_input_tensors():
    """Creates and prepares input tensors for DeepLab model.

  This method creates a 4-D uint8 image tensor 'ImageTensor' with shape
  [1, None, None, 3]. The actual input tensor name to use during inference is
  'ImageTensor:0'.
  输入图像名称:ImageTensor:0

  Returns:
    image: Preprocessed 4-D float32 tensor with shape [1, crop_height,
      crop_width, 3].
    original_image_size: Original image shape tensor [height, width].
    resized_image_size: Resized image shape tensor [height, width].
  """
    # input_preprocess takes 4-D image tensor as input.
    input_image = tf.placeholder(tf.uint8, [1, None, None, 3],
                                 name=_INPUT_NAME)
    original_image_size = tf.shape(input_image)[1:3]

    # Squeeze the dimension in axis=0 since `preprocess_image_and_label` assumes
    # image to be 3-D.
    image = tf.squeeze(input_image, axis=0)
    resized_image, image, _ = input_preprocess.preprocess_image_and_label(
        image,
        label=None,
        crop_height=FLAGS.crop_size[0],
        crop_width=FLAGS.crop_size[1],
        min_resize_value=FLAGS.min_resize_value,
        max_resize_value=FLAGS.max_resize_value,
        resize_factor=FLAGS.resize_factor,
        is_training=False,
        model_variant=FLAGS.model_variant)
    resized_image_size = tf.shape(resized_image)[:2]

    # Expand the dimension in axis=0, since the following operations assume the
    # image to be 4-D.
    image = tf.expand_dims(image, 0)

    return image, original_image_size, resized_image_size
Пример #11
0
def _create_input_tensors():
  """Creates and prepares input tensors for DeepLab model.

  This method creates a 4-D uint8 image tensor 'ImageTensor' with shape
  [1, None, None, 3]. The actual input tensor name to use during inference is
  'ImageTensor:0'.

  Returns:
    image: Preprocessed 4-D float32 tensor with shape [1, crop_height,
      crop_width, 3].
    original_image_size: Original image shape tensor [height, width].
    resized_image_size: Resized image shape tensor [height, width].
  """
  # input_preprocess takes 4-D image tensor as input.
  input_image = tf.placeholder(tf.uint8, [1, None, None, 3], name=_INPUT_NAME)
  original_image_size = tf.shape(input_image)[1:3]

  # Squeeze the dimension in axis=0 since `preprocess_image_and_label` assumes
  # image to be 3-D.
  image = tf.squeeze(input_image, axis=0)
  resized_image, image, _ = input_preprocess.preprocess_image_and_label(
      image,
      label=None,
      crop_height=FLAGS.crop_size[0],
      crop_width=FLAGS.crop_size[1],
      min_resize_value=FLAGS.min_resize_value,
      max_resize_value=FLAGS.max_resize_value,
      resize_factor=FLAGS.resize_factor,
      is_training=False,
      model_variant=FLAGS.model_variant)
  resized_image_size = tf.shape(resized_image)[:2]

  # Expand the dimension in axis=0, since the following operations assume the
  # image to be 4-D.
  image = tf.expand_dims(image, 0)

  return image, original_image_size, resized_image_size
def get(dataset,
        crop_size,
        batch_size,
        min_resize_value=None,
        max_resize_value=None,
        resize_factor=None,
        min_scale_factor=1.,
        max_scale_factor=1.,
        scale_factor_step_size=0,
        num_readers=1,
        num_threads=1,
        dataset_split=None,
        is_training=True,
        model_variant=None):
    """Gets the dataset split for semantic segmentation.

  This functions gets the dataset split for semantic segmentation. In
  particular, it is a wrapper of (1) dataset_data_provider which returns the raw
  dataset split, (2) input_preprcess which preprocess the raw data, and (3) the
  Tensorflow operation of batching the preprocessed data. Then, the output could
  be directly used by training, evaluation or visualization.

  Args:
    dataset: An instance of slim Dataset.
    crop_size: Image crop size [height, width].
    batch_size: Batch size.
    min_resize_value: Desired size of the smaller image side.
    max_resize_value: Maximum allowed size of the larger image side.
    resize_factor: Resized dimensions are multiple of factor plus one.
    min_scale_factor: Minimum scale factor value.
    max_scale_factor: Maximum scale factor value.
    scale_factor_step_size: The step size from min scale factor to max scale
      factor. The input is randomly scaled based on the value of
      (min_scale_factor, max_scale_factor, scale_factor_step_size).
    num_readers: Number of readers for data provider.
    num_threads: Number of threads for batching data.
    dataset_split: Dataset split.
    is_training: Is training or not.
    model_variant: Model variant (string) for choosing how to mean-subtract the
      images. See feature_extractor.network_map for supported model variants.

  Returns:
    A dictionary of batched Tensors for semantic segmentation.

  Raises:
    ValueError: dataset_split is None, failed to find labels, or label shape
      is not valid.
  """
    if dataset_split is None:
        raise ValueError('Unknown dataset split.')
    if model_variant is None:
        tf.logging.warning('Please specify a model_variant. See '
                           'feature_extractor.network_map for supported model '
                           'variants.')

    data_provider = dataset_data_provider.DatasetDataProvider(
        dataset,
        num_readers=num_readers,
        num_epochs=None if is_training else 1,
        shuffle=is_training)
    image, label, image_name, height, width = _get_data(
        data_provider, dataset_split)
    if label is not None:
        if label.shape.ndims == 2:
            label = tf.expand_dims(label, 2)
        elif label.shape.ndims == 3 and label.shape.dims[2] == 1:
            pass
        else:
            raise ValueError('Input label shape must be [height, width], or '
                             '[height, width, 1].')

        label.set_shape([None, None, 1])
    original_image, image, label = input_preprocess.preprocess_image_and_label(
        image,
        label,
        crop_height=crop_size[0],
        crop_width=crop_size[1],
        min_resize_value=min_resize_value,
        max_resize_value=max_resize_value,
        resize_factor=resize_factor,
        min_scale_factor=min_scale_factor,
        max_scale_factor=max_scale_factor,
        scale_factor_step_size=scale_factor_step_size,
        ignore_label=dataset.ignore_label,
        is_training=is_training,
        model_variant=model_variant)

    #tf.Print(image_name, [image_name])
    #print(image_name.get_shape().as_list())
    #print(image.get_shape().as_list())
    #print(label.get_shape().as_list())
    print(image_name)

    sample = {
        common.IMAGE: image,
        common.IMAGE_NAME: image_name,
        common.HEIGHT: height,
        common.WIDTH: width
    }
    if label is not None:
        sample[common.LABEL] = label

    if not is_training:
        # Original image is only used during visualization.
        sample[common.ORIGINAL_IMAGE] = original_image,
        num_threads = 1

    return tf.train.batch(sample,
                          batch_size=batch_size,
                          num_threads=num_threads,
                          capacity=32 * batch_size,
                          allow_smaller_final_batch=not is_training,
                          dynamic_pad=True)
Пример #13
0
  def __call__(self, params):
    """Reads, preprocesses and batches data for TPUEstimator."""
    data_provider = dataset_data_provider.DatasetDataProvider(
        self._dataset,
        num_readers=self._num_readers,
        shuffle=self._is_training,
        num_epochs=None if self._is_training else 1
    )
    image, label, image_name, height, width = get_data(
        data_provider, self._split_name)

    if label is not None:
      if label.shape.ndims == 2:
        label = tf.expand_dims(label, 2)
      elif label.shape.ndims == 3 and label.shape.dims[2] == 1:
        pass
      else:
        raise ValueError('Input label shape must be [height, width], or '
                         '[height, width, 1].')
    label.set_shape([None, None, 1])

    crop_height, crop_width = params['crop_size']
    original_image, image, label = input_preprocess.preprocess_image_and_label(
        image,
        label,
        crop_height=crop_height,
        crop_width=crop_width,
        min_resize_value=params['min_resize_value'],
        max_resize_value=params['max_resize_value'],
        resize_factor=params['resize_factor'],
        min_scale_factor=params['min_scale_factor'],
        max_scale_factor=params['max_scale_factor'],
        scale_factor_step_size=params['scale_factor_step_size'],
        ignore_label=self._dataset.ignore_label,
        is_training=self._is_training,
        model_variant=self._model_variant)

    sample = {
        'image': image,
        'image_name': image_name,
        'height': height,
        'width': width
    }
    if label is not None:
      sample['label'] = label

    num_threads = self._num_threads
    if not self._is_training:
      # Original image is only used during visualization.
      sample['original_image'] = original_image,
      num_threads = 1

    # TODO(shizhiw): switch to tf.data.
    batch = tf.train.batch(
        sample,
        batch_size=params['batch_size'],
        num_threads=num_threads,
        capacity=32 * params['batch_size'],
        allow_smaller_final_batch=False,
        dynamic_pad=True)

    return batch['image'], batch['label']
Пример #14
0
def main(unused_argv):
    dataset = segmentation_dataset.get_dataset(FLAGS.dataset,
                                               FLAGS.train_split,
                                               dataset_dir=FLAGS.dataset_dir)

    is_training = True
    num_readers = 1
    data_provider = dataset_data_provider.DatasetDataProvider(
        dataset,
        num_readers=num_readers,
        num_epochs=None if is_training else 1,
        shuffle=is_training)

    #    items=['image','image_name','height','width','labels_class']
    #    for item in items:
    #        print(data_provider.get([item]))

    image, height, width = data_provider.get(
        [common.IMAGE, common.HEIGHT, common.WIDTH])

    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    tf.train.start_queue_runners(sess)

    np_image, np_height, np_width = sess.run([image, height, width])
    print(np_image.shape, np_height, np_width)

    # Some datasets do not contain image_name.
    if common.IMAGE_NAME in data_provider.list_items():
        image_name, = data_provider.get([common.IMAGE_NAME])
    else:
        image_name = tf.constant('')

    label = None
    if FLAGS.train_split != common.TEST_SET:
        label, = data_provider.get([common.LABELS_CLASS])

    if label is not None:
        if label.shape.ndims == 2:
            label = tf.expand_dims(label, 2)
        elif label.shape.ndims == 3 and label.shape.dims[2] == 1:
            pass
        else:
            raise ValueError('Input label shape must be [height, width], or '
                             '[height, width, 1].')

        label.set_shape([None, None, 1])
    crop_size = FLAGS.train_crop_size
    original_image, image, label = input_preprocess.preprocess_image_and_label(
        image,
        label,
        crop_height=crop_size[0],
        crop_width=crop_size[1],
        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,
        ignore_label=dataset.ignore_label,
        is_training=is_training,
        model_variant=FLAGS.model_variant)

    np_org_img, np_img, np_label = sess.run([original_image, image, label])
    print(np_org_img.shape, np_img.shape, np_label.shape)

    sample = {
        common.IMAGE: image,
        common.IMAGE_NAME: image_name,
        common.HEIGHT: height,
        common.WIDTH: width
    }
    if label is not None:
        sample[common.LABEL] = label

    num_threads = 1
    if not is_training:
        # Original image is only used during visualization.
        sample[common.ORIGINAL_IMAGE] = original_image,
        num_threads = 1

    batch_size = 2

    #    device='/device:GPU:0' if tf.test.is_gpu_available() else '/device:CPU:0'

    samples = tf.train.batch(sample,
                             batch_size=batch_size,
                             num_threads=num_threads,
                             capacity=2 * batch_size,
                             allow_smaller_final_batch=not is_training,
                             dynamic_pad=True)

    for key in samples.keys():
        print(key)

    num_clones = 2
    inputs_queue = prefetch_queue.prefetch_queue(samples,
                                                 capacity=2 * num_clones)

    samples = inputs_queue.dequeue()
    print('dequeue' + '*' * 50)
    # 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)

    print('image shape', image.shape, type(image))
    print(samples[common.IMAGE].shape)
    print(samples[common.LABEL].shape)
    print(samples[common.IMAGE].dtype)
    print(samples[common.LABEL].dtype)
    tf.train.start_queue_runners(sess)
    image = sess.run(samples[common.IMAGE])
    label = sess.run(samples[common.LABEL])
    ids = np.unique(label)

    for idx in range(batch_size):
        samples[common.IMAGE][idx, :, :, :] = samples[common.IMAGE][
            batch_size - 1 - idx, :, :, :]
    print('unique ids is: ', ids)
    print('end' + '.' * 50)

    edge = batch_get_edge(label)
    samples[common.EDGE] = tf.convert_to_tensor(edge,
                                                dtype=tf.int32,
                                                name=common.EDGE)
Пример #15
0
def get(dataset,
        crop_size,
        batch_size,
        min_resize_value=None,
        max_resize_value=None,
        resize_factor=None,
        min_scale_factor=1.,
        max_scale_factor=1.,
        scale_factor_step_size=0,
        num_readers=1,
        num_threads=1,
        dataset_split=None,
        is_training=True,
        model_variant=None):
  """Gets the dataset split for semantic segmentation.

  This functions gets the dataset split for semantic segmentation. In
  particular, it is a wrapper of (1) dataset_data_provider which returns the raw
  dataset split, (2) input_preprcess which preprocess the raw data, and (3) the
  Tensorflow operation of batching the preprocessed data. Then, the output could
  be directly used by training, evaluation or visualization.

  Args:
    dataset: An instance of slim Dataset.
    crop_size: Image crop size [height, width].
    batch_size: Batch size.
    min_resize_value: Desired size of the smaller image side.
    max_resize_value: Maximum allowed size of the larger image side.
    resize_factor: Resized dimensions are multiple of factor plus one.
    min_scale_factor: Minimum scale factor value.
    max_scale_factor: Maximum scale factor value.
    scale_factor_step_size: The step size from min scale factor to max scale
      factor. The input is randomly scaled based on the value of
      (min_scale_factor, max_scale_factor, scale_factor_step_size).
    num_readers: Number of readers for data provider.
    num_threads: Number of threads for batching data.
    dataset_split: Dataset split.
    is_training: Is training or not.
    model_variant: Model variant (string) for choosing how to mean-subtract the
      images. See feature_extractor.network_map for supported model variants.

  Returns:
    A dictionary of batched Tensors for semantic segmentation.

  Raises:
    ValueError: dataset_split is None, failed to find labels, or label shape
      is not valid.
  """
  if dataset_split is None:
    raise ValueError('Unknown dataset split.')
  if model_variant is None:
    tf.logging.warning('Please specify a model_variant. See '
                       'feature_extractor.network_map for supported model '
                       'variants.')

  data_provider = dataset_data_provider.DatasetDataProvider(
      dataset,
      num_readers=num_readers,
      num_epochs=None if is_training else 1,
      shuffle=is_training)
  image, label, image_name, height, width = _get_data(data_provider,
                                                      dataset_split)
  if label is not None:
    if label.shape.ndims == 2:
      label = tf.expand_dims(label, 2)
    elif label.shape.ndims == 3 and label.shape.dims[2] == 1:
      pass
    else:
      raise ValueError('Input label shape must be [height, width], or '
                       '[height, width, 1].')

    label.set_shape([None, None, 1])
  original_image, image, label = input_preprocess.preprocess_image_and_label(
      image,
      label,
      crop_height=crop_size[0],
      crop_width=crop_size[1],
      min_resize_value=min_resize_value,
      max_resize_value=max_resize_value,
      resize_factor=resize_factor,
      min_scale_factor=min_scale_factor,
      max_scale_factor=max_scale_factor,
      scale_factor_step_size=scale_factor_step_size,
      ignore_label=dataset.ignore_label,
      is_training=is_training,
      model_variant=model_variant)
  sample = {
      common.IMAGE: image,
      common.IMAGE_NAME: image_name,
      common.HEIGHT: height,
      common.WIDTH: width
  }
  if label is not None:
    sample[common.LABEL] = label

  if not is_training:
    # Original image is only used during visualization.
    sample[common.ORIGINAL_IMAGE] = original_image,
    num_threads = 1

  return tf.train.batch(
      sample,
      batch_size=batch_size,
      num_threads=num_threads,
      capacity=32 * batch_size,
      allow_smaller_final_batch=not is_training,
      dynamic_pad=True)
Пример #16
0
def get(data_dict,
        crop_size,
        num_threads=2,
        is_training=True,
        model_variant=None):

    batch_size = data_dict['batch_size']
    # img_names, img_paths, ann_paths, labels_mapping, project_meta = structure
    img_names = [sample.img_path for sample in data_dict['samples']]
    img_paths = [sample.img_path for sample in data_dict['samples']]
    ann_paths = [sample.ann_path for sample in data_dict['samples']]

    def load_ann(ann_path,
                 labels_mapping=data_dict['classes_mapping'],
                 project_meta=data_dict['project_meta'],
                 out_size=crop_size):
        label = ann2label(ann_path,
                          class_mapping=labels_mapping,
                          project_meta=project_meta,
                          out_size_wh=out_size)

        return label

    def read_images_and_labels(input_queue, out_size=crop_size):
        value = tf.read_file(input_queue[0])
        image = tf.image.decode_jpeg(value)
        image = tf.image.resize_images(image, out_size)
        label_part = tf.identity(input_queue[1])
        label = tf.py_func(load_ann, [label_part], tf.float32, stateful=False)
        label.set_shape(out_size)
        label = tf.cast(label, tf.float32)

        return image, label

    image_name = ops.convert_to_tensor(img_names, dtype=dtypes.string)

    images = ops.convert_to_tensor(img_paths)
    anns = ops.convert_to_tensor(ann_paths)
    input_images_and_labels_queue = tf.train.slice_input_producer(
        [images, anns], shuffle=True)

    image, label = read_images_and_labels(input_images_and_labels_queue)

    if label is not None:
        if label.shape.ndims == 2:
            label = tf.expand_dims(label, 2)
        elif label.shape.ndims == 3 and label.shape.dims[2] == 1:
            pass
        else:
            raise ValueError('Input label shape must be [height, width], or '
                             '[height, width, 1].')

        label.set_shape([None, None, 1])
    original_image, image, label = input_preprocess.preprocess_image_and_label(
        image,
        label,
        crop_height=crop_size[0],
        crop_width=crop_size[1],
        ignore_label=255,
        is_training=is_training,
        model_variant=model_variant)
    # image.set_shape([crop_size[1], crop_size[0], 3])
    # label.set_shape([crop_size[1], crop_size[0], 1])
    # label = tf.cast(label, tf.int32)

    sample = {'image': image, 'image_name': image_name, 'label': label}

    return tf.train.batch(sample,
                          batch_size=batch_size,
                          num_threads=num_threads,
                          capacity=32 * batch_size,
                          allow_smaller_final_batch=is_training,
                          dynamic_pad=True)