SoFunction
Updated on 2024-10-30

pytorch view channel number dimension size size way

View tensor

# Size
() # Shape
# Dimension

for example

import torch
parser = (description='PyTorch')
parser.add_argument('--img_w', default=144, type=int, metavar='imgw', help='img width')
parser.add_argument('--img_h', default=288, type=int, metavar='imgh', help='img height')
parser.add_argument('--batch-size', default=32, type=int,metavar='B', help='training batch size')
parser.add_argument('--test-batch', default=64, type=int, metavar='tb', help='testing batch size')
 
class net():
 def __init__(self, arch='resnet18'):
 super(net, self).__init__()
 model_ft = models.resnet50(pretrained=True)
  = model_ft
 
 def forward(self, x):
 print()
 print(())
 print()
 
 input = .conv1(x)
 
 print()
 print(())
 print()

From left to right, 32 indicates the training set batch_size size, 3 is the number of image channels, 288 is the height of the image, 144 is the width of the image, the image size 288*144, and the number of dimensions is 4. Many blogs do not accurately express the names of these parameters when presenting their papers, and they tend to appear to be open-ended, which leads to readers' subsequent use of the process by the teacher ma

After the first layer of convolution of the standard resnet50, the result is: 32 indicates the training set batch_size size, 64 is the number of image channels, 72 is the height of the image, 36 is the width of the image, and the image size is 72*36, and the dimension is 4

Additional knowledge:A few dimension/transformation related functions in pytorch

()

Let's start with the () function, because this is what all the later methods use to see the dimension of the transformed matrix

With this method, you can view the dimension of the current Tensor, and the usage is simple:

>>>import torch
>>>a = ([[[1, 2, 3], [4, 5, 6]]])
>>>()
([1, 2, 3])

()

Explanation in the official documentation:

Simply put, take the original tensor size and change it to the size you want, e.g. if the original size was 23, you can now change it to 32 or 16, etc., but make sure that the equation is valid and that you can't have a target size of 33!

In addition, it is also possible to set one of the sizes to -1, indicating that the machine calculates itself internally, but only one of them can be -1 at the same time, as used below:

>>> b=(-1, 3, 2)
>>> b
tensor([[[1., 2.],
  [3., 4.],
  [5., 6.]]])
>>> ()
([1, 3, 2])

() / ()

The (n) function denotes the number of dimensions in the compressed tensor whose nth dimension is 1. For example, for the first one below, (2).size(), the original b is ([1, 3, 2]) as above, and the second dimension is 2 ≠ 1, so it is not compressed, and the dimensions are kept unchanged; whereas, if (0).size(), it is found to be 1 in the first dimension, and therefore compressed to a 3x2 tensor

>>> (2).size()
([1, 3, 2])
>>> (0).size()
([3, 2])

Conversely, (n) adds a dimension = 1 to the nth dimension, as follows, indicating that adding one dimension to the second dimension of the original b changes the dimension to 1 * 3 * 1 * 2

>>> (2).size()
([1, 3, 1, 2])
>>> (2)
tensor([[[[1., 2.]],

  [[3., 4.]],

  [[5., 6.]]]])

()

This function indicates that the original tensor, in accordance with their desired position reordering, for example, the original tensor of the 0th, 1th, 2nd dimension were 1, 3, 2, then when I perform permute(2, 0, 1), the third dimension will be placed in the forefront, the first dimension is placed in the middle, and the second dimension is placed in the end, which also becomes 2 * 1 * 3, note that the number of dimensions here expressed by the index, not the number of dimensions:

>>> (2, 0, 1).size()
([2, 1, 3])
>>> (2, 0, 1)
tensor([[[1., 3., 5.]],

 [[2., 4., 6.]]])

This is all I can think of for now, please correct me if there are any errors, or if there are other related functions, I will continue to update them.

This pytorch view channel number dimension size way above is all I have shared with you, I hope it can give you a reference, and I hope you can support me more.