Exemplo n.º 1
0
def test_bilinear_interpolation_cpu():

    # Basic MappedConvolution layer
    layer = MappedConvolution(in_channels=in_channels,
                              out_channels=out_channels,
                              kernel_size=kernel_size).double()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test(
        layer,
        weight=params.weights_unit(in_channels, out_channels),
        input=params.input_4x5().repeat(bs, in_channels, 1, 1),
        sample_map=params.sample_map1(),
        cuda=False)

    # Manually computed correct result
    correct_output = 2 + params.in_channels * torch.tensor(
        [[35.75, 16.00, 28.00, 39.25, 45.00], [
            32.25, 32.00, 23.00, 36.75, 29.00
        ], [34.50, 47.00, 31.00, 34.25, 33.75],
         [39.00, 27.00, 35.25, 40.75, 39.50]]).double()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 2
0
def test_out_of_bounds_sampling_cuda():

    # Basic MappedConvolution layer
    layer = MappedConvolution(in_channels=in_channels,
                              out_channels=out_channels,
                              kernel_size=kernel_size).double().cuda()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test(
        layer,
        weight=params.weights_unit(in_channels, out_channels),
        input=params.input_4x5().repeat(bs, in_channels, 1, 1),
        sample_map=params.sample_map4(),
        cuda=True)

    # Manually computed correct result
    correct_output = 2 + params.in_channels * torch.tensor(
        [[29, 23, 26, 10, 13], [18, 45, 23, 20, 34], [18, 0, 22, 13, 17],
         [15, 14, 17, 15, 25]]).double().cuda()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 3
0
def test_integer_sampling_cuda():

    # Basic MappedConvolution layer
    layer = MappedConvolution(in_channels=in_channels,
                              out_channels=out_channels,
                              kernel_size=kernel_size).double().cuda()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test(
        layer,
        weight=params.weights_unit(in_channels, out_channels),
        input=params.input_4x5().repeat(bs, in_channels, 1, 1),
        sample_map=params.sample_map0(),
        cuda=True)

    # Manually computed correct result
    correct_output = 2 + in_channels * torch.tensor(
        [[30, 25, 31, 39, 33], [49, 40, 40, 54, 43], [46, 35, 47, 26, 33],
         [50, 36, 27, 40, 45]]).double().cuda()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 4
0
def test_integer_sampling_cpu():

    # Basic MappedConvolution layer
    layer = MappedConvolution(in_channels=in_channels,
                              out_channels=out_channels,
                              kernel_size=kernel_size).double()
    w = params.weights_unit(in_channels, out_channels)
    in_4x5 = params.input_4x5()
    m = params.sample_map0()
    '''
        m (the sample map) samples from in_4x5 (the input) as:
            in_4x5[0, 0, m[0, 0, 0, 1].long(), m[0, 0, 0, 0].long()]
            i.e. input[batch, channel, y, x]
            where y, x = sample_map's last dim coords
            and the sample_maps 3rd dim corresponds 
            to the neighboring samples for each of the kernel's center points

            >>> in_4x5[0, 0, m[0,0,0,1].long(), m[0,0,0,0].long()]
                        tensor(11., dtype=torch.float64)
            >>> in_4x5[0, 0, m[0,0,1,1].long(), m[0,0,1,0].long()]
                        tensor(8., dtype=torch.float64)
            >>> in_4x5[0, 0, m[0,0,2,1].long(), m[0,0,2,0].long()]
                        tensor(2., dtype=torch.float64)
            >>> in_4x5[0, 0, m[0,1,2,1].long(), m[0,1,2,0].long()]
                        tensor(0., dtype=torch.float64)
    '''
    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test(
        layer,
        weight=w,
        input=in_4x5.repeat(bs, in_channels, 1, 1),
        sample_map=m,
        cuda=False)

    # Manually computed correct result
    correct_output = 2 + in_channels * torch.tensor(
        [[30, 25, 31, 39, 33], [49, 40, 40, 54, 43], [46, 35, 47, 26, 33],
         [50, 36, 27, 40, 45]]).double()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 5
0
def test_max_pool_bilinear_downsampling_cpu():

    # Basic MappedTransposedConvolution layer
    layer = MappedMaxPool(kernel_size=kernel_size).double()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_pool_test(
        layer,
        input=params.input_4x5().repeat(bs, 1, 1, 1),
        sample_map=params.sample_map3(),
        cuda=False)

    # Manually computed correct result
    correct_output = torch.tensor([[18.5, 16.5], [18.5, 18.5]]).double()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 6
0
def test_max_pool_integer_sampling_cpu():

    # Basic MaxPool layer
    layer = MappedMaxPool(kernel_size=kernel_size).double()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_pool_test(
        layer,
        input=params.input_4x5().repeat(bs, 1, 1, 1),
        sample_map=params.sample_map0(),
        cuda=False)

    # Manually computed correct result
    correct_output = torch.tensor([[11, 19, 14, 18, 13], [18, 17, 19, 18, 19],
                                   [19, 12, 19, 10, 17], [18, 19, 10, 17,
                                                          17]]).double()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 7
0
def test_avg_pool_integer_sampling_cuda():

    # Basic MappedTransposedConvolution layer
    layer = MappedAvgPool(kernel_size=kernel_size).double().cuda()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_pool_test(
        layer,
        input=params.input_4x5().repeat(bs, 1, 1, 1),
        sample_map=params.sample_map0(),
        cuda=True)

    # Manually computed correct result
    correct_output = torch.tensor(
        [[7.5, 6.25, 7.75, 9.75, 8.25], [12.25, 10., 10., 13.5, 10.75],
         [11.5, 8.75, 11.75, 6.5, 8.25], [12.5, 9., 6.75, 10., 11.25]
         ], ).double().cuda()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 8
0
def test_max_pool_bilinear_interpolation_sampling_cuda():

    # Basic MappedTransposedConvolution layer
    layer = MappedMaxPool(kernel_size=kernel_size).double().cuda()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_pool_test(
        layer,
        input=params.input_4x5().repeat(bs, 1, 1, 1),
        sample_map=params.sample_map1(),
        cuda=True)

    # Manually computed correct result
    correct_output = torch.tensor([[14, 5, 8.25, 13, 16],
                                   [9.25, 9, 9.25, 13,
                                    13], [13, 15, 15, 13, 11],
                                   [13, 13, 13, 13, 15]]).double().cuda()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)
Exemplo n.º 9
0
def test_downsampling_with_integer_sampling_cpu():

    # Basic MappedConvolution layer
    layer = MappedConvolution(in_channels=in_channels,
                              out_channels=out_channels,
                              kernel_size=kernel_size).double()

    # Run a forward and backward pass
    output, forward_time, backward_time, gradcheck_res = utils.mapped_conv_test(
        layer,
        weight=params.weights_unit(in_channels, out_channels),
        input=params.input_4x5().repeat(bs, in_channels, 1, 1),
        sample_map=params.sample_map2(),
        cuda=False)

    # Manually computed correct result
    correct_output = 2 + params.in_channels * torch.tensor([[48, 50], [28, 57]
                                                            ]).double()

    # Assert gradient check has passed
    assert gradcheck_res

    # Assert outputs match
    testing.assert_allclose(output, correct_output)