-
Notifications
You must be signed in to change notification settings - Fork 387
Chapter 4 (Classifying images using deep CNNs) #81
Copy link
Copy link
Open
Description
In the Training cell I am getting this error
RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (3 x 3). Kernel size can't be greater than actual input size
Training
model, loss_fn, optimizer = get_model()
accuracies, losses =[], []
epochs = 5
for epoch in range(epochs):
epoch_losses, epoch_accuracies = [], []
for ix, batch in enumerate(iter(trn_dl)):
x, y = batch
loss = train_batch(x, y, model, loss_fn, optimizer)
epoch_losses.append(loss)
epoch_loss = np.array(epoch_losses).mean()
# accuracy check
for ix, batch in enumerate(iter(trn_dl)):
x, y = batch
acc = accuracy_fn(x, y, model)
epoch_accuracies.append(acc)
epoch_acc = np.mean(epoch_accuracies)
accuracies.append(epoch_acc)
losses.append(epoch_loss)
CNN model architecture
# defining the CNN architecture
def get_model():
model = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=3),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=3),
nn.MaxPool2d(2),
nn.ReLU(),
nn.Flatten(),
nn.Linear(3200, 256),
nn.ReLU(),
nn.Linear(256, 10)
).to(device)
loss_fn = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=1e-3)
return model, loss_fn, optimizer
The Training and Accuracy functions
# creating a training function
def train_batch(x, y, model, loss_fn, opt):
model.train()
prediction = model(x)
batch_loss = loss_fn(prediction, y)
batch_loss.backward()
opt.zero_grad()
opt.step()
return batch_loss.item()
@torch.no_grad()
def accuracy_fn(x, y, model):
model.eval()
prediction = model(x)
max_val, argmaxes = prediction.max(-1)
is_correct = argmaxes == y
is_correct = is_correct.cpu().numpy().tolist()
return is_correct
I have no idea what must have gone.. I Think the images was reduced to size smaller for the filter with 3x3. How do i fix this?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
