ImageFolder
一個通用的數據加載器,數據集中的數據以以下方式組織
root/dog/xxx.png
root/dog/xxy.png
root/dog/xxz.png
root/cat/123.png
root/cat/nsdf3.png
root/cat/asd932_.png
datasets.ImageFolder(root="root folder path", [transform, target_transform])
使用時要注意圖片的存儲格式,如上所示
用此函數進行處理的時候,會自動會圖片的label命名 0,1,3... 方便接下來的loss計算
class_names = image_datasets['train'].classes 可以會獲得cat、dog 等組成的列表
data_transforms = {
'train': transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),