예제 #1
0
    def test_xgraph_add_remove(self):

        xgraph = XGraph()
        xgraph.add(
            XLayer(name='in1', type=['Input'], bottoms=[], tops=[],
                   targets=[]))

        assert (len(xgraph) == 1)
        assert (len(xgraph.get_layer_names()) == 1)
        assert (len(xgraph.get_output_names()) == 1)
        assert (len(xgraph.get_input_names()) == 1)

        X_conv = XLayer(name='conv1',
                        type=['Convolution'],
                        bottoms=['in1'],
                        tops=[],
                        data=ConvData(weights=np.array([[[[1, 2], [3, 4]]]],
                                                       dtype=np.float32),
                                      biases=np.array([0., 1.],
                                                      dtype=np.float32)),
                        targets=[])
        xgraph.add(X_conv)

        assert (len(xgraph) == 2)
        assert (len(xgraph.get_layer_names()) == 2)
        assert (len(xgraph.get_output_names()) == 1)
        assert (len(xgraph.get_input_names()) == 1)

        xgraph.remove(X_conv.name)

        assert (len(xgraph) == 1)
        assert (len(xgraph.get_layer_names()) == 1)
        assert (len(xgraph.get_output_names()) == 1)
        assert (len(xgraph.get_input_names()) == 1)
예제 #2
0
        def _test_add_remove(in_name: str, conv_name: str):
            in1 = px.ops.input(op_name=in_name, shape=[1, 2, 4, 4])
            W = px.ops.constant(
                "W", np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
            X_conv = px.ops.conv2d(op_name=conv_name,
                                   input_layer=in1,
                                   weights_layer=W,
                                   kernel_size=[2, 2])

            xgraph = XGraph()
            xgraph.add(in1)

            assert len(xgraph) == 1
            assert len(xgraph.get_layer_names()) == 1
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            xgraph.add(X_conv)

            assert len(xgraph) == 2
            assert len(xgraph.get_layer_names()) == 2
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            xgraph.remove(X_conv.name)

            assert len(xgraph) == 1
            assert len(xgraph.get_layer_names()) == 1
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1
예제 #3
0
    def test_xgraph_add_get(self):

        xgraph = XGraph()

        xgraph.add(XLayer(
            name='in1',
            type=['Input'],
            bottoms=[],
            tops=[],
            targets=[]
        ))

        assert len(xgraph) == 1
        assert len(xgraph.get_layer_names()) == 1
        assert len(xgraph.get_output_names()) == 1
        assert len(xgraph.get_input_names()) == 1

        assert isinstance(xgraph.get('in1'), XLayer)
        assert xgraph.get('in1').bottoms == []
        assert xgraph.get('in1').tops == []

        X_conv = XLayer(
            name='conv1',
            type=['Convolution'],
            bottoms=['in1'],
            tops=[],
            data=ConvData(
                weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                biases=np.array([0., 1.], dtype=np.float32)
            ),
            targets=[]
        )
        xgraph.add(X_conv)

        assert len(xgraph) == 2
        assert xgraph.get_layer_names() == ['in1', 'conv1']
        assert xgraph.get_output_names() == ['conv1']
        assert xgraph.get_input_names() == ['in1']

        assert xgraph.get('in1').tops == ['conv1']

        assert isinstance(xgraph.get('conv1'), XLayer)
        assert xgraph.get('conv1').bottoms == ['in1']
        assert xgraph.get('conv1').tops == []
        assert xgraph.get('conv1').type == ['Convolution']

        np.testing.assert_array_equal(
            xgraph.get('conv1').data.weights,
            np.array([[[[1, 2], [3, 4]]]], dtype=np.float32)
        )
        np.testing.assert_array_equal(
            xgraph.get('conv1').data.biases,
            np.array([0., 1.], dtype=np.float32)
        )

        xgraph.get('conv1').data = ConvData(
            weights=xgraph.get('conv1').data.weights * 2,
            biases=xgraph.get('conv1').data.biases
        )
        np.testing.assert_array_equal(
            xgraph.get('conv1').data.weights,
            np.array([[[[2, 4], [6, 8]]]], dtype=np.float32)
        )

        xgraph.remove(X_conv.name)

        assert len(xgraph) == 1
        assert 'in1' in xgraph
        assert len(xgraph.get_layer_names()) == 1
        assert len(xgraph.get_output_names()) == 1
        assert len(xgraph.get_input_names()) == 1
예제 #4
0
    def test_xgraph_insert(self):

        xgraph = XGraph()
        xgraph.add(XLayer(
            name='in1',
            type=['Input'],
            bottoms=[],
            tops=[],
            targets=[]
        ))

        assert(len(xgraph) == 1)
        assert(len(xgraph.get_layer_names()) == 1)
        assert(len(xgraph.get_output_names()) == 1)
        assert(len(xgraph.get_input_names()) == 1)

        X_conv = XLayer(
            name='conv1',
            type=['Convolution'],
            bottoms=['in1'],
            tops=[],
            data=ConvData(
                weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                biases=np.array([0., 1.], dtype=np.float32)
            ),
            targets=[]
        )
        xgraph.add(X_conv)

        assert len(xgraph) == 2
        assert len(xgraph.get_layer_names()) == 2
        assert len(xgraph.get_output_names()) == 1
        assert len(xgraph.get_input_names()) == 1

        X_pool = XLayer(
            name='pool1',
            type=['Pooling'],
            bottoms=['in1'],
            tops=['conv1'],
            targets=[]
        )
        xgraph.insert(X_pool)

        assert len(xgraph) == 3
        assert len(xgraph.get_layer_names()) == 3
        assert len(xgraph.get_output_names()) == 1
        assert len(xgraph.get_input_names()) == 1

        xlayers = xgraph.get_layers()
        assert xlayers[0].name == 'in1'
        assert xlayers[0].bottoms == []
        assert xlayers[0].tops == ['pool1']
        assert xlayers[1].name == 'pool1'
        assert xlayers[1].bottoms == ['in1']
        assert xlayers[1].tops == ['conv1']
        assert xlayers[2].name == 'conv1'
        assert xlayers[2].bottoms == ['pool1']
        assert xlayers[2].tops == []

        X_in2 = XLayer(
            name='in2',
            type=['Input'],
            bottoms=[],
            tops=[],
            targets=[]
        )
        xgraph.add(X_in2)

        X_add = XLayer(
            name='add1',
            type=['Eltwise'],
            bottoms=['conv1', 'in2'],
            tops=[],
            targets=[]
        )
        xgraph.add(X_add)

        X_conv2 = XLayer(
            name='conv2',
            type=['Convolution'],
            bottoms=['in2'],
            tops=['add1'],
            data=ConvData(
                weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                biases=np.array([0., 1.], dtype=np.float32)
            ),
            targets=[]
        )
        xgraph.insert(X_conv2)

        assert(len(xgraph) == 6)
        assert(len(xgraph.get_layer_names()) == 6)
        assert(len(xgraph.get_output_names()) == 1)
        assert(len(xgraph.get_input_names()) == 2)

        xlayers = xgraph.get_layers()
        assert xlayers[0].name == 'in1'
        assert xlayers[0].bottoms == []
        assert xlayers[0].tops == ['pool1']
        assert xlayers[1].name == 'pool1'
        assert xlayers[1].bottoms == ['in1']
        assert xlayers[1].tops == ['conv1']
        assert xlayers[2].name == 'conv1'
        assert xlayers[2].bottoms == ['pool1']
        assert xlayers[2].tops == ['add1']
        assert xlayers[3].name == 'in2'
        assert xlayers[3].bottoms == []
        assert xlayers[3].tops == ['conv2']
        assert xlayers[4].name == 'conv2'
        assert xlayers[4].bottoms == ['in2']
        assert xlayers[4].tops == ['add1']
        assert xlayers[5].name == 'add1'
        assert xlayers[5].bottoms == ['conv1', 'conv2']
        assert xlayers[5].tops == []
예제 #5
0
        def _test_add_get(in_name: str, conv_name: str):

            expected_in_name = px.stringify(in_name)
            expected_conv_name = px.stringify(conv_name)

            in1 = px.ops.input(op_name=expected_in_name, shape=[1, 2, 4, 4])
            W = px.ops.constant(
                "W", np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
            X_conv = px.ops.conv2d(op_name=conv_name,
                                   input_layer=in1,
                                   weights_layer=W,
                                   kernel_size=[2, 2])

            xgraph = XGraph()
            xgraph.add(in1)

            assert len(xgraph) == 1
            assert len(xgraph.get_layer_names()) == 1
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            assert isinstance(xgraph.get(in_name), XLayer)
            assert xgraph.get(in_name).bottoms == []
            assert xgraph.get(in_name).tops == []

            xgraph.add(X_conv)

            assert len(xgraph) == 2
            assert xgraph.get_layer_names() == [
                expected_in_name, expected_conv_name
            ]
            assert xgraph.get_output_names() == [expected_conv_name]
            assert xgraph.get_input_names() == [expected_in_name]

            assert xgraph.get(in_name).tops == [expected_conv_name]

            assert isinstance(xgraph.get(conv_name), XLayer)
            assert xgraph.get(conv_name).bottoms == [expected_in_name]
            assert xgraph.get(conv_name).tops == []
            assert xgraph.get(conv_name).type == ["Convolution"]

            np.testing.assert_array_equal(
                xgraph.get(conv_name).data.weights,
                np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
            )
            np.testing.assert_array_equal(
                xgraph.get(conv_name).data.biases,
                np.array([0.0], dtype=np.float32),
            )

            xgraph.get(conv_name).data = ConvData(
                weights=xgraph.get(conv_name).data.weights * 2,
                biases=xgraph.get(conv_name).data.biases,
            )
            np.testing.assert_array_equal(
                xgraph.get(conv_name).data.weights,
                np.array([[[[2, 4], [6, 8]]]], dtype=np.float32),
            )

            xgraph.remove(X_conv.name)

            assert len(xgraph) == 1
            assert in_name in xgraph
            assert len(xgraph.get_layer_names()) == 1
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1
예제 #6
0
        def _test_xgraph_insert(
            in_name: str,
            in2_name: str,
            conv_name: str,
            pool_name: str,
            add_name: str,
            conv2_name: str,
        ):

            expected_in_name = px.stringify(in_name)
            expected_in2_name = px.stringify(in2_name)
            expected_conv_name = px.stringify(conv_name)
            expected_pool_name = px.stringify(pool_name)
            expected_add_name = px.stringify(add_name)
            expected_conv2_name = px.stringify(conv2_name)

            in1 = px.ops.input(op_name=in_name, shape=[1, 2, 4, 4])
            W = px.ops.constant(
                "W", np.array([[[[1, 2], [3, 4]]]], dtype=np.float32))
            X_conv = px.ops.conv2d(op_name=conv_name,
                                   input_layer=in1,
                                   weights_layer=W,
                                   kernel_size=[2, 2])
            # X_pool = px.ops.pool2d(op_name=pool_name, input_layer=X_conv, )

            xgraph = XGraph()
            xgraph.add(in1)

            assert len(xgraph) == 1
            assert len(xgraph.get_layer_names()) == 1
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            xgraph.add(X_conv)

            assert len(xgraph) == 2
            assert len(xgraph.get_layer_names()) == 2
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            X_pool = XLayer(
                name=pool_name,
                type=["Pooling"],
                bottoms=[in_name],
                tops=[conv_name],
                targets=[],
            )
            xgraph.insert(X_pool)

            assert len(xgraph) == 3
            assert len(xgraph.get_layer_names()) == 3
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 1

            xlayers = xgraph.get_layers()
            assert xlayers[0].name == expected_in_name
            assert xlayers[0].bottoms == []
            assert xlayers[0].tops == [expected_pool_name]
            assert xlayers[1].name == expected_pool_name
            assert xlayers[1].bottoms == [expected_in_name]
            assert xlayers[1].tops == [expected_conv_name]
            assert xlayers[2].name == expected_conv_name
            assert xlayers[2].bottoms == [expected_pool_name]
            assert xlayers[2].tops == []

            X_in2 = px.ops.input(op_name=in2_name, shape=[1, 2, 4, 4])
            xgraph.add(X_in2)

            X_add = XLayer(
                name=add_name,
                type=["Eltwise"],
                bottoms=[conv_name, in2_name],
                tops=[],
                targets=[],
            )
            xgraph.add(X_add)

            X_conv2 = XLayer(
                name=conv2_name,
                type=["Convolution"],
                bottoms=[in2_name],
                tops=[add_name],
                data=ConvData(
                    weights=np.array([[[[1, 2], [3, 4]]]], dtype=np.float32),
                    biases=np.array([0.0, 1.0], dtype=np.float32),
                ),
                targets=[],
            )
            xgraph.insert(X_conv2)

            assert len(xgraph) == 6
            assert len(xgraph.get_layer_names()) == 6
            assert len(xgraph.get_output_names()) == 1
            assert len(xgraph.get_input_names()) == 2

            xlayers = xgraph.get_layers()

            assert xlayers[0].name == expected_in_name
            assert xlayers[0].bottoms == []
            assert xlayers[0].tops == [expected_pool_name]
            assert xlayers[1].name == expected_pool_name
            assert xlayers[1].bottoms == [expected_in_name]
            assert xlayers[1].tops == [expected_conv_name]
            assert xlayers[2].name == expected_conv_name
            assert xlayers[2].bottoms == [expected_pool_name]
            assert xlayers[2].tops == [expected_add_name]
            assert xlayers[3].name == expected_in2_name
            assert xlayers[3].bottoms == []
            assert xlayers[3].tops == [expected_conv2_name]
            assert xlayers[4].name == expected_conv2_name
            assert xlayers[4].bottoms == [expected_in2_name]
            assert xlayers[4].tops == [expected_add_name]
            assert xlayers[5].name == expected_add_name
            assert xlayers[5].bottoms == [
                expected_conv_name, expected_conv2_name
            ]
            assert xlayers[5].tops == []