pytorch a to z - Serbipunk/notes GitHub Wiki

OSError: [Errno 24] Too many open files

https://discuss.pytorch.org/t/too-many-open-files-when-using-dataloader/9476

import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')

presision

torch.set_printoptions(precision=10)

sequential

A sequential container.
    Modules will be added to it in the order they are passed in the constructor. 模块被集合加入其中,作为constructor的输入
    Alternatively, an ordered dict of modules can also be passed in. 这种模式之外,ordered dict也可以作为输入

bmm

randn

randn(...)
    randn(*sizes, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
    
    Returns a tensor filled with random numbers from **a normal distribution
    with mean `0` and variance `1` (also called the standard normal
    distribution).**

grid_sample

grid_sample(input, grid, mode='bilinear', padding_mode='zeros')

warped_cloth = F.grid_sample(c, grid, padding_mode='border')

load saved model

https://github.com/soeaver/Parsing-R-CNN/blob/master/utils/checkpointer.py#L21

def load_weights(model, weights_path, local_rank=0):
    try:
        weights_dict = torch.load(weights_path, map_location=torch.device("cpu"))['model']
    except:
        weights_dict = torch.load(weights_path, map_location=torch.device("cpu"))
    weights_dict = strip_prefix_if_present(weights_dict, prefix='module.')
    model_state_dict = model.state_dict()
    model_state_dict, mismatch_keys = align_and_update_state_dicts(model_state_dict, weights_dict, -1)
    model.load_state_dict(model_state_dict)
    logging_rank('The mismatch keys: {}.'.format(list(mismatch_params_filter(sorted(mismatch_keys)))),
                 local_rank=local_rank)
    logging_rank('Loading from weights: {}.'.format(weights_path), local_rank=local_rank)

torch.from_numpy

https://github.com/soeaver/Parsing-R-CNN/blob/master/rcnn/modeling/model_builder.py#L28

torch.from_numpy

p.requires_grad

https://github.com/soeaver/Parsing-R-CNN/blob/master/rcnn/modeling/model_builder.py#L73

pooling

torch.nn.MaxPool2d()

dialated pooling

torch.nn.max_pool2d(data_in, kernel_size=4, stride=2)

https://blog.csdn.net/m0_46653437/article/details/111475541

affine_grid

affine_grid(theta, size)
    Generates a 2d flow field, given a batch of affine matrices :attr:`theta`.
    Generally used in conjunction with :func:`grid_sample` to
    implement Spatial Transformer Networks.
    根据2d输入特征,根据theta矩阵,经常和 grid_sample 同时使用
    
    Args:
        theta (Tensor): input batch of affine matrices (:math:`N \times 2 \times 3`)
        size (torch.Size): the target output image size (:math:`N \times C \times H \times W`).
            Example: torch.Size((32, 3, 24, 24))
    
    Returns:
        output (Tensor): output Tensor of size (:math:`N \times H \times W \times 2`)

https://www.cnblogs.com/zi-wang/p/9950917.html

大概是对Tensor进行Crop的方法,可在GPU中实现。

  • affine_grid -> 生成二维网格。 affine_grid 的输入是仿射矩阵(Nx2x3)和输出Tensor的尺寸(Tensor.Size(NxHxWx2)),输出的是归一化的二维网格。

  • grid_sample -> 对网格进行双现行采样。 grid_sample函数中将图像坐标归一化到[−1,1],其中0对应-1,width-1对应1。

crossEntropyLoss

https://mathpretty.com/12068.html

torch.permute() and np.transpose()

https://blog.csdn.net/qq_34806812/article/details/89385831

multi-gpu

https://pytorch.org/tutorials/intermediate/ddp_tutorial.html (ddp)

https://www.youtube.com/watch?v=RQfK_ViGzH0 (ddp + rpc)