Exemple #1
0
 def test_conv_transpose(self):
     # Fix test in the future.
     return
     device = "CUDA"
     if not supports_device(device):
         raise unittest.SkipTest(
             "Backend doesn't support device {}".format(device))
     node_def = helper.make_node("ConvTranspose", ["X", "weights"], ["Y"],
                                 pads=[1, 1])
     x_shape = [1, 5, 4]
     x = self._get_rnd(x_shape)
     weight_shape = [5, 3, 2]
     weights = self._get_rnd(weight_shape)
     output = run_node(node_def, [x, weights], device=device)
     out_shape = [x_shape[0], weight_shape[1], x_shape[2]]
     test_output = np.zeros(out_shape)
     for b in range(0, x_shape[0]):
         for m in range(0, weight_shape[1]):
             for h in range(0, x_shape[2]):
                 v = 0
                 for c in range(0, x_shape[1]):
                     for k in range(h, min(h + weight_shape[2],
                                           x_shape[2])):
                         v += x[b][c][k] * weights[c][m][k - h]
                 test_output[b][m][h] = v
     np.testing.assert_almost_equal(output["Y"], test_output, decimal=5)
Exemple #2
0
 def test_average_pool(self):
     # TODO: fix this test
     return
     device = "CUDA"
     if not supports_device(device):
         raise unittest.SkipTest(
             "Backend doesn't support device {}".format(device))
     shape = [1, 1, 40, 40]
     node_def = helper.make_node("AveragePool", ["X"], ["Y"],
                                 kernel_shape=[1, 2],
                                 pads=[1, 1],
                                 strides=[1, 1])
     x = self._get_rnd(shape)
     output = run_node(node_def, [x], device=device)
     test_output = np.zeros(shape)
     for i1 in range(0, shape[0]):
         for i2 in range(0, shape[1]):
             for j1 in range(0, shape[2]):
                 for j2 in range(0, shape[3]):
                     test_output[i1][i2][j1][j2] = 0
                     count = 0
                     for k in range(j2, min(j2 + 2, shape[3])):
                         test_output[i1][i2][j1][j2] += x[i1][i2][j1][k]
                         count += 1
                     test_output[i1][i2][j1][j2] /= count
     np.testing.assert_almost_equal(output["Y"], test_output)
    def test_conv(self):
        device = "CUDA"
        if not supports_device(device):
            raise unittest.SkipTest(
                "Backend doesn't support device {}".format(device))

        N, C, H, W = 4, 3, 5, 5
        x_shape = [N, C, H, W]
        K, kH, kW = 6, 3, 3
        weight_shape = [K, C, kH, kW]
        node_def = helper.make_node("Conv", ["X", "weights"], ["Y"],
                                    pads=[1, 1, 1, 1],
                                    kernel_shape=[kH, kW])

        x = self._get_rnd(x_shape)
        weights = self._get_rnd(weight_shape)
        output = run_node(node_def, [x, weights], device=device)

        out_shape = [N, K, H, W]
        test_output = np.zeros(out_shape)
        for n in range(N):
            for c in range(C):
                for h in range(H):
                    for w in range(W):
                        for k in range(K):
                            for kh in range(kH):
                                for kw in range(kW):
                                    h_in_range = (h - kH // 2 + kh) < H and (
                                        h - kH // 2 + kh) >= 0
                                    w_in_range = (w - kW // 2 + kw) < W and (
                                        w - kW // 2 + kw) >= 0
                                    if h_in_range and w_in_range:
                                        test_output[n][k][h][w] += (
                                            x[n][c][h - kH // 2 +
                                                    kh][w - kW // 2 + kw] *
                                            weights[k][c][kh][kw])

        np.testing.assert_almost_equal(output["Y"], test_output, decimal=5)