示例#1
0
    def test_to_dict(self):
        X = XLayer(
            name='d',
            type=['Dict'],
            shapes=[1, 3, 224, 224],
            sizes=[3*224*224],
            tops=['t1', 't2'],
            bottoms=[],
            layer=['d'],
            data=[np.array([1, 2, 3], dtype=np.float32),
                  np.array([4, 5], dtype=np.float32)],
            targets=['cpu', 'dpu'],
            target='cpu',
            subgraph='xp0',
            subgraph_data=[
                XLayer(name='sg',
                       data=[np.array([1., 0.], dtype=np.float32)])],
            internal=False,
            attrs={
                'a': [1, 2],
                'b': {'b1': ['b1'], 'b2': ['b2', 'b2']}
            }
        )

        # To dict without data storage (params)
        d = X.to_dict()
        assert d['name'] == 'd'
        assert d['type'] == ['Dict']
        assert d['shapes'] == [1, 3, 224, 224]
        assert d['sizes'] == [3*224*224]
        assert d['tops'] == ['t1', 't2']
        assert d['bottoms'] == []
        assert d['layer'] == ['d']
        assert d['data'] == []
        assert d['targets'] == ['cpu', 'dpu']
        assert d['target'] == 'cpu'
        assert d['subgraph'] == 'xp0'
        assert len(d['subgraph_data']) == 1
        assert d['subgraph_data'][0]['name'] == 'sg'
        assert d['internal'] is False
        assert len(d['attrs']) == 2
        assert d['attrs']['a'] == [1, 2]
        assert d['attrs']['b'] == {'b1': ['b1'], 'b2': ['b2', 'b2']}

        X_c = XLayer.from_dict(d)
        assert X_c.name == 'd'
        assert X_c.type == ['Dict']
        assert X_c.shapes == [1, 3, 224, 224]
        assert X_c.sizes == [3*224*224]
        assert X_c.tops == ['t1', 't2']
        assert X_c.bottoms == []
        assert X_c.layer == ['d']
        assert X_c.data == []
        assert X_c.targets == ['cpu', 'dpu']
        assert X_c.target == 'cpu'
        assert X_c.subgraph == 'xp0'
        assert len(X_c.subgraph_data) == 1
        assert X_c.subgraph_data[0].name == 'sg'
        assert X_c.internal is False
        assert len(X_c.attrs) == 2
        assert X_c.attrs['a'] == [1, 2]
        assert X_c.attrs['b'] == {'b1': ['b1'], 'b2': ['b2', 'b2']}

        # To dict with data storage (params)
        d = X.to_dict(data=True)

        np.testing.assert_array_equal(
            d['data'][0],
            np.array([1, 2, 3], dtype=np.float32)
        )
        np.testing.assert_array_equal(
            d['data'][1],
            np.array([4, 5], dtype=np.float32)
        )

        assert d['subgraph_data'][0]['name'] == 'sg'
        assert len(d['subgraph_data'][0]['data']) == 1
        np.testing.assert_array_equal(
            d['subgraph_data'][0]['data'][0],
            np.array([1., 0.], dtype=np.float32)
        )

        X_c = XLayer.from_dict(d)
        np.testing.assert_array_equal(
            X_c.data[0],
            np.array([1, 2, 3], dtype=np.float32)
        )
        np.testing.assert_array_equal(
            X_c.data[1],
            np.array([4, 5], dtype=np.float32)
        )

        assert X_c.subgraph_data[0].name == 'sg'
        assert len(X_c.subgraph_data[0].data) == 1
        np.testing.assert_array_equal(
            X_c.subgraph_data[0].data[0],
            np.array([1., 0.], dtype=np.float32)
        )
示例#2
0
    def test_to_dict(self):
        X = XLayer(
            name="d",
            type=["Dict"],
            shapes=[1, 3, 224, 224],
            sizes=[3 * 224 * 224],
            tops=["t1", "t2"],
            bottoms=[],
            layer=["d"],
            data=[
                np.array([1, 2, 3], dtype=np.float32),
                np.array([4, 5], dtype=np.float32),
            ],
            targets=["cpu", "dpu"],
            target="cpu",
            subgraph="xp0",
            subgraph_data=[
                XLayer(name="sg",
                       data=[np.array([1.0, 0.0], dtype=np.float32)])
            ],
            internal=False,
            attrs={
                "a": [1, 2],
                "b": {
                    "b1": ["b1"],
                    "b2": ["b2", "b2"]
                }
            },
        )

        # To dict without data storage (params)
        d = X.to_dict()
        assert d["name"] == "d"
        assert d["type"] == ["Dict"]
        assert d["shapes"] == [1, 3, 224, 224]
        assert d["sizes"] == [3 * 224 * 224]
        assert d["tops"] == ["t1", "t2"]
        assert d["bottoms"] == []
        assert d["layer"] == ["d"]
        assert d["data"] == []
        assert d["targets"] == ["cpu", "dpu"]
        assert d["target"] == "cpu"
        assert d["subgraph"] == "xp0"
        assert len(d["subgraph_data"]) == 1
        assert d["subgraph_data"][0]["name"] == "sg"
        assert d["internal"] is False
        assert len(d["attrs"]) == 2
        assert d["attrs"]["a"] == [1, 2]
        assert d["attrs"]["b"] == {"b1": ["b1"], "b2": ["b2", "b2"]}

        X_c = XLayer.from_dict(d)
        assert X_c.name == "d"
        assert X_c.type == ["Dict"]
        assert X_c.shapes == [1, 3, 224, 224]
        assert X_c.sizes == [3 * 224 * 224]
        assert X_c.tops == ["t1", "t2"]
        assert X_c.bottoms == []
        assert X_c.layer == ["d"]
        assert X_c.data == []
        assert X_c.targets == ["cpu", "dpu"]
        assert X_c.target == "cpu"
        assert X_c.subgraph == "xp0"
        assert len(X_c.subgraph_data) == 1
        assert X_c.subgraph_data[0].name == "sg"
        assert X_c.internal is False
        assert len(X_c.attrs) == 2
        assert X_c.attrs["a"] == [1, 2]
        assert X_c.attrs["b"] == {"b1": ["b1"], "b2": ["b2", "b2"]}

        # To dict with data storage (params)
        d = X.to_dict(data=True)

        np.testing.assert_array_equal(d["data"][0],
                                      np.array([1, 2, 3], dtype=np.float32))
        np.testing.assert_array_equal(d["data"][1],
                                      np.array([4, 5], dtype=np.float32))

        assert d["subgraph_data"][0]["name"] == "sg"
        assert len(d["subgraph_data"][0]["data"]) == 1
        np.testing.assert_array_equal(d["subgraph_data"][0]["data"][0],
                                      np.array([1.0, 0.0], dtype=np.float32))

        X_c = XLayer.from_dict(d)
        np.testing.assert_array_equal(X_c.data[0],
                                      np.array([1, 2, 3], dtype=np.float32))
        np.testing.assert_array_equal(X_c.data[1],
                                      np.array([4, 5], dtype=np.float32))

        assert X_c.subgraph_data[0].name == "sg"
        assert len(X_c.subgraph_data[0].data) == 1
        np.testing.assert_array_equal(X_c.subgraph_data[0].data[0],
                                      np.array([1.0, 0.0], dtype=np.float32))