Aug 7, 2024
numpy
Pillow
torch
torchvision
Import Libraries:
numpy
, PIL
, torch
, torch.nn
, torch.optim
, torchvision
, torchvision.transforms
Data Transformations:
transforms.Compose
to combine transformations:
transforms.ToTensor
transforms.Normalize(mean=0.5, std=0.5)
Load the CIFAR-10 Dataset:
train=True
for training dataset, train=False
for test dataset.download=True
to download the dataset if not present.Define the CNN Class:
nn.Module
.__init__
method (Constructor) to define the network architecture:
nn.Conv2d(in_channels=3, out_channels=12, kernel_size=5)
nn.MaxPool2d(kernel_size=2)
nn.Conv2d(in_channels=12, out_channels=24, kernel_size=5)
Forward Method:
Define Loss Function and Optimizer:
nn.CrossEntropyLoss()
.Training Loop:
Save Model Parameters:
Test the Model:
Predict on New Images: