Pytoch Pocket Book II

Time:2022-5-28

Pytoch Pocket Book II

pytorch pocket reference

Original book download address:
I shared “oreilly.pytorch.pocket.r… Models.149209000x.pdf” on the alicloud disk. You can download it without speed limit
Copy this content and open the alicloud disk app to get it
Link:https://www.aliyundrive.com/s/NZvnGbTYr6C

Chapter I Introduction to pytoch

In recent years, pytoch has become more and more popular among researchers or engineering application groups.

In this chapter, the author briefly introduces what pytoch is and why it can be so widely used (make it popular). At the same time, this chapter will also introduce the installation and use of pytoch on the cloud or local computer. Finally, we will learn how to verify that pytoch is properly installed and running a simple pytoch application.

Pytoch is a free and open-source artificial intelligence framework of Facebook’s artificial intelligence research laboratory (fair), and has more than 1700 contributors so far.

  • Easy operation of array structure type
  • Creating dynamic neural networks
  • Automatic differential solution based on GPU acceleration
  • Simple and flexible interface for experiment testing (loading data, calling data conversion, creating models, and easily constructing training, verification, and testing modes)
  • The ecosystem is also highly active, such as universities or well-known large enterprises
    Based on some features, there are more and more users of pytoch. Some of them are used for tensor computing acceleration, while others are used for deep learning development.

Many developers and researchers use PyTorch to accelerate deep learning research experimentation and prototyping. Its simple Python API, GPU support, and flexibility make it a popular choice among academic and commercial research organizations.

With the increasing number of users and scenarios, pytoch’s update iteration is also fast, and it also caters to the use needs of different devices (ECS and mobile terminal platforms).

A good AI framework can bring us many conveniences in AI application research: loading data, preprocessing, model design, training and deployment.

A deep learning framework makes it easy to perform common tasks such data loading, preprocessing, model design, training, and deployment.

Several advantages of pytoch

  • Pytoch is very popular in both the University Circle and the engineering field
  • Pytoch supports most mainstream cloud platforms in the market (AWS, GCP, azure, Alibaba cloud).
  • Pytoch supports Google’s colab and kaggle competitions
  • Pytoch is mature and stable (the current version is 1.8, and the latest version should be 1.9)
  • Pytoch supports CPU, GPU, TPU and parallel processing
  • Pytoch supports distributed training
  • Pytorch applications can be easily deployed to cloud servers (torchscript, torchserve)
  • Pytoch begins to support mobile device deployment applications (Android, IOS)
  • Pytoch has a good ecosystem and relevant supplementary optimization packages (NLP, CV)
  • Pytoch supports c++ front-end interfaces
  • Pytoch supports onnx (open neural network exchange)
  • Pytoch community is highly active and has many users

Pytoch installation and use

There are a lot of resources on the Internet, so I won’t record much in this note

It mainly introduces the use of pytoch in Google Colorado, the installation and use of pytoch in ECs and local computers.
Please refer to the official installation page for details.
https://pytorch.org/get-started/locally/

import torch

#View version number
print(torch.__version__)
#See whether GPU acceleration is supported
print(torch.cuda.is_available())

An interesting example

In the book, a picture on the Internet is downloaded directly through the urllib package. In view of the domestic network problems, the download is directly saved here as a follow-up example.

Sample image download address:https://www.aliyundrive.com/s/LqmvEpYgWMy

%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image

fpath = 'coffee.jpg'

img = Image.open(fpath)
plt.imshow(img)

# SIZE
print(img.size)
# CHANNELS  ('R', 'G', 'B')
print(img.getbands())
# RGB
print(img.mode)
Pytoch Pocket Book II

coffee.png

From the above program, we can get that the size of the picture is (1107 * 827). Next, we use pytoch to preprocess the picture and convert it into an appropriate format to make it meet the data type requirements of pytoch’s neural network model.

import torch
from torchvision import transforms

transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]
        )
])
#Convert PIL image to tensor
img_tensor = transform(img)
print(type(img_tensor), img_tensor.shape)
# <class 'torch.Tensor'> torch.Size([3, 224, 224])

#Convert tensor to PIL image
# image = img_tensor.cpu().clone()
#Image = image Squeeze (0) \\
Image = transforms Topilimage() (img\u tensor) \

plt.imshow(image)
Pytoch Pocket Book II

transform

Compare the image effects before and after transform:

plt.figure(figsize=(8, 8)) 
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(image)
plt.tight_layout()
plt.show()
Pytoch Pocket Book II

after transform

In the above example, we found that the image is converted through the compose () method, and a series of transformations are defined in this process, such as resize, crop, totensor and normalization.

Normalizing the image improves the accuracy of the classifier.

Image classification and prediction based on trained model (alexnet)

We know that efficient machine learning processing is basically based on batches, so we need to do some dimension processing of “adding batches” for the single image data structure we just loaded.
In pytorch, we call the unsqueeze () method to add a dimension to a single picture data as the batch value, that is, the entire data structure becomes[1, 3, 224, 224]Where 1 means the batch value is 1, 3 means three RGB channels, and 224*224 means the input size of the picture.

The labels data file download address of Imagenet in the sample:https://www.aliyundrive.com/s/NoLcfzUeXLv

batch = img_tensor.unsqueeze(0)
print(batch.shape)
# torch.Size([1, 3, 224, 224])

The next step is to call the alexnet model to infer and predict the picture.
AlexNetAs a classic classification model, there is no too much introduction here. You can understand the relevant content in Baidu by yourself.

%matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
import torch
from torchvision import transforms, models

#Define device for later programs
device = 'cuda' if torch.cuda.is_available() else 'cpu'

#Picture address and category file address of Imagenet
image_path = 'coffee.jpg'
imagenet_labels_path = 'imagenet_class_labels.txt'

#Read picture
img = Image.open(image_path)
#Display picture
# plt.imshow(img)

#Define converter
transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(
            mean=[0.485, 0.456, 0.406],
            std=[0.229, 0.224, 0.225]
        )
])

#Convert to tensor
img_tensor = transform(img)

#Convert tensor to PIL image format
Image_ Trans = transforms Topilimage() (img\u tensor) \

# plt.imshow(image_trans)
#Comparison of pictures before and after transform
plt.figure(figsize=(8, 8)) 
plt.subplot(1, 2, 1)
plt.imshow(img)
plt.subplot(1, 2, 2)
plt.imshow(image_trans)
plt.tight_layout()
plt.show()

#Build a single picture structure in batch form [batch size, channels, width, height]
batch = img_tensor.unsqueeze(0)
print(batch.shape) 
# torch.Size([1, 3, 224, 224])

#Load alexnet model, pre trained=true, indicating the model with trained parameters
model = models.alexnet(pretrained=True)

#Eval () indicates that the model is only used for reasoning or prediction, and does not update related parameters.
model.eval()
model.to(device)
y = model(batch.to(device))
# print(y.shape)
# torch.Size([1, 1000])

y_max, index = torch.max(y, 1)
# print(index, y_max)
# tensor([967], device='cuda:0') tensor([21.9117], device='cuda:0', grad_fn=<MaxBackward0>)
#Corresponding to category name and index
with open('imagenet_class_labels.txt') as f:
    classes = [line.strip() for line in f.readlines()]
# print(classes[967])
# 967: 'espresso'

prob = torch.nn.functional.softmax(y, dim=1)[0] * 100
print(classes[index[0]], prob[index[0]].item())
# 967: 'espresso', 86.61658477783203

#View the previous scores
_, indices = torch.sort(y, descending=True)
Print ('view the previous scores and categories: ')
for idx in indices[0][:5]:
    print(classes[idx], prob[idx].item())
Pytoch Pocket Book II

prediction

From the above figure, we can see that the predicted results are quite reliable (espresso).

Recommended Today

The idea compilation project always prompts that the package cannot be found. Delete the dependency in ieda, update maven, and re import. Ensure that the local warehouse location is the location of the project dependency reference

The idea compilation project always prompts that the package cannot be found. Delete the dependency in ieda, update maven, and re import. Ensure that the local warehouse location is the location of the project dependency reference https://blog.csdn.net/qq_36698385/article/details/119423197 Problem description In idea, the code does not display any error prompt, but when compiling the code, it […]