How transforms.normalize sets standardized parameters for a specific data set

In image classification experiments, it is often seen that data enhancement operations are performed on datasets, including transforms.Normalize(). This function is defined as follows:

torchvision.transforms.Normalize(mean, std, inplace=False)

Function: Normalize images for RGB3 channels respectively

output = ( input - mean ) / std

  • mean: the mean of each channel
  • std: standard deviation of each channel
  • inplace: Whether to operate in place

Usually ImageNet has its own standardized parameters, which are obtained by sampling the mean and variance of images, so how to obtain suitable parameters for local specific data sets? I refer to PyTorch data normalization processing: transforms.Normalize and calculate the mean and variance of the image dataset_Zizhi's Blog-CSDN Blog_pytorch Data Normalization

There is an error in the original code, you need to set transform to transforms.ToTensor() instead of None, otherwise it will run wrong. Here is the corrected code:

def getStat(train_data):
    '''
    Compute mean and variance for training data
    :param train_data: 自定义类Dataset(或ImageFolder即可)
    :return: (mean, std)
    '''
    print('Compute mean and variance for training data.')
    print(len(train_data))
    train_loader = torch.utils.data.DataLoader(
        train_data, batch_size=1, shuffle=False, num_workers=0,
        pin_memory=True)
    mean = torch.zeros(3)
    std = torch.zeros(3)
    for X, _ in train_loader:
        for d in range(3):
            mean[d] += X[:, d, :, :].mean()
            std[d] += X[:, d, :, :].std()
    mean.div_(len(train_data))
    std.div_(len(train_data))
    return list(mean.numpy()), list(std.numpy())


if __name__ == '__main__':
    train_dataset = ImageFolder(root=r'/data1/sharedata/leafseg/', transform=transforms.ToTensor())
    print(getStat(train_dataset))

Compute mean and variance for training data.
3257
([0.059938803, 0.08676067, 0.041085023], [0.10522498, 0.1488454, 0.07508467])

Just write the result to the transform list. 

data_transforms = {
    'train': transforms.Compose([
        transforms.Resize(640),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.0599, 0.0868, 0.0411], [0.1052, 0.1488, 0.0751])
    ]),
    'val': transforms.Compose([
        transforms.Resize(640),
        transforms.ToTensor(),
        transforms.Normalize([0.0599, 0.0868, 0.0411], [0.1052, 0.1488, 0.0751])
    ]),
}

Guess you like

Origin blog.csdn.net/u013685264/article/details/126764095