-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataloader.py
38 lines (32 loc) · 1.36 KB
/
dataloader.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @Time : 2019/2/8 11:39
# @Author : ylin
# Description:
#
from torch.utils.data.dataset import Dataset
from os.path import join
from os import listdir
from PIL import Image
def is_image_file(filename):
return any(filename.endswith(extension) for extension in ['.png', '.jpg', '.jpeg', '.JPG', '.JPEG', '.PNG'])
class DatasetFromFolder(Dataset):
def __init__(self, dataset_dir, input_transform=None, target_transform=None):
super(DatasetFromFolder, self).__init__()
self.image_dir = dataset_dir + 'X4/'
self.target_dir = dataset_dir + 'HR/'
self.image_filenames = [join(self.image_dir, x) for x in listdir(self.image_dir) if is_image_file(x)]
self.target_filenames = [join(self.target_dir, x) for x in listdir(self.target_dir) if is_image_file(x)]
self.input_transform = input_transform
self.target_transform = target_transform
def __getitem__(self, index):
image = Image.open(self.image_filenames[index]).convert('RGB')
target = Image.open(self.target_filenames[index]).convert('RGB')
if self.input_transform:
image = self.input_transform(image)
if self.target_transform:
target = self.target_transform(target)
return image, target
def __len__(self):
return len(self.image_filenames)