Exemple #1
0
    def test_xlayer_data(self):

        X = XLayer(data=[np.array([1, 2, 3])])

        assert isinstance(X.data, list)

        assert len(X.data) == 1
        np.testing.assert_array_equal(X.data[0], np.array([1, 2, 3]))

        X.data[0] *= 2
        np.testing.assert_array_equal(X.data[0], np.array([2, 4, 6]))

        X.data = [np.array([3., 5., 7.], dtype=np.float32)]
        np.testing.assert_array_equal(
            X.data[0], np.array([3., 5., 7.], dtype=np.float32))

        X.data = [np.array([2., 4., 6.], dtype=np.float32),
                  np.array([0, 0], dtype=np.float32)]
        np.testing.assert_array_equal(
            X.data[0], np.array([2., 4., 6.], dtype=np.float32))
        np.testing.assert_array_equal(
            X.data[1], np.array([0., 0.], dtype=np.float32))

        c_data = ConvData(
            weights=np.ones((4, 2, 3, 3), dtype=np.float32),
            biases=np.array([3, 3], dtype=np.float16)
        )
        # print("c_data", c_data)
        X2 = XLayer(
            type=['Convolution'],
            data=[np.ones((4, 2, 3, 3), dtype=np.float32) * 2.,
                  np.array([3., 3.], dtype=np.float16)]
        )

        assert isinstance(X2.data, ConvData)
        np.testing.assert_array_equal(X2.data.weights, c_data.weights * 2)
        np.testing.assert_array_equal(X2.data.biases, c_data.biases)

        X2.data = ConvData(
            weights=np.ones((4, 2, 3, 3), dtype=np.float32) * 3,
            biases=np.copy(X2.data.biases)
        )
        np.testing.assert_array_equal(
            X2.data.weights,
            np.ones((4, 2, 3, 3), dtype=np.float32) * 3
        )
        np.testing.assert_array_equal(
            X2.data.biases, np.array([3, 3], dtype=np.float16))

        # Scale
        X2.type[0] = 'Scale'
        X2.data = ScaleData(
            gamma=np.array([1, 2], dtype=np.float32),
            beta=np.array([3, 3], dtype=np.float32)
        )
        assert X2.type == ['Scale']
        assert isinstance(X2.data, ScaleData)
        np.testing.assert_array_equal(
            X2.data.gamma,
            np.array([1, 2], dtype=np.float32)
        )
        np.testing.assert_array_equal(
            X2.data.beta, np.array([3, 3], dtype=np.float32))

        # BatchData
        X2.type[0] = 'BatchNorm'
        X2.data = BatchData(
            mu=np.array([1, 0.5], dtype=np.float32),
            sigma_square=np.array([1, 2], dtype=np.float32),
            gamma=np.array([1, 2], dtype=np.float32),
            beta=np.array([3, 3], dtype=np.float32)
        )
        assert X2.type == ['BatchNorm']
        assert isinstance(X2.data, BatchData)
        np.testing.assert_array_equal(
            X2.data.mu,
            np.array([1, 0.5], dtype=np.float32)
        )
        np.testing.assert_array_equal(
            X2.data.sigma_square,
            np.array([1, 2], dtype=np.float32)
        )
        np.testing.assert_array_equal(
            X2.data.gamma,
            np.array([1, 2], dtype=np.float32)
        )
        np.testing.assert_array_equal(
            X2.data.beta,
            np.array([3, 3], dtype=np.float32)
        )
Exemple #2
0
    def test_xgraph_serialization_basic(self):
        net = [
            XLayer(name='in1',
                   type=['Input'],
                   shapes=TensorShape([1, 1, 4, 4]),
                   bottoms=[],
                   tops=['add1'],
                   targets=[]),
            XLayer(name='in2',
                   type=['Input'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=[],
                   tops=['add1'],
                   targets=[]),
            XLayer(name='conv1',
                   type=['Convolution'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['in1'],
                   tops=['bias_add1'],
                   data=ConvData(weights=np.array([[[[1, 2], [3, 4]]]],
                                                  dtype=np.float32),
                                 biases=np.array([0., 1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='bias_add1',
                   type=['BiasAdd'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['conv1'],
                   tops=['bn1'],
                   data=[np.array([0., -1.], dtype=np.float32)],
                   targets=[]),
            XLayer(name='bn1',
                   type=['BatchNorm'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['bias_add1'],
                   tops=['scale1'],
                   data=BatchData(mu=np.array([.5, 2.], dtype=np.float32),
                                  sigma_square=np.array([1., 1.],
                                                        dtype=np.float32),
                                  gamma=np.array([.5, 2.], dtype=np.float32),
                                  beta=np.array([0., -1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='scale1',
                   type=['Scale'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['bn1'],
                   tops=['add1'],
                   data=ScaleData(np.array([.5, 2.], dtype=np.float32),
                                  np.array([0., -1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='add1',
                   type=['Eltwise'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['scale1', 'in2'],
                   tops=[],
                   targets=[])
        ]
        xgraph = TestIOAPIs.xgraph_factory.build_from_xlayer(net)

        xgraph_str = api.get_xgraph_str(xgraph)

        xg = api.read_xgraph_str(xgraph_str)
        xg_layers = xg.get_layers()
        # import pdb; pdb.set_trace()

        assert len(xg_layers) == 7

        assert xg_layers[0].type[0] == 'Input'

        assert xg_layers[1].type[0] == 'Convolution'
        np.testing.assert_array_equal(
            xg_layers[1].data[0],
            np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
    def test_io_params(self):
        net = [
            XLayer(name='in1',
                   type=['Input'],
                   shapes=TensorShape([1, 1, 4, 4]),
                   bottoms=[],
                   tops=['add1'],
                   targets=[]),
            XLayer(name='in2',
                   type=['Input'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=[],
                   tops=['add1'],
                   targets=[]),
            XLayer(name='conv1',
                   type=['Convolution'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['in1'],
                   tops=['bias_add1'],
                   data=ConvData(weights=np.array([[[[1, 2], [3, 4]]]],
                                                  dtype=np.float32),
                                 biases=np.array([0., 1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='bias_add1',
                   type=['BiasAdd'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['conv1'],
                   tops=['bn1'],
                   data=[np.array([0., -1.], dtype=np.float32)],
                   targets=[]),
            XLayer(name='bn1',
                   type=['BatchNorm'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['bias_add1'],
                   tops=['scale1'],
                   data=BatchData(mu=np.array([.5, 2.], dtype=np.float32),
                                  sigma_square=np.array([1., 1.],
                                                        dtype=np.float32),
                                  gamma=np.array([.5, 2.], dtype=np.float32),
                                  beta=np.array([0., -1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='scale1',
                   type=['Scale'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['bn1'],
                   tops=['add1'],
                   data=ScaleData(np.array([.5, 2.], dtype=np.float32),
                                  np.array([0., -1.], dtype=np.float32)),
                   targets=[]),
            XLayer(name='add1',
                   type=['Eltwise'],
                   shapes=TensorShape([1, 2, 3, 3]),
                   bottoms=['scale1', 'in2'],
                   tops=[],
                   targets=[])
        ]
        xgraph = TestXGraphIO.xgraph_factory.build_from_xlayer(net)
        xlayers = xgraph.get_layers()

        conv_weights = xlayers[1].data.weights
        conv_biases = xlayers[1].data.biases

        bias_add_biases = xlayers[2].data[0]

        bn_mu = xlayers[3].data.mu
        bn_var = xlayers[3].data.sigma_square
        bn_gamma = xlayers[3].data.gamma
        bn_beta = xlayers[3].data.beta

        scale_gamma = xlayers[4].data.gamma
        scale_beta = xlayers[4].data.beta

        TestXGraphIO.xgraph_io.save(xgraph, 'test')

        loaded_xgraph = TestXGraphIO.xgraph_io.load('test.json', 'test.h5')
        assert isinstance(loaded_xgraph.get_layers()[0].shapes, TensorShape)

        assert (len(loaded_xgraph.get_layers()) == len(xgraph.get_layers()))
        loaded_xlayers = loaded_xgraph.get_layers()

        loaded_conv_weights = loaded_xlayers[1].data.weights
        loaded_conv_biases = loaded_xlayers[1].data.biases
        loaded_bias_add_biases = loaded_xlayers[2].data[0]

        loaded_bn_mu = loaded_xlayers[3].data.mu
        loaded_bn_var = loaded_xlayers[3].data.sigma_square
        loaded_bn_gamma = loaded_xlayers[3].data.gamma
        loaded_bn_beta = loaded_xlayers[3].data.beta

        loaded_scale_gamma = loaded_xlayers[4].data.gamma
        loaded_scale_beta = loaded_xlayers[4].data.beta

        np.testing.assert_array_almost_equal(conv_weights, loaded_conv_weights)
        np.testing.assert_array_almost_equal(conv_biases, loaded_conv_biases)

        np.testing.assert_array_almost_equal(bias_add_biases,
                                             loaded_bias_add_biases)

        np.testing.assert_array_almost_equal(bn_mu, loaded_bn_mu)
        np.testing.assert_array_almost_equal(bn_var, loaded_bn_var)
        np.testing.assert_array_almost_equal(bn_gamma, loaded_bn_gamma)
        np.testing.assert_array_almost_equal(bn_beta, loaded_bn_beta)

        np.testing.assert_array_almost_equal(scale_gamma, loaded_scale_gamma)
        np.testing.assert_array_almost_equal(scale_beta, loaded_scale_beta)

        os.remove('test.json')
        os.remove('test.h5')
Exemple #4
0
    def test_inception_block(self):

        W1 = np.reshape(
            np.array([[[1, 0, 1], [1, 0, 1], [1, 0, 1]]], dtype=np.float32),
            (1, 1, 3, 3))
        B1 = np.array([0.], dtype=np.float32)
        W2 = np.reshape(
            np.array([[[1, 1, 0], [1, 1, 0], [1, 1, 0]]], dtype=np.float32),
            (1, 1, 3, 3))
        B2 = np.array([0.], dtype=np.float32)

        gamma = np.array([2.])
        beta = np.array([1.])

        net = [
            XLayer(name='in1',
                   type=['Input'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=[],
                   tops=['scale1'],
                   layer=['in1'],
                   targets=[]),
            XLayer(name='scale1',
                   type=['Scale'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=['in1'],
                   tops=['conv1', 'conv2'],
                   layer=['scale1'],
                   targets=[],
                   data=ScaleData(gamma, beta),
                   attrs={'axis': 1}),
            XLayer(name='conv1',
                   type=['Convolution'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=['scale1'],
                   tops=['concat1'],
                   layer=['conv1'],
                   data=ConvData(W1, B1),
                   attrs={
                       'data_layout': 'NCHW',
                       'kernel_layout': 'OIHW',
                       'shape': [1, 1, 4, 4],
                       'padding': [[0, 0], [0, 0], [1, 1], [1, 1]],
                       'strides': [1, 1],
                       'dilation': [1, 1],
                       'groups': 1
                   },
                   targets=[]),
            XLayer(name='conv2',
                   type=['Convolution'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=['scale1'],
                   tops=['concat1'],
                   layer=['conv2'],
                   data=ConvData(W2, B2),
                   attrs={
                       'data_layout': 'NCHW',
                       'kernel_layout': 'OIHW',
                       'shape': [1, 1, 4, 4],
                       'padding': [[0, 0], [0, 0], [1, 1], [1, 1]],
                       'strides': [1, 1],
                       'dilation': [1, 1],
                       'groups': 1
                   },
                   targets=[]),
            XLayer(name='concat1',
                   type=['Concat'],
                   shapes=[-1, 2, 4, 4],
                   sizes=[16],
                   bottoms=['conv1', 'conv2'],
                   tops=[],
                   layer=['concat1'],
                   targets=[],
                   attrs={'axis': 1})
        ]
        xgraph = TestMSEQuantizer.xgraph_factory.build_from_xlayer(net)

        def inputs_func(iter):
            inputs = np.reshape(
                np.array([[1, 3, -1, -11], [3, 1, -1, 0], [1, 4, -3, -3],
                          [1, 1, -1, -1]],
                         dtype=np.float32), (1, 1, 4, 4))

            return {'in1': inputs}

        quantizer = XGraphMSEThresholdQuantizer(xgraph,
                                                inputs_func,
                                                work_dir=FILE_PATH)
        q_xgraph = quantizer.quantize(subgraphs_only=False)

        assert 'xgraph' in quantizer._quant_layers
        assert len(quantizer._quant_layers['xgraph']) == 4

        # assert(('scale1', 'Scale', None) in \
        #    quantizer._quant_layers['xgraph'])
        assert ('conv1', 'Convolution', None) in\
            quantizer._quant_layers['xgraph']
        assert ('conv2', 'Convolution', None) in\
            quantizer._quant_layers['xgraph']
        assert ('concat1', 'Concat', None) in\
            quantizer._quant_layers['xgraph']

        assert quantizer._quant_param.th_layer_in['scale1'][0] <= 11.
        assert quantizer._quant_param.th_layer_in['scale1'][0] >= 0.
        assert quantizer._quant_param.th_layer_out['scale1'][0] <= 22.
        assert quantizer._quant_param.th_layer_out['scale1'][0] >= 0.

        assert quantizer._quant_param.th_layer_in['conv1'] ==\
            quantizer._quant_param.th_layer_out['scale1']
        np.testing.assert_array_equal(
            quantizer._quant_param.th_params['conv1'], np.array([1.]))
        # NOTE: Conv2d does not take over threshold from subqequent concat
        #   layer because it's expected that a scaling layer will be inserted
        assert quantizer._quant_param.th_layer_out['conv1'][0] <= 22.

        assert quantizer._quant_param.th_layer_in['conv2'][0] ==\
            quantizer._quant_param.th_layer_out['scale1']
        np.testing.assert_array_equal(
            quantizer._quant_param.th_params['conv2'], np.array([1.]))
        assert quantizer._quant_param.th_layer_out['conv2'][0] <= 33.

        # print("TEST")
        # print(quantizer._quant_param.th_layer_in['concat1'])
        # print(quantizer._quant_param.th_layer_out['conv2'])
        assert math.isclose(quantizer._quant_param.th_layer_in['concat1'],
                            quantizer._quant_param.th_layer_out['conv2'],
                            rel_tol=1e-4)
        # assert quantizer._quant_param.th_layer_in['concat1'] ==\
        #     quantizer._quant_param.th_layer_out['conv2']
        assert quantizer._quant_param.th_layer_out['concat1'] ==\
            quantizer._quant_param.th_layer_in['concat1']

        quant_file = os.path.join(FILE_PATH, 'xgraph_quant.json')
        with open(quant_file) as f:
            qp_d = json.load(f)
            network = qp_d['network']

        assert network[0]['name'] == 'scale1'
        assert network[0]['th_layer_in'] ==\
            quantizer._quant_param.th_layer_in['scale1'][0]
        assert network[0]['th_layer_out'] ==\
            quantizer._quant_param.th_layer_out['scale1'][0]

        assert network[1]['name'] == 'conv1'
        assert network[1]['th_layer_in'] ==\
            quantizer._quant_param.th_layer_in['conv1'][0]
        # NOTE: adjustment for different scaling of concat input layers ! conv2
        assert network[1]['th_layer_out'] ==\
            quantizer._quant_param.th_layer_out['conv2'][0]
        assert network[1]['th_params'] == [1.0]

        assert network[2]['name'] == 'conv2'
        assert network[2]['th_layer_in'] ==\
            quantizer._quant_param.th_layer_in['conv2'][0]
        assert network[2]['th_layer_out'] ==\
            quantizer._quant_param.th_layer_out['conv2'][0]
        assert network[2]['th_params'] == [1.0]

        assert network[3]['name'] == 'concat1'
        assert network[3]['th_layer_in'] ==\
            quantizer._quant_param.th_layer_in['concat1'][0]
        assert network[3]['th_layer_out'] ==\
            quantizer._quant_param.th_layer_out['concat1'][0]

        os.remove(quant_file)
Exemple #5
0
    def test_inception_block(self):

        W1 = np.reshape(
            np.array([[[1, 0, 1], [1, 0, 1], [1, 0, 1]]], dtype=np.float32),
            (1, 1, 3, 3))
        B1 = np.array([0.], dtype=np.float32)
        W2 = np.reshape(
            np.array([[[1, 1, 0], [1, 1, 0], [1, 1, 0]]], dtype=np.float32),
            (1, 1, 3, 3))
        B2 = np.array([0.], dtype=np.float32)

        gamma = np.array([2.])
        beta = np.array([1.])

        net = [
            XLayer(name='in1',
                   type=['Input'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=[],
                   tops=['scale1'],
                   layer=['in1'],
                   targets=[]),
            XLayer(name='scale1',
                   type=['Scale'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=['in1'],
                   tops=['conv1', 'conv2'],
                   layer=['scale1'],
                   targets=[],
                   data=ScaleData(gamma, beta),
                   attrs={'axis': 1}),
            XLayer(name='conv1',
                   type=['Convolution'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=['scale1'],
                   tops=['concat1'],
                   layer=['conv1'],
                   data=ConvData(W1, B1),
                   attrs={
                       'data_layout': 'NCHW',
                       'kernel_layout': 'OIHW',
                       'shape': [1, 1, 4, 4],
                       'padding': [[0, 0], [0, 0], [1, 1], [1, 1]],
                       'strides': [1, 1],
                       'dilation': [1, 1],
                       'groups': 1
                   },
                   targets=[]),
            XLayer(name='conv2',
                   type=['Convolution'],
                   shapes=[-1, 1, 4, 4],
                   sizes=[16],
                   bottoms=['scale1'],
                   tops=['concat1'],
                   layer=['conv2'],
                   data=ConvData(W2, B2),
                   attrs={
                       'data_layout': 'NCHW',
                       'kernel_layout': 'OIHW',
                       'shape': [1, 1, 4, 4],
                       'padding': [[0, 0], [0, 0], [1, 1], [1, 1]],
                       'strides': [1, 1],
                       'dilation': [1, 1],
                       'groups': 1
                   },
                   targets=[]),
            XLayer(name='concat1',
                   type=['Concat'],
                   shapes=[-1, 2, 4, 4],
                   sizes=[16],
                   bottoms=['conv1', 'conv2'],
                   tops=[],
                   layer=['concat1'],
                   targets=[],
                   attrs={'axis': 1})
        ]
        xgraph = TestDefaultQuantizer.xgraph_factory.build_from_xlayer(net)

        def inputs_func(iter):
            inputs = np.reshape(
                np.array([[1, 3, -1, -11], [3, 1, -1, 0], [1, 4, -3, -3],
                          [1, 1, -1, -1]],
                         dtype=np.float32), (1, 1, 4, 4))

            return {'in1': inputs}

        quantizer = XGraphDefaultQuantizer(xgraph,
                                           inputs_func,
                                           work_dir=FILE_PATH)
        quantizer._quantize(subgraphs_only=False)

        assert ('xgraph' in quantizer._quant_layers)
        assert (len(quantizer._quant_layers['xgraph']) == 5)
        assert (('in1', 'Input', None) in quantizer._quant_layers['xgraph'])
        # assert(('scale1', 'Scale', None) in \
        #    quantizer._quant_layers['xgraph'])
        assert (('conv1', 'Convolution', None)
                in quantizer._quant_layers['xgraph'])
        assert (('conv2', 'Convolution', None)
                in quantizer._quant_layers['xgraph'])
        assert (('concat1', 'Concat', None)
                in quantizer._quant_layers['xgraph'])

        assert (quantizer._quant_param.th_layer_out['in1'] == [11.])

        assert (quantizer._quant_param.th_layer_in['scale1'] == [11.])
        assert (quantizer._quant_param.th_layer_out['scale1'] == [21.])

        assert (quantizer._quant_param.th_layer_in['conv1'] == [21.])
        np.testing.assert_array_equal(
            quantizer._quant_param.th_params['conv1'], np.array([1.]))
        # NOTE: Conv2d takes over threshold of input layer because of
        #   subsequent add layer
        assert (quantizer._quant_param.th_layer_out['conv1'] == [32.])

        assert (quantizer._quant_param.th_layer_in['conv2'] == [21.])
        np.testing.assert_array_equal(
            quantizer._quant_param.th_params['conv2'], np.array([1.]))
        # NOTE: Conv2d takes over threshold of input layer because of
        #   subsequent add layer
        assert (quantizer._quant_param.th_layer_out['conv2'] == [32.])

        assert (quantizer._quant_param.th_layer_in['concat1'] == [32.])
        assert (quantizer._quant_param.th_layer_out['concat1'] == [32.])

        # Test json saving
        quant_file = os.path.join(FILE_PATH, 'quant3.json')
        quantizer._quant_param.save_to_dpu_v1_json(
            quantizer._quant_layers['xgraph'], quant_file)

        with open(quant_file) as f:
            qp_d = json.load(f)
            network = qp_d['network']

        assert (network[0]['name'] == 'scale1')
        assert (network[0]['th_layer_in'] == 11.0)
        assert (network[0]['th_layer_out'] == 21.0)

        assert (network[1]['name'] == 'conv1')
        assert (network[1]['th_layer_in'] == 21.0)
        assert (network[1]['th_layer_out'] == 32.0)
        assert (network[1]['th_params'] == [1.0])

        assert (network[2]['name'] == 'conv2')
        assert (network[2]['th_layer_in'] == 21.0)
        assert (network[2]['th_layer_out'] == 32.0)
        assert (network[2]['th_params'] == [1.0])

        assert (network[3]['name'] == 'concat1')
        assert (network[3]['th_layer_in'] == 32.)
        assert (network[3]['th_layer_out'] == 32.)

        os.remove(quant_file)