import os,sys,glob,shutil,json
import cv2
from PIL import Image
import numpy as np
import torch
from torch.utils.data.dataset import Dataset
import torchvision.transforms as transforms
1.建立pytorch的数据集Dataset对象
- 必须继承和重定义__getitem__和__len__
- 【可选】自定义tansform参数进行数据扩增
2.pytorch数据扩增方法
class SVHNDataset(Dataset):
def __init__(self,img_path,img_label,transform=None):
self.img_path = img_path
self.img_label = img_label
if transform is not None:
self.transform = transform
else :
transform=None
def __getitem__(self,index):
img = Image.open(self.img_path[index]).convert('RGB')
if self.transform is not None:
img = self.transform(img)
# 原始SVHN中类别10为0
labels = np.array(self.img_label[index],dtype=np.int)
# 填充字符至5定长字