def get_split(split_name, dataset_dir=None):
    """Gets a dataset tuple with instructions for reading cifar100.

  Args:
    split_name: A train/test split name.
    dataset_dir: The base directory of the dataset sources.

  Returns:
    A `Dataset` namedtuple. Image tensors are integers in [0, 255].

  Raises:
    ValueError: if `split_name` is not a valid train/test split.
  """
    if split_name not in _SPLITS_TO_SIZES:
        raise ValueError('split name %s was not recognized.' % split_name)

    file_pattern = os.path.join(dataset_dir, _FILE_PATTERN % split_name)

    keys_to_features = {
        'image/encoded':
        tf.FixedLenFeature((), tf.string, default_value=''),
        'image/format':
        tf.FixedLenFeature((), tf.string, default_value=''),
        'image/class/label':
        tf.FixedLenFeature([1],
                           tf.int64,
                           default_value=tf.zeros([1], dtype=tf.int64)),
        'image/class/fine_label':
        tf.FixedLenFeature([1],
                           tf.int64,
                           default_value=tf.zeros([1], dtype=tf.int64)),
    }

    if split_name == 'train':
        items_to_handlers = {
            'image': tfexample_decoder.Image(shape=[32, 32, 3]),
            'label': tfexample_decoder.Tensor('image/class/label'),
        }
    else:
        items_to_handlers = {
            'image': tfexample_decoder.Image(shape=[32, 32, 3]),
            'label': tfexample_decoder.Tensor('image/class/fine_label'),
        }

    decoder = tfexample_decoder.TFExampleDecoder(keys_to_features,
                                                 items_to_handlers)

    return dataset.Dataset(data_sources=file_pattern,
                           reader=tf.TFRecordReader,
                           decoder=decoder,
                           num_samples=_SPLITS_TO_SIZES[split_name],
                           num_classes=_NUM_CLASSES,
                           items_to_descriptions=_ITEMS_TO_DESCRIPTIONS)
def _create_tfrecord_dataset(tmpdir):
  if not gfile.Exists(tmpdir):
    gfile.MakeDirs(tmpdir)

  data_sources = test_utils.create_tfrecord_files(tmpdir, num_files=1)

  keys_to_features = {
      'image/encoded':
          parsing_ops.FixedLenFeature(
              shape=(), dtype=dtypes.string, default_value=''),
      'image/format':
          parsing_ops.FixedLenFeature(
              shape=(), dtype=dtypes.string, default_value='jpeg'),
      'image/class/label':
          parsing_ops.FixedLenFeature(
              shape=[1],
              dtype=dtypes.int64,
              default_value=array_ops.zeros(
                  [1], dtype=dtypes.int64))
  }

  items_to_handlers = {
      'image': tfexample_decoder.Image(),
      'label': tfexample_decoder.Tensor('image/class/label'),
  }

  decoder = tfexample_decoder.TFExampleDecoder(keys_to_features,
                                               items_to_handlers)

  return dataset.Dataset(
      data_sources=data_sources,
      reader=io_ops.TFRecordReader,
      decoder=decoder,
      num_samples=100,
      items_to_descriptions=None)
    def create_dataset(self, data_source):
        keys_to_features = {
        'image/encoded': tf.FixedLenFeature(shape=(), dtype=tf.string, default_value=''),
        'image/format': tf.FixedLenFeature(shape=(), dtype=tf.string, default_value='png'),
        'image/object/bbox/xmin': tf.FixedLenFeature(shape=(), dtype= tf.string, default_value=''),
        'image/object/bbox/xmax': tf.FixedLenFeature(shape=(), dtype= tf.string, default_value=''),
        'image/object/bbox/ymin': tf.FixedLenFeature(shape=(), dtype= tf.string, default_value=''),
        'image/object/bbox/ymax': tf.FixedLenFeature(shape=(), dtype= tf.string, default_value=''),
        'image/object/bbox/label': tf.FixedLenFeature(shape=(), dtype = tf.string, default_value=''),
        'image/object/size': tf.FixedLenFeature(shape=(), dtype = tf.int64, default_value=0)
        }

        items_to_handlers = {
        'image': tfexample_decoder.Image(),
        'xmin': tfexample_decoder.Tensor('image/object/bbox/xmin'),
        'xmax': tfexample_decoder.Tensor('image/object/bbox/xmax'),
        'ymin': tfexample_decoder.Tensor('image/object/bbox/ymin'),
        'ymax': tfexample_decoder.Tensor('image/object/bbox/ymax'),
        #'bbox': tfexample_decoder.BoundingBox(prefix='image/object/bbox/'),
        'label': tfexample_decoder.Tensor('image/object/bbox/label'),
        'size': tfexample_decoder.Tensor('image/object/size')
        }

        decoder = tfexample_decoder.TFExampleDecoder(keys_to_features, items_to_handlers)
        return dataset.Dataset(data_sources=data_source, reader = tf.TFRecordReader,
            decoder=decoder, num_samples = 1, items_to_descriptions=None)
Beispiel #4
0
def get_split(split_name='train',
              dataset_dir=None,
              num_classes_per_attribute=None):
  """Gets a dataset tuple with instructions for reading 2D shapes data.

  Args:
    split_name: A train/test split name.
    dataset_dir: The base directory of the dataset sources.
    num_classes_per_attribute: The number of labels for the classfication
      problem corresponding to each attribute. For example, if the first
      attribute is "shape" and there are three possible shapes, then
      then provide a value 3 in the first index, and so on.

  Returns:
    A `Dataset` namedtuple.
    metadata: A dictionary with some metadata about the dataset we just
      constructed.

  Raises:
    ValueError: if `split_name` is not a valid train/test split.
  """
  if split_name not in _SPLITS_TO_SIZES:
    raise ValueError('split name %s was not recognized.' % split_name)

  if num_classes_per_attribute is None:
    num_classes_per_attribute = _NUM_CLASSES_PER_ATTRIBUTE

  if dataset_dir is None:
    dataset_dir = _DATASET_DIR

  file_pattern = os.path.join(dataset_dir, _FILE_PATTERN % (_SPLIT_TYPE, split_name))

  keys_to_features = {
      'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''),
      'image/format': tf.FixedLenFeature((), tf.string, default_value='raw'),
      'labels': tf.FixedLenFeature([len(num_classes_per_attribute)], tf.int64),
      'latents': tf.FixedLenFeature([_NUM_LATENTS], tf.float32),
  }

  items_to_handlers = {
      'image': tfexample_decoder.Image(shape=[64, 64, 3]),
      'labels': tfexample_decoder.Tensor('labels'),
      'latents': tfexample_decoder.Tensor('latents'),
  }

  decoder = tfexample_decoder.TFExampleDecoder(keys_to_features,
                                               items_to_handlers)

  metadata = {
      'num_classes_per_attribute': num_classes_per_attribute,
      'split_type': _SPLIT_TYPE
  }

  return dataset.Dataset(
      data_sources=file_pattern,
      reader=tf.TFRecordReader,
      decoder=decoder,
      num_samples=_SPLITS_TO_SIZES[split_name],
      items_to_descriptions=_ITEMS_TO_DESCRIPTIONS), metadata
Beispiel #5
0
 def test_decode_example_with_jpeg_encoding_at_16Bit_causes_error(self):
     image_shape = (2, 3, 3)
     unused_image, serialized_example = self.generate_image(
         image_format='jpeg', image_shape=image_shape)
     with self.assertRaises((TypeError, ValueError)):
         self.run_decode_example(serialized_example,
                                 tfexample_decoder.Image(dtype=dtypes.uint16),
                                 image_format='jpeg')
 def testDecodeExampleWithJpegEncodingAt16BitCausesError(self):
     image_shape = (2, 3, 3)
     unused_image, serialized_example = self.GenerateImage(
         image_format='jpeg', image_shape=image_shape)
     with self.assertRaises(TypeError):
         unused_decoded_image = self.RunDecodeExample(
             serialized_example,
             tfexample_decoder.Image(dtype=dtypes.uint16),
             image_format='jpeg')
Beispiel #7
0
    def test_decode_example_with_RAW_encoding(self):
        image_shape = (2, 3, 3)
        image, serialized_example = self.generate_image(image_format='RAW', image_shape=image_shape)

        decoded_image = self.run_decode_example(
            serialized_example,
            tfexample_decoder.Image(shape=image_shape),
            image_format='RAW')

        self.assertAllClose(image, decoded_image, atol=0)
Beispiel #8
0
    def test_decode_example_with_jpeg_encoding(self):
        image_shape = (2, 3, 3)
        image, serialized_example = self.generate_image(image_format='jpeg',
                                                        image_shape=image_shape)

        decoded_image = self.run_decode_example(
            serialized_example, tfexample_decoder.Image(), image_format='jpeg')

        # Need to use a tolerance of 1 because of noise in the jpeg encode/decode
        self.assertAllClose(image, decoded_image, atol=1.001)
 def testDecodeExampleWithJpegEncodingAt16BitDoesNotCauseError(self):
     image_shape = (2, 3, 3)
     # Image has type uint8 but decoding at uint16 should not cause problems.
     image, serialized_example = self.GenerateImage(image_format='jpeg',
                                                    image_shape=image_shape)
     decoded_image = self.RunDecodeExample(
         serialized_example,
         tfexample_decoder.Image(dtype=dtypes.uint16),
         image_format='jpeg')
     self.assertAllClose(image, decoded_image, atol=1.001)
    def testDecodeExampleWithRAWEncoding(self):
        image_shape = (2, 3, 3)
        image, serialized_example = self.GenerateImage(image_format='RAW',
                                                       image_shape=image_shape)

        decoded_image = self.RunDecodeExample(
            serialized_example,
            tfexample_decoder.Image(shape=image_shape),
            image_format='RAW')

        self.assertAllClose(image, decoded_image, atol=0)
Beispiel #11
0
  def testDecodeExampleWithRawEncodingFloatDtype(self):
    image_shape = (2, 3, 3)
    image, serialized_example = self.GenerateImage(
        image_format='raw', image_shape=image_shape, image_dtype=np.float32)

    decoded_image = self.RunDecodeExample(
        serialized_example,
        tfexample_decoder.Image(shape=image_shape, dtype=dtypes.float32),
        image_format='raw')

    self.assertAllClose(image, decoded_image, atol=0)
 def testDecodeExampleWithJpegEncodingAt16BitCausesError(self):
     image_shape = (2, 3, 3)
     unused_image, serialized_example = self.GenerateImage(
         image_format='jpeg', image_shape=image_shape)
     # decode_raw support uint16 now so ValueError will be thrown instead.
     with self.assertRaisesRegexp(
             ValueError,
             'true_fn and false_fn must have the same type: uint16, uint8'):
         unused_decoded_image = self.RunDecodeExample(
             serialized_example,
             tfexample_decoder.Image(dtype=dtypes.uint16),
             image_format='jpeg')
Beispiel #13
0
    def test_decode_example_with_no_shape_info(self):
        test_image_channels = [1, 3]
        for channels in test_image_channels:
            image_shape = (2, 3, channels)
            _, serialized_example = self.generate_image(image_format='jpeg',
                                                        image_shape=image_shape)

            tf_decoded_image = self.decode_example(
                serialized_example,
                tfexample_decoder.Image(shape=None, channels=channels),
                image_format='jpeg')
            self.assertEqual(tf_decoded_image.get_shape().ndims, 3)
 def testDecodeExampleWithJpegEncodingAt16BitCausesError(self):
     image_shape = (2, 3, 3)
     unused_image, serialized_example = self.GenerateImage(
         image_format='jpeg', image_shape=image_shape)
     expected_regex = (
         'jpeg decoder can only be used to decode to tf.uint8 but '
         '.* was requested for a jpeg image.')
     with self.assertRaisesRegexp(ValueError, expected_regex):
         unused_decoded_image = self.RunDecodeExample(
             serialized_example,
             tfexample_decoder.Image(dtype=dtypes.uint16),
             image_format='jpeg')
Beispiel #15
0
    def test_decode_example_with_PNG_encoding(self):
        test_image_channels = [1, 3, 4]
        for channels in test_image_channels:
            image_shape = (2, 3, channels)
            image, serialized_example = self.generate_image(
                image_format='PNG', image_shape=image_shape)

            decoded_image = self.run_decode_example(
                serialized_example,
                tfexample_decoder.Image(channels=channels),
                image_format='PNG')

            self.assertAllClose(image, decoded_image, atol=0)
    def testDecodeExampleWithPNGEncoding(self):
        test_image_channels = [1, 3, 4]
        for channels in test_image_channels:
            image_shape = (2, 3, channels)
            image, serialized_example = self.GenerateImage(
                image_format='PNG', image_shape=image_shape)

            decoded_image = self.RunDecodeExample(
                serialized_example,
                tfexample_decoder.Image(channels=channels),
                image_format='PNG')

            self.assertAllClose(image, decoded_image, atol=0)
    def testDecodeExampleWithJPEGEncoding(self):
        test_image_channels = [1, 3]
        for channels in test_image_channels:
            image_shape = (2, 3, channels)
            image, serialized_example = self.GenerateImage(
                image_format='JPEG', image_shape=image_shape)

            decoded_image = self.RunDecodeExample(
                serialized_example,
                tfexample_decoder.Image(channels=channels),
                image_format='JPEG')

            # Need to use a tolerance of 1 because of noise in the jpeg encode/decode
            self.assertAllClose(image, decoded_image, atol=1.001)
Beispiel #18
0
    def make_data_provider(self, **kwargs):

        context_keys_to_features = {
            self.params["image_field"]:
            tf.FixedLenFeature([], dtype=tf.string),
            "image/format":
            tf.FixedLenFeature([],
                               dtype=tf.string,
                               default_value=self.params["image_format"]),
        }

        sequence_keys_to_features = {
            self.params["caption_ids_field"]:
            tf.FixedLenSequenceFeature([], dtype=tf.int64),
            self.params["caption_tokens_field"]:
            tf.FixedLenSequenceFeature([], dtype=tf.string)
        }

        items_to_handlers = {
            "image":
            tfexample_decoder.Image(image_key=self.params["image_field"],
                                    format_key="image/format",
                                    channels=3),
            "target_ids":
            tfexample_decoder.Tensor(self.params["caption_ids_field"]),
            "target_tokens":
            tfexample_decoder.Tensor(self.params["caption_tokens_field"]),
            "target_len":
            tfexample_decoder.ItemHandlerCallback(
                keys=[self.params["caption_tokens_field"]],
                func=lambda x: tf.size(x[self.params["caption_tokens_field"]]))
        }

        decoder = TFSEquenceExampleDecoder(context_keys_to_features,
                                           sequence_keys_to_features,
                                           items_to_handlers)

        dataset = tf.contrib.slim.dataset.Dataset(
            data_sources=self.params["files"],
            reader=tf.TFRecordReader,
            decoder=decoder,
            num_samples=None,
            items_to_descriptions={})

        return tf.contrib.slim.dataset_data_provider.DatasetDataProvider(
            dataset=dataset,
            shuffle=self.params["shuffle"],
            num_epochs=self.params["num_epochs"],
            **kwargs)
Beispiel #19
0
 def items_to_handlers(self):
     """Items to handlers
 """
     default = {
         'image':
         tfexample_decoder.Image(shape=self.params['image_shape'],
                                 image_key='image/encoded',
                                 format_key='image/format'),
         'label':
         tfexample_decoder.Tensor(tensor_key='image/class'),
         'text':
         tfexample_decoder.Tensor(tensor_key='image/text'),
         'length':
         tfexample_decoder.Tensor(tensor_key='image/text_length'),
         'name':
         tfexample_decoder.Tensor(tensor_key='image/name')
     }
     return default
    def testDecodeExampleWithRepeatedImages(self):
        image_shape = (2, 3, 3)
        image_format = 'png'
        image, _ = self.GenerateImage(image_format=image_format,
                                      image_shape=image_shape)
        tf_encoded = self._Encoder(image, image_format)
        with self.test_session():
            tf_string = tf_encoded.eval()

        example = example_pb2.Example(features=feature_pb2.Features(
            feature={
                'image/encoded':
                feature_pb2.Feature(bytes_list=feature_pb2.BytesList(
                    value=[tf_string, tf_string])),
                'image/format':
                self._StringFeature(image_format),
            }))
        serialized_example = example.SerializeToString()

        with self.test_session():
            serialized_example = array_ops.reshape(serialized_example,
                                                   shape=[])

            decoder = tfexample_decoder.TFExampleDecoder(
                keys_to_features={
                    'image/encoded':
                    parsing_ops.FixedLenFeature((2, ), dtypes.string),
                    'image/format':
                    parsing_ops.FixedLenFeature((),
                                                dtypes.string,
                                                default_value=image_format),
                },
                items_to_handlers={
                    'image': tfexample_decoder.Image(repeated=True)
                })
            [tf_image] = decoder.decode(serialized_example, ['image'])

            output_image = tf_image.eval()

            self.assertEqual(output_image.shape, (2, 2, 3, 3))
            self.assertAllEqual(np.squeeze(output_image[0, :, :, :]), image)
            self.assertAllEqual(np.squeeze(output_image[1, :, :, :]), image)