PyTorch学习笔记——图像处理(transforms.Normalize 归一化)

Posted CV-杨帆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyTorch学习笔记——图像处理(transforms.Normalize 归一化)相关的知识,希望对你有一定的参考价值。

PyTorch学习笔记——图像处理 transforms.Normalize 归一化

回顾 torchvision.ToTensor

在看这一片博客之前,需要先浏览以下我的上一篇博客
PyTorch学习笔记——图像处理(torchvision.ToTensor)

import torchvision.transforms as transforms
import numpy as np
import torch
import cv2
from matplotlib import pyplot as plt
import matplotlib.image as imgplt

img_path = "1.jpg"

# transforms.ToTensor()
transform1 = transforms.Compose([
    transforms.ToTensor(), # range [0, 255] -> [0.0,1.0]
    ]
)

##numpy.ndarray
img = cv2.imread(img_path)# 读取图像
img1 = transform1(img) # 归一化到 [0.0,1.0]
print("img.shape = ",img.shape)
print("img1.shape = ",img1.shape)

print("img = ",img)
print("img1 = ",img1)

img.shape =  (424, 640, 3)
img1.shape =  torch.Size([3, 424, 640])
img =  [[[249 246 231]
  [248 246 228]
  [247 245 227]
  ...
  [ 17  19  20]
  [ 20  19  21]
  [ 32  14  21]]

 [[249 246 231]
  [248 246 228]
  [247 245 227]
  ...
  [ 29  30  20]
  [ 29  30  20]
  [ 15  24  14]]

 [[248 245 230]
  [248 245 230]
  [248 246 228]
  ...
  [ 80  41  26]
  [ 80  41  26]
  [ 45  36  16]]

 ...

 [[169 175 216]
  [174 178 219]
  [167 173 210]
  ...
  [ 14  17  22]
  [ 14  17  22]
  [ 15  18  23]]

 [[151 157 202]
  [163 167 208]
  [172 174 209]
  ...
  [ 14  17  22]
  [ 13  16  21]
  [ 13  16  21]]

 [[168 171 215]
  [173 174 212]
  [179 177 207]
  ...
  [ 13  16  21]
  [ 12  15  20]
  [ 11  13  21]]]
img1 =  tensor([[[0.9765, 0.9725, 0.9686,  ..., 0.0667, 0.0784, 0.1255],
         [0.9765, 0.9725, 0.9686,  ..., 0.1137, 0.1137, 0.0588],
         [0.9725, 0.9725, 0.9725,  ..., 0.3137, 0.3137, 0.1765],
         ...,
         [0.6627, 0.6824, 0.6549,  ..., 0.0549, 0.0549, 0.0588],
         [0.5922, 0.6392, 0.6745,  ..., 0.0549, 0.0510, 0.0510],
         [0.6588, 0.6784, 0.7020,  ..., 0.0510, 0.0471, 0.0431]],

        [[0.9647, 0.9647, 0.9608,  ..., 0.0745, 0.0745, 0.0549],
         [0.9647, 0.9647, 0.9608,  ..., 0.1176, 0.1176, 0.0941],
         [0.9608, 0.9608, 0.9647,  ..., 0.1608, 0.1608, 0.1412],
         ...,
         [0.6863, 0.6980, 0.6784,  ..., 0.0667, 0.0667, 0.0706],
         [0.6157, 0.6549, 0.6824,  ..., 0.0667, 0.0627, 0.0627],
         [0.6706, 0.6824, 0.6941,  ..., 0.0627, 0.0588, 0.0510]],

        [[0.9059, 0.8941, 0.8902,  ..., 0.0784, 0.0824, 0.0824],
         [0.9059, 0.8941, 0.8902,  ..., 0.0784, 0.0784, 0.0549],
         [0.9020, 0.9020, 0.8941,  ..., 0.1020, 0.1020, 0.0627],
         ...,
         [0.8471, 0.8588, 0.8235,  ..., 0.0863, 0.0863, 0.0902],
         [0.7922, 0.8157, 0.8196,  ..., 0.0863, 0.0824, 0.0824],
         [0.8431, 0.8314, 0.8118,  ..., 0.0824, 0.0784, 0.0824]]])

这是上一篇的代码,可以看到,使用transforms.ToTensor将图像转化为tensor

归一化 transforms.Normalize

transform1 = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean = (0.5, 0.5, 0.5), std = (0.5, 0.5, 0.5))
    ]
)

将源代码修改为现在这样

img_path = "1.jpg"

# transforms.ToTensor()
transform2 = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean = (0.5, 0.5, 0.5), std = (0.5, 0.5, 0.5))
    ]
)

##numpy.ndarray
import torchvision.transforms as transforms
import numpy as np
import torch
import cv2
from matplotlib import pyplot as plt
import matplotlib.image as imgplt
img_path = "1.jpg"

# transforms.ToTensor()
transform1 = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize(mean = (0.5, 0.5, 0.5), std = (0.5, 0.5, 0.5))
    ]
)

##numpy.ndarray
img = cv2.imread(img_path)# 读取图像
img1 = transform1(img) # 归一化到 [0.0,1.0]
print("img.shape = ",img.shape)
print("img1.shape = ",img1.shape)

print("img = ",img)
print("img1 = ",img1)

img.shape =  (424, 640, 3)
img1.shape =  torch.Size([3, 424, 640])
img =  [[[249 246 231]
  [248 246 228]
  [247 245 227]
  ...
  [ 17  19  20]
  [ 20  19  21]
  [ 32  14  21]]

 [[249 246 231]
  [248 246 228]
  [247 245 227]
  ...
  [ 29  30  20]
  [ 29  30  20]
  [ 15  24  14]]

 [[248 245 230]
  [248 245 230]
  [248 246 228]
  ...
  [ 80  41  26]
  [ 80  41  26]
  [ 45  36  16]]

 ...

 [[169 175 216]
  [174 178 219]
  [167 173 210]
  ...
  [ 14  17  22]
  [ 14  17  22]
  [ 15  18  23]]

 [[151 157 202]
  [163 167 208]
  [172 174 209]
  ...
  [ 14  17  22]
  [ 13  16  21]
  [ 13  16  21]]

 [[168 171 215]
  [173 174 212]
  [179 177 207]
  ...
  [ 13  16  21]
  [ 12  15  20]
  [ 11  13  21]]]
img1 =  tensor([[[ 0.9529,  0.9451,  0.9373,  ..., -0.8667, -0.8431, -0.7490],
         [ 0.9529,  0.9451,  0.9373,  ..., -0.7725, -0.7725, -0.8824],
         [ 0.9451,  0.9451,  0.9451,  ..., -0.3725, -0.3725, -0.6471],
         ...,
         [ 0.3255,  0.3647,  0.3098,  ..., -0.8902, -0.8902, -0.8824],
         [ 0.1843,  0.2784,  0.3490,  ..., -0.8902, -0.8980, -0.8980],
         [ 0.3176,  0.3569,  0.4039,  ..., -0.8980, -0.9059, -0.9137]],

        [[ 0.9294,  0.9294,  0.9216,  ..., -0.8510, -0.8510, -0.8902],
         [ 0.9294,  0.9294,  0.9216,  ..., -0.7647, -0.7647, -0.8118],
         [ 0.9216,  0.9216,  0.9294,  ..., -0.6784, -0.6784, -0.7176],
         ...,
         [ 0.3725,  0.3961,  0.3569,  ..., -0.8667, -0.8667, -0.8588],
         [ 0.2314,  0.3098,  0.3647,  ..., -0.8667, -0.8745, -0.8745],
         [ 0.3412,  0.3647,  0.3882,  ..., -0.8745, -0.8824, -0.8980]],

        [[ 0.8118,  0.7882,  0.7804,  ..., -0.8431, -0.8353, -0.8353],
         [ 0.8118,  0.7882,  0.7804,  ..., -0.8431, -0.8431, -0.8902],
         [ 0.8039,  0.8039,  0.7882,  ..., -0.7961, -0.7961, -0.8745],
         ...,
         [ 0.6941,  0.7176,  0.6471,  ..., -0.8275, -0.8275, -0.8196],
         [ 0.5843,  0.6314,  0.6392,  ..., -0.8275, -0.8353, -0.8353],
         [ 0.6863,  0.6627,  0.6235,  ..., -0.8353, -0.8431, -0.8353]]])

可以看到,最后的数组,值的范围变成了[-1,1]

公式

transforms.Normalize使用如下公式进行归一化:

channel=(channel-mean)/std
也就是说 ( (0,1) - 0.5 ) / 0.5 = (-1,1)

以上是关于PyTorch学习笔记——图像处理(transforms.Normalize 归一化)的主要内容,如果未能解决你的问题,请参考以下文章

pytorch图像基本操作

PyTorch学习笔记 ---------数据集的简单创建

pytorch学习笔记:全连接层处理图像分类问题

学习笔记:深度学习——基于PyTorch的BERT应用实践

pytorch学习笔记第五篇——训练分类器

Pytorch Dataset和Dataloader 学习笔记