Esempio n. 1
0
    def test_features_shape_dynamic_multi_none(
            self, encoding: features_lib.Encoding):
        x = np.random.randint(256, size=(2, 3, 1), dtype=np.uint8)
        x_other_shape = np.random.randint(256, size=(4, 4, 1), dtype=np.uint8)
        wrong_shape = np.random.randint(256, size=(2, 3, 2), dtype=np.uint8)

        self.assertFeature(
            feature=features_lib.Tensor(
                shape=(None, None, 1),
                dtype=tf.uint8,
                encoding=encoding,
            ),
            shape=(None, None, 1),
            dtype=tf.uint8,
            tests=[
                testing.FeatureExpectationItem(
                    value=x,
                    expected=x,
                ),
                testing.FeatureExpectationItem(
                    value=x_other_shape,
                    expected=x_other_shape,
                ),
                testing.FeatureExpectationItem(
                    value=wrong_shape,  # Wrong shape
                    raise_cls=ValueError,
                    raise_msg='are incompatible',
                ),
            ],
        )
Esempio n. 2
0
def test_custom_feature_connector(tmp_path: pathlib.Path):
    module_name = 'tensorflow_datasets.core.features.test_feature'
    feature_qualname = f'{module_name}.CustomFeatureConnector'
    assert module_name not in sys.modules
    assert feature_qualname not in features_lib.FeatureConnector._registered_features

    # Save tfds.feature.Tensor to avoid importing the custom feature connector
    feature = features_lib.Tensor(shape=(), dtype=tf.int32)
    feature.save_config(tmp_path)

    # Update the features.json file to simulate as if the original
    # FeatureConnector had been save
    feature_path = tmp_path / 'features.json'
    content = feature_path.read_text()
    content = content.replace(
        'tensorflow_datasets.core.features.tensor_feature.Tensor',
        feature_qualname,
    )
    feature_path.write_text(content)

    # Loading will load the custom feature connector, even if it is not
    # registered yet.
    feature = features_lib.FeatureConnector.from_config(tmp_path)

    # After loading, the custom feature connector is registered
    assert module_name in sys.modules
    assert feature_qualname in features_lib.FeatureConnector._registered_features

    # Check that the loaded feature is the custom feature
    from tensorflow_datasets.core.features import test_feature  # pylint: disable=g-import-not-at-top
    assert isinstance(feature, test_feature.CustomFeatureConnector)
Esempio n. 3
0
 def test_bool_flat(self, encoding: features_lib.Encoding):
     self.assertFeature(feature=features_lib.Tensor(
         shape=(),
         dtype=tf.bool,
         encoding=encoding,
     ),
                        dtype=tf.bool,
                        shape=(),
                        tests=[
                            testing.FeatureExpectationItem(
                                value=np.array(True),
                                expected=True,
                            ),
                            testing.FeatureExpectationItem(
                                value=np.array(False),
                                expected=False,
                            ),
                            testing.FeatureExpectationItem(
                                value=True,
                                expected=True,
                            ),
                            testing.FeatureExpectationItem(
                                value=False,
                                expected=False,
                            ),
                        ])
Esempio n. 4
0
    def test_features_multi_none_sequence(
        self,
        encoding: features_lib.Encoding,
        shape,
    ):
        x = np.random.randint(256, size=(3, 2, 3, 1), dtype=np.uint8)
        x_other_shape = np.random.randint(256,
                                          size=(3, 2, 2, 1),
                                          dtype=np.uint8)

        self.assertFeature(
            feature=features_lib.Sequence(
                features_lib.Tensor(
                    shape=shape,
                    dtype=tf.uint8,
                    encoding=encoding,
                ), ),
            shape=(None, ) + shape,
            dtype=tf.uint8,
            tests=[
                testing.FeatureExpectationItem(
                    value=x,
                    expected=x,
                ),
                testing.FeatureExpectationItem(
                    value=x_other_shape,
                    expected=x_other_shape,
                ),
                # TODO(epot): Is there a way to catch if the user try to encode
                # tensors with different shapes ?
            ],
        )
Esempio n. 5
0
    def test_shape_dynamic(self):

        np_input_dynamic_1 = np.random.randint(256,
                                               size=(2, 3, 2),
                                               dtype=np.int32)
        np_input_dynamic_2 = np.random.randint(256,
                                               size=(5, 3, 2),
                                               dtype=np.int32)

        self.assertFeature(
            feature=features_lib.Tensor(shape=(None, 3, 2), dtype=tf.int32),
            dtype=tf.int32,
            shape=(None, 3, 2),
            tests=[
                test_utils.FeatureExpectationItem(
                    value=np_input_dynamic_1,
                    expected=np_input_dynamic_1,
                ),
                test_utils.FeatureExpectationItem(
                    value=np_input_dynamic_2,
                    expected=np_input_dynamic_2,
                ),
                # Invalid shape
                test_utils.FeatureExpectationItem(
                    value=np.random.randint(256,
                                            size=(2, 3, 1),
                                            dtype=np.int32),
                    raise_cls=ValueError,
                    raise_msg='are incompatible',
                ),
            ])
Esempio n. 6
0
    def test_shape_dynamic_none_second(self, encoding: features_lib.Encoding):
        np_input_dynamic_1 = np.random.randint(256,
                                               size=(3, 2, 2),
                                               dtype=np.int32)
        np_input_dynamic_2 = np.random.randint(256,
                                               size=(3, 5, 2),
                                               dtype=np.int32)

        self.assertFeature(
            feature=features_lib.Tensor(
                shape=(3, None, 2),  # None not at the first position.
                dtype=tf.int32,
                encoding=encoding,
            ),
            dtype=tf.int32,
            shape=(3, None, 2),
            tests=[
                testing.FeatureExpectationItem(
                    value=np_input_dynamic_1,
                    expected=np_input_dynamic_1,
                ),
                testing.FeatureExpectationItem(
                    value=np_input_dynamic_2,
                    expected=np_input_dynamic_2,
                ),
                # Invalid shape
                testing.FeatureExpectationItem(
                    value=np.random.randint(256,
                                            size=(2, 3, 1),
                                            dtype=np.int32),
                    raise_cls=ValueError,
                    raise_msg='are incompatible',
                ),
            ])
 def _info(self):
   return dataset_info.DatasetInfo(
       builder=self,
       features=features.FeaturesDict({
           "frames": features.Sequence({
               "coordinates": features.Sequence(
                   features.Tensor(shape=(2,), dtype=tf.int32)
               ),
           }),
       }),
   )
Esempio n. 8
0
 def __init__(self, base_feature, shape):
     if not isinstance(shape, tuple):
         raise ValueError("shape must be a tuple, got %s" % str(shape))
     num_unknown = shape.count(None)
     self._base_info = base_feature.get_tensor_info()
     if num_unknown < 2:
         raise ValueError(
             "shape must contain at least 2 None values, got %s. Use "
             "StaticShapedTensor." % shape)
     base_shape = self._base_info.shape
     _assert_reshapable(base_shape, shape)
     self._shape_tuple = shape
     self._base = base_feature
     self._shape = features.Tensor(shape=(len(shape), ), dtype=tf.int64)
     self._tensor_shape = tf.TensorShape(shape)
Esempio n. 9
0
    def test_repr_tensor(self):

        # Top level Tensor is printed expanded
        self.assertEqual(
            repr(features_lib.Tensor(shape=(), dtype=tf.int32)),
            'Tensor(shape=(), dtype=tf.int32)',
        )

        # Sequences colapse tensor repr
        self.assertEqual(
            repr(features_lib.Sequence(tf.int32)),
            'Sequence(tf.int32)',
        )

        class ChildTensor(features_lib.Tensor):
            pass

        self.assertEqual(
            repr(
                features_lib.FeaturesDict({
                    'colapsed':
                    features_lib.Tensor(shape=(), dtype=tf.int32),
                    # Tensor with defined shape are printed expanded
                    'noncolapsed':
                    features_lib.Tensor(shape=(1, ), dtype=tf.int32),
                    # Tensor inherited are expanded
                    'child':
                    ChildTensor(shape=(), dtype=tf.int32),
                })),
            textwrap.dedent("""\
        FeaturesDict({
            'child': ChildTensor(shape=(), dtype=tf.int32),
            'colapsed': tf.int32,
            'noncolapsed': Tensor(shape=(1,), dtype=tf.int32),
        })"""),
        )
Esempio n. 10
0
    def test_bool_array(self):

        self.assertFeature(feature=features_lib.Tensor(shape=(3, ),
                                                       dtype=tf.bool),
                           dtype=tf.bool,
                           shape=(3, ),
                           tests=[
                               testing.FeatureExpectationItem(
                                   value=np.array([True, True, False]),
                                   expected=[True, True, False],
                               ),
                               testing.FeatureExpectationItem(
                                   value=[True, False, True],
                                   expected=[True, False, True],
                               ),
                           ])
Esempio n. 11
0
    def test_shape_static(self, encoding: features_lib.Encoding):
        np_input = np.random.rand(2, 3).astype(np.float32)
        array_input = [
            [1, 2, 3],
            [4, 5, 6],
        ]

        self.assertFeature(
            feature=features_lib.Tensor(
                shape=(2, 3),
                dtype=tf.float32,
                encoding=encoding,
            ),
            dtype=tf.float32,
            shape=(2, 3),
            tests=[
                # Np array
                testing.FeatureExpectationItem(
                    value=np_input,
                    expected=np_input,
                ),
                # Python array
                testing.FeatureExpectationItem(
                    value=array_input,
                    expected=array_input,
                ),
                # Invalid dtype
                testing.FeatureExpectationItem(
                    # On Windows, np default dtype is `int32`
                    value=np.random.randint(256, size=(2, 3), dtype=np.int64),
                    raise_cls=ValueError,
                    raise_msg='int64 do not match',
                ),
                # Invalid shape
                testing.FeatureExpectationItem(
                    value=np.random.rand(2, 4).astype(np.float32),
                    raise_cls=ValueError,
                    raise_msg='are incompatible',
                ),
            ],
            test_attributes={
                '_encoding': encoding,
            },
        )
Esempio n. 12
0
    def test_shapes_dynamic(self):
        specs = features.SpecDict({
            'input':
            features.Tensor(shape=(None, None, 1), dtype=tf.int32),
        })

        # Numpy array
        np_input = np.random.randint(256, size=(2, 3, 1), dtype=np.int32)
        tf_output = _encode_decode(specs, {
            'input': np_input,
        })
        self.assertAllEqual(tf_output['input'], np_input)

        # Invalid shape
        with self.assertRaises(ValueError) as err:
            _encode_decode(specs, {
                'input':
                np.random.randint(256, size=(2, 3, 2), dtype=np.int32),
            })
        self.assertIn('Shape (2, 3, 2) do not match', str(err.exception))
Esempio n. 13
0
    def test_shapes_dynamic(self):
        specs = features.SpecDict({
            'input':
            features.Tensor(shape=(None, None, 1), dtype=tf.int32),
        })

        # Numpy array
        np_input = np.random.randint(256, size=(2, 3, 1), dtype=np.int32)
        tf_output = _encode_decode(specs, {
            'input': np_input,
        })
        self.assertAllEqual(tf_output['input'], np_input)

        # Invalid shape
        with self.assertRaisesWithPredicateMatch(ValueError,
                                                 'are incompatible'):
            _encode_decode(specs, {
                'input':
                np.random.randint(256, size=(2, 3, 2), dtype=np.int32),
            })
Esempio n. 14
0
    def test_shape_static(self):

        np_input = np.random.rand(2, 3).astype(np.float32)
        array_input = [
            [1, 2, 3],
            [4, 5, 6],
        ]

        self.assertFeature(
            feature=features_lib.Tensor(shape=(2, 3), dtype=tf.float32),
            dtype=tf.float32,
            shape=(2, 3),
            tests=[
                # Np array
                testing.FeatureExpectationItem(
                    value=np_input,
                    expected=np_input,
                ),
                # Python array
                testing.FeatureExpectationItem(
                    value=array_input,
                    expected=array_input,
                ),
                # Invalid dtype
                testing.FeatureExpectationItem(
                    value=np.random.randint(256, size=(2, 3)),
                    raise_cls=ValueError,
                    raise_msg='int64 do not match',
                ),
                # Invalid shape
                testing.FeatureExpectationItem(
                    value=np.random.rand(2, 4).astype(np.float32),
                    raise_cls=ValueError,
                    raise_msg='are incompatible',
                ),
            ],
        )
Esempio n. 15
0
    def test_shapes_static(self):
        specs = features.SpecDict({
            'input':
            features.Tensor(shape=(2, 3), dtype=tf.float32),
        })

        # Numpy array
        np_input = np.random.rand(2, 3).astype(np.float32)
        tf_output = _encode_decode(specs, {
            'input': np_input,
        })
        self.assertAllEqual(tf_output['input'], np_input)

        # Python array
        array_input = [
            [1, 2, 3],
            [4, 5, 6],
        ]
        tf_output = _encode_decode(specs, {
            'input': array_input,
        })
        self.assertAllEqual(tf_output['input'], array_input)

        # Invalid type
        with self.assertRaises(ValueError) as err:
            _encode_decode(specs, {
                'input': np.random.randint(256, size=(2, 3)),
            })
        self.assertIn('Dtype int64 do not match', str(err.exception))

        # Invalid shape
        with self.assertRaises(ValueError) as err:
            _encode_decode(specs, {
                'input': np.random.rand(2, 4).astype(np.float32),
            })
        self.assertIn('Shape (2, 4) do not match', str(err.exception))
Esempio n. 16
0
    def test_bool_flat(self):

        self.assertFeature(feature=features_lib.Tensor(shape=(),
                                                       dtype=tf.bool),
                           dtype=tf.bool,
                           shape=(),
                           tests=[
                               test_utils.FeatureExpectationItem(
                                   value=np.array(True),
                                   expected=True,
                               ),
                               test_utils.FeatureExpectationItem(
                                   value=np.array(False),
                                   expected=False,
                               ),
                               test_utils.FeatureExpectationItem(
                                   value=True,
                                   expected=True,
                               ),
                               test_utils.FeatureExpectationItem(
                                   value=False,
                                   expected=False,
                               ),
                           ])
Esempio n. 17
0
    def test_shapes_static(self):
        specs = features.SpecDict({
            'input':
            features.Tensor(shape=(2, 3), dtype=tf.float32),
        })

        # Numpy array
        np_input = np.random.rand(2, 3).astype(np.float32)
        tf_output = _encode_decode(specs, {
            'input': np_input,
        })
        self.assertAllEqual(tf_output['input'], np_input)

        # Python array
        array_input = [
            [1, 2, 3],
            [4, 5, 6],
        ]
        tf_output = _encode_decode(specs, {
            'input': array_input,
        })
        self.assertAllEqual(tf_output['input'], array_input)

        # Invalid type
        with self.assertRaisesWithPredicateMatch(ValueError,
                                                 'int64 do not match'):
            _encode_decode(specs, {
                'input': np.random.randint(256, size=(2, 3)),
            })

        # Invalid shape
        with self.assertRaisesWithPredicateMatch(ValueError,
                                                 'are incompatible'):
            _encode_decode(specs, {
                'input': np.random.rand(2, 4).astype(np.float32),
            })
Esempio n. 18
0
def test_extract_features():
  features = features_lib.FeaturesDict({
      'img': features_lib.Image(shape=(256, 256, 3)),
      'img2': features_lib.Image(shape=(256, 256, 3)),
      'metadata': {
          'label': features_lib.ClassLabel(num_classes=4),
          'other': tf.string,
      },
      'sequence': features_lib.Sequence({
          'x': tf.int64,
          'y': tf.int64,
      }),
      'sequence_flat': features_lib.Sequence(tf.int64),
  })

  result = _extract_features(
      feature=features,
      expected_feature={},
  )
  testing.assert_features_equal(result, features_lib.FeaturesDict({}))

  # Feature spec accepted
  result = _extract_features(
      feature=features,
      expected_feature={
          'img': features_lib.Image(shape=(None, None, 3)),
          'metadata': {
              'other': tf.string,
          },
          'sequence': features_lib.Sequence({
              'x': tf.int64,
          }),
      },
  )
  testing.assert_features_equal(
      result,
      features_lib.FeaturesDict({
          'img': features_lib.Image(shape=(256, 256, 3)),
          'metadata': {
              'other': tf.string,
          },
          'sequence': features_lib.Sequence({
              'x': tf.int64,
          }),
      }),
  )

  # Failure mode:
  # * Structure not matching
  # * Type not matching
  # * Shape/dtype not matching
  # * Sequence values not matching (e.g. try bad dtype)

  with pytest.raises(ValueError, match="Missing expected feature 'unknown'"):
    _extract_features(
        feature=features,
        expected_feature={
            'sequence': features_lib.Sequence({
                'unknown': tf.bool,
            })
        },
    )

  with pytest.raises(ValueError, match="Missing expected feature 'non_exista"):
    _extract_features(
        feature=features,
        expected_feature={
            'non_existant': features_lib.Image(shape=(None, None, 3)),
        },
    )

  with pytest.raises(TypeError, match='Expected: Tensor.*. Got: Image'):
    _extract_features(
        feature=features,
        expected_feature={
            'img': features_lib.Tensor(shape=(256, 256, 3), dtype=tf.uint8),
        },
    )

  with pytest.raises(ValueError, match='Expected: Image.*. Got: Image'):
    _extract_features(
        feature=features,
        expected_feature={
            'img': features_lib.Image(shape=(None, None, 1)),
        },
    )

  with pytest.raises(ValueError, match='Expected: Tensor.*. Got: Tensor'):
    _extract_features(
        feature=features,
        expected_feature={
            'sequence_flat': features_lib.Sequence(tf.float32),  # Wrong dtype
        },
    )
Esempio n. 19
0
    def test_input_dict(self):

        self.assertFeatureEagerOnly(
            feature=feature_lib.Dataset({
                'a': tf.string,
                'b': {
                    'c': feature_lib.Tensor(shape=(4, 2), dtype=tf.int32),
                    'd': tf.uint8,
                }
            }, length=None),
            shape={
                'a': (),
                'b': {
                    'c': (4, 2),
                    'd': (),
                }
            },
            dtype={
                'a': tf.string,
                'b': {
                    'c': tf.int32,
                    'd': tf.uint8,
                }
            },
            tests=[
                testing.FeatureExpectationItem(
                    value={
                        'a': ['aa', 'b', 'ccc'],
                        'b': {
                            'c': np.ones(shape=(3, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    },
                    expected=tf.data.Dataset.from_tensor_slices({
                        'a': [
                            tf.compat.as_bytes(t) for t in ('aa', 'b', 'ccc')
                        ],
                        'b': {
                            'c': np.ones(shape=(3, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    }),
                ),
                testing.FeatureExpectationItem(
                    value={
                        'a': [str(i) for i in range(100)],
                        'b': {   # pylint: disable=g-complex-comprehension
                            'c': [np.ones(shape=(4, 2), dtype=np.int32) for _ in range(100)],
                            'd': [5 for _ in range(100)],
                        }
                    },
                    expected=tf.data.Dataset.from_tensor_slices({
                        'a': [tf.compat.as_bytes(str(i)) for i in range(100)],
                        'b': {
                            'c': np.ones(shape=(100, 4, 2), dtype=np.int32),
                            'd': [5] * 100,
                        }
                    }),
                    ),
                # Wrong length in one of the lists.
                testing.FeatureExpectationItem(
                    value={
                        'a': ['aa'],
                        'b': {
                            'c': np.ones(shape=(3, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    },
                    raise_cls=ValueError,
                    raise_msg='The length of all elements of one sequence should be the same.',
                ),
            ],
        )
Esempio n. 20
0
    def test_nested(self):

        self.assertFeatureEagerOnly(
            feature=feature_lib.Dataset({
                'a': tf.string,
                'b': {
                    'c': feature_lib.Tensor(shape=(4, 2), dtype=tf.int32),
                    'd': tf.uint8,
                }
            }, length=None),
            shape={
                'a': (),
                'b': {
                    'c': (4, 2),
                    'd': (),
                }
            },
            dtype={
                'a': tf.string,
                'b': {
                    'c': tf.int32,
                    'd': tf.uint8,
                }
            },
            tests=[
                testing.FeatureExpectationItem(
                    value=dataset_utils.as_numpy(tf.data.Dataset.from_tensor_slices({
                        'a': ['aa', 'b', 'ccc'],
                        'b': {
                            'c': np.ones(shape=(3, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    })),
                    expected=tf.data.Dataset.from_tensor_slices({
                        'a': [
                            tf.compat.as_bytes(t) for t in ('aa', 'b', 'ccc')
                        ],
                        'b': {
                            'c': np.ones(shape=(3, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    }),
                ),
                testing.FeatureExpectationItem(
                    value=dataset_utils.as_numpy(tf.data.Dataset.from_tensor_slices({
                        'a': [str(i) for i in range(100)],
                        'b': {   # pylint: disable=g-complex-comprehension
                            'c': [np.ones(shape=(4, 2), dtype=np.int32) for _ in range(100)],
                            'd': [5 for _ in range(100)],
                        }
                    })),
                    expected=tf.data.Dataset.from_tensor_slices({
                        'a': [tf.compat.as_bytes(str(i)) for i in range(100)],
                        'b': {
                            'c': np.ones(shape=(100, 4, 2), dtype=np.int32),
                            'd': [5] * 100,
                        }
                    }),
                ),
            ],
        )
Esempio n. 21
0
    def test_string(self, encoding: features_lib.Encoding):
        nonunicode_text = 'hello world'
        unicode_text = u'你好'

        self.assertFeature(
            feature=features_lib.Tensor(
                shape=(),
                dtype=tf.string,
                encoding=encoding,
            ),
            shape=(),
            dtype=tf.string,
            tests=[
                # Non-unicode
                testing.FeatureExpectationItem(
                    value=nonunicode_text,
                    expected=tf.compat.as_bytes(nonunicode_text),
                ),
                # Unicode
                testing.FeatureExpectationItem(
                    value=unicode_text,
                    expected=tf.compat.as_bytes(unicode_text),
                ),
                # Empty string
                testing.FeatureExpectationItem(
                    value='',
                    expected=b'',
                ),
                # Trailing zeros
                testing.FeatureExpectationItem(
                    value=b'abc\x00\x00',
                    expected=b'abc\x00\x00',
                ),
            ],
        )

        self.assertFeature(
            feature=features_lib.Tensor(
                shape=(2, 1),
                dtype=tf.string,
                encoding=encoding,
            ),
            shape=(2, 1),
            dtype=tf.string,
            tests=[
                testing.FeatureExpectationItem(
                    value=[[nonunicode_text], [unicode_text]],
                    expected=[
                        [tf.compat.as_bytes(nonunicode_text)],
                        [tf.compat.as_bytes(unicode_text)],
                    ],
                ),
                testing.FeatureExpectationItem(
                    value=[nonunicode_text, unicode_text],  # Wrong shape
                    raise_cls=ValueError,
                    raise_msg='(2,) and (2, 1) must have the same rank',
                ),
                testing.FeatureExpectationItem(
                    value=[['some text'], [123]],  # Wrong dtype
                    raise_cls=TypeError,
                    raise_msg='Expected binary or unicode string, got 123',
                ),
            ],
        )
Esempio n. 22
0
 def features(self):
     return dict(
         camera_position=features.Tensor(shape=(3, ), dtype=tf.float32))
Esempio n. 23
0
 def features(self):
     return dict(
         vertices=features.Tensor(shape=(None, 3), dtype=tf.float32),
         faces=features.Tensor(shape=(None, 3), dtype=tf.int64),
     )
Esempio n. 24
0
    def test_2lvl_sequences(self):

        self.assertFeature(
            feature=feature_lib.Sequence(
                feature_lib.Sequence(
                    feature_lib.Tensor(shape=(2, ), dtype=tf.int32), ), ),
            shape=(None, None, 2),
            dtype=tf.int32,
            tests=[
                testing.FeatureExpectationItem(
                    value=[
                        [[0, 1], [2, 3]],
                        [],
                        [[4, 5]],
                    ],
                    expected=testing.RaggedConstant([
                        [[0, 1], [2, 3]],
                        [],
                        [[4, 5]],
                    ],
                                                    inner_shape=(2, )),
                ),
                # Empty
                testing.FeatureExpectationItem(
                    value=[],
                    expected=[],
                ),
                # List of empty lists
                testing.FeatureExpectationItem(
                    value=[[], [], []],
                    expected=[[], [], []],
                ),
                # List of empty np.array
                testing.FeatureExpectationItem(
                    value=[
                        np.empty(shape=(0, 2), dtype=np.int32),
                        np.empty(shape=(0, 2), dtype=np.int32),
                    ],
                    expected=[
                        [],
                        [],
                    ],
                ),
                testing.FeatureExpectationItem(
                    value=[
                        np.empty(shape=(0, 2), dtype=np.int32),
                        np.empty(shape=(0, 2), dtype=np.int32),
                        np.ones(shape=(3, 2), dtype=np.int32),
                    ],
                    expected=[
                        [],
                        [],
                        [[1, 1], [1, 1], [1, 1]],
                    ],
                ),
                # Wrong types should fails
                testing.FeatureExpectationItem(
                    value=[
                        np.ones(shape=(3, 2), dtype=np.float32),
                    ],
                    raise_cls=ValueError,
                    raise_msg='float32 do not match int32',
                ),
            ],
        )
Esempio n. 25
0
    def test_nested(self):

        self.assertFeature(
            feature=feature_lib.Sequence({
                'a': tf.string,
                'b': {
                    'c': feature_lib.Tensor(shape=(4, 2), dtype=tf.int32),
                    'd': tf.uint8,
                }
            }, length=None),
            shape={
                'a': (None,),
                'b': {
                    'c': (None, 4, 2),
                    'd': (None,),
                }
            },
            dtype={
                'a': tf.string,
                'b': {
                    'c': tf.int32,
                    'd': tf.uint8,
                }
            },
            tests=[
                testing.FeatureExpectationItem(
                    value={
                        'a': ['aa', 'b', 'ccc'],
                        'b': {
                            'c': np.ones(shape=(3, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    },
                    expected={
                        'a': [
                            tf.compat.as_bytes(t) for t in ('aa', 'b', 'ccc')
                        ],
                        'b': {
                            'c': np.ones(shape=(3, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    },
                ),
                testing.FeatureExpectationItem(
                    value={
                        'a': [str(i) for i in range(100)],
                        'b': [{   # pylint: disable=g-complex-comprehension
                            'c': np.ones(shape=(4, 2), dtype=np.int32),
                            'd': 5,
                        } for _ in range(100)]
                    },
                    expected={
                        'a': [tf.compat.as_bytes(str(i)) for i in range(100)],
                        'b': {
                            'c': np.ones(shape=(100, 4, 2), dtype=np.int32),
                            'd': [5] * 100,
                        }
                    },
                ),
                # Test inputs not same sequence length
                testing.FeatureExpectationItem(
                    value={
                        'a': ['aa', 'b', 'ccc'],
                        'b': {
                            'c': np.ones(shape=(4, 4, 2), dtype=np.int32),
                            'd': [1, 2, 3],
                        }
                    },
                    raise_cls=ValueError,
                    raise_msg='length of all elements of one sequence should',
                ),
            ],
        )
Esempio n. 26
0
  def expectations(self):

    np_input = np.random.rand(2, 3).astype(np.float32)
    array_input = [
        [1, 2, 3],
        [4, 5, 6],
    ]

    np_input_dynamic_1 = np.random.randint(256, size=(2, 3, 2), dtype=np.int32)
    np_input_dynamic_2 = np.random.randint(256, size=(5, 3, 2), dtype=np.int32)

    return [
        test_utils.FeatureExpectation(
            name='shape_static',
            feature=features_lib.Tensor(shape=(2, 3), dtype=tf.float32),
            dtype=tf.float32,
            shape=(2, 3),
            tests=[
                # Np array
                test_utils.FeatureExpectationItem(
                    value=np_input,
                    expected=np_input,
                ),
                # Python array
                test_utils.FeatureExpectationItem(
                    value=array_input,
                    expected=array_input,
                ),
                # Invalid dtype
                test_utils.FeatureExpectationItem(
                    value=np.random.randint(256, size=(2, 3)),
                    raise_cls=ValueError,
                    raise_msg='int64 do not match',
                ),
                # Invalid shape
                test_utils.FeatureExpectationItem(
                    value=np.random.rand(2, 4).astype(np.float32),
                    raise_cls=ValueError,
                    raise_msg='are incompatible',
                ),
            ],
        ),
        test_utils.FeatureExpectation(
            name='shape_dynamic',
            feature=features_lib.Tensor(shape=(None, 3, 2), dtype=tf.int32),
            dtype=tf.int32,
            shape=(None, 3, 2),
            tests=[
                test_utils.FeatureExpectationItem(
                    value=np_input_dynamic_1,
                    expected=np_input_dynamic_1,
                ),
                test_utils.FeatureExpectationItem(
                    value=np_input_dynamic_2,
                    expected=np_input_dynamic_2,
                ),
                # Invalid shape
                test_utils.FeatureExpectationItem(
                    value=
                    np.random.randint(256, size=(2, 3, 1), dtype=np.int32),
                    raise_cls=ValueError,
                    raise_msg='are incompatible',
                ),
            ]
        ),
        test_utils.FeatureExpectation(
            name='bool_flat',
            feature=features_lib.Tensor(shape=(), dtype=tf.bool),
            dtype=tf.bool,
            shape=(),
            tests=[
                test_utils.FeatureExpectationItem(
                    value=np.array(True),
                    expected=True,
                ),
                test_utils.FeatureExpectationItem(
                    value=np.array(False),
                    expected=False,
                ),
                test_utils.FeatureExpectationItem(
                    value=True,
                    expected=True,
                ),
                test_utils.FeatureExpectationItem(
                    value=False,
                    expected=False,
                ),
            ]
        ),
        test_utils.FeatureExpectation(
            name='bool_array',
            feature=features_lib.Tensor(shape=(3,), dtype=tf.bool),
            dtype=tf.bool,
            shape=(3,),
            tests=[
                test_utils.FeatureExpectationItem(
                    value=np.array([True, True, False]),
                    expected=[True, True, False],
                ),
                test_utils.FeatureExpectationItem(
                    value=[True, False, True],
                    expected=[True, False, True],
                ),
            ]
        ),
    ]