pytorch torchvision.transforms.Normalize()源碼

代碼get_mean_std.py:

# coding:utf-8
import os
import numpy as np
from torchvision.datasets import ImageFolder
import torchvision.transforms as transforms
from dataloader import Dataloader
from options import options
import pickle
"""
    在訓練前先運行該函數獲得數據的均值和標準差
"""

class Dataloader():
    def __init__(self, opt):
        # 訓練,驗證,測試數據集文件夾名
        self.opt = opt
        self.dirs = ['train', 'test', 'testing']

        self.means = [0, 0, 0]
        self.stdevs = [0, 0, 0]

        self.transform = transforms.Compose([transforms.Resize(opt.isize),
                                        transforms.CenterCrop(opt.isize),
                                        transforms.ToTensor(),#數據值從[0,255]範圍轉爲[0,1],相當於除以255操作
                                        # transforms.Normalize((0.485,0.456,0.406), (0.229,0.224,0.225))
                                        ])

        # 因爲這裏使用的是ImageFolder,按文件夾給數據分類,一個文件夾爲一類,label會自動標註好
        self.dataset = {x: ImageFolder(os.path.join(opt.dataroot, x), self.transform) for x in self.dirs}


    def get_mean_std(self, type, mean_std_path):
        """
        計算數據集的均值和標準差
        :param type: 使用的是那個數據集的數據,有'train', 'test', 'testing'
        :param mean_std_path: 計算出來的均值和標準差存儲的文件
        :return: 
        """
        num_imgs = len(self.dataset[type])
        for data in self.dataset[type]:
            img = data[0]
            for i in range(3):
                # 一個通道的均值和標準差
                self.means[i] += img[i, :, :].mean()
                self.stdevs[i] += img[i, :, :].std()


        self.means = np.asarray(self.means) / num_imgs
        self.stdevs = np.asarray(self.stdevs) / num_imgs

        print("{} : normMean = {}".format(type, self.means))
        print("{} : normstdevs = {}".format(type, self.stdevs))
        
        # 將得到的均值和標準差寫到文件中,之後就能夠從中讀取
        with open(mean_std_path, 'wb') as f:
            pickle.dump(self.means, f)
            pickle.dump(self.stdevs, f)
            print('pickle done')

if __name__ == '__main__':
    opt = options().parse()
    dataloader = Dataloader(opt)
    for x in dataloader.dirs:
        mean_std_path = 'mean_std_value_' + x + '.pkl'
        dataloader.get_mean_std(x, mean_std_path)

然後再從相應的文件讀取均值和標準差放到dataloader.py的transforms.Normalize函數中即可:

# coding:utf-8
import os
import torch
import torchvision.transforms as transforms
from torchvision.datasets import ImageFolder
import numpy as np
import pickle


"""
    用於加載訓練train、驗證test和測試數據testing
"""

class Dataloader():
    def __init__(self, opt):
        # 訓練,驗證,測試數據集文件夾名
        self.opt = opt
        self.dirs = ['train', 'test', 'testing']
        # 均值和標準差存儲的文件路徑
        self.mean_std_path = {x: 'mean_std_value_' + x + '.pkl' for x in self.dirs}

        # 初始化爲0
        self.means = {x: [0, 0, 0] for x in self.dirs}
        self.stdevs = {x: [0, 0, 0] for x in self.dirs}
        print(type(self.means['train']))
        print(self.means)
        print(self.stdevs)

        for x in self.dirs:
            #如果存在則說明之前有獲取過均值和標準差
            if os.path.exists(self.mean_std_path[x]):
                with open(self.mean_std_path[x], 'rb') as f:
                    self.means[x] = pickle.load(f)
                    self.stdevs[x] = pickle.load(f)
                    print('pickle load done')

        print(self.means)
        print(self.stdevs)
        # 將相應的均值和標準差設置到transforms.Normalize函數中
        self.transform = {x: transforms.Compose([transforms.Resize(opt.isize),
                                        transforms.CenterCrop(opt.isize),
                                        transforms.ToTensor(),
                                        transforms.Normalize(self.means[x], self.stdevs[x]),
                                        ]) for x in self.dirs}
...
from . import functional as F

class Normalize(object):
    """Normalize a tensor image with mean and standard deviation.
    Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
    will normalize each channel of the input ``torch.*Tensor`` i.e.
    ``input[channel] = (input[channel] - mean[channel]) / std[channel]``

    Args:
        mean (sequence): Sequence of means for each channel.
        std (sequence): Sequence of standard deviations for each channel.
    """

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

    def __call__(self, tensor):
        """
        Args:
            tensor (Tensor): Tensor image of size (C, H, W) to be normalized.

        Returns:
            Tensor: Normalized Tensor image.
        """
        return F.normalize(tensor, self.mean, self.std)

    def __repr__(self):
        return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)

可見相當於調用F.normalize()方法

def normalize(tensor, mean, std, inplace=False):
    """Normalize a tensor image with mean and standard deviation.

    .. note::
        This transform acts out of place by default, i.e., it does not mutates the input tensor.

    See :class:`~torchvision.transforms.Normalize` for more details.

    Args:
        tensor (Tensor): Tensor image of size (C, H, W) to be normalized.
        mean (sequence): Sequence of means for each channel.
        std (sequence): Sequence of standard deviations for each channel.
        inplace(bool,optional): Bool to make this operation inplace.

    Returns:
        Tensor: Normalized Tensor image.
    """
    if not _is_tensor_image(tensor):
        raise TypeError('tensor is not a torch image.')

    if not inplace:
        tensor = tensor.clone()

    dtype = tensor.dtype
    mean = torch.as_tensor(mean, dtype=dtype, device=tensor.device)
    std = torch.as_tensor(std, dtype=dtype, device=tensor.device)
    tensor.sub_(mean[:, None, None]).div_(std[:, None, None])
    return tensor

F.normalize()方法就是在執行 (x-mean)/std

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章