使用 Torchvision 的数据集后,我尝试在 Pytorch 中加载 HDF5 文件,但没有成功。我读到我应该定义自己的 Dataset 和 Dataloader 类,其中包含 getitem 以启用索引和 len 以返回数据集的长度。另外,我应该定义转换,因为 pytorch 的默认选项需要 PIL 图像。我尝试这样做,但我收到错误“ValueError:没有足够的值来解包(预期 2,得到 1)”我做错了什么?
#PyTorch packages
import torch
from torch.utils.data import Dataset, DataLoader
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import transforms, datasets
from torch import optim
from torch.autograd import Variable
from torch.utils import data
import h5py
import numpy as np
import matplotlib.pyplot as plt
torch.manual_seed(0)
#open training file
with h5py.File('train_catvnoncat.h5', 'r') as hdf:
ls = list(hdf.keys())
print('List of datasets in this file: \n', ls)
data = hdf.get('dataset')
dataset1 = np.array(data)
print('Shape of dataset1: \n', dataset1.shape)
length = len(h5py.File('train_catvnoncat.h5', 'r'))
print(length)
#image size (64,64,3) 64*64*3=12,288.
#209 training examples
#50 test examples
# Example of a picture
#image size (64,64,3) 64*64*3=12,288.
#209 training examples
#50 test examples
#Def the dataloader for h5 files:
class HDF5Dataset(Dataset):
def __init__(self, h5_path):
self.h5_path = '/Users/teff/Downloads/'
self.train = train_catvnoncat.h5(h5_path, 'r')
self.train = test_catvnoncat.h5(h5_path, 'r')
self.length = len(h5py.File(h5_path, 'r'))
# self.transform = transform_hdf5 #I need to define the "transformToTensor"
def __getitem__(self, index): #to enable indexing
record = self.train[str(index)]
return (
record['X'].value,
record['y'].value,
)
def __len__(self): #returns the lenght of the dataset
return self.length
train_loader = torch.utils.data.DataLoader('train_catvnoncat.h5', shuffle=True)
test_loader = torch.utils.data.DataLoader('test_catvnoncat.h5', shuffle=True)