Book Image

Modern Computer Vision with PyTorch

By : V Kishore Ayyadevara, Yeshwanth Reddy
Book Image

Modern Computer Vision with PyTorch

By: V Kishore Ayyadevara, Yeshwanth Reddy

Overview of this book

Deep learning is the driving force behind many recent advances in various computer vision (CV) applications. This book takes a hands-on approach to help you to solve over 50 CV problems using PyTorch1.x on real-world datasets. You’ll start by building a neural network (NN) from scratch using NumPy and PyTorch and discover best practices for tweaking its hyperparameters. You’ll then perform image classification using convolutional neural networks and transfer learning and understand how they work. As you progress, you’ll implement multiple use cases of 2D and 3D multi-object detection, segmentation, human-pose-estimation by learning about the R-CNN family, SSD, YOLO, U-Net architectures, and the Detectron2 platform. The book will also guide you in performing facial expression swapping, generating new faces, and manipulating facial expressions as you explore autoencoders and modern generative adversarial networks. You’ll learn how to combine CV with NLP techniques, such as LSTM and transformer, and RL techniques, such as Deep Q-learning, to implement OCR, image captioning, object detection, and a self-driving car agent. Finally, you'll move your NN model to production on the AWS Cloud. By the end of this book, you’ll be able to leverage modern NN architectures to solve over 50 real-world CV problems confidently.
Table of Contents (25 chapters)
1
Section 1 - Fundamentals of Deep Learning for Computer Vision
5
Section 2 - Object Classification and Detection
13
Section 3 - Image Manipulation
17
Section 4 - Combining Computer Vision with Other Techniques

Training a neural network

To train a neural network, we must perform the following steps:

  1. Import the relevant packages.
  2. Build a dataset that can fetch data one data point at a time.
  3. Wrap the DataLoader from the dataset.
  4. Build a model and then define the loss function and the optimizer.
  5. Define two functions to train and validate a batch of data, respectively.
  6. Define a function that will calculate the accuracy of the data.
  7. Perform weight updates based on each batch of data over increasing epochs.

In the following lines of code, we'll perform each of the following steps:

The following code is available as Steps_to_build_a_neural_network_on_FashionMNIST.ipynb in the Chapter03 folder of this book's GitHub repository - https://tinyurl.com/mcvp-packt
  1. Import the relevant packages and the FMNIST dataset:
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
device = "cuda" if...