Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Image convolution Operation of pytorch Neural Network in Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Python pytorch neural network image convolution operation example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "Python pytorch neural network image convolution operation example analysis" this article.

Cross-correlation operation

Strictly speaking, convolution layer is a wrong name because the operation it expresses is actually a cross-correlation operation, not a convolution operation. In the convolution layer, the input tensor and the kernel tensor generate the output tensor by cross-correlation operation.

First of all, let's ignore the channel (third dimension) for the time being and see how to deal with two-dimensional image data and hidden representation. In the following illustration, the input is a two-dimensional tensor with a height of 3 and a width of 3 (that is, the shape is 3 × 3 3\ times3 3 × 3). The height and width of the convolution core are both 2.

Note that the output size is slightly smaller than the input size. This is because we need enough space to "move" the convolution kernel on the image. Later, we'll see how to keep the output size constant by filling zeros around the image boundary to ensure that there is enough space to move the kernel.

Def corr2d (X, K): "calculates dimensional cross-correlation operations." H, w = K.shape Y = torch.zeros ((X.shape [0]-h + 1, X.shape [1]-w + 1)) for i in range (Y.shape [0]): for j in range (Y.shape [1]): y [I, j] = (X [iX.shape I + h, sum j + w] * K) .sum () return Y convolution layer

The convolution layer performs cross-correlation between the input and the convolution kernel weight, and produces output after adding a scalar offset. Therefore, the two trained parameters in the convolution layer are convolution kernel weight kernel scalar bias. Just as we randomly initialize the full connection layer before, when we train the convolution layer-based model, we also randomly initialize the convolution kernel weight.

Implement the 2D convolution layer based on the corr2d function defined above. In the _ _ init__ constructor, declare weight and bias as two model parameters. The forward propagation function calls the corr2d function and adds an offset.

Class Conv2D (nn.Module): def _ init__ (self, kernel_size): super (). _ _ init__ () self.weight = nn.Parameter (torch.rand (kernel_size)) self.bias = nn.Parameter (torch.zeros (1)) def forward (self, x): return corr2d (x, self.weight) + self.bias

The convolution kernels with h and w height and width respectively can be called h × w convolution kernels or h × w convolution kernels. We also call the convolution layer with h × w convolution kernel h × w convolution layer.

Feature mapping

The convolution layer output in the following figure is sometimes referred to as feature mapping (Feature Map) because it can be seen as a converter for input mapping to the spatial dimensions of the next layer. In CNN, for any element of a layer x x x, the receptive field (Receptive Field) refers to all the elements (from all previous layers) that may affect the x x x calculation during forward propagation.

Note that the coverage of the receptive field may be greater than the actual area size of a layer input.

For example, the above image: given a 2 × 2 convolution kernel, the receiving domain of the shadow output element value 19 is the four elements of the shadow part. Suppose the previous output is Y with a size of 2 × 2, and now we add a convolution layer that takes Y as input and outputs a single element z. In this case, the receive field of z on Y includes all four elements of Y, while the input receptive field includes all the initial nine input elements.

Therefore, when any element in a feature graph needs to detect the input features of a wider area, we can build a deeper network.

The above is all the contents of the article "example Analysis of pytorch Neural Network Image convolution Operation in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report