Neural Network Implementation - ( Image Recognition with PyTorch )
Heading h2
Syntax
PyTorch
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
# Define Hyper-parameters
input_size = 28*28
num_classes = 10
learning_rate = 0.001
batch_size = 100
num_epochs = 5
# Define Dataset
train_dataset = datasets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = datasets.MNIST(root='./data',
train=False,
transform=transforms.ToTensor())
# Define Data Loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
# Define Model
model = nn.Sequential(
nn.Linear(input_size, 256),
nn.ReLU(),
nn.Linear(256, 64),
nn.ReLU(),
nn.Linear(64, num_classes)
)
# Define Loss Function
criterion = nn.CrossEntropyLoss()
# Define Optimizer
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Train Model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.reshape(-1, input_size)
# Forward Pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backward Pass and Optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# Test Model
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, input_size)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test Accuracy of the model on the 10000 test images: {} %'.format((correct / total) * 100))
Example
PyTorch
# Importing Libraries
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
# Define Hyper-parameters
input_size = 28*28
num_classes = 10
learning_rate = 0.001
batch_size = 100
num_epochs = 5
# Define Dataset
train_dataset = datasets.MNIST(root='./data',
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = datasets.MNIST(root='./data',
train=False,
transform=transforms.ToTensor())
# Define Data Loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
# Define Model
model = nn.Sequential(
nn.Linear(input_size, 256),
nn.ReLU(),
nn.Linear(256, 64),
nn.ReLU(),
nn.Linear(64, num_classes)
)
# Define Loss Function
criterion = nn.CrossEntropyLoss()
# Define Optimizer
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# Train Model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.reshape(-1, input_size)
# Forward Pass
outputs = model(images)
loss = criterion(outputs, labels)
# Backward Pass and Optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# Test Model
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.reshape(-1, input_size)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Test Accuracy of the model on the 10000 test images: {} %'.format((correct / total) * 100))
Output
PyTorch
Epoch [1/5], Step [100/600], Loss: 0.4625
Epoch [1/5], Step [200/600], Loss: 0.2098
Epoch [1/5], Step [300/600], Loss: 0.2782
Epoch [1/5], Step [400/600], Loss: 0.1869
Epoch [1/5], Step [500/600], Loss: 0.1555
Epoch [1/5], Step [600/600], Loss: 0.1650
Epoch [2/5], Step [100/600], Loss: 0.0788
Epoch [2/5], Step [200/600], Loss: 0.0922
Epoch [2/5], Step [300/600], Loss: 0.0444
Epoch [2/5], Step [400/600], Loss: 0.0523
Epoch [2/5], Step [500/600], Loss: 0.0186
Epoch [2/5], Step [600/600], Loss: 0.1682
Epoch [3/5], Step [100/600], Loss: 0.1118
Epoch [3/5], Step [200/600], Loss: 0.0563
Epoch [3/5], Step [300/600], Loss: 0.0846
Epoch [3/5], Step [400/600], Loss: 0.0377
Epoch [3/5], Step [500/600], Loss: 0.1112
Epoch [3/5], Step [600/600], Loss: 0.0239
Epoch [4/5], Step [100/600], Loss: 0.0139
Epoch [4/5], Step [200/600], Loss: 0.0590
Epoch [4/5], Step [300/600], Loss: 0.0141
Epoch [4/5], Step [400/600], Loss: 0.0280
Epoch [4/5], Step [500/600], Loss: 0.0387
Epoch [4/5], Step [600/600], Loss: 0.0685
Epoch [5/5], Step [100/600], Loss: 0.0454
Epoch [5/5], Step [200/600], Loss: 0.0138
Epoch [5/5], Step [300/600], Loss: 0.0600
Epoch [5/5], Step [400/600], Loss: 0.0274
Epoch [5/5], Step [500/600], Loss: 0.0829
Epoch [5/5], Step [600/600], Loss: 0.0170
Test Accuracy of the model on the 10000 test images: 97.97 %
Explanation
In this example, we have implemented a neural network for image recognition using PyTorch. We have used the MNIST dataset which contains images of handwritten digits, and our goal is to correctly classify them. We have defined a simple neural network with two hidden layers and an output layer. We have used the Adam optimizer and the CrossEntropyLoss function for training the network. After training the network for 5 epochs, we have achieved an accuracy of 97.97% on the test set.
Use
This example can be used as a starting point for implementing other image recognition tasks using PyTorch. You can modify the network architecture, hyperparameters, and dataset as per your requirements.
Important Points
- PyTorch is a popular deep learning framework for Python
- Neural networks can be used for image recognition tasks
- The MNIST dataset is commonly used for image classification tasks
- The Adam optimizer and CrossEntropyLoss function can be used to train neural networks
Summary
In summary, we have implemented a neural network for image recognition using PyTorch. We have used the MNIST dataset and achieved a high accuracy on the test set. This example can be used as a starting point for implementing other image recognition tasks using PyTorch.