TensorDatasetで生画像をTensor化して読み込む[PyTorch]

8月 1, 2020

from glob import glob
from PIL import Image
import numpy as np
import torch
import torch.utils.data as data_utils
from torchvision.transforms import ToTensor

path = "./imgs/*"
files = glob(path)
labels = np.array([1,0,1,1,0])
labels = torch.Tensor(labels)
images = []

for file in files:
    image = Image.open(file)
    image = ToTensor()(image)
    images.append(image)
    features = torch.stack(images)
    train = data_utils.TensorDataset(features, labels)

Tensorflowの知識を深めたいときには、以下の書籍を読むことをお勧めします。

PyTorchPython

Posted by vastee