Hello World Convolution on Images in PyTorch
I am trying to verify some results with PyTorch's 2D convolution with the following: 


Input matrix: X (10, 10, 3) [Dummy numpy image]
Weight matrix: W (3, 3, 3) [My Conv Filter to test]
Output matrix: Y (10, 10, 1)


I have the following code but I am not able to assign the weights properly and run the model without errors.
What am I doing wrong here??

import torch
import torch.nn as nn
import torchvision.transforms
import numpy as np

# Convert image to tensor
image2tensor = torchvision.transforms.ToTensor()

class ConvNet(nn.Module):
    def __init__(self, num_classes=10):
        super(ConvNet, self).__init__()
        # Test layer
        self.layer1 = nn.Conv2d(3, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

    def forward(self, x):
        out = self.layer1(x)
        return out

# Test image
image = np.ones((10, 10, 3))
tensor = image2tensor(image).unsqueeze(0)

# Create new model
conv = ConvNet()

# Assign test weight - NOT WORKING!!
weight = torch.nn.Parameter(torch.ones(3, 3, 3))
conv.layer1.weight.data = weight

# Run the model
output = conv(tensor)


Next time please post your corresponding error message.
Turns out the matrix dimension also has to match the batch size (i.e. needs an additional fourth dimension): Thus, you are initializing the weight matrix with the wrong parameters.

Correct would be this instead:

weight = torch.nn.Parameter(torch.ones(1, 3, 3, 3))


Additionally, for my version of PyTorch (0.4.1), I had to manually cast the tensor again to a float because it otherwise threw a different error. Avoid by doing it like so:

tensor = image2tensor(image).unsqueeze(0).float() # note the additional .float()


Then it runs successfully for me.
