Get startedGet started for free

Binary classification model

As a deep learning practitioner, one of your main tasks is training models for image classification. You often encounter binary classification, where you need to distinguish between two classes. To streamline your workflow and ensure reusability, you have decided to create a template for a binary image classification CNN model, which can be applied to future projects.

The package torch and torch.nn as nn have been imported. All image sizes are 64x64 pixels.

This exercise is part of the course

Deep Learning for Images with PyTorch

View Course

Exercise instructions

  • Create a convolutional layer with 3 channels, 16 output channels, kernel size of 3, stride of 1, and padding of 1.
  • Create a fully connected layer with an input size of 16x32x32 and a number of classes equal to 1; include only the values in the provided order (16*32*32, 1).
  • Create a sigmoid activation function.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class BinaryImageClassifier(nn.Module):
    def __init__(self):
        super(BinaryImageClassifier, self).__init__()
        
        # Create a convolutional layer
        self.conv1 = ____(____)
        self.relu = nn.ReLU()
        self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.flatten = nn.Flatten()
        
        # Create a fully connected layer
        self.fc = ____(____)
        
        # Create an activation function
        self.sigmoid = ____
Edit and Run Code