Search code examples
pythonneural-networkpytorchconv-neural-networkvalueerror

ValueError: expected sequence of length 0 at dim 2 (got 1)


I've recently started a tutorial about Neural Networks with Python. I am working on a cat/dog classification task with a CNN. However even though I thought I've done exactly what the tutorial told me to do, I somehow ended up with a dim error.

This is the tutorial. I believe he uses Python 3.7, I'm using Python 3.9(64-bit).

The Error: ValueError: expected sequence of length 0 at dim 2 (got 1)
The line of code: y = torch.Tensor([i[1] for i in training_data])

It sounds like I might have made a mistake in preparing the training data, but I'm not sure. Here is the code for that:

class DogsVSCats:
    IMG_SIZE = 50
    CATS = '[Path]'
    DOGS = '[Path]'

    LABELS = {CATS: 0, DOGS: 1}

    training_data = []

    catcount = 0
    dogcount = 0

    def make_training_data(self):
        for label in self.LABELS:
            print label

            for f in tqdm(os.listdir(label)):
                try:
                    path = os.path.join(label, f)
                    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
                    img = cv2.resize(img, (self.IMG_SIZE,
                            self.IMG_SIZE))

                    self.training_data.append([np.array(img), np.eye(2,
                            self.LABELS[label])])

                    if label == self.CATS:
                        self.catcount += 1
                    elif label == self.DOGS:

                        self.dogcount += 1
                except Exception, e:

                    pass

            np.random.shuffle(self.training_data)
            np.save('training_data.npy', self.training_data)

            print ('Cats: ', self.catcount)
            print ('Dogs: ', self.dogcount)

    if REBUILD_DATA:
        dogsvcats = DogsVSCats()
        dogsvcats.make_training_data()


print 'Nothing found!!'

That all seemed to work as it did in the tutorial, with no errors and showing the same amount of pictures for each category. Here is also the problematic line:

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 32, 5)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.conv3 = nn.Conv2d(64, 128, 5)

        x = torch.randn(50, 50).view(-1, 1, 50, 50)
        self._to_linear = None
        self.convs(x)

        self.fc1 = nn.Linear(self._to_linear, 512)
        self.fc2 = nn.Linear(512, 2)

    def convs(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv3(x)), (2, 2))

        if self._to_linear is None:
            self._to_linear = x[0].shape[0] * x[0].shape[1] * x[0].shape[2]
        return x

    def forward(self, x):
        x = self.convs(x)
        x = x.view(-1, self._to_linear)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)

        return F.softmax(x, dim = 1)

net = Net()

optimizer = optim.Adam(net.parameters(), lr = 1e-3)
loss_function = nn.MSELoss()

X = torch.Tensor([i[0] for i in training_data]).view(-1, 50, 50)
X = X / 255.0
y = torch.Tensor([i[1] for i in training_data]) !!!Error Line!!!

VAL_PCT = 0.1
val_size = int(len(X) * VAL_PCT)

train_X = X[:-val_size]
train_y = y[:-val_size]

test_X = X[-val_size:]
test_y = y[-val_size:]

print(val_size)

Solution

  • You didn't define the labels properly, it shouldn't be

    np.eye(2, self.LABELS[label])
    

    but instead:

    np.eye(2)[self.LABELS[label]]