Image classification with PyTorch

Image classification with PyTorch

CIFAR 10 Data set using logistic regression. In my previous posts we have gone through. Let us try to solve image classification of CIFAR-10 data set with Logistic regression.

CIFAR 10 Data set using logistic regression

Image for post

In my previous posts we have gone through

  1. Deep Learning — Artificial Neural Network(ANN)
  2. Tensors — Basics of pytorch programming
  3. Linear Regression with PyTorch

Let us try to solve image classification of [CIFAR-10_](https://www.cs.toronto.edu/~kriz/cifar.html) data set with Logistic regression._


Step 1 : Import necessary libraries & Explore the data set

We are importing the necessary libraries pandas , numpy , matplotlib ,torch ,torchvision. With basic EDA we could infer that CIFAR-10 data set contains 10 classes of image, with training data set size of 50000 images , test data set size of 10000.Each image is of [3 x 32 x 32 ]. Which represents 3 channels RGB,32 x 32 pixel size.

#Explore CIFAR Data set
dataset = CIFAR10(root='data/', download=True, transform=ToTensor())
test_dataset = CIFAR10(root='data/', train=False, transform=ToTensor())
#size of training data
dataset_size = len(dataset)
dataset_size
#size of test data
test_dataset_size = len(test_dataset)
test_dataset_size
#number of classes in the data set
classes = dataset.classes
classes

Visualizing a sample image and the size of the sample image.

#Let us understand the size of one image
img, label = dataset[0]
img_shape = img.shape
img_shape
#Let us look at a sample image
img, label = dataset[0]
plt.imshow(img.permute((1, 2, 0)))
print('Label (numeric):', label)
print('Label (textual):', classes[label])

As this is a 3 channel RGB image Pytorch expects the channels as first dimension where as matplotlib expects as last dimension of the image.Here .permute tesnor method is used to shift channels to last dimesnion

Image for post

Image for post

Label (numeric): 6

Label (textual): frog

Step 2 : Prepare data for training

We using training set , validation set , Test set. Why we need them ?

Training set : used to train our model,computing loss & adjust weights Validation set : To evaluate the model with hyper parameters & pick the best model during training. we are using 10% of training data as validation set Test data set : Used to compare different models & report the final accuracy.

We are using the random_split from pytorch for creating train_ds,val_ds. torch.manual_seed(43) is set for reproducing the results.

#validation set size 5000 ie 10% 
torch.manual_seed(43)
val_size = 5000
train_size = len(dataset) - val_size
#creating training & validation set using random_split
train_ds, val_ds = random_split(dataset, [train_size, val_size])
len(train_ds), len(val_ds)

We are using the data loader as we used in our previous example , with a batch size of 128. To visualize our data we are using make_grid helper function from torch vision.

pytorch neural-networks image-classification deep-learning logistic-regression deep learning

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PyTorch Images and Logistic Regression | Deep Learning with PyTorch: Zero to GANs | Part 2 of 6

PyTorch Images and Logistic Regression | Deep Learning with PyTorch | Working with images from the MNIST dataset; Training and validation dataset creation; Softmax function and categorical cross entropy loss; Model training, evaluation, and sample predictions

Deep Learning with CIFAR-10 Image Classification

In this article, we will be implementing a Deep Learning Model using CIFAR-10 dataset. The dataset is commonly used in Deep Learning for testing models of Image Classification.

PyTorch For Deep Learning — Convolutional Neural Networks ( Fashion-MNIST )

PyTorch For Deep Learning — Convolutional Neural Networks ( Fashion-MNIST ). This blog post is all about how to create a model to predict fashion mnist images and shows how to implement convolutional layers in the network.

Bayesian Neural Networks: 2 Fully Connected in TensorFlow and Pytorch

This chapter continues the series on Bayesian deep learning. In the chapter we’ll explore alternative solutions to conventional dense neural networks.

Classification of Fruit Images Using Neural Networks (PyTorch)

Find a neural network model that achieves the highest accuracy rate for the classification of Fruits360 images. An easy-to-read guide on neural networks to achieve a high accuracy rate Let's learn more about classification of fruit images using Neural Networks (PyTorch)