示例代码
from PIL import Image
from torch. utils. tensorboard import SummaryWriter
from torchvision import transforms
img_path= "hymenoptera_data/val/bees/10870992_eebeeb3a12.jpg"
img= Image. open ( img_path)
writer= SummaryWriter( "logs" )
trans_totensor= transforms. ToTensor( )
img_tensor= trans_totensor( img)
writer. add_image( "img_tensor" , img_tensor)
print ( img_tensor[ 0 ] [ 0 ] [ 0 ] )
trans_normal= transforms. Normalize( [ 0.5 , 0.5 , 0.5 ] , [ 0.5 , 0.5 , 0.5 ] )
img_normalize= trans_normal( img_tensor)
print ( img_normalize[ 0 ] [ 0 ] [ 0 ] )
writer. add_image( "img_normalize" , img_normalize)
trans_resize= transforms. Resize( ( 500 , 400 ) )
img_resize= trans_resize( img)
img_tensor_2= trans_totensor( img_resize)
writer. add_image( "image_resize" , img_tensor_2)
trains_resize_2= transforms. Resize( 450 )
trans_compose= transforms. Compose( [ trains_resize_2, trans_totensor] )
img_compose= trans_compose( img)
writer. add_image( "img_compose" , img_compose)
trans_rdm= transforms. RandomCrop( 200 )
trans_compose_2= transforms. Compose( [ trans_rdm, trans_totensor] )
for i in range ( 10 ) : img_crop= trans_compose_2( img) writer. add_image( "img_crop" , img_crop, i)
writer. close( )
运行结果