I am trying to verify some results with PyTorch’s 2D convolution with the following:
- Input matrix: X (10, 10, 3) (Dummy numpy matrix)
- 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)