Example #1
0
def test_ssd_mobilenet_v2_model():
    model = DLRModel(MODEL_PATH.as_posix())
    data = np.load(DATA_PATH)
    assert model.get_input_names() == ['image_tensor']
    assert model.get_output_names() == [
        'detection_scores:0', 'detection_classes:0', 'num_detections:0'
    ]
    assert model.get_input_dtypes() == ['uint8']
    assert model.get_output_dtypes() == ['float32', 'float32', 'float32']
    outputs = model.run({"image_tensor": data})
    assert outputs[0].shape == (1, 100, 4)
    assert outputs[1].shape == (1, 100)
    assert outputs[2].shape == (1, 100)
    detections = np.multiply(np.ceil(outputs[1]), outputs[2])
    expected = np.zeros(detections.shape)
    expected[:, :6] = np.array([[1., 1., 1., 2., 3., 1]])
    comparison = detections == expected
    assert comparison.all()
Example #2
0
def test_resnet():
    # Load the model
    model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
            'resnet18_v1')
    device = 'cpu'
    model = DLRModel(model_path, device)

    # Run the model
    image = np.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dog.npy')).astype(np.float32)
    #flatten within a input array
    input_data = {'data': image}
    print('Testing inference on resnet18...')
    probabilities = model.run(input_data) #need to be a list of input arrays matching input names
    assert probabilities[0].argmax() == 151
    assert model.get_input_names() == ["data"]
    assert model.get_input_dtypes() == ["float32"]
    assert model.get_output_dtypes() == ["float32"]
    assert model.get_input_dtype(0) == "float32"
    assert model.get_output_dtype(0) == "float32"
def test_ssd_mobilenet_v2_model():
    model = DLRModel(MODEL_PATH.as_posix())
    data = np.load(DATA_PATH)
    assert model.get_input_names() == ['image_tensor']
    assert model.get_output_names() == [
        'detection_classes:0', 'num_detections:0', 'detection_boxes:0',
        'detection_scores:0'
    ]
    assert model.get_input_dtypes() == ['uint8']
    assert model.get_output_dtypes() == [
        'float32', 'float32', 'float32', 'float32'
    ]
    outputs = model.run({"image_tensor": data})
    assert outputs[0].shape == (1, 100)
    assert outputs[1].shape == (1, )
    assert outputs[2].shape == (1, 100, 4)
    assert outputs[3].shape == (1, 100)
    detections = np.multiply(np.ceil(outputs[3]), outputs[0])
    expected = np.zeros(detections.shape)
    assert np.count_nonzero(detections) == outputs[1][0]
def test_mobilenet_v1_0_75_224_quant():
    # Load the model
    model_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                              'mobilenet_v1_0.75_224_quant')
    device = 'cpu'
    model = DLRModel(model_path, device)
    # load image (dtype: uint8)
    image = np.load(
        os.path.join(os.path.dirname(os.path.abspath(__file__)),
                     'cat_224_uint8.npy'))
    print('Testing inference on mobilenet_v1_0.75_224_quant...')
    probabilities = model.run({'input': image})
    assert probabilities[0].argmax() == 282
    assert model.get_input_names() == ["input"]
    assert model.get_input_dtypes() == ["uint8"]
    assert model.get_output_dtypes() == ["uint8"]
    assert model.get_input_dtype(0) == "uint8"
    assert model.get_output_dtype(0) == "uint8"
    input2 = model.get_input("input")
    assert input2.dtype == 'uint8'
    assert input2.shape == (1, 224, 224, 3)
    assert (input2 == image).all()
Example #5
0
def test_tf_model(dev_type=None, dev_id=None):
    _generate_frozen_graph()
    model = DLRModel(FROZEN_GRAPH_PATH, dev_type, dev_id)
    inp_names = model.get_input_names()
    assert inp_names == ['import/input1:0', 'import/input2:0']

    out_names = model.get_output_names()
    assert out_names == [
        'import/preproc/output1:0', 'import/preproc/output2:0'
    ]

    inp1 = [[4., 1.], [3., 2.]]
    inp2 = [[0., 1.], [1., 0.]]

    res = model.run({'import/input1:0': inp1, 'import/input2:0': inp2})
    assert res is not None
    assert len(res) == 2
    assert np.alltrue(res[0] == [[36., 361.], [49., 324.]])
    assert res[1] == 1

    m_inp1 = model.get_input('import/input1:0')
    m_inp2 = model.get_input('import/input2:0')
    assert np.alltrue(m_inp1 == inp1)
    assert np.alltrue(m_inp2 == inp2)
Example #6
0
def test_tflite_model():
    _generate_tflite_file()

    m = DLRModel(TFLITE_FILE_PATH)
    inp_names = m.get_input_names()
    assert sorted(inp_names) == ['input1', 'input2']

    out_names = m.get_output_names()
    assert sorted(out_names) == ['preproc/output1', 'preproc/output2']

    inp1 = np.array([[4., 1.], [3., 2.]]).astype("float32")
    inp2 = np.array([[0., 1.], [1., 0.]]).astype("float32")

    res = m.run({'input1': inp1, 'input2': inp2})
    assert res is not None
    assert len(res) == 2
    exp_out0 = np.array([[36., 361.], [49., 324.]]).astype("float32")
    assert np.alltrue(res[0] == exp_out0)
    assert res[1] == 1

    m_inp1 = m.get_input('input1')
    m_inp2 = m.get_input('input2')
    assert np.alltrue(m_inp1 == inp1)
    assert np.alltrue(m_inp2 == inp2)