def test_pytorch_model_preprocessing_shape_change():
    import torch
    import torch.nn as nn

    num_classes = 1000
    bounds = (0, 255)
    channels = num_classes

    class Net(nn.Module):
        def __init__(self):
            super(Net, self).__init__()

        def forward(self, x):
            x = torch.mean(x, 3)
            x = torch.mean(x, 2)
            logits = x
            return logits

    model = Net()

    model1 = PyTorchModel(model, bounds=bounds, num_classes=num_classes)

    def preprocessing2(x):
        if x.ndim == 3:
            x = np.transpose(x, axes=(2, 0, 1))
        elif x.ndim == 4:
            x = np.transpose(x, axes=(0, 3, 1, 2))

        def grad(dmdp):
            assert dmdp.ndim == 3
            dmdx = np.transpose(dmdp, axes=(1, 2, 0))
            return dmdx

        return x, grad

    model2 = PyTorchModel(model,
                          bounds=bounds,
                          num_classes=num_classes,
                          preprocessing=preprocessing2)

    np.random.seed(22)
    test_images_nhwc = np.random.rand(2, 5, 5, channels).astype(np.float32)
    test_images_nchw = np.transpose(test_images_nhwc, (0, 3, 1, 2))

    p1 = model1.batch_predictions(test_images_nchw)
    p2 = model2.batch_predictions(test_images_nhwc)

    assert np.all(p1 == p2)

    p1 = model1.predictions(test_images_nchw[0])
    p2 = model2.predictions(test_images_nhwc[0])

    assert np.all(p1 == p2)

    g1 = model1.gradient(test_images_nchw[0], 3)
    assert g1.ndim == 3
    g1 = np.transpose(g1, (1, 2, 0))
    g2 = model2.gradient(test_images_nhwc[0], 3)

    np.testing.assert_array_almost_equal(g1, g2)
示例#2
0
def test_pytorch_model_preprocessing():
    import torch
    import torch.nn as nn

    num_classes = 1000
    bounds = (0, 255)
    channels = num_classes

    class Net(nn.Module):

        def __init__(self):
            super(Net, self).__init__()

        def forward(self, x):
            x = torch.mean(x, 3)
            x = torch.mean(x, 2)
            logits = x
            return logits

    model = Net()
    preprocessing = (np.arange(num_classes)[:, None, None],
                     np.random.uniform(size=(channels, 5, 5)) + 1)

    model1 = PyTorchModel(
        model,
        bounds=bounds,
        num_classes=num_classes)

    model2 = PyTorchModel(
        model,
        bounds=bounds,
        num_classes=num_classes,
        preprocessing=preprocessing)

    model3 = PyTorchModel(
        model,
        bounds=bounds,
        num_classes=num_classes)

    np.random.seed(22)
    test_images = np.random.rand(2, channels, 5, 5).astype(np.float32)
    test_images_copy = test_images.copy()

    p1 = model1.batch_predictions(test_images)
    p2 = model2.batch_predictions(test_images)

    # make sure the images have not been changed by
    # the in-place preprocessing
    assert np.all(test_images == test_images_copy)

    p3 = model3.batch_predictions(test_images)

    assert p1.shape == p2.shape == p3.shape == (2, num_classes)

    np.testing.assert_array_almost_equal(
        p1 - p1.max(),
        p3 - p3.max(),
        decimal=5)
示例#3
0
def test_pytorch_model_preprocessing():
    num_classes = 1000
    bounds = (0, 255)
    channels = num_classes

    class Net(nn.Module):
        def __init__(self):
            super(Net, self).__init__()

        def forward(self, x):
            x = torch.mean(x, 3)
            x = torch.squeeze(x, dim=3)
            x = torch.mean(x, 2)
            x = torch.squeeze(x, dim=2)
            logits = x
            return logits

    model = Net()

    def preprocess_fn(x):
        # modify x in-place
        x /= 2
        return x

    model1 = PyTorchModel(model,
                          bounds=bounds,
                          num_classes=num_classes,
                          cuda=False)

    model2 = PyTorchModel(model,
                          bounds=bounds,
                          num_classes=num_classes,
                          cuda=False,
                          preprocess_fn=preprocess_fn)

    model3 = PyTorchModel(model,
                          bounds=bounds,
                          num_classes=num_classes,
                          cuda=False)

    np.random.seed(22)
    test_images = np.random.rand(2, channels, 5, 5).astype(np.float32)
    test_images_copy = test_images.copy()

    p1 = model1.batch_predictions(test_images)
    p2 = model2.batch_predictions(test_images)

    # make sure the images have not been changed by
    # the in-place preprocessing
    assert np.all(test_images == test_images_copy)

    p3 = model3.batch_predictions(test_images)

    assert p1.shape == p2.shape == p3.shape == (2, num_classes)

    np.testing.assert_array_almost_equal(p1 - p1.max(),
                                         p3 - p3.max(),
                                         decimal=5)
示例#4
0
def test_pytorch_model_preprocessing():
    import torch
    import torch.nn as nn

    num_classes = 1000
    bounds = (0, 255)
    channels = num_classes

    class Net(nn.Module):

        def __init__(self):
            super(Net, self).__init__()

        def forward(self, x):
            x = torch.mean(x, 3)
            x = torch.mean(x, 2)
            logits = x
            return logits

    model = Net()
    preprocessing = (np.arange(num_classes)[:, None, None],
                     np.random.uniform(size=(channels, 5, 5)) + 1)

    model1 = PyTorchModel(
        model,
        bounds=bounds,
        num_classes=num_classes,
        cuda=False)

    model2 = PyTorchModel(
        model,
        bounds=bounds,
        num_classes=num_classes,
        cuda=False,
        preprocessing=preprocessing)

    model3 = PyTorchModel(
        model,
        bounds=bounds,
        num_classes=num_classes,
        cuda=False)

    np.random.seed(22)
    test_images = np.random.rand(2, channels, 5, 5).astype(np.float32)
    test_images_copy = test_images.copy()

    p1 = model1.batch_predictions(test_images)
    p2 = model2.batch_predictions(test_images)

    # make sure the images have not been changed by
    # the in-place preprocessing
    assert np.all(test_images == test_images_copy)

    p3 = model3.batch_predictions(test_images)

    assert p1.shape == p2.shape == p3.shape == (2, num_classes)

    np.testing.assert_array_almost_equal(
        p1 - p1.max(),
        p3 - p3.max(),
        decimal=5)