Pytorch讀取圖片並顯示
阿新 • • 發佈:2019-01-09
# -*- coding: utf-8 -*-
# @Time : 18-3-15 下午6:43
# @Author : zhwzhong
# @File : model.py
# @Contact : [email protected]
# @Function:
from torchvision import transforms, datasets as ds
import torchvision as tv
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
import numpy as np
transform = transforms.Compose(
[
transforms.ToTensor()
]
)
train_set = tv.datasets.ImageFolder(root='./', transform=transform)
data_loader = DataLoader(dataset=train_set)
to_pil_image = transforms.ToPILImage()
for image, label in data_loader:
# 方法1:Image.show()
# transforms.ToPILImage()中有一句
# npimg = np.transpose(pic.numpy(), (1, 2, 0))
# 因此pic只能是3-D Tensor,所以要用image[0]消去batch那一維
img = to_pil_image(image[0])
img.show()
# 方法2:plt.imshow(ndarray)
img = image[0] # plt.imshow()只能接受3-D Tensor,所以也要用image[0]消去batch那一維
img = img.numpy() # FloatTensor轉為ndarray
img = np.transpose(img, (1 , 2, 0)) # 把channel那一維放到最後
# 顯示圖片
plt.imshow(img)
plt.show()