import pandas as pd
import numpy as np
import cv2
from torch.utils.data.dataset import Dataset
class CustomDatasetFromCSV(Dataset):
def __init__(self, csv_path, transform=None):
self.data = pd.read_csv(csv_path)
self.labels = pd.get_dummies(self.data['emotion']).as_matrix()
self.height = 48
self.width = 48
self.transform = transform
def __getitem__(self, index):
pixels = self.data['pixels'].tolist()
faces = []
for pixel_sequence in pixels:
face = [int(pixel) for pixel in pixel_sequence.split(' ')]
# print(np.asarray(face).shape)
face = np.asarray(face).reshape(self.width, self.height)
face = cv2.resize(face.astype('uint8'), (self.width, self.height))
faces.append(face.astype('float32'))
faces = np.asarray(faces)
faces = np.expand_dims(faces, -1)
return faces, self.labels
def __len__(self): …Run Code Online (Sandbox Code Playgroud) 在 Pytorch 中,有没有办法使用类加载特定的torch.utils.data.DataLoader单个样本?我想用它做一些测试。
教程使用
trainloader = torch.utils.data.DataLoader(...)
images, labels = next(iter(trainloader))
Run Code Online (Sandbox Code Playgroud)
获取一批随机样本。有没有办法使用DataLoader来获取特定样本?
干杯