-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
50 lines (37 loc) · 1.72 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
42
43
44
45
46
47
48
49
50
from os import listdir
from os.path import join
import torch.utils.data as data
from PIL import Image
from torchvision import transforms
def is_image_file(filename):
return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg", ".bmp"])
def load_img(filepath):
img = Image.open(filepath).convert('YCbCr')
y, _, _ = img.split()
return y
class DatasetFromFolder(data.Dataset):
def __init__(self, image_dir_1, image_dir_2, input_transform=None, target_transform=None):
super(DatasetFromFolder, self).__init__()
self.image_filenames_1 = [join(image_dir_1, x) for x in listdir(image_dir_1) if is_image_file(x)]
self.image_filenames_2 = [join(image_dir_2, x) for x in listdir(image_dir_2) if is_image_file(x)]
self.input_transform = input_transform
self.target_transform = target_transform
self.transform = transforms.Compose([
transforms.ToTensor(), # 转化为pytorch中的tensor
# transforms.Lambda(lambda x: x.repeat(1, 1, 1)),
# transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])
def __getitem__(self, index):
# input = load_img(self.image_filenames_1[index])
# target = load_img(self.image_filenames_2[index])
input = Image.open(self.image_filenames_1[index])
target = Image.open(self.image_filenames_2[index])
input = self.transform(input)
target = self.transform(target)
# if self.input_transform:
# input = self.input_transform(input)
# if self.target_transform:
# target = self.target_transform(target)
return input, target
def __len__(self):
return len(self.image_filenames_1)