pytorch常见函数

1. torch.clamp

  • 作用:将所有的值放到[min,max]之间
torch.clamp(input, min=None, max=None, *, out=None) → Tensor

f ( x ) = { m i n x < m i n x m i n < x < m a x m a x x > m a x (1) f(x)=\left\{ \begin{aligned} & min && x<min\\ & x&&min<x<max \\ & max && x>max \end{aligned} \right. \tag{1} f(x)=minxmaxx<minmin<x<maxx>max(1)

  • 代码
import torch

x = torch.randint(low=1,high=10,size=(3,4))
print(f'x={x}')
# 我们设置的最小值为4,最大值为 8
y = torch.clamp(x,4,8)
print(f'y={y}')
print(x == y)
  • 结果
x=tensor([[8, 6, 2, 3],
        [5, 5, 6, 1],
        [8, 6, 3, 3]])
y=tensor([[8, 6, 4, 4],
        [5, 5, 6, 4],
        [8, 6, 4, 4]])
tensor([[ True,  True, False, False],
        [ True,  True,  True, False],
        [ True,  True, False, False]])

2. torchvision.transforms.ToTensor()

转换PIL图像或numpy.ndarray张量。此转换不支持torchscript.比如我们用opencv读取得到的数据为size=[height,width,channels],一般channels=3;0 <= height <= 255; 0 <=width<= 255;我们通过ToTensor;分两步;

  • 将张量的通道数放在前面变成大小为[channels,height,width]
  • 将张量里面的每个值都乘以255
  • 代码
# -*- coding: utf-8 -*-
# @Project: zc
# @Author: zc
# @File name: torch.clamp_test
# @Create time: 2022/1/7 10:37
import cv2 as cv
import os
import torchvision
import torch

path = os.path.join(os.getcwd(), 'img', 'catdog.jpg')
img = cv.imread(path)

print(f'img.shape={img.shape}')
print(f'type(img)={type(img)}')
print(f'img={img}')
torchvision_tensor = torchvision.transforms.ToTensor()(img)
print(f'torchvision_tensor.shape={torchvision_tensor.shape}')
print(f'type(torchvision_tensor)={type(torchvision_tensor)}')
print(f'torchvision_tensor={torchvision_tensor}')
  • 结果
img.shape=(825, 1045, 3)
type(img)=<class 'numpy.ndarray'>
img=[[[ 66  54  43]
  [ 66  54  43]
  [ 66  54  43]
  ...
  [ 66  54  43]
  [ 66  54  43]
  [ 66  54  43]]

 [[ 66  54  43]
  [ 66  54  43]
  [ 66  54  43]
  ...
  [ 66  54  43]
  [ 66  54  43]
  [ 66  54  43]]

 [[ 66  54  43]
  [ 66  54  43]
  [ 66  54  43]
  ...
  [ 66  54  43]
  [ 66  54  43]
  [ 66  54  43]]

 ...

 [[167 161 155]
  [167 161 155]
  [167 161 155]
  ...
  [167 161 155]
  [167 161 155]
  [167 161 155]]

 [[167 161 155]
  [167 161 155]
  [167 161 155]
  ...
  [167 161 155]
  [167 161 155]
  [167 161 155]]

 [[167 161 155]
  [167 161 155]
  [167 161 155]
  ...
  [167 161 155]
  [167 161 155]
  [167 161 155]]]
torchvision_tensor.shape=torch.Size([3, 825, 1045])
type(torchvision_tensor)=<class 'torch.Tensor'>
torchvision_tensor=tensor([[[0.2588, 0.2588, 0.2588,  ..., 0.2588, 0.2588, 0.2588],
         [0.2588, 0.2588, 0.2588,  ..., 0.2588, 0.2588, 0.2588],
         [0.2588, 0.2588, 0.2588,  ..., 0.2588, 0.2588, 0.2588],
         ...,
         [0.6549, 0.6549, 0.6549,  ..., 0.6549, 0.6549, 0.6549],
         [0.6549, 0.6549, 0.6549,  ..., 0.6549, 0.6549, 0.6549],
         [0.6549, 0.6549, 0.6549,  ..., 0.6549, 0.6549, 0.6549]],

        [[0.2118, 0.2118, 0.2118,  ..., 0.2118, 0.2118, 0.2118],
         [0.2118, 0.2118, 0.2118,  ..., 0.2118, 0.2118, 0.2118],
         [0.2118, 0.2118, 0.2118,  ..., 0.2118, 0.2118, 0.2118],
         ...,
         [0.6314, 0.6314, 0.6314,  ..., 0.6314, 0.6314, 0.6314],
         [0.6314, 0.6314, 0.6314,  ..., 0.6314, 0.6314, 0.6314],
         [0.6314, 0.6314, 0.6314,  ..., 0.6314, 0.6314, 0.6314]],

        [[0.1686, 0.1686, 0.1686,  ..., 0.1686, 0.1686, 0.1686],
         [0.1686, 0.1686, 0.1686,  ..., 0.1686, 0.1686, 0.1686],
         [0.1686, 0.1686, 0.1686,  ..., 0.1686, 0.1686, 0.1686],
         ...,
         [0.6078, 0.6078, 0.6078,  ..., 0.6078, 0.6078, 0.6078],
         [0.6078, 0.6078, 0.6078,  ..., 0.6078, 0.6078, 0.6078],
         [0.6078, 0.6078, 0.6078,  ..., 0.6078, 0.6078, 0.6078]]])

3. torchvision.transforms.Normalize

用均值和标准差对张量图像进行归一化。这个转换不支持PIL图像。给定n个通道的均值:([1]的均值,…,均值[n])和std:([1]的均值,…,std[n]),这个转换将标准化输入火炬的每个通道。*张量,即输出[通道]=(输入[通道]-平均[通道])/ std[通道];即在给定一个均值和方差的时候,我们可以根据给定的均值和方差,将里面的每个通道里面的每个元素进行归一化处理。
o u t p u t _ c h a n n e l = i n p u t _ c h a n n e l − m e a n ( c h a n n e l ) s t d ( c h a n n e l ) (2) output\_channel=\frac{input\_channel-mean(channel)}{std(channel)}\tag{2} output_channel=std(channel)input_channelmean(channel)(2)

  • 代码
import torch
import torchvision

x = torch.arange(60, dtype=torch.float).reshape(3, 4, 5)
y = torchvision.transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[2, 2, 2])(x)
print(f'x={x}')
print(f'x_shape={x.shape}')
print(f'x[2,3,4]={x[2, 3, 4]}')
y_calculate = (x[2, 3, 4] - 0.5) / 2
print(f'y_calculate={y_calculate}')
print(f'y[2,3,4]={y[2, 3, 4]}')
print(f'y[2,3,4] = y_calculate is  ={y[2, 3, 4] == y_calculate}')
print(f'y={y}')
print(f'y_shape={y.shape}')
  • 分析
    我们先生成一个张量,并把最后一个值 y[2,3,4]=59抽出来手动计算得到如下:
    59 − 0.5 2 = 29.25 (3) \frac{59-0.5}{2}=29.25\tag{3} 2590.5=29.25(3)
    我们发现通过 Normalize 得到值跟手动计算的值一致。
  • 结果
x=tensor([[[ 0.,  1.,  2.,  3.,  4.],
         [ 5.,  6.,  7.,  8.,  9.],
         [10., 11., 12., 13., 14.],
         [15., 16., 17., 18., 19.]],

        [[20., 21., 22., 23., 24.],
         [25., 26., 27., 28., 29.],
         [30., 31., 32., 33., 34.],
         [35., 36., 37., 38., 39.]],

        [[40., 41., 42., 43., 44.],
         [45., 46., 47., 48., 49.],
         [50., 51., 52., 53., 54.],
         [55., 56., 57., 58., 59.]]])
x_shape=torch.Size([3, 4, 5])
x[2,3,4]=59.0
y_calculate=29.25
y[2,3,4]=29.25
y[2,3,4] = y_calculate is  =True
y=tensor([[[-0.2500,  0.2500,  0.7500,  1.2500,  1.7500],
         [ 2.2500,  2.7500,  3.2500,  3.7500,  4.2500],
         [ 4.7500,  5.2500,  5.7500,  6.2500,  6.7500],
         [ 7.2500,  7.7500,  8.2500,  8.7500,  9.2500]],

        [[ 9.7500, 10.2500, 10.7500, 11.2500, 11.7500],
         [12.2500, 12.7500, 13.2500, 13.7500, 14.2500],
         [14.7500, 15.2500, 15.7500, 16.2500, 16.7500],
         [17.2500, 17.7500, 18.2500, 18.7500, 19.2500]],

        [[19.7500, 20.2500, 20.7500, 21.2500, 21.7500],
         [22.2500, 22.7500, 23.2500, 23.7500, 24.2500],
         [24.7500, 25.2500, 25.7500, 26.2500, 26.7500],
         [27.2500, 27.7500, 28.2500, 28.7500, 29.2500]]])
y_shape=torch.Size([3, 4, 5])

4. torchvision.transforms.ToPILImage

将一个形状为C x H x W的张量tensor或形状H x W x C的numpy.ndarray到PIL图像,同时保持取值不变。

torchvision.transforms.ToPILImage(mode=None)
  • 代码
# -*- coding: utf-8 -*-
# @Project: zc
# @Author: ZhangChu
# @File name: ToPILPicture_tets
# @Create time: 2022/1/7 17:49
# 1. 导入相关数据库
import torchvision
import os
import cv2 as cv

# 2.设置导入图片路径
path = os.path.join(os.getcwd(), 'img', 'cat2.jpg')

# 3.导入图片
input_img = cv.imread(path)

# 4. 查看导入图片的数据类型: numpy.ndarray
print(f'type(input_img)={type(input_img)}')

# 5. 将 numpy.ndarray 数据转换成 PIL图片格式
img_torchvision = torchvision.transforms.ToPILImage()(input_img)
print(f'type(img_torchvision)={type(img_torchvision)}')

# 6. 输出图片
img_torchvision.show()
  • 结果
type(input_img)=<class 'numpy.ndarray'>
type(img_torchvision)=<class 'PIL.Image.Image'>

5. torchvision.transforms.Compose

在图像进行预处理的时候,我们经常的是需要将目标进行各种组合处理,为了满足上述要求,我们可以用Compose,将所有的预处理组合起来

class torchvision.transforms.Compose(transforms)
  • 代码
Transforms_Compose = torchvision.transforms.Compose([
	transforms.CenterCrop(10),
	transforms.PILToTensor(),
	transforms.ConvertImageDtype(torch.float)])

6. torch.optim.lr_scheduler.StepLR

torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoch=- 1, verbose=False)

每一个步长时期,每个参数组的学习速率以伽马衰减。请注意,这种衰减可能与这个调度程序外部对学习速率的其他改变同时发生。当last_epoch=-1时,将初始lr设置为lr

  • 我们可以根据此类来直接对优化器的学习率进行设置,一般是跟优化器一起使用的
  • optimizer: 优化器 :如 SGD,ADAM等等

7. torch.nn.DataParallel

在模块级实现数据并行

8. torch.utils.data.DataLoader

数据加载程序。结合一个数据集和一个采样器,并在给定数据集上提供一个可迭代对象

9. torch.utils.data.TensorDataset

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值