-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
30 lines (25 loc) · 952 Bytes
/
dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from torch.utils.data import Dataset
import glob
import os
from PIL import Image
class CatDogDataset(Dataset):
def __init__(self, path, transform=None):
self.classes = os.listdir(path)
self.path = [f"{path}/{className}" for className in self.classes]
self.file_list = [glob.glob(f"{x}/*") for x in self.path]
self.transform = transform
files = []
for i, className in enumerate(self.classes):
for fileName in self.file_list[i]:
files.append([i, className, fileName])
self.file_list = files
files = None
def __len__(self):
return len(self.file_list)
def __getitem__(self, idx):
fileName = self.file_list[idx][2]
classCategory = self.file_list[idx][0]
im = Image.open(fileName)
if self.transform:
im = self.transform(im)
return im.view(3, 224, 224), classCategory