Exemplo n.º 1
0
def test_slice_cudaNo0():
    gaussian3d = Gaussian([2, 4, 4])
    x = gaussian3d(3).cuda(2)
    netStructure = [[3, 2, 1, 1], [4, 2, 1, 1], [3, 2, 1, 0], [1, 2, 1, 0]]
    sList3d = [
        CNN(netStructure),
        CNN(netStructure),
        CNN(netStructure),
        CNN(netStructure)
    ]
    tList3d = [
        CNN(netStructure),
        CNN(netStructure),
        CNN(netStructure),
        CNN(netStructure)
    ]

    realNVP = RealNVP([2, 4, 4], sList3d, tList3d, gaussian3d)
    realNVP = realNVP.cuda(2)
    z = realNVP._generateWithSlice(x, 0, True)
    print(realNVP._logProbabilityWithSlice(z, 0))
    zz = realNVP._inferenceWithSlice(z, 0, True)
    assert_array_almost_equal(x.cpu().data.numpy(), zz.cpu().data.numpy())
    assert_array_almost_equal(realNVP._generateLogjac.data.cpu().numpy(),
                              -realNVP._inferenceLogjac.data.cpu().numpy())
Exemplo n.º 2
0
def test_parallel():
    gaussian3d = Gaussian([2, 4, 4])
    x = gaussian3d(3)
    netStructure = [[3, 2, 1, 1], [4, 2, 1, 1], [3, 2, 1, 0], [1, 2, 1, 0]]
    sList3d = [
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2)
    ]
    tList3d = [
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2)
    ]

    realNVP = RealNVP([2, 4, 4], sList3d, tList3d, gaussian3d)
    z = realNVP(x)
    print(z)
    net = torch.nn.DataParallel(realNVP.cuda(0), device_ids=[0, 1])
    output = net(x.cuda())
    print(output)

    assert_array_almost_equal(z.data.numpy(),
                              output.cpu().data.numpy(),
                              decimal=5)
Exemplo n.º 3
0
def test_tempalte_contractionCNN_checkerboard_cuda():
    gaussian3d = Gaussian([2, 4, 4])
    x3d = gaussian3d(3).cuda()
    netStructure = [[3, 2, 1, 1], [4, 2, 1, 1], [3, 2, 1, 0], [2, 2, 1, 0]]
    sList3d = [
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2)
    ]
    tList3d = [
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2)
    ]

    realNVP3d = RealNVP([2, 4, 4], sList3d, tList3d, gaussian3d)
    realNVP3d = realNVP3d.cuda()
    mask3d = realNVP3d.createMask(["checkerboard"] * 4, ifByte=0, cuda=0)
    z3d = realNVP3d._generate(x3d, realNVP3d.mask, realNVP3d.mask_, True)
    zp3d = realNVP3d._inference(z3d, realNVP3d.mask, realNVP3d.mask_, True)
    print(realNVP3d._logProbability(z3d, realNVP3d.mask, realNVP3d.mask_))
    assert_array_almost_equal(x3d.cpu().data.numpy(), zp3d.cpu().data.numpy())
    assert_array_almost_equal(realNVP3d._generateLogjac.data.cpu().numpy(),
                              -realNVP3d._inferenceLogjac.data.cpu().numpy())
Exemplo n.º 4
0
def test_contraction_cuda_withDifferentMasks():
    gaussian3d = Gaussian([2, 4, 4])
    x = gaussian3d(3).cuda()
    #z3dp = z3d[:,0,:,:].view(10,-1,4,4)
    #print(z3dp)

    #print(x)
    netStructure = [[3, 2, 1, 1], [4, 2, 1, 1], [3, 2, 1, 0],
                    [1, 2, 1, 0]]  # [channel, filter_size, stride, padding]

    sList3d = [
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2)
    ]
    tList3d = [
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2),
        CNN(netStructure, inchannel=2)
    ]

    realNVP = RealNVP([2, 4, 4], sList3d, tList3d, gaussian3d)
    realNVP = realNVP.cuda()
    mask = realNVP.createMask(
        ["channel", "checkerboard", "channel", "checkerboard"], 1, cuda=0)

    z = realNVP._generateWithContraction(x, realNVP.mask, realNVP.mask_, 2,
                                         True)
    print(
        realNVP._logProbabilityWithContraction(z, realNVP.mask, realNVP.mask_,
                                               2))
    zz = realNVP._inferenceWithContraction(z, realNVP.mask, realNVP.mask_, 2,
                                           True)

    assert_array_almost_equal(x.cpu().data.numpy(), zz.cpu().data.numpy())
    assert_array_almost_equal(realNVP._generateLogjac.data.cpu().numpy(),
                              -realNVP._inferenceLogjac.data.cpu().numpy())