-
Notifications
You must be signed in to change notification settings - Fork 14
/
load_edata.py
273 lines (236 loc) · 9.24 KB
/
load_edata.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from PIL import Image
from torchvision import transforms
from os.path import join
import abc
import numpy as np
import torch
import torch.utils.data as data
import imageio
import os
class BaseData(data.Dataset):
'''
The dataset used for the IFDL dataset.
'''
def __init__(self, args):
super(BaseData, self).__init__()
self.crop_size = args.crop_size
## demo dataset:
self.mani_data_dir = './data_dir'
## the full dataset:
# self.mani_data_dir = './data'
self.image_names = []
self.image_class = []
self.mask_names = []
def __getitem__(self, index):
res = self.get_item(index)
return res
def __len__(self):
return len(self.image_names)
def generate_mask(self, mask):
'''
generate the corresponding binary mask.
'''
mask = mask.astype(np.float32) / 255
mask[mask > 0.5] = 1
mask[mask <= 0.5] = 0
mask = np.expand_dims(mask, axis=0)
mask = torch.from_numpy(mask)
return mask
def rgba2rgb(self, rgba, background=(255, 255, 255)):
'''
turn rgba to rgb.
'''
row, col, ch = rgba.shape
rgb = np.zeros((row, col, 3), dtype='float32')
r, g, b, a = rgba[:, :, 0], rgba[:, :, 1], rgba[:, :, 2], rgba[:, :, 3]
a = np.asarray(a, dtype='float32') / 255.0
R, G, B = background
rgb[:, :, 0] = r * a + (1.0 - a) * R
rgb[:, :, 1] = g * a + (1.0 - a) * G
rgb[:, :, 2] = b * a + (1.0 - a) * B
return np.asarray(rgb, dtype='uint8') # the output value is uint8 that belongs to [0,255]
def get_image(self, image_name):
'''
return the image with the tensor.
'''
image = imageio.imread(image_name)
if len(image.shape) == 2:
image = imageio.imread(image_name, as_gray=False, pilmode="RGB")
if image.shape[-1] == 4:
image = self.rgba2rgb(image)
image = torch.from_numpy(image.astype(np.float32) / 255)
return image.permute(2, 0, 1)
def get_mask(self, mask_name):
'''
return the binary mask.
'''
mask = Image.open(mask_name).convert('L')
mask = mask.resize(self.crop_size, resample=Image.BICUBIC)
mask = np.asarray(mask)
mask = self.generate_mask(mask)
return mask
@abc.abstractmethod
def get_item(self, index):
'''
blur
image = Image.fromarray(image)
image = image.filter(ImageFilter.GaussianBlur(radius=7))
image = np.asarray(image)
resize
image = Image.fromarray(image)
image = image.resize((int(image.width*0.25), int(image.height*0.25)), resample=Image.BILINEAR)
image = np.asarray(image)
noise
import skimage
image = skimage.util.random_noise(image/255., mode='gaussian', mean=0, var=15/255) * 255
jpeg compression
im = Image.open(image_name)
temp_name = './temp/' + image_name.split('/')[-1][:-3] + 'jpg'
im.save(temp_name, 'JPEG', quality=50)
image = Image.open(temp_name)
image = np.asarray(image)
'''
pass
class ValColumbia(BaseData):
def __init__(self, args):
super(ValColumbia, self).__init__(args)
ddir = os.path.join(self.mani_data_dir, 'columbia')
with open(join(ddir, 'vallist.txt')) as f:
contents = f.readlines()
for content in contents:
_ = os.path.join(ddir, '4cam_splc', content.strip())
self.image_names.append(_)
self.image_class = [1] * len(self.image_names)
def get_item(self, index):
image_name = self.image_names[index]
cls = self.image_class[index]
# image
image = self.get_image(image_name)
# mask
if '4cam_splc' in image_name:
mask_name = image_name.replace('4cam_splc', 'mask').replace('.tif', '.jpg')
mask = self.get_mask(mask_name)
else:
mask = np.zeros((1, 256, 256), dtype='float32')
return image, mask, cls, image_name
class ValCoverage(BaseData):
def __init__(self, args):
super(ValCoverage, self).__init__(args)
ddir = os.path.join(self.mani_data_dir, 'Coverage')
with open(join(ddir, 'fake.txt')) as f:
contents = f.readlines()
for content in contents:
_ = os.path.join(ddir, 'image', content.strip())
self.image_names.append(_)
self.image_class = [2] * len(self.image_names)
def get_item(self, index):
image_name = self.image_names[index]
cls = self.image_class[index]
## read image.
image = self.get_image(image_name)
# mask
mask_name = image_name.replace('image', 'mask').replace('t.tif', 'forged.tif')
mask = self.get_mask(mask_name)
return image, mask, cls, image_name
class ValCasia(BaseData):
def __init__(self, args):
super(ValCasia, self).__init__(args)
ddir = os.path.join(self.mani_data_dir, 'CASIA/CASIA1')
with open(join(ddir, 'fake.txt')) as f:
contents = f.readlines()
for content in contents:
tag = content.split('/')[-1].split('_')[1]
if tag == 'D':
self.image_class.append(1)
elif tag == 'S':
self.image_class.append(2)
else:
raise Exception('unknown class: {}'.format(content))
self.image_names.append(os.path.join(ddir, 'fake', content.strip()))
ddir = os.path.join(self.mani_data_dir, 'CASIA/CASIA2')
with open(join(ddir, 'fake.txt')) as f:
contents = f.readlines()
for content in contents:
tag = content.split('/')[-1].split('_')[1]
if tag == 'D':
self.image_class.append(1)
elif tag == 'S':
self.image_class.append(2)
else:
raise Exception('unknown class: {}'.format(content))
self.image_names.append(os.path.join(ddir, 'fake', content.strip()))
def get_item(self, index):
image_name = self.image_names[index]
cls = self.image_class[index]
# image
image = self.get_image(image_name)
# mask
if '.jpg' in image_name:
mask_name = image_name.replace('fake', 'mask').replace('.jpg', '_gt.png')
else:
mask_name = image_name.replace('fake', 'mask').replace('.tif', '_gt.png')
mask = self.get_mask(mask_name)
return image, mask, cls, image_name
class ValNIST16(BaseData):
def __init__(self, args):
super(ValNIST16, self).__init__(args)
ddir = os.path.join(self.mani_data_dir, 'NIST16')
file_name = 'alllist.txt'
with open(join(ddir, file_name)) as f:
contents = f.readlines()
for content in contents:
image_name, mask_name = content.split(' ')
self.image_names.append(join(ddir, image_name))
self.mask_names.append(join(ddir, mask_name.strip()))
def get_item(self, index):
image_name = self.image_names[index]
mask_name = self.mask_names[index]
if 'splice' in mask_name:
cls = 1
elif 'manipulation' in mask_name:
cls = 2
elif 'remove' in mask_name:
cls = 3
else:
cls = 0
# image
image = self.get_image(image_name)
if image.size()[2]*image.size()[1] >= 1000*1000:
image = imageio.imread(image_name)
if image.shape[-1] == 4:
image = self.rgba2rgb(image)
image = Image.fromarray(image)
image = image.resize((1000, 1000), resample=Image.BICUBIC)
image = np.asarray(image)
image = torch.from_numpy(image.astype(np.float32) / 255)
image = image.permute(2, 0, 1)
# mask
mask = self.get_mask(mask_name)
mask = torch.abs(mask - 1)
return image, mask, cls, image_name
class ValIMD2020(BaseData):
def __init__(self, args):
super(ValIMD2020, self).__init__(args)
ddir = os.path.join(self.mani_data_dir, 'IMD2020')
file_name = 'fake.txt'
with open(join(ddir, file_name)) as f:
contents = f.readlines()
for content in contents:
image_name = content.strip()
if '.jpg' in image_name:
mask_name = image_name.replace('.jpg', '_mask.png')
else:
mask_name = image_name.replace('.png', '_mask.png')
self.image_names.append(join(ddir, 'fake_img', image_name))
self.mask_names.append(join(ddir, 'mask', mask_name))
self.image_class = [2] * len(self.image_names)
def get_item(self, index):
image_name = self.image_names[index]
mask_name = self.mask_names[index]
cls = self.image_class[index]
try:
image = self.get_image(image_name)
except:
print(f"Fail at {image_name}.")
mask = self.get_mask(mask_name)
return image, mask, cls, image_name