Pytorch Torchvision规范化-它如何在均值/标准差元组上运行?

Pytorch Torchvision规范化-它如何在均值/标准差元组上运行?,pytorch,torchvision,Pytorch,Torchvision,我不明白torchvision的这种转变是如何运作的。最终,我想构建一个定制的规范化类,所以我需要首先弄清楚它是如何工作的 在文档中,它这样描述init: def __init__(self, mean, std, inplace=False): self.mean = mean self.std = std self.inplace = inplace class Normalize(object): """Convert ndarra

我不明白torchvision的这种转变是如何运作的。最终,我想构建一个定制的规范化类,所以我需要首先弄清楚它是如何工作的

在文档中,它这样描述init:

def __init__(self, mean, std, inplace=False):
        self.mean = mean
        self.std = std
        self.inplace = inplace
class Normalize(object):
    """Convert ndarrays in sample to Tensors."""

    def __init__(self, mean, std, inplace=False):
        self.mean = mean
        self.std = std
        self.inplace = inplace

    def __call__(self, sample):
        image, landmarks = sample['image'], sample['landmarks']
        return {'image': F.normalize(image, self.mean, self.std, self.inplace),
                'landmarks': landmarks}
当我传递这些参数时,通常(不是自定义类),我会将它们作为每个通道的列表或元组传递:

transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])

但如果我看一下电话:

返回F.normalize(张量、self.mean、self.std、self.inplace)

所有这些都将元组传递给F.normalize(),它只接受p参数的单个值

该类必须以某种方式通过通道进行迭代,以允许实现该功能,但它是如何实现的,以及如何在自定义类中实现它

基于此,我将这样描述:

def __init__(self, mean, std, inplace=False):
        self.mean = mean
        self.std = std
        self.inplace = inplace
class Normalize(object):
    """Convert ndarrays in sample to Tensors."""

    def __init__(self, mean, std, inplace=False):
        self.mean = mean
        self.std = std
        self.inplace = inplace

    def __call__(self, sample):
        image, landmarks = sample['image'], sample['landmarks']
        return {'image': F.normalize(image, self.mean, self.std, self.inplace),
                'landmarks': landmarks}

但是这不起作用,因为它不经过每个通道。

在那里调用的
normalize
函数就是这个

输入是形状的张量
(C,H,W)
平均值
std
可以是内部转换为张量的序列。通过广播实现标准化:


tensor.sub_(mean[:,None,None]).div_(std[:,None,None])

噢,哇,这太令人困惑了,因为torch有自己的函数类,通常被称为
F。以同样的方式规范化
。谢谢,
torchvision
torch
都有自己的功能界面。