2

I attempting to create a logistic model on CIFAR10 dataset by PyTorch. However I am getting an error:

ValueError: Expected input batch_size (900) to match target batch_size (300).

What I think is happening is that 3*100 is 300. So may be the 3 axis of the RGB image is doing that but I cant figure how to solve.

These are my hyperparameters.

batch_size = 100
learning_rate = 0.001

# Other constants
input_size = 32*32
num_classes = 10

Here I divide my data into train, validation and test data.


transform_train = transforms.Compose([transforms.Resize((32,32)),
                                      transforms.RandomHorizontalFlip(),
                                      transforms.RandomRotation(10),
                                      transforms.RandomAffine(0, shear=10, scale=(0.8,1.2)),
                                      transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2),
                                      transforms.ToTensor(),
                                      transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
                               ])


transform = transforms.Compose([transforms.Resize((32,32)),
                               transforms.ToTensor(),
                               transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
                               ])
training_dataset = CIFAR10(root='D:\PyTorch\cifar-10-python', train=True, download=True, transform=transform_train)
train_ds, val_ds = random_split(training_dataset, [40000, 10000])
test_ds = CIFAR10(root='D:\PyTorch\cifar-10-python', train=False, download=True, transform=transform)

train_loader = DataLoader(train_ds, batch_size=100, shuffle=True)
val_loader = DataLoader(val_ds, batch_size = 100, shuffle = False)
test_loader = DataLoader(test_ds, batch_size = 100, shuffle=False)

This is the model.

class CifarModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear = nn.Linear(input_size,  num_classes)
    def forward(self, xb):
        xb = xb.view(-1, 32*32)
        #xb = xb.reshape(-1, 784)
        print(xb.shape)
        out = self.linear(xb)
        return out

    def training_step(self, batch):
        images, labels = batch 
        out = self(images)                  # Generate predictions
        loss = F.cross_entropy(out, labels) # Calculate loss
        return loss

    def validation_step(self, batch):
        images, labels = batch 
        out = self(images)                    # Generate predictions
        loss = F.cross_entropy(out, labels)   # Calculate loss
        acc = accuracy(out, labels)           # Calculate accuracy
        return {'val_loss': loss.detach(), 'val_acc': acc.detach()}

    def validation_epoch_end(self, outputs):
        batch_losses = [x['val_loss'] for x in outputs]
        epoch_loss = torch.stack(batch_losses).mean()   # Combine losses
        batch_accs = [x['val_acc'] for x in outputs]
        epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies
        return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}

    def epoch_end(self, epoch, result):
        print("Epoch [{}], val_loss: {:.4f}, val_acc: {:.4f}".format(epoch, result['val_loss'], result['val_acc']))

model = CifarModel()
def accuracy(outputs, labels):
    _, preds = torch.max(outputs, dim=1)
    return torch.tensor(torch.sum(preds == labels).item() / len(preds))
def evaluate(model, val_loader):
    outputs = [model.validation_step(batch) for batch in val_loader]
    return model.validation_epoch_end(outputs)

def fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):
    history = []
    optimizer = opt_func(model.parameters(), lr)
    for epoch in range(epochs):
        # Training Phase 
        for batch in train_loader:
            loss = model.training_step(batch)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()
        # Validation phase
        result = evaluate(model, val_loader)
        model.epoch_end(epoch, result)
        history.append(result)
    return history
evaluate(model, val_loader)

Here's the error I encounter when I run evaluate function:

torch.Size([900, 1024])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-3621eab8de1a> in <module>
     21         history.append(result)
     22     return history
---> 23 evaluate(model, val_loader)

<ipython-input-23-3621eab8de1a> in evaluate(model, val_loader)
      3     return torch.tensor(torch.sum(preds == labels).item() / len(preds))
      4 def evaluate(model, val_loader):
----> 5     outputs = [model.validation_step(batch) for batch in val_loader]
      6     return model.validation_epoch_end(outputs)
      7 

<ipython-input-23-3621eab8de1a> in <listcomp>(.0)
      3     return torch.tensor(torch.sum(preds == labels).item() / len(preds))
      4 def evaluate(model, val_loader):
----> 5     outputs = [model.validation_step(batch) for batch in val_loader]
      6     return model.validation_epoch_end(outputs)
      7 

<ipython-input-22-c9e17d21eaff> in validation_step(self, batch)
     19         images, labels = batch
     20         out = self(images)                    # Generate predictions
---> 21         loss = F.cross_entropy(out, labels)   # Calculate loss
     22         acc = accuracy(out, labels)           # Calculate accuracy
     23         return {'val_loss': loss.detach(), 'val_acc': acc.detach()}

~\Anaconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2019     if size_average is not None or reduce is not None:
   2020         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2021     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   2022 
   2023 

~\Anaconda3\lib\site-packages\torch\nn\functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   1834     if input.size(0) != target.size(0):
   1835         raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
-> 1836                          .format(input.size(0), target.size(0)))
   1837     if dim == 2:
   1838         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

ValueError: Expected input batch_size (900) to match target batch_size (300).
3
  • One problem that I am seeing is this line: xb = xb.view(-1, 32*32) Here you are saying that the input image has only one channels. In other words, grayscale. Change it to reflect the number of channels (RGB).
    – bhristov
    Commented Jun 2, 2020 at 19:35
  • Heey!! that worked!!!!!!!!!!!!! Thank you. I really appreciate it. It was a stupid mistake on my part.
    – harry r
    Commented Jun 2, 2020 at 20:16
  • No problem. I am glad that I could help you. I posted it as an answer to the question below. Could you please mark it as the answer?
    – bhristov
    Commented Jun 2, 2020 at 20:23

1 Answer 1

1

One problem that I am seeing is this line:

xb = xb.view(-1, 32*32) 

Here you are saying that the input image has only one channels. In other words, grayscale. Change it to reflect the number of channels (RGB):

xb = xb.view(-1, 32*32*3) 

Not the answer you're looking for? Browse other questions tagged or ask your own question.