Error in transformation of EMNIST data through Pytorch

Viewed 863

I was trying to train my model for prediction of EMNIST by using Pytorch. Edit:- Here's the link of colab notebook for the problem.

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(28, 64, (5, 5), padding=2)
        self.conv1_bn = nn.BatchNorm2d(64)
        self.conv2 = nn.Conv2d(64, 128, 2, padding=2)
        self.fc1 = nn.Linear(2048, 1024)
        self.dropout = nn.Dropout(0.3)
        self.fc2 = nn.Linear(1024, 512)
        self.bn = nn.BatchNorm1d(1)
        self.fc3 = nn.Linear(512, 128)
        self.fc4 = nn.Linear(128, 47)
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = self.conv1_bn(x)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 2048)
        x = F.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        x = x.view(-1, 1, 512)
        x = self.bn(x)
        x = x.view(-1, 512)
        x = self.fc3(x)
        x = self.fc4(x)
        return F.log_softmax(x, dim=1)
        return x

I am getting this type of error as shown below, whenever I am training my model.

<ipython-input-11-07c68cf1cac2> in forward(self, x)
     24     def forward(self, x):
     25         x = F.relu(self.conv1(x))
---> 26         x = F.max_pool2d(x, 2, 2)
     27         x = self.conv1_bn(x)
RuntimeError: Given input size: (64x28x1). Calculated output size: (64x14x0). Output size is too small

I tried to searched for the solutions and found that I should transform the data before. So i tried transforming it by the most common suggestion:-

transform_valid = transforms.Compose(
    [
     transforms.ToTensor(),  
    ])

But then again I am getting the error mentioned below. Maybe the problem lies here in the transformation part.

/opt/conda/lib/python3.7/site-packages/torchvision/datasets/mnist.py:469: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at  /opt/conda/conda-bld/pytorch_1595629403081/work/torch/csrc/utils/tensor_numpy.cpp:141.)
  return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)

I wanted to make that particular numpy array writable by using "ndarray.setflags(write=None, align=None, uic=None)" but I'm not able to figure out from where and what type of array should I make writable, as I'm directly loading the dataset using -> "datasets.EMNIST(root, split="balanced", train=False, download=True, transform=transform_valid)"

1 Answers

welcome to Stackoverflow !

Your problem is not related to the toTensor transform, this error is yielded because of the dimension of the tensor you input in your maxpool : the error clearly states that you are trying to maxppol a tensor of which one of the dimensions is 1 (64, 28, 1) and thus it will output a tensor with a dimension of 0 (64,14,0), which makes no sense.

You need to check the dimensions of the tensors you input in your model. They are definitely too small. Maybe you made a mistake with a view somewhere (hard to tell without a minimal reproducible example).

If I can try to guess, you have at the beginning a tensor size 28x28x1 (typical MNIST), and you put it into a convolution that expects a tensor of dims BxCxWxH (batch_size, channels, width, height), i.e something like (B, 1, 28, 28), but you confuse the width (28) from the input channels (nn.Conv2d(->28<-, 64, (5, 5), padding=2))

I believe you want your first layer to be nn.Conv2d(1, 64, (5, 5), padding=2), and you need to resize your tensors to give them the shape (B, 1, 28, 28) (the value of B is up to you) before giving them to the network.

Sidenote : the warning about writable numpy arrays is completely unrelated, it just means that pytorch will possibly override the "non-writable" data of your numpy array. If you don't care about this numpy array being modified, you can ignore the warning.

Related