Esempio n. 1
0
def test_empty_tensor_tensorlist():
    arr = np.array([], dtype=np.float32)
    tensor = TensorCPU(arr, "NHWC")
    tensorlist = TensorListCPU(arr, "NHWC")
    assert_array_equal(np.array(tensor), tensorlist.as_array())
    assert (np.array(tensor).shape == (0, ))
    assert (tensorlist.as_array().shape == (0, ))
Esempio n. 2
0
def test_data_ptr_tensor_list_cpu():
    arr = np.random.rand(3, 5, 6)
    tensorlist = TensorListCPU(arr, "NHWC")
    tensor = tensorlist.as_tensor()
    from_tensor_list = py_buffer_from_address(
        tensorlist.data_ptr(), tensor.shape(),
        types.to_numpy_type(tensor.dtype))
    assert (np.array_equal(arr, from_tensor_list))
Esempio n. 3
0
def test_tensorlist_shape():
    shapes = [(3, 4, 5, 6), (1, 8, 7, 6, 5), (1, ), (1, 1)]
    for shape in shapes:
        arr = np.empty(shape)
        tl = TensorListCPU(arr)
        tl_gpu = tl._as_gpu()
        assert tl.shape() == [shape[1:]] * shape[0]
        assert tl_gpu.shape() == [shape[1:]] * shape[0]
Esempio n. 4
0
def test_tensorlist_dtype():
    dali_types = types._all_types
    np_types = list(map(dali_type_to_np, dali_types))

    for dali_type, np_type in zip(dali_types, np_types):
        tl = TensorListCPU([TensorCPU(np.zeros((1), dtype=np_type))])

        assert tl.dtype == dali_type
        assert tl._as_gpu().dtype == dali_type
Esempio n. 5
0
def test_tensorlist_getitem_cpu():
    arr = np.random.rand(3, 5, 6)
    tensorlist = TensorListCPU(arr, "NHWC")
    list_of_tensors = [x for x in tensorlist]

    assert (type(tensorlist.at(0)) == np.ndarray)
    assert (type(tensorlist[0]) != np.ndarray)
    assert (type(tensorlist[0]) == TensorCPU)
    assert (type(tensorlist[-3]) == TensorCPU)
    assert (len(list_of_tensors) == len(tensorlist))
    with assert_raises(IndexError, glob="out of range"):
        tensorlist[len(tensorlist)]
    with assert_raises(IndexError, glob="out of range"):
        tensorlist[-len(tensorlist) - 1]
Esempio n. 6
0
def test_tl_from_list_of_tensors_empty():
    with assert_raises(RuntimeError,
                       glob='Cannot create TensorList from an empty list.'):
        TensorListCPU([])
    with assert_raises(RuntimeError,
                       glob='Cannot create TensorList from an empty list.'):
        TensorListGPU([])
Esempio n. 7
0
def test_tensorlist_str_scalars():
    arr = np.arange(10)
    tl = TensorListCPU(arr)
    params = [
        arr, 'DALIDataType.INT64', 10,
        '[(), (), (), (), (), (), (), (), (), ()]'
    ]
    _test_str(tl, params, _expected_tensorlist_str)
Esempio n. 8
0
def test_array_interface_tensor_cpu():
    arr = np.random.rand(3, 5, 6)
    tensorlist = TensorListCPU(arr, "NHWC")
    assert tensorlist[0].__array_interface__['data'][0] == tensorlist[
        0].data_ptr()
    assert tensorlist[0].__array_interface__['data'][1] == True
    assert np.array_equal(tensorlist[0].__array_interface__['shape'],
                          tensorlist[0].shape())
    assert np.dtype(tensorlist[0].__array_interface__['typestr']) == np.dtype(
        types.to_numpy_type(tensorlist[0].dtype))
Esempio n. 9
0
def test_tl_from_list_of_tensors_different_dtypes():
    np_types = [np.float32, np.float16, np.int16, np.int8, np.uint16, np.uint8]
    for dtypes in np.random.choice(np_types, size=(3, 2), replace=False):
        t1 = TensorCPU(np.zeros((1), dtype=dtypes[0]))
        t2 = TensorCPU(np.zeros((1), dtype=dtypes[1]))
        with assert_raises(
                TypeError,
                glob=
                f"Tensors cannot have different data types. Tensor at position "
                "1 has type '*' expected to have type '*'."):
            TensorListCPU([t1, t2])
Esempio n. 10
0
def test_tl_from_list_of_tensors_different_backends():
    t1 = TensorCPU(np.zeros((1)))
    t2 = TensorCPU(np.zeros((1)))._as_gpu()
    with assert_raises(
            TypeError,
            glob='Object at position 1 cannot be converted to TensorCPU'):
        TensorListCPU([t1, t2])
    with assert_raises(
            TypeError,
            glob='Object at position 1 cannot be converted to TensorGPU'):
        TensorListGPU([t2, t1])
Esempio n. 11
0
def test_tl_from_list_of_tensors_different_shapes():
    shapes = [(1, 2, 3), (4, 5, 6), (128, 128, 128), (8, 8, 8), (13, 47, 131)]
    for size in [10, 5, 36, 1]:
        np_arrays = [
            np.random.rand(*shapes[i])
            for i in np.random.choice(range(len(shapes)), size=size)
        ]

        tl_cpu = TensorListCPU([TensorCPU(a) for a in np_arrays])
        tl_gpu = TensorListGPU([TensorCPU(a)._as_gpu() for a in np_arrays])

        for arr, tensor_cpu, tensor_gpu in zip(np_arrays, tl_cpu, tl_gpu):
            np.testing.assert_array_equal(arr, tensor_cpu)
            np.testing.assert_array_equal(arr, tensor_gpu.as_cpu())
Esempio n. 12
0
def test_tl_from_list_of_tensors_same_shape():
    for shape in [(10, 1), (4, 5, 6), (13, 1), (1, 1)]:
        arr = np.random.rand(*shape)

        tl_cpu_from_np = TensorListCPU(arr)
        tl_cpu_from_tensors = TensorListCPU([TensorCPU(a) for a in arr])
        np.testing.assert_array_equal(tl_cpu_from_np.as_array(),
                                      tl_cpu_from_tensors.as_array())

        tl_gpu_from_np = tl_cpu_from_np._as_gpu()
        tl_gpu_from_tensors = TensorListGPU(
            [TensorCPU(a)._as_gpu() for a in arr])
        np.testing.assert_array_equal(tl_gpu_from_np.as_cpu().as_array(),
                                      tl_gpu_from_tensors.as_cpu().as_array())
Esempio n. 13
0
def test_create_tensorlist_as_tensor():
    arr = np.random.rand(3, 5, 6)
    tensorlist = TensorListCPU(arr, "NHWC")
    tensor = tensorlist.as_tensor()
    assert_array_equal(np.array(tensor), tensorlist.as_array())
Esempio n. 14
0
def test_tensorlist_str_empty():
    tl = TensorListCPU(np.empty(0))
    params = [[], 'DALIDataType.FLOAT64', 0, []]
    _test_str(tl, params, _expected_tensorlist_str)