コード例 #1
0
def build(input_reader_config):
  """Builds a DataDecoder based only on the open source config proto.

  Args:
    input_reader_config: An input_reader_pb2.InputReader object.

  Returns:
    A DataDecoder based on the input_reader_config.

  Raises:
    ValueError: On invalid input reader proto.
  """
  if not isinstance(input_reader_config, input_reader_pb2.InputReader):
    raise ValueError('input_reader_config not of type '
                     'input_reader_pb2.InputReader.')

  if input_reader_config.WhichOneof('input_reader') == 'tf_record_input_reader':
    label_map_proto_file = None
    if input_reader_config.HasField('label_map_path'):
      label_map_proto_file = input_reader_config.label_map_path
    decoder = tf_example_decoder.TfExampleDecoder(
        load_instance_masks=input_reader_config.load_instance_masks,
        load_multiclass_scores=input_reader_config.load_multiclass_scores,
        load_context_features=input_reader_config.load_context_features,
        instance_mask_type=input_reader_config.mask_type,
        label_map_proto_file=label_map_proto_file,
        use_display_name=input_reader_config.use_display_name,
        num_additional_channels=input_reader_config.num_additional_channels,
        num_keypoints=input_reader_config.num_keypoints)

    return decoder

  raise ValueError('Unsupported input_reader_config.')
コード例 #2
0
  def testDecodeClassConfidence(self):
    image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)
    class_confidence = [0.0, 1.0, 0.0]
    example = tf.train.Example(
        features=tf.train.Features(
            feature={
                'image/encoded':
                    dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                    dataset_util.bytes_feature(six.b('jpeg')),
                'image/class/confidence':
                    dataset_util.float_list_feature(class_confidence),
            })).SerializeToString()

    example_decoder = tf_example_decoder.TfExampleDecoder()
    tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

    self.assertAllEqual(
        (tensor_dict[fields.InputDataFields.groundtruth_image_confidences]
         .get_shape().as_list()), [3])
    with self.test_session() as sess:
      tensor_dict = sess.run(tensor_dict)

    self.assertAllEqual(
        class_confidence,
        tensor_dict[fields.InputDataFields.groundtruth_image_confidences])
コード例 #3
0
  def testDecodeAdditionalChannels(self):
    image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)

    additional_channel_tensor = np.random.randint(
        256, size=(4, 5, 1)).astype(np.uint8)
    encoded_additional_channel = self._EncodeImage(additional_channel_tensor)
    decoded_additional_channel = self._DecodeImage(encoded_additional_channel)

    example = tf.train.Example(
        features=tf.train.Features(
            feature={
                'image/encoded':
                    self._BytesFeature(encoded_jpeg),
                'image/additional_channels/encoded':
                    self._BytesFeatureFromList(
                        np.array([encoded_additional_channel] * 2)),
                'image/format':
                    self._BytesFeature('jpeg'),
                'image/source_id':
                    self._BytesFeature('image_id'),
            })).SerializeToString()

    example_decoder = tf_example_decoder.TfExampleDecoder(
        num_additional_channels=2)
    tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

    with self.test_session() as sess:
      tensor_dict = sess.run(tensor_dict)
      self.assertAllEqual(
          np.concatenate([decoded_additional_channel] * 2, axis=2),
          tensor_dict[fields.InputDataFields.image_additional_channels])
コード例 #4
0
ファイル: exporter.py プロジェクト: allengun2000/roscar
def _tf_example_input_placeholder():
  tf_example_placeholder = tf.placeholder(
      tf.string, shape=[], name='tf_example')
  tensor_dict = tf_example_decoder.TfExampleDecoder().decode(
      tf_example_placeholder)
  image = tensor_dict[fields.InputDataFields.image]
  return tf.expand_dims(image, axis=0)
コード例 #5
0
def build(input_reader_config):
    """Builds a tensor dictionary based on the InputReader config.

  Args:
    input_reader_config: A input_reader_pb2.InputReader object.

  Returns:
    A tensor dict based on the input_reader_config.

  Raises:
    ValueError: On invalid input reader proto.
  """
    if not isinstance(input_reader_config, input_reader_pb2.InputReader):
        raise ValueError('input_reader_config not of type '
                         'input_reader_pb2.InputReader.')

    if input_reader_config.WhichOneof(
            'input_reader') == 'tf_record_input_reader':
        config = input_reader_config.tf_record_input_reader
        _, string_tensor = parallel_reader.parallel_read(
            config.input_path,
            reader_class=tf.TFRecordReader,
            num_epochs=(input_reader_config.num_epochs
                        if input_reader_config.num_epochs else None),
            num_readers=input_reader_config.num_readers,
            shuffle=input_reader_config.shuffle,
            dtypes=[tf.string, tf.string],
            capacity=input_reader_config.queue_capacity,
            min_after_dequeue=input_reader_config.min_after_dequeue)

        return tf_example_decoder.TfExampleDecoder().decode(string_tensor)

    raise ValueError('Unsupported input_reader_config.')
コード例 #6
0
 def decode(tf_example_string_tensor):
     tensor_dict = tf_example_decoder.TfExampleDecoder().decode(
         tf_example_string_tensor)
     image_tensor = tensor_dict[fields.InputDataFields.image]
     if input_shape is not None:
         image_tensor = tf.image.resize(image_tensor, input_shape[1:3])
     return image_tensor
コード例 #7
0
    def testDecodeJpegImage(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        decoded_jpeg = self._DecodeImage(encoded_jpeg)
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded': dataset_util.bytes_feature(encoded_jpeg),
                'image/format': dataset_util.bytes_feature(six.b('jpeg')),
                'image/source_id': dataset_util.bytes_feature(six.b(
                    'image_id')),
            })).SerializeToString()

        example_decoder = tf_example_decoder.TfExampleDecoder()
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

        self.assertAllEqual(
            (tensor_dict[fields.InputDataFields.image].get_shape().as_list()),
            [None, None, 3])
        self.assertAllEqual(
            (tensor_dict[fields.InputDataFields.original_image_spatial_shape].
             get_shape().as_list()), [2])
        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(decoded_jpeg,
                            tensor_dict[fields.InputDataFields.image])
        self.assertAllEqual(
            [4, 5],
            tensor_dict[fields.InputDataFields.original_image_spatial_shape])
        self.assertEqual(six.b('image_id'),
                         tensor_dict[fields.InputDataFields.source_id])
コード例 #8
0
  def testDecodeDefaultGroundtruthWeights(self):
    image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)
    bbox_ymins = [0.0, 4.0]
    bbox_xmins = [1.0, 5.0]
    bbox_ymaxs = [2.0, 6.0]
    bbox_xmaxs = [3.0, 7.0]
    example = tf.train.Example(features=tf.train.Features(feature={
        'image/encoded': self._BytesFeature(encoded_jpeg),
        'image/format': self._BytesFeature('jpeg'),
        'image/object/bbox/ymin': self._FloatFeature(bbox_ymins),
        'image/object/bbox/xmin': self._FloatFeature(bbox_xmins),
        'image/object/bbox/ymax': self._FloatFeature(bbox_ymaxs),
        'image/object/bbox/xmax': self._FloatFeature(bbox_xmaxs),
    })).SerializeToString()

    example_decoder = tf_example_decoder.TfExampleDecoder()
    tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

    self.assertAllEqual((tensor_dict[fields.InputDataFields.groundtruth_boxes].
                         get_shape().as_list()), [None, 4])

    with self.test_session() as sess:
      tensor_dict = sess.run(tensor_dict)

    self.assertAllClose(tensor_dict[fields.InputDataFields.groundtruth_weights],
                        np.ones(2, dtype=np.float32))
コード例 #9
0
  def testDecodePngInstanceMasks(self):
    image_tensor = np.random.randint(256, size=(10, 10, 3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)
    mask_1 = np.random.randint(0, 2, size=(10, 10, 1)).astype(np.uint8)
    mask_2 = np.random.randint(0, 2, size=(10, 10, 1)).astype(np.uint8)
    encoded_png_1 = self._EncodeImage(mask_1, encoding_type='png')
    decoded_png_1 = np.squeeze(mask_1.astype(np.float32))
    encoded_png_2 = self._EncodeImage(mask_2, encoding_type='png')
    decoded_png_2 = np.squeeze(mask_2.astype(np.float32))
    encoded_masks = [encoded_png_1, encoded_png_2]
    decoded_masks = np.stack([decoded_png_1, decoded_png_2])
    example = tf.train.Example(
        features=tf.train.Features(
            feature={
                'image/encoded': self._BytesFeature(encoded_jpeg),
                'image/format': self._BytesFeature('jpeg'),
                'image/object/mask': self._BytesFeature(encoded_masks)
            })).SerializeToString()

    example_decoder = tf_example_decoder.TfExampleDecoder(
        load_instance_masks=True, instance_mask_type=input_reader_pb2.PNG_MASKS)
    tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

    with self.test_session() as sess:
      tensor_dict = sess.run(tensor_dict)

    self.assertAllEqual(
        decoded_masks,
        tensor_dict[fields.InputDataFields.groundtruth_instance_masks])
コード例 #10
0
    def testDecodeObjectGroupOf(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        object_group_of = [0, 1]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature(six.b('jpeg')),
                'image/object/group_of':
                dataset_util.int64_list_feature(object_group_of),
            })).SerializeToString()

        example_decoder = tf_example_decoder.TfExampleDecoder()
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

        self.assertAllEqual(
            (tensor_dict[fields.InputDataFields.groundtruth_group_of].
             get_shape().as_list()), [2])
        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            [bool(item) for item in object_group_of],
            tensor_dict[fields.InputDataFields.groundtruth_group_of])
コード例 #11
0
    def testDecodeObjectWeight(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        object_weights = [0.75, 1.0]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature(six.b('jpeg')),
                'image/object/weight':
                dataset_util.float_list_feature(object_weights),
            })).SerializeToString()

        example_decoder = tf_example_decoder.TfExampleDecoder()
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

        self.assertAllEqual((tensor_dict[
            fields.InputDataFields.groundtruth_weights].get_shape().as_list()),
                            [None])
        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            object_weights,
            tensor_dict[fields.InputDataFields.groundtruth_weights])
コード例 #12
0
    def testDecodeEmptyPngInstanceMasks(self):
        image_tensor = np.random.randint(256,
                                         size=(10, 10, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        encoded_masks = []
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded': dataset_util.bytes_feature(encoded_jpeg),
                'image/format': dataset_util.bytes_feature(six.b('jpeg')),
                'image/object/mask': dataset_util.bytes_list_feature(
                    encoded_masks),
                'image/height': dataset_util.int64_feature(10),
                'image/width': dataset_util.int64_feature(10),
            })).SerializeToString()

        example_decoder = tf_example_decoder.TfExampleDecoder(
            load_instance_masks=True,
            instance_mask_type=input_reader_pb2.PNG_MASKS)
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)
            self.assertAllEqual(
                tensor_dict[
                    fields.InputDataFields.groundtruth_instance_masks].shape,
                [0, 10, 10])
コード例 #13
0
    def testDecodeEmptyMultiClassScores(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        bbox_ymins = [0.0, 4.0]
        bbox_xmins = [1.0, 5.0]
        bbox_ymaxs = [2.0, 6.0]
        bbox_xmaxs = [3.0, 7.0]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature(six.b('jpeg')),
                'image/object/bbox/ymin':
                dataset_util.float_list_feature(bbox_ymins),
                'image/object/bbox/xmin':
                dataset_util.float_list_feature(bbox_xmins),
                'image/object/bbox/ymax':
                dataset_util.float_list_feature(bbox_ymaxs),
                'image/object/bbox/xmax':
                dataset_util.float_list_feature(bbox_xmaxs),
            })).SerializeToString()

        example_decoder = tf_example_decoder.TfExampleDecoder(
            load_multiclass_scores=True)
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))
        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)
        self.assertEqual(
            0, tensor_dict[fields.InputDataFields.multiclass_scores].size)
コード例 #14
0
    def testDecodeObjectLabel(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        bbox_classes = [0, 1]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature('jpeg'),
                'image/object/class/label':
                dataset_util.int64_list_feature(bbox_classes),
            })).SerializeToString()

        example_decoder = tf_example_decoder.TfExampleDecoder()
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

        self.assertAllEqual((tensor_dict[
            fields.InputDataFields.groundtruth_classes].get_shape().as_list()),
                            [2])

        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            bbox_classes,
            tensor_dict[fields.InputDataFields.groundtruth_classes])
コード例 #15
0
  def _predict_input_fn(params=None):
    """Decodes serialized tf.Examples and returns `ServingInputReceiver`.

    Args:
      params: Parameter dictionary passed from the estimator.

    Returns:
      `ServingInputReceiver`.
    """
    del params
    example = tf.placeholder(dtype=tf.string, shape=[], name='input_feature')

    num_classes = config_util.get_number_of_classes(model_config)
    model = model_builder.build(model_config, is_training=False)
    image_resizer_config = config_util.get_image_resizer_config(model_config)
    image_resizer_fn = image_resizer_builder.build(image_resizer_config)

    transform_fn = functools.partial(
        transform_input_data, model_preprocess_fn=model.preprocess,
        image_resizer_fn=image_resizer_fn,
        num_classes=num_classes,
        data_augmentation_fn=None)

    decoder = tf_example_decoder.TfExampleDecoder(load_instance_masks=False)
    input_dict = transform_fn(decoder.decode(example))
    images = tf.to_float(input_dict[fields.InputDataFields.image])
    images = tf.expand_dims(images, axis=0)

    return tf.estimator.export.ServingInputReceiver(
        features={fields.InputDataFields.image: images},
        receiver_tensors={SERVING_FED_EXAMPLE_KEY: example})
コード例 #16
0
def main(_):
    file = FLAGS.file
    no_fail = FLAGS.no_fail
    num_threads = 10
    print(file)
    example_decoder = tf_example_decoder.TfExampleDecoder()
    dataset = tf.data.TFRecordDataset(file)
    dataset = dataset.map(TFReader._parse_example_encoded,
                          num_parallel_calls=num_threads)
    # dataset = dataset.map(tf.convert_to_tensor, num_parallel_calls=num_threads)
    # dataset = dataset.map(example_decoder.decode, num_parallel_calls=num_threads)
    iterator = dataset.make_one_shot_iterator()
    feeder = iterator.get_next()

    sess = tf.Session(config=tf.ConfigProto(
        device_count={'GPU': 0}))  # don't need GPU to check the dataset

    failed, all_count = check_feeder(sess, feeder, no_fail)
    if failed:
        print('there is {} faulty samples of {} in data'.format(
            len(failed), all_count))
        print('failures info:')
        print(failed)
    else:
        print('No error in data of {} samples.'.format(all_count))
コード例 #17
0
  def testInstancesNotAvailableByDefault(self):
    num_instances = 4
    image_height = 5
    image_width = 3
    # Randomly generate image.
    image_tensor = np.random.randint(256, size=(image_height,
                                                image_width,
                                                3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)

    # Randomly generate instance segmentation masks.
    instance_masks = (
        np.random.randint(2, size=(num_instances,
                                   image_height,
                                   image_width)).astype(np.float32))
    instance_masks_flattened = np.reshape(instance_masks, [-1])

    # Randomly generate class labels for each instance.
    object_classes = np.random.randint(
        100, size=(num_instances)).astype(np.int64)

    example = tf.train.Example(features=tf.train.Features(feature={
        'image/encoded': self._BytesFeature(encoded_jpeg),
        'image/format': self._BytesFeature('jpeg'),
        'image/height': self._Int64Feature([image_height]),
        'image/width': self._Int64Feature([image_width]),
        'image/object/mask': self._FloatFeature(instance_masks_flattened),
        'image/object/class/label': self._Int64Feature(
            object_classes)})).SerializeToString()
    example_decoder = tf_example_decoder.TfExampleDecoder()
    tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))
    self.assertTrue(fields.InputDataFields.groundtruth_instance_masks
                    not in tensor_dict)
コード例 #18
0
def build(input_reader_config, batch_size=None, transform_input_data_fn=None):

    if not isinstance(input_reader_config, input_reader_pb2.InputReader):
        raise ValueError('input_reader_config not of type '
                         'input_reader_pb2.InputReader.')

    if input_reader_config.WhichOneof(
            'input_reader') == 'tf_record_input_reader':
        config = input_reader_config.tf_record_input_reader
        if not config.input_path:
            raise ValueError('At least one input path must be specified in '
                             '`input_reader_config`.')

        label_map_proto_file = None
        if input_reader_config.HasField('label_map_path'):
            label_map_proto_file = input_reader_config.label_map_path
        decoder = tf_example_decoder.TfExampleDecoder(
            load_instance_masks=input_reader_config.load_instance_masks,
            load_multiclass_scores=input_reader_config.load_multiclass_scores,
            instance_mask_type=input_reader_config.mask_type,
            label_map_proto_file=label_map_proto_file,
            use_display_name=input_reader_config.use_display_name,
            num_additional_channels=input_reader_config.num_additional_channels
        )

        def process_fn(value):
            """Sets up tf graph that decodes, transforms and pads input data."""
            processed_tensors = decoder.decode(value)
            if transform_input_data_fn is not None:
                processed_tensors = transform_input_data_fn(processed_tensors)
            return processed_tensors

        dataset = read_dataset(
            functools.partial(tf.data.TFRecordDataset,
                              buffer_size=8 * 1000 * 1000),
            config.input_path[:], input_reader_config)
        if input_reader_config.sample_1_of_n_examples > 1:
            dataset = dataset.shard(input_reader_config.sample_1_of_n_examples,
                                    0)
        # TODO(rathodv): make batch size a required argument once the old binaries
        # are deleted.
        if batch_size:
            num_parallel_calls = batch_size * input_reader_config.num_parallel_batches
        else:
            num_parallel_calls = input_reader_config.num_parallel_map_calls
        # TODO(b/123952794): Migrate to V2 function.
        if hasattr(dataset, 'map_with_legacy_function'):
            data_map_fn = dataset.map_with_legacy_function
        else:
            data_map_fn = dataset.map
        dataset = data_map_fn(process_fn,
                              num_parallel_calls=num_parallel_calls)
        if batch_size:
            dataset = dataset.apply(
                tf.contrib.data.batch_and_drop_remainder(batch_size))
        dataset = dataset.prefetch(input_reader_config.num_prefetch_batches)
        return dataset

    raise ValueError('Unsupported input_reader_config.')
コード例 #19
0
    def testDecodeInstanceSegmentation(self):
        num_instances = 4
        image_height = 5
        image_width = 3

        # Randomly generate image.
        image_tensor = np.random.randint(256,
                                         size=(image_height, image_width,
                                               3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)

        # Randomly generate instance segmentation masks.
        instance_masks = (np.random.randint(2,
                                            size=(num_instances, image_height,
                                                  image_width)).astype(
                                                      np.float32))
        instance_masks_flattened = np.reshape(instance_masks, [-1])

        # Randomly generate class labels for each instance.
        object_classes = np.random.randint(100, size=(num_instances)).astype(
            np.int64)

        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature(six.b('jpeg')),
                'image/height':
                dataset_util.int64_feature(image_height),
                'image/width':
                dataset_util.int64_feature(image_width),
                'image/object/mask':
                dataset_util.float_list_feature(instance_masks_flattened),
                'image/object/class/label':
                dataset_util.int64_list_feature(object_classes)
            })).SerializeToString()
        example_decoder = tf_example_decoder.TfExampleDecoder(
            load_instance_masks=True)
        tensor_dict = example_decoder.decode(
            tf.convert_to_tensor(value=example))

        self.assertAllEqual(
            (tensor_dict[fields.InputDataFields.groundtruth_instance_masks].
             get_shape().as_list()), [4, 5, 3])

        self.assertAllEqual((tensor_dict[
            fields.InputDataFields.groundtruth_classes].get_shape().as_list()),
                            [4])

        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            instance_masks.astype(np.float32),
            tensor_dict[fields.InputDataFields.groundtruth_instance_masks])
        self.assertAllEqual(
            object_classes,
            tensor_dict[fields.InputDataFields.groundtruth_classes])
コード例 #20
0
  def testDecodeKeypointNoVisibilities(self):
    image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)
    bbox_ymins = [0.0, 4.0]
    bbox_xmins = [1.0, 5.0]
    bbox_ymaxs = [2.0, 6.0]
    bbox_xmaxs = [3.0, 7.0]
    keypoint_ys = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    keypoint_xs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
    example = tf.train.Example(
        features=tf.train.Features(
            feature={
                'image/encoded':
                    dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                    dataset_util.bytes_feature(six.b('jpeg')),
                'image/object/bbox/ymin':
                    dataset_util.float_list_feature(bbox_ymins),
                'image/object/bbox/xmin':
                    dataset_util.float_list_feature(bbox_xmins),
                'image/object/bbox/ymax':
                    dataset_util.float_list_feature(bbox_ymaxs),
                'image/object/bbox/xmax':
                    dataset_util.float_list_feature(bbox_xmaxs),
                'image/object/keypoint/y':
                    dataset_util.float_list_feature(keypoint_ys),
                'image/object/keypoint/x':
                    dataset_util.float_list_feature(keypoint_xs),
            })).SerializeToString()

    example_decoder = tf_example_decoder.TfExampleDecoder(num_keypoints=3)
    tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

    self.assertAllEqual((tensor_dict[fields.InputDataFields.groundtruth_boxes]
                         .get_shape().as_list()), [None, 4])
    self.assertAllEqual(
        (tensor_dict[fields.InputDataFields.groundtruth_keypoints].get_shape()
         .as_list()), [2, 3, 2])
    with self.test_session() as sess:
      tensor_dict = sess.run(tensor_dict)

    expected_boxes = np.vstack([bbox_ymins, bbox_xmins, bbox_ymaxs,
                                bbox_xmaxs]).transpose()
    self.assertAllEqual(expected_boxes,
                        tensor_dict[fields.InputDataFields.groundtruth_boxes])

    expected_keypoints = (
        np.vstack([keypoint_ys, keypoint_xs]).transpose().reshape((2, 3, 2)))
    self.assertAllEqual(
        expected_keypoints,
        tensor_dict[fields.InputDataFields.groundtruth_keypoints])

    expected_visibility = np.ones((2, 3))
    self.assertAllEqual(
        expected_visibility,
        tensor_dict[fields.InputDataFields.groundtruth_keypoint_visibilities])
コード例 #21
0
ファイル: input_reader_builder.py プロジェクト: Asharib90/OCR
def build(input_reader_config):
    """Builds a tensor dictionary based on the InputReader config.

  Args:
    input_reader_config: A input_reader_pb2.InputReader object.

  Returns:
    A tensor dict based on the input_reader_config.

  Raises:
    ValueError: On invalid input reader proto.
    ValueError: If no input paths are specified.
  """
    if not isinstance(input_reader_config, input_reader_pb2.InputReader):
        raise ValueError('input_reader_config not of type '
                         'input_reader_pb2.InputReader.')

    if input_reader_config.WhichOneof(
            'input_reader') == 'tf_record_input_reader':
        config = input_reader_config.tf_record_input_reader
        if not config.input_path:
            raise ValueError('At least one input path must be specified in '
                             '`input_reader_config`.')
        _, string_tensor = parallel_reader.parallel_read(
            config.input_path[:],  # Convert `RepeatedScalarContainer` to list.
            reader_class=tf.TFRecordReader,
            num_epochs=(input_reader_config.num_epochs
                        if input_reader_config.num_epochs else None),
            num_readers=input_reader_config.num_readers,
            shuffle=input_reader_config.shuffle,
            dtypes=[tf.string, tf.string],
            capacity=input_reader_config.queue_capacity,
            min_after_dequeue=input_reader_config.min_after_dequeue)

        label_map_proto_file = None
        if input_reader_config.HasField('label_map_path'):
            label_map_proto_file = input_reader_config.label_map_path
        input_type = input_reader_config.input_type
        if input_type == input_reader_pb2.InputType.Value('TF_EXAMPLE'):
            decoder = tf_example_decoder.TfExampleDecoder(
                load_instance_masks=input_reader_config.load_instance_masks,
                instance_mask_type=input_reader_config.mask_type,
                label_map_proto_file=label_map_proto_file,
                load_context_features=input_reader_config.load_context_features
            )
            return decoder.decode(string_tensor)
        elif input_type == input_reader_pb2.InputType.Value(
                'TF_SEQUENCE_EXAMPLE'):
            decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder(
                label_map_proto_file=label_map_proto_file,
                load_context_features=input_reader_config.load_context_features
            )
            return decoder.decode(string_tensor)
        raise ValueError('Unsupported input_type.')
    raise ValueError('Unsupported input_reader_config.')
コード例 #22
0
    def testDecodeInstanceSegmentation(self):
        num_instances = 4
        image_height = 5
        image_width = 3

        # Randomly generate image.
        image_tensor = np.random.randint(255,
                                         size=(image_height, image_width,
                                               3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)

        # Randomly generate instance segmentation masks.
        instance_segmentation = (np.random.randint(
            2,
            size=(num_instances, image_height, image_width)).astype(np.int64))

        # Randomly generate class labels for each instance.
        instance_segmentation_classes = np.random.randint(
            100, size=(num_instances)).astype(np.int64)

        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                self._BytesFeature(encoded_jpeg),
                'image/format':
                self._BytesFeature('jpeg'),
                'image/height':
                self._Int64Feature([image_height]),
                'image/width':
                self._Int64Feature([image_width]),
                'image/segmentation/object':
                self._Int64Feature(instance_segmentation.flatten()),
                'image/segmentation/object/class':
                self._Int64Feature(instance_segmentation_classes)
            })).SerializeToString()
        example_decoder = tf_example_decoder.TfExampleDecoder()
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

        self.assertAllEqual(
            (tensor_dict[fields.InputDataFields.groundtruth_instance_masks].
             get_shape().as_list()), [None, None, None])

        self.assertAllEqual(
            (tensor_dict[fields.InputDataFields.groundtruth_instance_classes].
             get_shape().as_list()), [None])

        with self.test_session() as sess:
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            instance_segmentation.astype(np.bool),
            tensor_dict[fields.InputDataFields.groundtruth_instance_masks])
        self.assertAllEqual(
            instance_segmentation_classes,
            tensor_dict[fields.InputDataFields.groundtruth_instance_classes])
コード例 #23
0
ファイル: decoder_builder.py プロジェクト: zss1980/models
def build(input_reader_config):
    """Builds a DataDecoder based only on the open source config proto.

  Args:
    input_reader_config: An input_reader_pb2.InputReader object.

  Returns:
    A DataDecoder based on the input_reader_config.

  Raises:
    ValueError: On invalid input reader proto.
  """
    if not isinstance(input_reader_config, input_reader_pb2.InputReader):
        raise ValueError('input_reader_config not of type '
                         'input_reader_pb2.InputReader.')

    if input_reader_config.WhichOneof(
            'input_reader') == 'tf_record_input_reader':
        label_map_proto_file = None
        if input_reader_config.HasField('label_map_path'):
            label_map_proto_file = input_reader_config.label_map_path
        input_type = input_reader_config.input_type
        if input_type == input_reader_pb2.InputType.Value('TF_EXAMPLE'):
            decoder = tf_example_decoder.TfExampleDecoder(
                load_instance_masks=input_reader_config.load_instance_masks,
                load_multiclass_scores=input_reader_config.
                load_multiclass_scores,
                load_context_features=input_reader_config.
                load_context_features,
                instance_mask_type=input_reader_config.mask_type,
                label_map_proto_file=label_map_proto_file,
                use_display_name=input_reader_config.use_display_name,
                num_additional_channels=input_reader_config.
                num_additional_channels,
                num_keypoints=input_reader_config.num_keypoints,
                expand_hierarchy_labels=input_reader_config.
                expand_labels_hierarchy,
                load_dense_pose=input_reader_config.load_dense_pose,
                load_track_id=input_reader_config.load_track_id)
            return decoder
        elif input_type == input_reader_pb2.InputType.Value(
                'TF_SEQUENCE_EXAMPLE'):
            decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder(
                label_map_proto_file=label_map_proto_file,
                load_context_features=input_reader_config.
                load_context_features,
                load_context_image_ids=input_reader_config.
                load_context_image_ids)
            return decoder
        raise ValueError('Unsupported input_type in config.')

    raise ValueError('Unsupported input_reader_config.')
コード例 #24
0
    def minibatch(self,
                  dataset,
                  subset,
                  use_datasets,
                  cache_data,
                  shift_ratio=-1):
        if shift_ratio < 0:
            shift_ratio = self.shift_ratio
        self.example_decoder = tf_example_decoder.TfExampleDecoder()

        with tf.name_scope('batch_processing'):
            images = [[] for _ in range(self.num_splits)]
            labels = [[] for _ in range(self.num_splits)]

            glob_pattern = dataset.tf_record_pattern(subset)
            filenames = gfile.Glob(glob_pattern)
            ssd_input = ssd_dataloader.SSDInputReader(filenames,
                                                      subset == 'train')
            ds = ssd_input({
                'batch_size_per_split': self.batch_size_per_split,
                'num_splits': self.num_splits
            })
            ds_iterator = data_utils.create_iterator(ds)
            for d in range(self.num_splits):
                images[d], labels[d] = ds_iterator.get_next()
            for split_index in xrange(self.num_splits):
                images[split_index] = tf.reshape(images[split_index],
                                                 shape=[
                                                     self.batch_size_per_split,
                                                     self.height, self.width,
                                                     self.depth
                                                 ])
                labels[split_index] = tf.reshape(
                    labels[split_index],
                    # Encoded tensor with object category, number of bounding boxes and
                    # their locations. The 0th dimension is batch size, and for each
                    # item in batch the tensor looks like this ((x, y, w, h) is the
                    # cordinates and size of a bounding box, c is class):
                    #
                    # [[x,      y,      w,      h,      c     ],       ^
                    #  [x,      y,      w,      h,      c     ],       |
                    #  [...,    ...,    ...,    ...,    ...   ],  NUM_SSD_BOXES+1
                    #  [x,      y,      w,      h,      c     ],       |
                    #  [nboxes, nboxes, nboxes, nboxes, nboxes]]       v
                    #
                    # |<---------- 4 cordinates + 1 ---------->|
                    shape=[
                        self.batch_size_per_split,
                        ssd_constants.NUM_SSD_BOXES + 1, 5
                    ])
            return images, labels
コード例 #25
0
  def testDecodeImageKeyAndFilename(self):
    image_tensor = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)
    example = tf.train.Example(features=tf.train.Features(feature={
        'image/encoded': self._BytesFeature(encoded_jpeg),
        'image/key/sha256': self._BytesFeature('abc'),
        'image/filename': self._BytesFeature('filename')
    })).SerializeToString()

    example_decoder = tf_example_decoder.TfExampleDecoder()
    tensor_dict = example_decoder.Decode(tf.convert_to_tensor(example))

    with self.test_session() as sess:
      tensor_dict = sess.run(tensor_dict)

    self.assertEqual('abc', tensor_dict[fields.InputDataFields.key])
    self.assertEqual('filename', tensor_dict[fields.InputDataFields.filename])
コード例 #26
0
    def testDecodeObjectLabelWithText(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        bbox_classes_text = [six.b('cat'), six.b('dog')]
        # Annotation label gets overridden by labelmap id.
        annotated_bbox_classes = [3, 4]
        expected_bbox_classes = [1, 2]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature(six.b('jpeg')),
                'image/object/class/text':
                dataset_util.bytes_list_feature(bbox_classes_text),
                'image/object/class/label':
                dataset_util.int64_list_feature(annotated_bbox_classes),
            })).SerializeToString()
        label_map_string = """
      item {
        id:1
        name:'cat'
      }
      item {
        id:2
        name:'dog'
      }
    """
        label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt')
        with tf.io.gfile.GFile(label_map_path, 'wb') as f:
            f.write(label_map_string)

        example_decoder = tf_example_decoder.TfExampleDecoder(
            label_map_proto_file=label_map_path)
        tensor_dict = example_decoder.decode(
            tf.convert_to_tensor(value=example))

        init = tf.compat.v1.tables_initializer()
        with self.test_session() as sess:
            sess.run(init)
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            expected_bbox_classes,
            tensor_dict[fields.InputDataFields.groundtruth_classes])
コード例 #27
0
def build(input_reader_config):
    """Builds a tensor dictionary based on the InputReader config.

    Args:
      input_reader_config: A input_reader_pb2.InputReader object.

    Returns:
      A tensor dict based on the input_reader_config.

    Raises:
      ValueError: On invalid input reader proto.
      ValueError: If no input paths are specified.
    """
    if not isinstance(input_reader_config, input_reader_pb2.InputReader):
        raise ValueError('input_reader_config not of type '
                         'input_reader_pb2.InputReader.')

    if input_reader_config.WhichOneof(
            'input_reader') == 'tf_record_input_reader':
        config = input_reader_config.tf_record_input_reader
        if not config.input_path:
            raise ValueError('At least one input path must be specified in '
                             '`input_reader_config`.')
        # shuffle is True => the common_queue would be a RandomShuffleQueue otherwise
        # shuffle is False =>  it would be a FIFOQueue.
        _, string_tensor = parallel_reader.parallel_read(
            config.input_path[:],  # Convert `RepeatedScalarContainer` to list.
            reader_class=tf.TFRecordReader,
            num_epochs=(input_reader_config.num_epochs
                        if input_reader_config.num_epochs else None),
            num_readers=input_reader_config.num_readers,
            shuffle=input_reader_config.shuffle,
            dtypes=[tf.string, tf.string],
            capacity=input_reader_config.queue_capacity,
            min_after_dequeue=input_reader_config.min_after_dequeue)

        label_map_proto_file = None
        if input_reader_config.HasField('label_map_path'):
            label_map_proto_file = input_reader_config.label_map_path
        decoder = tf_example_decoder.TfExampleDecoder(
            load_instance_masks=input_reader_config.load_instance_masks,
            label_map_proto_file=label_map_proto_file)
        return decoder.decode(string_tensor)

    raise ValueError('Unsupported input_reader_config.')
コード例 #28
0
    def testDecodeObjectLabelUnrecognizedNameWithMappingWithDisplayName(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        bbox_classes_text = [six.b('cat'), six.b('cheetah')]
        bbox_classes_id = [5, 6]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature(six.b('jpeg')),
                'image/object/class/text':
                dataset_util.bytes_list_feature(bbox_classes_text),
                'image/object/class/label':
                dataset_util.int64_list_feature(bbox_classes_id),
            })).SerializeToString()

        label_map_string = """
      item {
        name:'/m/cat'
        id:3
        display_name:'cat'
      }
      item {
        name:'/m/dog'
        id:1
        display_name:'dog'
      }
    """
        label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt')
        with tf.io.gfile.GFile(label_map_path, 'wb') as f:
            f.write(label_map_string)
        example_decoder = tf_example_decoder.TfExampleDecoder(
            label_map_proto_file=label_map_path)
        tensor_dict = example_decoder.decode(
            tf.convert_to_tensor(value=example))

        with self.test_session() as sess:
            sess.run(tf.compat.v1.tables_initializer())
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            [3, -1], tensor_dict[fields.InputDataFields.groundtruth_classes])
コード例 #29
0
    def testDecodeObjectLabelNoText(self):
        image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
        encoded_jpeg = self._EncodeImage(image_tensor)
        bbox_classes = [1, 2]
        example = tf.train.Example(features=tf.train.Features(
            feature={
                'image/encoded':
                dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                dataset_util.bytes_feature('jpeg'),
                'image/object/class/label':
                dataset_util.int64_list_feature(bbox_classes),
            })).SerializeToString()
        label_map_string = """
      item {
        id:1
        name:'cat'
      }
      item {
        id:2
        name:'dog'
      }
    """
        label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt')
        with tf.gfile.Open(label_map_path, 'wb') as f:
            f.write(label_map_string)

        example_decoder = tf_example_decoder.TfExampleDecoder(
            label_map_proto_file=label_map_path)
        tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))

        self.assertAllEqual((tensor_dict[
            fields.InputDataFields.groundtruth_classes].get_shape().as_list()),
                            [None])

        init = tf.tables_initializer()
        with self.test_session() as sess:
            sess.run(init)
            tensor_dict = sess.run(tensor_dict)

        self.assertAllEqual(
            bbox_classes,
            tensor_dict[fields.InputDataFields.groundtruth_classes])
コード例 #30
0
  def testDecodeContextFeatures(self):
    image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8)
    encoded_jpeg = self._EncodeImage(image_tensor)
    bbox_ymins = [0.0, 4.0]
    bbox_xmins = [1.0, 5.0]
    bbox_ymaxs = [2.0, 6.0]
    bbox_xmaxs = [3.0, 7.0]
    num_features = 8
    context_feature_length = 10
    context_features = np.random.random(num_features*context_feature_length)
    example = tf.train.Example(
        features=tf.train.Features(
            feature={
                'image/encoded':
                    dataset_util.bytes_feature(encoded_jpeg),
                'image/format':
                    dataset_util.bytes_feature(six.b('jpeg')),
                'image/context_features':
                    dataset_util.float_list_feature(context_features),
                'image/context_feature_length':
                    dataset_util.int64_feature(context_feature_length),
                'image/object/bbox/ymin':
                    dataset_util.float_list_feature(bbox_ymins),
                'image/object/bbox/xmin':
                    dataset_util.float_list_feature(bbox_xmins),
                'image/object/bbox/ymax':
                    dataset_util.float_list_feature(bbox_ymaxs),
                'image/object/bbox/xmax':
                    dataset_util.float_list_feature(bbox_xmaxs),
            })).SerializeToString()

    example_decoder = tf_example_decoder.TfExampleDecoder(
        load_context_features=True)
    tensor_dict = example_decoder.decode(tf.convert_to_tensor(example))
    with self.test_session() as sess:
      tensor_dict = sess.run(tensor_dict)
    self.assertAllClose(
        context_features.reshape(num_features, context_feature_length),
        tensor_dict[fields.InputDataFields.context_features])
    self.assertAllEqual(
        context_feature_length,
        tensor_dict[fields.InputDataFields.context_feature_length])