-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdataset.py
41 lines (33 loc) · 1.41 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
# Custom dataset
from PIL import Image
import torch.utils.data as data
import os
import random
class DatasetFromFolder(data.Dataset):
def __init__(self, image_dir, subfolder='train', transform=None, resize_scale=None, crop_size=None, fliplr=False):
super(DatasetFromFolder, self).__init__()
self.input_path = os.path.join(image_dir, subfolder)
self.image_filenames = [x for x in sorted(os.listdir(self.input_path))]
self.transform = transform
self.resize_scale = resize_scale
self.crop_size = crop_size
self.fliplr = fliplr
def __getitem__(self, index):
# Load Image
img_fn = os.path.join(self.input_path, self.image_filenames[index])
img = Image.open(img_fn).convert('RGB')
# preprocessing
if self.resize_scale:
img = img.resize((self.resize_scale, self.resize_scale), Image.BILINEAR)
if self.crop_size:
x = random.randint(0, self.resize_scale - self.crop_size + 1)
y = random.randint(0, self.resize_scale - self.crop_size + 1)
img = img.crop((x, y, x + self.crop_size, y + self.crop_size))
if self.fliplr:
if random.random() < 0.5:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
if self.transform is not None:
img = self.transform(img)
return img
def __len__(self):
return len(self.image_filenames)