Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 4 (Classifying images using deep CNNs) #81

Open
Abeltemi opened this issue Feb 22, 2024 · 1 comment
Open

Chapter 4 (Classifying images using deep CNNs) #81

Abeltemi opened this issue Feb 22, 2024 · 1 comment

Comments

@Abeltemi
Copy link

In the Training cell I am getting this error

image

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?

@mahmuudtolba
Copy link

  1. Try to increase the size of the image to something like 224x224 through your dataset object
  2. use padding with the CONV layer so that the shape of the image not decrease that much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants