-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatasets.py
65 lines (49 loc) · 2.08 KB
/
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from os import listdir
from os.path import join
import random
from random import randint
import numpy as np
import PIL.Image as pil_image
from torch.utils.data import Dataset
import torchvision.transforms as transforms
# From clean Image
class DataFromFolder(Dataset) :
def __init__(self, noisy_image_dir, clean_image_dir, mode, seed) :
# Inheritance
super(DataFromFolder, self).__init__()
# Fix Seed
random.seed(seed)
np.random.seed(seed)
# Create Path List Instance
self.noisy_image_path_list = [join(noisy_image_dir, image) for image in listdir(noisy_image_dir)]
self.clean_image_path_list = [join(clean_image_dir, image) for image in listdir(clean_image_dir)]
# Sort Path List in Order
self.noisy_image_path_list.sort()
self.clean_image_path_list.sort()
# Get Probability
p_h = randint(0, 1)
p_v = randint(0, 1)
# Data Augmentation
if mode == "train" :
self.transform = transforms.Compose([
transforms.RandomHorizontalFlip(p = p_h),
transforms.RandomVerticalFlip(p = p_v),
transforms.ToTensor()]
)
elif mode == "valid" :
self.transform = transforms.ToTensor()
def _load_image_(self, image_path) :
# Convert into Pillow Image
image = pil_image.open(image_path)
return image
def __getitem__(self, index) :
# Load Image
input = self._load_image_(self.noisy_image_path_list[index])
target = self._load_image_(self.clean_image_path_list[index])
# Apply PyTorch Transforms
input = self.transform(input)
target = self.transform(target)
return input, target
def __len__(self) :
# Get Number of Data
return len(self.clean_image_path_list)