Deep Learning Tutorial

Introduction to PyTorch for Deep Learning

Image classification with PyTorch using transfer learning.

Tirendaz AI
Geek Culture

--

Photo by Aziz Acharki on Unsplash

Artificial intelligence applications such as recommendation systems, web search, language translation, and driverless cars have changed our lives. One of the hottest subfields of AI is deep learning.

Deep learning is an extension of artificial neural networks. While artificial neural networks consist of one hidden layer, deep neural networks consist of multiple hidden layers.

Many matrix operations are used in deep learning analysis. You need to use a library to implement these operations. The most used libraries for deep learning are PyTorch and TensorFlow. TensorFlow is mostly used in industry while PyTorch is used more for academic research.

In this post, I’ll talk about PyTorch for deep learning. In summary,

  • What is PyTorch?
  • The advantages of PyTorch.
  • How to install PyTorch?
  • Practicing with a real-world dataset.

As a simple example, I’ll get a picture from the internet and predict the class of this picture using PyTorch. This is a perfect example for the beginner who wants to learn PyTorch. In this example, I’ll use a dog picture and predict the dog’s genus. If you want you can use a different picture such as tea or flowers.

Before getting started, please don’t forget to subscribe to our youtube channel where we create content about AI, data science, machine learning, and deep learning👇

Let’s dive into what PyTorch is.

What is PyTorch

PyTorch is a popular library written in the Python language. It is used specifically for deep learning. PyTorch was primarily developed by Facebook. It later became a free and open-source library in 2017.

With PyTorch, you can easily perform array operations and build dynamic neural networks. You can also speed up these processes using the GPU. In other words, you can make fast deep learning projects with the simple and flexible use of PyTorch.

So why should you choose PyTorch? Let’s take a look at the advantages of PyTorch.

Why PyTorch?

It is very popular because PyTorch is easy to use. It has a large community of developers and users. Many companies and research groups use PyTorch for deep learning. Therefore, knowing PyTorch is a sought-after skill in both industry and academia.

You can also do end-to-end projects with PyTorch. That is, you can use it with PyTorch for deep learning project steps such as data preprocessing, model building, and model deployment.

As you know, Python is an interpretive language. In PyTorch, it works in eager mode since it is written in Python. In other words, you can immediately see the results of the codes you wrote in PyTorch. Thus, you can easily find your debug codes.

Cloud is important for big data. Cloud services like AWS or Azure make your work easier because deep learning works with big data. You can use PyTorch on cloud platforms.

A powerful computer is required for deep learning. If you don’t have a powerful computer, don’t worry. You can use PyTorch for free on both Google Colab and Kaggle Kernels.

PyTorch supports parallel computing. So you can use GPU or TPU for mathematical operations in PyTorch. Thus, you can do your deep learning processes faster.

PyTorch has a large ecosystem as well as open-source libraries based on PyTorch. For example, fastai, which makes it easy to write code for deep learning projects, is written based on PyTorch. With fastai, you can analyze both computer vision and natural language processing.

As you can see, PyTorch is a perfect library for deep learning projects. There are many more reasons to choose PyTorch. I have only mentioned some important advantages here. Let’s move on and take a look at how to install PyTorch.

PyTorch Installation

If you have a powerful computer, you can install PyTorch on your computer. Installing PyTorch on your computer is very easy. PyTorch’s official website has made installation easy by preparing an easy interface. To install PyTorch, go to the official website of PyTorch, choose the suitable options, and then a command is created.

PyTorch Installation

After that, run this command in the terminal or a virtual environment you created in anaconda. To install PyTorch on Anaconda, open the Anaconda prompt and set the following codes to create a new virtual environment.

conda create — name pytorch

A new virtual environment called PyTorch is created. Next, you have to activate this environment. To do this:

conda activate pytorch

After that, you paste the command you copied from PyTorch’s site here.

conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch

It’s that simple. If your computer has an NVIDIA graphics card, don’t forget to select the GPU option. With GPU, you can do your deep learning analysis 50 times faster.

If you don’t have a powerful computer, don’t worry, you can also do deep learning analysis using cloud platforms like AWS. But using this service is paid.

If you want to do your deep learning analysis for free, you can use Colab. Colab is a cloud-based free service offered by Google. Colab’s interface is similar to Jupyter Notebook. You can run your Python codes in Colab. You can also use Colab with GPU.

PyTorch is installed in Colab so you don’t need to install it separately. Let’s see the installed version of PyTorch now.

import torch
print(torch.__version__)

One advantage of Colab is that its interface is similar to Jupyter Notebook. If you know how to use Jupyter Notebook, you can use Colab easily. By default, Colab does not work with GPU. Let’s check this out.

print(torch.cuda.is_available())

The output is false. To work with GPU, choose Change Run Type from the Runtime menu and select GPU from the Hardware accelerator section.

Change Run Type

Now let’s run this command again.

print(torch.cuda.is_available())

And true is written to the screen. After that my codes are run on GPU. If you buy the Pro version of Colab, you can use a better GPU and more memory. The free version is sufficient for the analysis I’m going to do in this blog post.

So far I’ve been introduced to PyTorch and shown how to install it. I hope everything is alright. You can write your questions in the comments section. The best way to learn is by doing Let’s go ahead and make a simple application to understand PyTorch.

Pytorch Application

Now let me show you how to do image classification with PyTorch. In this practice, I’m goint to take a picture from the internet and specify the class of this picture.

Image classification was a difficult problem in the 2000s. But after 2010, with the development of new architectures and the improvement of GPUs, image classification became easier to do.

You can find the notebook I used in this blog post here.

Let’s take a picture of a dog, for example. If you want, you can use a different image for this analysis. Let’s use the urlib library to pull an image from the web. First, let’s import this library and request.

import urllib.request

Okay, now let’s take a url variable and assign the link of the site where we will get the picture to this variable.

url = “https://github.com/pytorch/hub/raw/master/images/dog.jpg"

Let’s assign the name of the image found in this link to the file_name variable.

file_name = “dog.jpg”

Now let me get a picture from the Internet.

urllib.request.urlretrieve(url, file_name)

Now, we got a picture.

Now let’s read the picture with the Pillow library and then see the picture with matplotlib. First, let’s import the libraries.

import matplotlib.pyplot as plt
from PIL import Image

Let’s assign the image to the img variable.

img = Image.open(‘dog.jpg’)

Let’s see the picture with matplotlib.

plt.imshow(img)
A cute dog

We understand that this image is a dog. But how will the computer understand this picture? I’m going to talk about this. My goal is to find the tag for this picture. For this, I’m going to use an algorithm based on neural networks.

Before giving the image to the algorithm, you need to preprocess the image. Make sure the picture is available for the algorithm. Our picture is in color format. It has 3 channels, RGB and the size of it is 1600*1200. Remember, data preprocessing steps in Pytorch are called transforms. Now let’s make the image suitable for analysis with transforms.

Let’s first import PyTorch and then transforms.

import torch
from torchvision import transforms

Let’s use the compose method to define a series of preprocessing steps.

preprocess = transforms.Compose([
transforms.Resize(256), #1
transforms.CenterCrop(224), #2
transforms.ToTensor(), #3
transforms.Normalize( #4
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])

Let’s go through these codes:

  1. I resize the image to 256 sizes with the Resize method.
  2. I apply central clipping to fit the neural network.
  3. As you know, neural networks work with numerical data. I turn this image into a tensor. A tensor is the basic data object in PyTorch. You can think of Tensor as a NumPy array with extra features.
  4. Images consist of pixel values. I scale these pixel values ​​between 0–1. With this scaling, you can make both faster and more accurate predictions.

Thus, we created data preprocessing steps to make the image suitable for the neural network and assigned it to the preprocess variable. Now let’s apply these steps to the picture we have.

img_tensor = preprocess(img)

As you can see, img_tensor is 224*224 in size and consists of 3 channels in the form of RGB. Deep learning algorithms work with millions of data. It doesn’t make sense to give all of these data to the model at once. Data is given to neural networks in groups called batches. The batch size for big data is usually 32 or 64. You specify these values ​​according to your analysis.

I only have one image, but deep learning models expect data to be batched. Now let’s add a batch to our data. I’m going to use the unsqueeze method to add a dimension to the data. In this method, we need to specify where to add the dimension. I’m going to set 0 to add dimension at the beginning.

batch = img_tensor.unsqueeze(0)

Let’s now see the size of the data.

print(batch.shape)

Thus, my data was 1 batch, 3 channels, and 224*224 pixels. Now the image is ready to enter the neural network.

As you know, training deep learning models is both time-consuming and costly. It makes more sense to use the pre-trained model. You can find the pre-trained models in the PyTorch repo.

For example, it’s very easy to upload a model used to classify images with Torchvision. Let’s use AlexNet, which won the ImageNet competition in 2012, to classify images. First, let’s import models from torchvision.

from torchvision import models

AlexNet works well for classifying pictures. There are different models from AlexNet. You can choose the model you want suitable for your analysis.

Deep learning is more an art than a science.

Now let’s install alexnet. With the pretrained=True option, the previously trained coefficients of the model are loaded.

model = models.alexnet(pretrained=True)

Now let’s create a device variable so that the analysis will run on the GPU if it’s on the GPU, and the CPU if it isn’t.

device = “cuda” if torch.cuda.is_available() else “cpu”

Since I’m making a simple application, a GPU isn’t required for this analysis. You can also run this analysis on the CPU. Now let’s configure the model with model.eval.

model.eval()

Let’s choose the device on which the model will run. For this, I use the device variable that I just created.

model.to(device)

Let’s assign the command to run the model to the variable y.

y = model(batch.to(device))
print(y.shape)
#outout : torch.Size([1, 1000])

Let’s see the dimension of y. Here 1 represents the batch of data. Since we have only 1 image, the batch number is 1. Since the number of classes is 1000, the value of 1000 is written on the screen. Now let’s see the class that gets the most value.

y_max, index = torch.max(y,1)

Let’s print these variables to the screen.

print(index, y_max)#output: tensor([258], device='cuda:0') tensor([16.8252], device='cuda:0', grad_fn=<MaxBackward0>)

My model found the class with the 258th index with the official highest value of 16.8252. But we don’t know the class with index 258. Let’s get the class names from PyTorch’s site now. Let me create a url variable first.

url = ‘https://pytorch.tips/imagenet-labels'

Let’s create a class label variable.

fname = ‘imagenet_class_labels.txt’

Now let’s get the list from the internet using these variables.

urllib.request.urlretrieve(url, fname)

Let’s open this list.

with open("imagenet_class_labels.txt") as f:
classes = [line.strip() for line in f.readlines()]
print(classes[258])
# output: 258: 'Samoyed, Samoyede'

As you can see, the breed of the dog is written on the screen. So, what percent probability did the model achieve this result? Let’s use the softmax() function to see this.

probe = torch.nn.functional.softmax(y, dim=1)[0] * 100

Let me show this possibility now. I’m going to use the tensor.item() method to see the probability.

print(classes[index[0]], probe[index[0]].item())# output: 258: 'Samoyed, Samoyede', 72.44749450683594

The model predicted the breed of the dog by about 72 percent. You can also print the top 5 predictions using the sort() method.

 _, indices = torch.sort(y, descending=True)

Now, let’s print the predictions up to 5.

for idx in indices[0][:5]:
print(classes[idx], probe[idx].item())

As you can see, my model initially predicted the dog breed as Samoyed 72 percent, while predicting the Wallaby breed by 13 percent and the Pomeranian by 5 percent.

Conclusion

In this post, I’ve talked about PyTorch for Deep Learning. In summary, I first explained what PyTorch is and its advantages. Next, I showed you the installation. And finally, I covered how to estimate the class of an image using alexnet pre-trained.

--

--