def test_encode_decode(self): specs = features.SpecDict({ 'img': features.Image(), 'img_shaped': features.Image(shape=(32, 64, 3)), }) img = np.random.randint(256, size=(128, 100, 1), dtype=np.uint8) img_shaped = np.random.randint(256, size=(32, 64, 3), dtype=np.uint8) decoded_sample = _encode_decode(specs, { 'img': img, 'img_shaped': img_shaped, }) self.assertAllEqual(decoded_sample['img'], img) self.assertAllEqual(decoded_sample['img_shaped'], img_shaped) # 'img' shape can be dynamic img2 = np.random.randint(256, size=(64, 200, 1), dtype=np.uint8) decoded_sample = _encode_decode(specs, { 'img': img2, 'img_shaped': img_shaped, }) self.assertAllEqual(decoded_sample['img'], img2) # 'img_shaped' shape should be static img_shaped2 = np.random.randint(256, size=(31, 64, 3), dtype=np.uint8) with self.assertRaises(ValueError) as err: _encode_decode(specs, { 'img': img2, 'img_shaped': img_shaped2, }) self.assertIn('Shape (31, 64, 3) do not match', str(err.exception))
def test_encode_decode(self): specs = features.SpecDict({ 'img': features.Image(), 'img_shaped': features.Image(shape=(32, 64, 3)), 'img_file': features.Image(), }) img = np.random.randint(256, size=(128, 100, 3), dtype=np.uint8) img_shaped = np.random.randint(256, size=(32, 64, 3), dtype=np.uint8) img_file_path = os.path.join(os.path.dirname(__file__), '../test_data/6pixels.png') img_file_expected_content = [ # see tests_data/README.md [[0, 255, 0], [255, 0, 0], [255, 0, 255]], [[0, 0, 255], [255, 255, 0], [126, 127, 128]], ] decoded_sample = test_utils.features_encode_decode( specs, { 'img': img, 'img_shaped': img_shaped, 'img_file': img_file_path, }) self.assertAllEqual(decoded_sample['img'], img) self.assertAllEqual(decoded_sample['img_shaped'], img_shaped) self.assertAllEqual(decoded_sample['img_file'], img_file_expected_content) # 'img' shape can be dynamic img2 = np.random.randint(256, size=(64, 200, 3), dtype=np.uint8) decoded_sample = test_utils.features_encode_decode( specs, { 'img': img2, 'img_shaped': img_shaped, 'img_file': img_file_path, }) self.assertAllEqual(decoded_sample['img'], img2) # 'img_shaped' shape should be static img_shaped2 = np.random.randint(256, size=(31, 64, 3), dtype=np.uint8) with self.assertRaisesWithPredicateMatch(ValueError, 'are incompatible'): test_utils.features_encode_decode( specs, { 'img': img2, 'img_shaped': img_shaped2, 'img_file': img_file_path, })
def _info(self): return dataset_info.DatasetInfo( builder=self, features=features.FeaturesDict({"im": features.Image()}), supervised_keys=("im", "im"), metadata=dataset_info.MetadataDict(), )
def test_images_float(self): img = np.random.rand(28, 28, 1).astype(np.float32) img_other_shape = np.random.rand(12, 34, 1).astype(np.float32) self.assertFeature( feature=features_lib.Image(shape=(None, None, 1), dtype=tf.float32), shape=(None, None, 1), dtype=tf.float32, tests=[ # Numpy array testing.FeatureExpectationItem( value=img, expected=img, ), # 'img' shape can be dynamic testing.FeatureExpectationItem( value=img_other_shape, expected=img_other_shape, ), # Invalid type testing.FeatureExpectationItem( value=img.astype(np.float64), raise_cls=ValueError, raise_msg='dtype should be', ), ], test_attributes=dict( _encoding_format=None, _use_colormap=False, ))
def test_image_custom_decode(self): # Do not uses random here because Jpeg compression has loss, so decoded # value isn't the same img_shaped = np.ones(shape=(30, 60, 3), dtype=np.uint8) x, y, w, h = 4, 7, 10, 13 img_cropped = img_shaped[y:y + h, x:x + w, :] class DecodeCrop(decode_lib.Decoder): """Simple class on how to customize the decoding.""" def decode_example(self, serialized_image): return tf.image.decode_and_crop_jpeg( serialized_image, [y, x, h, w], channels=self.feature.shape[-1], ) @decode_lib.make_decoder() def decode_crop(serialized_image, feature): return tf.image.decode_and_crop_jpeg( serialized_image, [y, x, h, w], channels=feature.shape[-1], ) image_path = os.fspath( utils.tfds_path('testing/test_data/test_image.jpg')) with tf.io.gfile.GFile(image_path, 'rb') as f: serialized_img = f.read() self.assertFeature( # Image with statically defined shape feature=features_lib.Image(shape=(30, 60, 3), encoding_format='jpeg'), shape=(30, 60, 3), dtype=tf.uint8, # Output shape is different. test_tensor_spec=False, tests=[ testing.FeatureExpectationItem( value=img_shaped, expected=img_cropped, shape=(13, 10, 3), # Shape is cropped decoders=DecodeCrop(), ), testing.FeatureExpectationItem( value=img_shaped, expected=img_cropped, shape=(13, 10, 3), # Shape is cropped decoders=decode_crop(), # pylint: disable=no-value-for-parameter ), testing.FeatureExpectationItem( value=image_path, expected=serialized_img, shape=(), dtype=tf.string, decoders=decode_lib.SkipDecoding(), ), ], )
def test_tensor_spec(self): feature = features_lib.FeaturesDict({ 'input': AnInputConnector(), 'output': AnOutputConnector(), 'img': { 'size': { 'height': features_lib.Tensor(shape=(2, 3), dtype=tf.int64), 'width': features_lib.Tensor(shape=[None, 3], dtype=tf.int64), }, 'image': features_lib.Image(shape=(28, 28, 1)), 'metadata/path': tf.string, } }) self.assertAllEqualNested( feature.get_tensor_spec(), { 'input': tf.TensorSpec(shape=[], dtype=tf.int64), 'output': tf.TensorSpec(shape=[], dtype=tf.float32), 'img': { 'size': { 'height': tf.TensorSpec(shape=[2, 3], dtype=tf.int64), 'width': tf.TensorSpec(shape=[None, 3], dtype=tf.int64), }, 'image': tf.TensorSpec(shape=[28, 28, 1], dtype=tf.uint8), 'metadata/path': tf.TensorSpec(shape=[], dtype=tf.string), } })
def test_image_shaped(self): img_shaped = randint(256, size=(32, 64, 1), dtype=np.uint8) self.assertFeature( # Image with statically defined shape feature=features_lib.Image( shape=(32, 64, 1), encoding_format='png', use_colormap=True, ), shape=(32, 64, 1), dtype=tf.uint8, tests=[ testing.FeatureExpectationItem( value=img_shaped, expected=img_shaped, ), # 'img_shaped' shape should be static testing.FeatureExpectationItem( value=randint(256, size=(31, 64, 1), dtype=np.uint8), raise_cls=ValueError, raise_msg='are incompatible', ), ], test_attributes=dict( _encoding_format='png', _use_colormap=True, ))
def _info(self): mnist_shape = (_MNIST_IMAGE_SIZE, _MNIST_IMAGE_SIZE, 1) return dataset_builder.DatasetInfo(specs=features.SpecDict({ "input": features.Image(shape=mnist_shape), "target": tf.int64, }), )
def _info(self): return dataset_info.DatasetInfo( builder=self, features=features.FeaturesDict({ "image": features.Image(shape=(28, 28, 1)), "label": features.ClassLabel(num_classes=10), }), )
def _info(self): cifar_shape = (_CIFAR_IMAGE_SIZE, _CIFAR_IMAGE_SIZE, 3) return dataset_builder.DatasetInfo( specs=features.SpecDict({ "input": features.Image(shape=cifar_shape), "target": tf.int64, # Could replace by features.Label() }), )
def test_feature_save_load_metadata_slashes(self): with testing.tmp_dir() as data_dir: fd = features_lib.FeaturesDict({ 'image/frame': features_lib.Image(shape=(32, 32, 3)), 'image/label': features_lib.ClassLabel(num_classes=2), }) fd.save_metadata(data_dir) fd.load_metadata(data_dir)
def _info(self): return dataset_info.DatasetInfo( builder=self, features=features.FeaturesDict({ 'image': features.Image(shape=(28, 28, 1)), 'label': features.ClassLabel(num_classes=10), }), description='Mnist description.', )
def test_images(self, dtype): np_dtype = dtype.as_numpy_dtype img = randint(256, size=(128, 100, 3), dtype=np_dtype) img_other_shape = randint(256, size=(64, 200, 3), dtype=np_dtype) img_file_path = os.path.join(os.path.dirname(__file__), '../../testing/test_data/6pixels.png') img_file_expected_content = np.array( [ # see tests_data/README.md [[0, 255, 0], [255, 0, 0], [255, 0, 255]], [[0, 0, 255], [255, 255, 0], [126, 127, 128]], ], dtype=np_dtype) if dtype == tf.uint16: img_file_expected_content *= 257 # Scale int16 images self.assertFeature( feature=features_lib.Image(dtype=dtype), shape=(None, None, 3), dtype=dtype, tests=[ # Numpy array testing.FeatureExpectationItem( value=img, expected=img, ), # File path testing.FeatureExpectationItem( value=img_file_path, expected=img_file_expected_content, ), # 'img' shape can be dynamic testing.FeatureExpectationItem( value=img_other_shape, expected=img_other_shape, ), # Invalid type testing.FeatureExpectationItem( value=randint(256, size=(128, 128, 3), dtype=np.uint32), raise_cls=ValueError, raise_msg='dtype should be', ), # Invalid number of dimensions testing.FeatureExpectationItem( value=randint(256, size=(128, 128), dtype=np_dtype), raise_cls=ValueError, raise_msg='must have the same rank', ), # Invalid number of channels testing.FeatureExpectationItem( value=randint(256, size=(128, 128, 1), dtype=np_dtype), raise_cls=ValueError, raise_msg='are incompatible', ), ], )
def test_feature__repr__(self): label = features_lib.ClassLabel(names=['m', 'f']) feature_dict = features_lib.FeaturesDict({ 'metadata': features_lib.Sequence({ 'frame': features_lib.Image(shape=(32, 32, 3)), }), 'label': features_lib.Sequence(label), }) self.assertEqual(repr(feature_dict), FEATURE_STR)
def _info(self): return dataset_info.DatasetInfo( builder=self, features=features.FeaturesDict({ "image": features.Image(shape=(16, 16, 1)), "label": features.ClassLabel(names=["dog", "cat"]), "id": tf.int32, }), supervised_keys=("x", "x"), metadata=dataset_info.BeamMetadataDict(), )
def _info(self): cifar_shape = (_CIFAR_IMAGE_SIZE, _CIFAR_IMAGE_SIZE, 3) label_to_use = "coarse_labels" if self._use_coarse_labels else "fine_labels" return dataset_builder.DatasetInfo( specs=features.SpecDict({ "input": features.Image(shape=cifar_shape), "target": features.OneOf(choice=label_to_use, feature_dict={ "coarse_labels": tf.int64, "fine_labels": tf.int64, }), }), )
def test_partial_decode(dummy_mnist: testing.DummyMnist): ds = dummy_mnist.as_dataset( split='train', decoders=decode.PartialDecoding({ 'image': features_lib.Image(shape=(None, None, 1)), }), ) assert ds.element_spec == { 'image': tf.TensorSpec(shape=(28, 28, 1), dtype=tf.uint8) } for ex in ds.take(2): assert ex['image'].shape == (28, 28, 1)
def _info(self) -> dataset_info.DatasetInfo: return dataset_info.DatasetInfo( builder=self, description='Generic image classification dataset.', features=features_lib.FeaturesDict({ 'image': features_lib.Image(), 'label': features_lib.ClassLabel(), 'image/filename': features_lib.Text(), }), supervised_keys=('image', 'label'), )
def _info(self): return dataset_info.DatasetInfo( builder=self, features=features.FeaturesDict({ 'image': features.Image(shape=(16, 16, 1)), 'label': features.ClassLabel(names=['dog', 'cat']), 'id': tf.int32, }), supervised_keys=('x', 'x'), metadata=dataset_info.BeamMetadataDict(), )
def test_partial_decode_with_skip_decode(dummy_mnist: testing.DummyMnist): ds = dummy_mnist.as_dataset( split='train', decoders=decode.PartialDecoding( { 'image': features_lib.Image(shape=(None, None, 1)), }, decoders={ 'image': decode.SkipDecoding(), }, ), ) assert ds.element_spec == {'image': tf.TensorSpec(shape=(), dtype=tf.string)} for ex in ds.take(2): assert ex['image'].dtype == tf.string
def test_image_nested_empty_len(self): imgs = [ np.random.randint(256, size=(28, 28, 3), dtype=np.uint8), np.random.randint(256, size=(28, 28, 3), dtype=np.uint8), ] imgs_stacked = np.stack(imgs) self.assertFeature( feature=feature_lib.Sequence({ 'a': feature_lib.Image(shape=(None, None, 3)), 'b': tf.int32, }), shape={ 'a': (None, None, None, 3), 'b': (None, ), }, dtype={ 'a': tf.uint8, 'b': tf.int32, }, tests=[ testing.FeatureExpectationItem( value={ 'a': imgs, 'b': [1, 2], }, expected={ 'a': imgs_stacked, 'b': [1, 2], }, ), testing.FeatureExpectationItem( value={ 'a': [], 'b': [], }, expected={ 'a': np.empty(shape=(0, 0, 0, 3), dtype=np.uint8), 'b': [], }, ), ], )
def test_image(self): imgs = [ np.random.randint(256, size=(128, 100, 3), dtype=np.uint8), np.random.randint(256, size=(128, 100, 3), dtype=np.uint8), np.random.randint(256, size=(128, 100, 3), dtype=np.uint8), np.random.randint(256, size=(128, 100, 3), dtype=np.uint8), ] imgs_stacked = np.stack(imgs) self.assertFeature( feature=feature_lib.Sequence( { 'image': feature_lib.Image(shape=(128, 100, 3)), }, length=None), shape={'image': (None, 128, 100, 3)}, dtype={'image': tf.uint8}, tests=[ testing.FeatureExpectationItem( value=[{ 'image': img } for img in imgs], expected={'image': imgs_stacked}, ), testing.FeatureExpectationItem( value={'image': imgs_stacked}, expected={'image': imgs_stacked}, ), testing.FeatureExpectationItem( value={'image': imgs}, expected={'image': imgs_stacked}, ), # Empty value testing.FeatureExpectationItem( value={'image': []}, # The empty value still has the right shape expected={ 'image': np.empty(shape=(0, 128, 100, 3), dtype=np.uint8) }, ), ], )
def test_wrong_input(self): specs = features.SpecDict({ 'img': features.Image(), }) # Correct shape/type should succeed test_utils.features_encode_decode(specs, { 'img': np.random.randint(256, size=(128, 128, 3), dtype=np.uint8), }) test_utils.features_encode_decode(specs, { 'img': np.random.randint(256, size=(64, 64, 3), dtype=np.uint8), }) # Invalid type with self.assertRaisesWithPredicateMatch(ValueError, 'should be uint8'): test_utils.features_encode_decode( specs, { 'img': np.random.randint(256, size=(128, 128, 3), dtype=np.uint32), }) # Invalid number of dimensions with self.assertRaisesWithPredicateMatch(ValueError, 'must have the same rank'): test_utils.features_encode_decode(specs, { 'img': np.random.randint(256, size=(128, 128), dtype=np.uint8), }) # Invalid number of channels with self.assertRaisesWithPredicateMatch(ValueError, 'are incompatible'): test_utils.features_encode_decode( specs, { 'img': np.random.randint(256, size=(128, 128, 1), dtype=np.uint8), })
def test_wrong_input(self): specs = features.SpecDict({ 'img': features.Image(), }) # Correct shape/type should succeed _encode_decode(specs, { 'img': np.random.randint(256, size=(128, 128, 1), dtype=np.uint8), }) _encode_decode(specs, { 'img': np.random.randint(256, size=(64, 64, 1), dtype=np.uint8), }) # Invalid type with self.assertRaises(ValueError) as err: _encode_decode( specs, { 'img': np.random.randint(256, size=(128, 128, 1), dtype=np.uint32), }) self.assertIn('Image should be uint8', str(err.exception)) # Invalid number of dimensions with self.assertRaises(ValueError) as err: _encode_decode(specs, { 'img': np.random.randint(256, size=(128, 128), dtype=np.uint8), }) self.assertIn('Shapes should have same length', str(err.exception)) # Invalid number of channels with self.assertRaises(ValueError) as err: _encode_decode( specs, { 'img': np.random.randint(256, size=(128, 128, 3), dtype=np.uint8), }) self.assertIn('Shape (128, 128, 3) do not match', str(err.exception))
def test_image_shaped(self): img_shaped = randint(256, size=(32, 64, 3), dtype=np.uint8) self.assertFeature( # Image with statically defined shape feature=features_lib.Image(shape=(32, 64, 3)), shape=(32, 64, 3), dtype=tf.uint8, tests=[ test_utils.FeatureExpectationItem( value=img_shaped, expected=img_shaped, ), # 'img_shaped' shape should be static test_utils.FeatureExpectationItem( value=randint(256, size=(31, 64, 3), dtype=np.uint8), raise_cls=ValueError, raise_msg='are incompatible', ), ], )
def test_feature__repr__(self): label = features_lib.ClassLabel(names=['m', 'f']) feature_dict = features_lib.FeaturesDict({ 'metadata': features_lib.Sequence({ 'frame': features_lib.Image(shape=(32, 32, 3)), }), 'label': features_lib.Sequence(label), }) self.assertEqual( repr(feature_dict), textwrap.dedent("""\ FeaturesDict({ 'label': Sequence(ClassLabel(shape=(), dtype=tf.int64, num_classes=2)), 'metadata': Sequence({ 'frame': Image(shape=(32, 32, 3), dtype=tf.uint8), }), })"""), )
def test_image_unknown_len(self): imgs = [ np.random.randint(256, size=(28, 28, 3), dtype=np.uint8), np.random.randint(256, size=(28, 28, 3), dtype=np.uint8), ] imgs_stacked = np.stack(imgs) self.assertFeature( feature=feature_lib.Sequence(feature_lib.Image(shape=(None, None, 3))), dtype=tf.uint8, shape=(None, None, None, 3), # (length, h, w, c) tests=[ testing.FeatureExpectationItem( value=[], # Empty input expected=np.empty(shape=(0, 0, 0, 3), dtype=np.uint8), ), testing.FeatureExpectationItem( value=imgs, expected=imgs_stacked, ), ], )
def expectations(self): randint = np.random.randint img = randint(256, size=(128, 100, 3), dtype=np.uint8) img_other_shape = randint(256, size=(64, 200, 3), dtype=np.uint8) img_file_path = os.path.join(os.path.dirname(__file__), '../../testing/test_data/6pixels.png') img_file_expected_content = [ # see tests_data/README.md [[0, 255, 0], [255, 0, 0], [255, 0, 255]], [[0, 0, 255], [255, 255, 0], [126, 127, 128]], ] img_shaped = randint(256, size=(32, 64, 3), dtype=np.uint8) return [ test_utils.FeatureExpectation( name='image', feature=features_lib.Image(), shape=(None, None, 3), dtype=tf.uint8, tests=[ # Numpy array test_utils.FeatureExpectationItem( value=img, expected=img, ), # File path test_utils.FeatureExpectationItem( value=img_file_path, expected=img_file_expected_content, ), # 'img' shape can be dynamic test_utils.FeatureExpectationItem( value=img_other_shape, expected=img_other_shape, ), # Invalid type test_utils.FeatureExpectationItem( value=randint(256, size=(128, 128, 3), dtype=np.uint32), raise_cls=ValueError, raise_msg='should be uint8', ), # Invalid number of dimensions test_utils.FeatureExpectationItem( value=randint(256, size=(128, 128), dtype=np.uint8), raise_cls=ValueError, raise_msg='must have the same rank', ), # Invalid number of channels test_utils.FeatureExpectationItem( value=randint(256, size=(128, 128, 1), dtype=np.uint8), raise_cls=ValueError, raise_msg='are incompatible', ), ], ), # Image with statically defined shape test_utils.FeatureExpectation( name='image_shaped', feature=features_lib.Image(shape=(32, 64, 3)), shape=(32, 64, 3), dtype=tf.uint8, tests=[ test_utils.FeatureExpectationItem( value=img_shaped, expected=img_shaped, ), # 'img_shaped' shape should be static test_utils.FeatureExpectationItem( value=randint(256, size=(31, 64, 3), dtype=np.uint8), raise_cls=ValueError, raise_msg='are incompatible', ), ], ), ]
def test_images(self, dtype, channels): np_dtype = dtype.as_numpy_dtype img = randint(256, size=(128, 100, channels), dtype=np_dtype) img_other_shape = randint(256, size=(64, 200, channels), dtype=np_dtype) filename = { 3: '6pixels.png', 4: '6pixels_4chan.png', }[channels] img_file_path = os.path.join(os.path.dirname(__file__), '../../testing/test_data', filename) with tf.io.gfile.GFile(img_file_path, 'rb') as f: img_byte_content = f.read() img_file_expected_content = np.array( [ # see tests_data/README.md [[0, 255, 0, 255], [255, 0, 0, 255], [255, 0, 255, 255]], [[0, 0, 255, 255], [255, 255, 0, 255], [126, 127, 128, 255]], ], dtype=np_dtype)[:, :, :channels] # Truncate (h, w, 4) -> (h, w, c) if dtype == tf.uint16: img_file_expected_content *= 257 # Scale int16 images self.assertFeature( feature=features_lib.Image(shape=(None, None, channels), dtype=dtype), shape=(None, None, channels), dtype=dtype, tests=[ # Numpy array testing.FeatureExpectationItem( value=img, expected=img, ), # File path testing.FeatureExpectationItem( value=img_file_path, expected=img_file_expected_content, ), # File Path testing.FeatureExpectationItem( value=pathlib.Path(img_file_path), expected=img_file_expected_content, ), # Images bytes testing.FeatureExpectationItem( value=img_byte_content, expected=img_file_expected_content, ), # 'img' shape can be dynamic testing.FeatureExpectationItem( value=img_other_shape, expected=img_other_shape, ), # Invalid type testing.FeatureExpectationItem( value=randint(256, size=(128, 128, channels), dtype=np.uint32), raise_cls=ValueError, raise_msg='dtype should be', ), # Invalid number of dimensions testing.FeatureExpectationItem( value=randint(256, size=(128, 128), dtype=np_dtype), raise_cls=ValueError, raise_msg='must have the same rank', ), # Invalid number of channels testing.FeatureExpectationItem( value=randint(256, size=(128, 128, 1), dtype=np_dtype), raise_cls=ValueError, raise_msg='are incompatible', ), ], test_attributes=dict( _encoding_format=None, _use_colormap=False, ))
def test_video_custom_decode(self): image_path = os.fspath( utils.tfds_path('testing/test_data/test_image.jpg')) with tf.io.gfile.GFile(image_path, 'rb') as f: serialized_img = f.read() self.assertFeature( # Image with statically defined shape feature=features_lib.Video(shape=(None, 30, 60, 3)), shape=(None, 30, 60, 3), dtype=tf.uint8, tests=[ testing.FeatureExpectationItem( value=[image_path] * 15, # 15 frames of video expected=[serialized_img] * 15, # Non-decoded image shape=(15, ), dtype=tf.string, # Only string are decoded decoders=decode_lib.SkipDecoding(), ), ], ) # Test with FeatureDict self.assertFeature( feature=features_lib.FeaturesDict({ 'image': features_lib.Image(shape=(30, 60, 3), encoding_format='jpeg'), 'label': tf.int64, }), shape={ 'image': (30, 60, 3), 'label': (), }, dtype={ 'image': tf.uint8, 'label': tf.int64, }, tests=[ testing.FeatureExpectationItem( decoders={ 'image': decode_lib.SkipDecoding(), }, value={ 'image': image_path, 'label': 123, }, expected={ 'image': serialized_img, 'label': 123, }, shape={ 'image': (), 'label': (), }, dtype={ 'image': tf.string, 'label': tf.int64, }, ), ], )