-
Notifications
You must be signed in to change notification settings - Fork 76
/
pix2pix.py
133 lines (100 loc) · 3.59 KB
/
pix2pix.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import torch.utils.data as data
from PIL import Image
import os
import os.path
import numpy as np
IMG_EXTENSIONS = [
'.jpg', '.JPG', '.jpeg', '.JPEG',
'.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', '',
]
def is_image_file(filename):
return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)
def make_dataset(dir):
images = []
if not os.path.isdir(dir):
raise Exception('Check dataroot')
for root, _, fnames in sorted(os.walk(dir)):
for fname in fnames:
if is_image_file(fname):
path = os.path.join(dir, fname)
item = path
images.append(item)
return images
def default_loader(path):
return Image.open(path).convert('RGB')
class pix2pix(data.Dataset):
def __init__(self, root, transform=None, loader=default_loader, seed=None):
imgs = make_dataset(root)
if len(imgs) == 0:
raise(RuntimeError("Found 0 images in subfolders of: " + root + "\n"
"Supported image extensions are: " + ",".join(IMG_EXTENSIONS)))
self.root = root
self.imgs = imgs
self.transform = transform
self.loader = loader
if seed is not None:
np.random.seed(seed)
def __getitem__(self, index):
# index = np.random.randint(self.__len__(), size=1)[0]
# index = np.random.randint(self.__len__(), size=1)[0]+1
# index = np.random.randint(self.__len__(), size=1)[0]
# index_folder = np.random.randint(1,4)
index_folder = np.random.randint(0,1)
index_sub = np.random.randint(2, 5)
label=index_folder
if index_folder==0:
path='/home/openset/Desktop/derain2018/facades/training2'+'/'+str(index)+'.jpg'
if index_folder==1:
if index_sub<4:
path='/home/openset/Desktop/derain2018/facades/DB_Rain_new/Rain_Heavy/train2018new'+'/'+str(index)+'.jpg'
if index_sub==4:
index = np.random.randint(0,400)
path='/home/openset/Desktop/derain2018/facades/DB_Rain/Rain_Heavy/trainnew'+'/'+str(index)+'.jpg'
if index_folder==2:
if index_sub<4:
path='/home/openset/Desktop/derain2018/facades/DB_Rain_new/Rain_Medium/train2018new'+'/'+str(index)+'.jpg'
if index_sub==4:
index = np.random.randint(0,400)
path='/home/openset/Desktop/derain2018/facades/DB_Rain/Rain_Medium/trainnew'+'/'+str(index)+'.jpg'
if index_folder==3:
if index_sub<4:
path='/home/openset/Desktop/derain2018/facades/DB_Rain_new/Rain_Light/train2018new'+'/'+str(index)+'.jpg'
if index_sub==4:
index = np.random.randint(0,400)
path='/home/openset/Desktop/derain2018/facades/DB_Rain/Rain_Light/trainnew'+'/'+str(index)+'.jpg'
# img = self.loader(path)
img = self.loader(path)
# NOTE: img -> PIL Image
# w, h = img.size
# w, h = 1024, 512
# img = img.resize((w, h), Image.BILINEAR)
# pix = np.array(I)
#
# r = 16
# eps = 1
#
# I = img.crop((0, 0, w/2, h))
# pix = np.array(I)
# base=guidedfilter(pix, pix, r, eps)
# base = PIL.Image.fromarray(numpy.uint8(base))
#
#
#
# imgA=base
# imgB=I-base
# imgC = img.crop((w/2, 0, w, h))
w, h = img.size
# img = img.resize((w, h), Image.BILINEAR)
# NOTE: split a sample into imgA and imgB
imgA = img.crop((0, 0, w/2, h))
# imgC = img.crop((2*w/3, 0, w, h))
imgB = img.crop((w/2, 0, w, h))
if self.transform is not None:
# NOTE preprocessing for each pair of images
# imgA, imgB, imgC = self.transform(imgA, imgB, imgC)
imgA, imgB = self.transform(imgA, imgB)
return imgA, imgB, label
def __len__(self):
# return 679
print(len(self.imgs))
return len(self.imgs)