-
Notifications
You must be signed in to change notification settings - Fork 25
/
datasets.py
28 lines (23 loc) · 845 Bytes
/
datasets.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
import numpy as np
import torch
from PIL import Image
from sklearn.preprocessing import LabelEncoder
from torch.utils.data import Dataset
class ImageDataset(Dataset):
def __init__(self, paths, transform, is_train, labels=None):
self.img_paths = np.array(paths)
self.transform = transform
self.is_train = is_train
if self.is_train:
self.labels = LabelEncoder().fit_transform(labels)
self.labels = torch.from_numpy(np.array(self.labels, dtype=np.float))
def __getitem__(self, index):
img_path = self.img_paths[index]
img = self.transform(Image.open(img_path))
if self.is_train:
label = self.labels[index]
return img, label
else:
return img
def __len__(self):
return len(self.img_paths)