After introducing the basic knowledge of pytorch in the first three blogs, here we will introduce the construction of simple network, detail the convolution operation, and finally build the convolution layer of neural network according to the convolution operation.
1. Simple use of nn.module
Official help documentation
First, let's start with the help document, Enter the pytorch official website , see pytoch's official help documentation
Then go to torch.nn (NN is the abbreviation of neural network) and check the container (also known as skeleton)
We can see that there are many things under torch.nn, such as convolution layer, pooling layer, nonlinear activation, regularization layer, etc. if you are interested, you can take a look in advance. I will introduce them in the follow-up blog.
We mainly review two parts: the introduction of torch.nn.Module and its use:
Code sample run
After reading the help documents, let's experiment (we need to use the previous tensorboard, datasets, transform and DataLoader. If we can't or forget, we need to refer to my previous blog).
Below, we define a MyModel class, which is inherited from torch.nn.Module. forward completes a simple addition operation.
import cv2 from PIL import Image import torch import torchvision from torch.utils.data import DataLoader from torch.utils.data import Dataset from torch.utils.tensorboard import SummaryWriter from torchvision import transforms class MyModel(torch.nn.Module): def __init__(self, delta): super(MyModel, self).__init__() self.delta = delta def forward(self, x): return x + self.delta if __name__ == "__main__": my_model = MyModel(torch.tensor(10)) x = torch.tensor(5) print(my_model(x))
2. Convolution
Friends who have studied digital image processing or related courses must be familiar with convolution operation. If you don't understand it, you can refer to it Knowing the convolution question , I think it's very good!
torch.nn.functional.conv2d()
First, we look at the official documents
According to the official documents, let's write an example to practice:
import torch import torchvision from torch.utils.tensorboard import SummaryWriter from torch.utils.data import DataLoader import torch.nn.functional as F input = torch.tensor([ [1, 2, 0, 3, 1], [0, 1, 2, 3, 1], [1, 2, 1, 0, 0], [5, 2, 3, 1, 1], [2, 1, 0, 1, 1] ]) kernel = torch.tensor([ [1, 2, 1], [0, 1, 0], [2, 1, 0] ]) # In order to satisfy the size type of torch.nn.functional.con2d input and convolution kernel, we need to reshape it my_list = list(input.shape) my_list.insert(0, 1) my_list.insert(0, -1) input = torch.reshape(input, my_list) # Insert directly like this... kernel = torch.reshape(kernel, [1, 1, -1, 3]) result1 = F.conv2d(input=input, weight=kernel, stride=1, padding=0) print(result1) result2 = F.conv2d(input=input, weight=kernel, stride=1, padding=1) print(result2)
3. Convolution layer of neural network
In the first two parts, we learned about torch.nn.Module, derived the Model using this abstract class, and learned torch.nn.functional.conv2d() convolution operation. Next, we will learn torch.nn.Conv2d() and use it to write a convolution layer of neural network
torch.nn.Conv2d()
Next, let's see Official documentation for Torch.nn.Conv2d And explain the parameters.
Simple convolutional neural network
The steps are as follows:
- First, we still use datasets and Dataloader to load datasets
- A simple neural network is derived using torch.nn.Module
- Write the running results of the network into the tensorboard visualization tool
- Use the tensorboard visualization to view the results
code:
import torch import torchvision from PIL import Image import cv2 from torch.utils.tensorboard import SummaryWriter from torch.utils.data import DataLoader class MyModel(torch.nn.Module): r""" a class used as neural network modek """ def __init__(self): super(MyModel, self).__init__() self.conv1 = torch.nn.Conv2d(in_channels=3, out_channels=1, kernel_size=(4, 4), stride=1, padding=1) self.conv2 = torch.nn.Conv2d(in_channels=1, out_channels=3, kernel_size=(3, 3), stride=2, padding=1) def forward(self, x): x = self.conv1(x) x = self.conv2(x) return x data_path = "../../data_cifar10" dataset_test = torchvision.datasets.CIFAR10(data_path, train=False, transform=torchvision.transforms.ToTensor(), download=True) data_loader = DataLoader(dataset_test, batch_size=64, shuffle=True, drop_last=False) log_path = "../../logs" writer = SummaryWriter(log_dir=log_path) step = 0 my_model = MyModel() step_list = [] for imgs, targets in data_loader: step_list.append(step) writer.add_images("original", imgs, step) imgs = my_model(imgs) writer.add_images("conventional", imgs, step) step += 1 writer.close() print(step_list)
author:luckylight(xyg)
date: 2021/11/11