CNN Implementation - ( Convolutional Neural Network in PyTorch )
Heading h2
Syntax
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# Convolutional layers
self.conv1 = nn.Conv2d(1, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
# Fully connected layers
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# Convolutional layers
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
# Flatten tensor for fully connected layers
x = x.view(-1, 16 * 5 * 5)
# Fully connected layers
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
Example
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
# Define transform for input images
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# Load the MNIST dataset
trainset = torchvision.datasets.MNIST(root='./data', train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)
# Define the network
net = Net()
# Define a loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
# Train the network
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
# Get the inputs
inputs, labels = data
# Zero the parameter gradients
optimizer.zero_grad()
# Forward + backward + optimize
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# Print statistics
running_loss += loss.item()
if i % 2000 == 1999: # print every 2000 mini-batches
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.0
print('Finished Training')
Explanation
Convolutional Neural Networks (CNNs) are a powerful type of neural network that are commonly used for image classification tasks. They use several convolutional layers to extract features from the input image, before passing these features through a series of fully connected layers to make the final classification.
The code above shows an implementation of a simple CNN in PyTorch, using the popular MNIST dataset as an example. The Net
class defines the overall architecture of the CNN, while the torch.utils.data.DataLoader
class is used to load and preprocess the dataset. The nn.CrossEntropyLoss
function is used as the loss function, and the optim.SGD
function as the optimizer.
The training loop runs for 2 epochs, printing the running loss after every 2000 mini-batches. After training, the CNN should be able to accurately classify handwritten digits from the MNIST dataset.
Use
Convolutional Neural Networks are useful for a wide range of image and video analysis tasks, including image classification, object detection, segmentation, and more. They have shown state-of-the-art performance on many benchmark datasets, and are a key component of modern computer vision systems.
Important Points
- CNNs are a powerful type of neural network commonly used for image classification tasks.
- PyTorch provides a convenient framework for building and training CNNs.
- The
Net
class defines the architecture of the CNN, whiletorch.utils.data.DataLoader
is used to load and preprocess the dataset. - The
nn.CrossEntropyLoss
function is used as the loss function, and theoptim.SGD
function as the optimizer. - CNNs are commonly used for a wide range of image and video analysis tasks, and have shown state-of-the-art performance on many benchmark datasets.
Summary
In conclusion, the implementation of a simple Convolutional Neural Network in PyTorch is shown above, using the MNIST dataset as an example. CNNs are a powerful type of neural network commonly used for image and video analysis tasks, and PyTorch provides a convenient framework for building and training these networks.