Beispiel #1
0
def test_createdescriptorforconcatenation_wrong_shape_for_axis():
    input_shape_vector = [ann.TensorShape((1, 2)), ann.TensorShape((3, 4)), ann.TensorShape((5, 6))]
    with pytest.raises(RuntimeError) as err:
        desc = ann.CreateDescriptorForConcatenation(input_shape_vector, 0)

    assert "All inputs to concatenation must be the same size along all dimensions  except the concatenation dimension" in str(
        err.value)
Beispiel #2
0
def test_createdescriptorforconcatenation_ctor():
    input_shape_vector = [ann.TensorShape((2, 1)), ann.TensorShape((3, 1)), ann.TensorShape((4, 1))]
    desc = ann.CreateDescriptorForConcatenation(input_shape_vector, 0)
    assert 3 == desc.GetNumViews()
    assert 0 == desc.GetConcatAxis()
    assert 2 == desc.GetNumDimensions()
    c = desc.GetViewOrigin(1)
    d = desc.GetViewOrigin(0)
Beispiel #3
0
def test_const_tensor_from_tensor_has_memory_area_access_after_deletion_of_original_tensor(
):
    tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), ann.DataType_Float32)
    tensor = ann.Tensor(tensor_info)

    tensor.get_memory_area()[0] = 100

    copied_mem = tensor.get_memory_area().copy()

    assert 100 == copied_mem[0], "Memory was copied correctly"

    copied_tensor = ann.ConstTensor(tensor)

    tensor.get_memory_area()[0] = 200

    assert 200 == tensor.get_memory_area(
    )[0], "Tensor and copied Tensor point to the same memory"
    assert 200 == copied_tensor.get_memory_area(
    )[0], "Tensor and copied Tensor point to the same memory"

    assert 100 == copied_mem[0], "Copied test memory not affected"

    copied_mem[0] = 200  # modify test memory to equal copied Tensor

    del tensor
    np.testing.assert_array_equal(copied_tensor.get_memory_area(), copied_mem), "After initial tensor was deleted, " \
                                                                                "copied Tensor still has " \
                                                                                "its memory as expected"
Beispiel #4
0
def test_const_tensor_multi_dimensional_input(dt, data):
    tensor = ann.ConstTensor(ann.TensorInfo(ann.TensorShape((2, 2, 3, 3)), dt),
                             data)

    assert data.size == tensor.GetNumElements()
    assert data.nbytes == tensor.GetNumBytes()
    assert dt == tensor.GetDataType()
    assert tensor.get_memory_area().data
Beispiel #5
0
def test_tensor_shape__set_item_out_of_bounds():
    tensor_shape = ann.TensorShape((1, 2, 3))
    with pytest.raises(ValueError) as err:
        for i in range(4):
            tensor_shape[i] = 1

    assert "Invalid dimension index: 3 (number of dimensions is 3)" in str(
        err.value)
Beispiel #6
0
def test_caffe_parser_end_to_end(shared_data_folder):
    parser = ann.ICaffeParser = ann.ICaffeParser()

    # Load the network specifying the inputs and outputs
    input_name = "Placeholder"
    tensor_shape = {input_name: ann.TensorShape((1, 1, 28, 28))}
    requested_outputs = ["output"]

    network = parser.CreateNetworkFromBinaryFile(
        os.path.join(shared_data_folder, 'mock_model.caffemodel'),
        tensor_shape, requested_outputs)

    # Specify preferred backend
    preferred_backends = [ann.BackendId('CpuAcc'), ann.BackendId('CpuRef')]

    input_binding_info = parser.GetNetworkInputBindingInfo(input_name)

    options = ann.CreationOptions()
    runtime = ann.IRuntime(options)

    opt_network, messages = ann.Optimize(network, preferred_backends,
                                         runtime.GetDeviceSpec(),
                                         ann.OptimizerOptions())

    assert 0 == len(messages)

    net_id, messages = runtime.LoadNetwork(opt_network)

    assert "" == messages

    # Load test image data stored in input_caffe.npy
    input_tensor_data = np.load(
        os.path.join(shared_data_folder,
                     'caffe_parser/input_caffe.npy')).astype(np.float32)
    input_tensors = ann.make_input_tensors([input_binding_info],
                                           [input_tensor_data])

    # Load output binding info and
    outputs_binding_info = []
    for output_name in requested_outputs:
        outputs_binding_info.append(
            parser.GetNetworkOutputBindingInfo(output_name))
    output_tensors = ann.make_output_tensors(outputs_binding_info)

    runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)

    output_vectors = ann.workload_tensors_to_ndarray(output_tensors)

    # Load golden output file for result comparison.
    expected_output = np.load(
        os.path.join(shared_data_folder,
                     'caffe_parser/golden_output_caffe.npy'))

    # Check that output matches golden output to 4 decimal places (there are slight rounding differences after this)
    np.testing.assert_almost_equal(output_vectors[0], expected_output, 4)
Beispiel #7
0
def test_create_const_tensor_from_tensor():
    tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), ann.DataType_Float32)
    tensor = ann.Tensor(tensor_info)
    copied_tensor = ann.ConstTensor(tensor)

    assert copied_tensor != tensor, "Different objects"
    assert copied_tensor.GetInfo() != tensor.GetInfo(), "Different objects"
    assert copied_tensor.get_memory_area(
    ).ctypes.data == tensor.get_memory_area().ctypes.data, "Same memory area"
    assert copied_tensor.GetNumElements() == tensor.GetNumElements()
    assert copied_tensor.GetNumBytes() == tensor.GetNumBytes()
    assert copied_tensor.GetDataType() == tensor.GetDataType()
Beispiel #8
0
def test_tensor_info_ctor_shape():
    tensor_shape = ann.TensorShape((1, 1, 2))

    tensor_info = ann.TensorInfo(tensor_shape, ann.DataType_QAsymmU8, 0.5, 1)

    assert 2 == tensor_info.GetNumElements()
    assert 3 == tensor_info.GetNumDimensions()
    assert ann.DataType_QAsymmU8 == tensor_info.GetDataType()
    assert 0.5 == tensor_info.GetQuantizationScale()
    assert 1 == tensor_info.GetQuantizationOffset()

    shape = tensor_info.GetShape()

    assert 2 == shape.GetNumElements()
    assert 3 == shape.GetNumDimensions()
Beispiel #9
0
    def test_output_slot(self, network):

        # Create input, addition & output layer
        input1 = network.AddInputLayer(0, "input1")
        input2 = network.AddInputLayer(1, "input2")
        add = network.AddAdditionLayer("addition")
        output = network.AddOutputLayer(0, "output")

        # Connect the input/output slots for each layer
        input1.GetOutputSlot(0).Connect(add.GetInputSlot(0))
        input2.GetOutputSlot(0).Connect(add.GetInputSlot(1))
        add.GetOutputSlot(0).Connect(output.GetInputSlot(0))

        # Check IInputSlot GetConnection()
        add_get_input_connection = add.GetInputSlot(0).GetConnection()
        output_get_input_connection = output.GetInputSlot(0).GetConnection()

        # Check IOutputSlot GetConnection()
        add_get_output_connect = add.GetOutputSlot(0).GetConnection(0)
        assert isinstance(add_get_output_connect.GetConnection(), ann.IOutputSlot)

        # Test IOutputSlot GetNumConnections() & CalculateIndexOnOwner()
        assert add_get_input_connection.GetNumConnections() == 1
        assert len(add_get_input_connection) == 1
        assert add_get_input_connection[0]
        assert add_get_input_connection.CalculateIndexOnOwner() == 0

        # Check GetOwningLayerGuid(). Check that it is different for add and output layer
        assert add_get_input_connection.GetOwningLayerGuid() != output_get_input_connection.GetOwningLayerGuid()

        # Set TensorInfo
        test_tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), ann.DataType_Float32)

        # Check IsTensorInfoSet()
        assert not add_get_input_connection.IsTensorInfoSet()
        add_get_input_connection.SetTensorInfo(test_tensor_info)
        assert add_get_input_connection.IsTensorInfoSet()

        # Check GetTensorInfo()
        output_tensor_info = add_get_input_connection.GetTensorInfo()
        assert 2 == output_tensor_info.GetNumDimensions()
        assert 6 == output_tensor_info.GetNumElements()

        # Check Disconnect()
        assert output_get_input_connection.GetNumConnections() == 1  # 1 connection to Outputslot0 from input1
        add.GetOutputSlot(0).Disconnect(output.GetInputSlot(0))  # disconnect add.OutputSlot0 from Output.InputSlot0
        assert output_get_input_connection.GetNumConnections() == 0
Beispiel #10
0
def test_filenotfound_exception(shared_data_folder):
    parser = ann.ICaffeParser()

    # path to model
    path_to_model = os.path.join(shared_data_folder,
                                 'some_unknown_network.caffemodel')

    # generic tensor shape [1, 1, 1, 1]
    tensor_shape = {'data': ann.TensorShape((1, 1, 1, 1))}

    # requested_outputs
    requested_outputs = [""]

    with pytest.raises(RuntimeError) as err:
        parser.CreateNetworkFromBinaryFile(path_to_model, tensor_shape,
                                           requested_outputs)

    # Only check for part of the exception since the exception returns
    # absolute path which will change on different machines.
    assert 'Failed to open graph file' in str(err.value)
Beispiel #11
0
def parser(shared_data_folder):
    """
    Parse and setup the test network to be used for the tests below
    """

    # Create caffe parser
    parser = ann.ICaffeParser()

    # Specify path to model
    path_to_model = os.path.join(shared_data_folder, 'mock_model.caffemodel')

    # Specify the tensor shape relative to the input [1, 1, 28, 28]
    tensor_shape = {'Placeholder': ann.TensorShape((1, 1, 28, 28))}

    # Specify the requested_outputs
    requested_outputs = ["output"]

    # Parse caffe binary & create network
    parser.CreateNetworkFromBinaryFile(path_to_model, tensor_shape,
                                       requested_outputs)

    yield parser
Beispiel #12
0
def test_tensor_shape_one():
    tensor_shape = ann.TensorShape((10, ))
    assert 1 == tensor_shape.GetNumDimensions()
    assert 10 == tensor_shape.GetNumElements()
Beispiel #13
0
def test_tensor_shape_tuple_mess():
    tensor_shape = ann.TensorShape((1, "2", 3.0))

    assert 3 == tensor_shape.GetNumDimensions()
    assert 6 == tensor_shape.GetNumElements()
Beispiel #14
0
def _get_tensor_info(dt):
    tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)), dt)

    return tensor_info
Beispiel #15
0
def test_tensor_shape_list():

    with pytest.raises(TypeError) as err:
        ann.TensorShape([1, 2, 3])

    assert "Argument is not a tuple" in str(err.value)
Beispiel #16
0
def test_add_constant_layer_to_fully_connected():

    inputWidth = 1
    inputHeight = 1
    inputChannels = 5
    inputNum = 2

    outputChannels = 3
    outputNum = 2

    inputShape = (inputNum, inputChannels, inputHeight, inputWidth)
    outputShape = (outputNum, outputChannels)
    weightsShape = (inputChannels, outputChannels)
    biasShape = (outputChannels, )

    input = np.array([[1.0, 2.0, 3.0, 4.0, 5.0], [5.0, 4.0, 3.0, 2.0, 1.0]],
                     dtype=np.float32)

    weights = np.array(
        [[.5, 2., .5], [.5, 2., 1.], [.5, 2., 2.], [.5, 2., 3.], [.5, 2., 4.]],
        dtype=np.float32)

    biasValues = np.array([10, 20, 30], dtype=np.float32)

    expectedOutput = np.array([[
        0.5 + 1.0 + 1.5 + 2.0 + 2.5 + biasValues[0], 2.0 + 4.0 + 6.0 + 8.0 +
        10. + biasValues[1], 0.5 + 2.0 + 6.0 + 12. + 20. + biasValues[2]
    ],
                               [
                                   2.5 + 2.0 + 1.5 + 1.0 + 0.5 + biasValues[0],
                                   10.0 + 8.0 + 6.0 + 4.0 + 2. + biasValues[1],
                                   2.5 + 4.0 + 6.0 + 6. + 4. + biasValues[2]
                               ]],
                              dtype=np.float32)

    network = ann.INetwork()

    input_info = ann.TensorInfo(ann.TensorShape(inputShape),
                                ann.DataType_Float32, 0, 0, True)
    input_tensor = ann.ConstTensor(input_info, input)
    input_layer = network.AddInputLayer(0, "input")

    w_info = ann.TensorInfo(ann.TensorShape(weightsShape),
                            ann.DataType_Float32, 0, 0, True)
    w_tensor = ann.ConstTensor(w_info, weights)
    w_layer = network.AddConstantLayer(w_tensor, "weights")

    b_info = ann.TensorInfo(ann.TensorShape(biasShape), ann.DataType_Float32,
                            0, 0, True)
    b_tensor = ann.ConstTensor(b_info, biasValues)
    b_layer = network.AddConstantLayer(b_tensor, "bias")

    fc_descriptor = ann.FullyConnectedDescriptor()
    fc_descriptor.m_BiasEnabled = True
    fc_descriptor.m_ConstantWeights = True
    fully_connected = network.AddFullyConnectedLayer(fc_descriptor, "fc")

    output_info = ann.TensorInfo(ann.TensorShape(outputShape),
                                 ann.DataType_Float32)
    output_tensor = ann.Tensor(output_info, np.zeros([1, 1], dtype=np.float32))
    output = network.AddOutputLayer(0, "output")

    input_layer.GetOutputSlot(0).Connect(fully_connected.GetInputSlot(0))
    w_layer.GetOutputSlot(0).Connect(fully_connected.GetInputSlot(1))
    b_layer.GetOutputSlot(0).Connect(fully_connected.GetInputSlot(2))
    fully_connected.GetOutputSlot(0).Connect(output.GetInputSlot(0))

    input_layer.GetOutputSlot(0).SetTensorInfo(input_info)
    w_layer.GetOutputSlot(0).SetTensorInfo(w_info)
    b_layer.GetOutputSlot(0).SetTensorInfo(b_info)
    fully_connected.GetOutputSlot(0).SetTensorInfo(output_info)

    preferred_backends = [ann.BackendId('CpuRef')]
    options = ann.CreationOptions()
    runtime = ann.IRuntime(options)
    opt_network, messages = ann.Optimize(network, preferred_backends,
                                         runtime.GetDeviceSpec(),
                                         ann.OptimizerOptions())
    net_id, messages = runtime.LoadNetwork(opt_network)

    input_tensors = [(0, input_tensor)]
    output_tensors = [(0, output_tensor)]
    runtime.EnqueueWorkload(net_id, input_tensors, output_tensors)

    output_vectors = ann.workload_tensors_to_ndarray(output_tensors)

    assert (output_vectors == expectedOutput).all()
Beispiel #17
0
def test_tensor_shape_tuple_mess_fail():

    with pytest.raises(TypeError) as err:
        ann.TensorShape((1, "two", 3.0))

    assert "All elements must be numbers" in str(err.value)
Beispiel #18
0
def test_tensor_shape_varags():
    with pytest.raises(TypeError) as err:
        ann.TensorShape(1, 2, 3)

    assert "__init__() takes 2 positional arguments but 4 were given" in str(
        err.value)
Beispiel #19
0
def test_tensor_shape_tuple():
    tensor_shape = ann.TensorShape((1, 2, 3))

    assert 3 == tensor_shape.GetNumDimensions()
    assert 6 == tensor_shape.GetNumElements()
Beispiel #20
0
def test_tensor_shape_empty():
    with pytest.raises(RuntimeError) as err:
        ann.TensorShape(())

    assert "Tensor numDimensions must be greater than 0" in str(err.value)
Beispiel #21
0
def test_tensor_info__str__():
    tensor_info = ann.TensorInfo(ann.TensorShape((2, 3)),
                                 ann.DataType_QAsymmU8, 0.5, 1)

    assert tensor_info.__str__() == "TensorInfo{DataType: 2, IsQuantized: 1, QuantizationScale: 0.500000, " \
                                    "QuantizationOffset: 1, NumDimensions: 2, NumElements: 6}"
Beispiel #22
0
def test_tensor_shape___str__():
    tensor_shape = ann.TensorShape((1, 2, 3))

    assert str(
        tensor_shape
    ) == "TensorShape{Shape(1, 2, 3), NumDimensions: 3, NumElements: 6}"