-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.py
executable file
·96 lines (78 loc) · 2.81 KB
/
data.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from os.path import exists, join, basename
from os import makedirs, remove
import numpy as np
from torchvision.transforms import Compose, Normalize, ToTensor
import torch
from dataset import DatasetFromFolder
class Poiss_noise(object):
"""
Add poisson noise to PIL image
"""
def __init__(self,PEAK):
self.PEAK=PEAK
def __call__(self,img):
sampled_lambda = np.random.uniform(1, 50)
return torch.poisson(img *sampled_lambda )/sampled_lambda
class Gaussian_noise(object):
"""
Add Gaussian noise to PIL image for random sigma between 1 and 75
"""
def __init__(self,std=10):
self.std=std
def __call__(self,img):
std = np.random.uniform(1, 75)
return img + torch.randn(img.shape)*float(std/255)
def input_transform(PEAK=30):
"""
Performe transformation on the input image
"""
return Compose([
ToTensor(),Normalize((0.0,),(1.0,)),Poiss_noise(PEAK),Gaussian_noise()
])
def target_transform():
"""
Performe transformation on the ground truth image
"""
return Compose([
ToTensor(),Normalize((0.0,),(1.0,))
])
def get_training_set(f_dir,synthesize=False):
"""
Get the training dataset.
"""
root_dir = f_dir
train_dir = join(root_dir, "train")
train_dir_inputs = join(train_dir, "inputs")
train_dir_labels = join(train_dir, "labels")
if synthesize:
return DatasetFromFolder(train_dir_inputs,train_dir_labels,
mode='train',synthesize=synthesize,
target_transform=target_transform(),
input_transform=input_transform()
)
else:
return DatasetFromFolder(train_dir_inputs,train_dir_labels,
mode='train',synthesize=synthesize,
target_transform=target_transform(),
input_transform=target_transform()
)
def get_test_set(f_dir,synthesize=False):
"""
Get the testing dataset.
"""
root_dir = f_dir
test_dir = join(root_dir, "val")
test_dir_inputs = join(test_dir, "inputs")
test_dir_labels = join(test_dir, "labels")
if synthesize:
return DatasetFromFolder(test_dir_inputs,test_dir_labels,
mode='val',synthesize=synthesize,
target_transform=target_transform(),
input_transform=input_transform()
)
else:
return DatasetFromFolder(test_dir_inputs,test_dir_labels,
mode='val',synthesize=synthesize,
target_transform=target_transform(),
input_transform=target_transform()
)