Image.open()输出PIL格式
例如:
from PIL import Image
path = "hymenoptera_data/train/bees/16838648_415acd9e3f.jpg"
img_PIL = Image.open(path)
print(img_PIL)
Totensor()输出tensor格式(Convert a PIL Image
or numpy.ndarray
to tensor.)
from PIL import Image
from torchvision import transforms
path = "hymenoptera_data/train/bees/16838648_415acd9e3f.jpg"
img_PIL = Image.open(path)
img_ToTensor = transforms.ToTensor()
img_transTensor = img_ToTensor(img_PIL)
print(img_transTensor)
[0.0784, 0.0863, 0.0863, ..., 0.0235, 0.0196, 0.0196],
[0.0510, 0.0784, 0.0863, ..., 0.0431, 0.0353, 0.0392],
...,
cv.imread()输出narrays格式
import cv2.cv2
path = "hymenoptera_data/train/bees/39672681_1302d204d1.jpg"
img_narrays = cv2.cv2.imread(path)
print(type(img_narrays))
SummaryWriter类,通过tensorboard将文件保存在要创建的“log”中。
from torch.utils.tensorboard import SummaryWriter
wirter = SummaryWriter("logs")
wirter.add_image("test",img_tensor)
wirter.close()