Totensor
把一个PIL格式的图片,或者ndarray格式的图片转换为tensor格式 使用方法,如下:
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
img = Image.open( "images/0013035.jpg" )
print( img)
writer = SummaryWriter( "logs" )
trans_tensor = transforms.ToTensor( )
img_tensor = trans_tensor( img)
writer.add_image( "Totensor" , img_tensor)
writer.close( )
Normalize
根据它的平均值和标准差来标准化一个tensor格式的图片,由于通常是RGB图片,所以信道数为3,传入三个平均值和标准差即可 传入的平均值和标准差需要是以序列的格式 计算公式如下: 代码如下:
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
img = Image.open( "images/0013035.jpg" )
print( img)
writer = SummaryWriter( "logs" )
trans_tensor = transforms.ToTensor( )
img_tensor = trans_tensor( img)
writer.add_image( "Totensor" , img_tensor)
trans_normal = transforms.Normalize( [ 0.5 , 0.5 , 0.5 ] , [ 0.5 , 0.5 , 0.5 ] )
img_normal = trans_normal.forward( img_tensor)
writer.add_image( "Normalize" , img_normal)
writer.close( )
结果如下:
Resize
将PIL或Tensor格式的输入图片,调整为指定的尺寸,并使用forward()函数返回对应格式的图片,如下: 传入的尺寸需要是以序列的格式
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
img = Image.open( "images/0013035.jpg" )
print( img)
writer = SummaryWriter( "logs" )
trans_tensor = transforms.ToTensor( )
img_tensor = trans_tensor( img)
print( img.size)
trans_resize = transforms.Resize(( 100 , 100 ))
img_resize = trans_resize.forward( img)
print( img_resize.size)
img_resize = trans_tensor( img_resize)
print( img_resize.size( ))
writer.add_image( "Resize" , img_resize)
writer.close( )
结果如下:
Compose
将多个transforms工具组合在一起,方便使用,相当于循环调用多个transforms工具,并把上一个输出传给下一个,当作输入 注意传入列表的第一个工具的输出格式要满足第二个的输入格式 代码如下:
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
img = Image.open( "images/0013035.jpg" )
print( img)
writer = SummaryWriter( "logs" )
trans_tensor = transforms.ToTensor( )
img_tensor = trans_tensor( img)
writer.add_image( "Totensor" , img_tensor)
trans_resize_2 = transforms.Resize( 800 )
trans_compose = transforms.Compose(( trans_resize_2, trans_tensor))
img_compose = trans_compose( img)
writer.add_image( "Compose" , img_compose)
注意:虽然Resize类中没有定义 _ _ call _ 方法,但是Resize继承自Module类,而Module类定义了 _ call _ 方法,因此当我们将resize对象作为一个函数调用时,python会在本身及其父类中寻找 _ call _ 方法, 因此这里可以正常调用,同时Module类的 _ call _ _方法和Raize类的forward()方法的基本实现如下:
class Module:
def __call__( self, *inputs, **kwargs) :
return self.forward( *inputs, **kwargs)
def forward( self, *inputs, **kwargs) :
raise NotImplementedError
class Rasize:
def forward( self, img) :
"" "
Args:
img ( PIL Image or Tensor) : Image to be scaled.
Returns:
PIL Image or Tensor: Rescaled image.
"" "
return F.resize( img, self.size, self.interpolation, self.max_size, self.antialias)
因此当我们将resize对象作为一个函数调用时,实际上调用的是它的forward方法。 结果如下: