forked from thuyngch/Image-Forgery-using-Deep-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_data.py
More file actions
194 lines (161 loc) · 5.01 KB
/
prepare_data.py
File metadata and controls
194 lines (161 loc) · 5.01 KB
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
#------------------------------------------------------------------------------
# Import
#------------------------------------------------------------------------------
import json, argparse, os
from multiprocessing import Pool, cpu_count
from utils import patches
import warnings
warnings.filterwarnings("ignore")
#------------------------------------------------------------------------------
# Check directories
#------------------------------------------------------------------------------
def check_directories(list_dirs):
for dir in list_dirs:
if not os.path.exists(dir):
print("makedirs", dir)
os.makedirs(dir)
#------------------------------------------------------------------------------
# Parse arguments
#------------------------------------------------------------------------------
parser = argparse.ArgumentParser()
parser.add_argument(
'--channel', type=str, default="RGB",
help='Color channel'
)
parser.add_argument(
'--patch_sz', type=int, default=64,
help='Patch size'
)
parser.add_argument(
'--test_subset', type=int, default=5,
help='Index of the test subset'
)
parser.add_argument(
'--dir', type=str, default="./data/",
help='Folder containing the extracted data'
)
parser.add_argument(
'--casia2_dir', type=str, default="/media/antiaegis/storing/datasets/CASIA2/",
help='Folder containing CASIA2 database'
)
parser.add_argument(
'--au_subdir', type=str, default="Au",
help='Sub-folder containing authentic images'
)
parser.add_argument(
'--tp_subdir', type=str, default="Tp",
help='Sub-folder containing tampered images'
)
parser.add_argument(
'--ratio', type=float, default=0.9,
help='Training samples ratio'
)
args = parser.parse_args()
#------------------------------------------------------------------------------
# Initial procedure
#------------------------------------------------------------------------------
# Check directories
DIR = os.path.join(args.dir, "%s_%d_%d" % (
args.channel, args.patch_sz, args.test_subset
))
TRAIN_AU_DIR = os.path.join(DIR, "train/au")
TRAIN_TP_DIR = os.path.join(DIR, "train/tp")
VALID_AU_DIR = os.path.join(DIR, "valid/au")
VALID_TP_DIR = os.path.join(DIR, "valid/tp")
list_dirs = [
TRAIN_AU_DIR,
TRAIN_TP_DIR,
VALID_AU_DIR,
VALID_TP_DIR,
]
check_directories(list_dirs)
# Create parallel pools
pools = Pool(processes=cpu_count())
#------------------------------------------------------------------------------
# Create tampering patches
#------------------------------------------------------------------------------
# Get patches' information
patches_info = patches.get_patches_info(
subsets_file="dataset/tp_subsets.json",
patches_info_file="dataset/patch_coord_pos_casiav2.txt",
test_subset=args.test_subset,
folder=os.path.join(args.casia2_dir, args.tp_subdir),
)
n_pos_samples = len(patches_info)
print("Number of positive samples:", n_pos_samples)
# Split training and validating set
train_patches, valid_patches = patches.split_train_eval(
data_patches=patches_info,
ratio=args.ratio,
)
# Read, crop, and save patches
n_train = len(train_patches)
print("Number of train samples:", n_train)
patches.crop_and_save(
pools=pools,
data_patches=train_patches,
patch_sz=args.patch_sz,
out_dir=TRAIN_TP_DIR,
prefix="train_tp",
)
n_valid = len(valid_patches)
print("Number of valid samples:", n_valid)
patches.crop_and_save(
pools=pools,
data_patches=valid_patches,
patch_sz=args.patch_sz,
out_dir=VALID_TP_DIR,
prefix="valid_tp",
)
#------------------------------------------------------------------------------
# Create authentic patches
#------------------------------------------------------------------------------
# Get list of files
with open("dataset/au_subsets.json", "r") as fp:
au_subsets = json.load(fp)
list_of_train_subsets_ind = list(range(6))
list_of_train_subsets_ind.remove(args.test_subset)
train_files = []
for i in list_of_train_subsets_ind:
files = au_subsets[str(i)]
train_files += files
# Generate file containing coordinates of patches in images
patches.create_neg_based_on_pos(
N_pos=n_pos_samples,
fnames=train_files,
out_file="dataset/patch_coord_neg_casiav2.txt",
au_dir=os.path.join(args.casia2_dir, args.au_subdir),
)
# Get patches' information
patches_info = patches.get_patches_info(
subsets_file="dataset/au_subsets.json",
patches_info_file="dataset/patch_coord_neg_casiav2.txt",
test_subset=args.test_subset,
folder=os.path.join(args.casia2_dir, args.au_subdir),
)
n_neg_samples = len(patches_info)
print("Number of negative samples:", n_neg_samples)
# Split training and validating set
train_patches, valid_patches = patches.split_train_eval(
data_patches=patches_info,
ratio=args.ratio,
)
# Read, crop, and save patches
n_train = len(train_patches)
print("Number of train samples:", n_train)
patches.crop_and_save(
pools=pools,
data_patches=train_patches,
patch_sz=args.patch_sz,
out_dir=TRAIN_AU_DIR,
prefix="train_au",
)
n_valid = len(valid_patches)
print("Number of valid samples:", n_valid)
patches.crop_and_save(
pools=pools,
data_patches=valid_patches,
patch_sz=args.patch_sz,
out_dir=VALID_AU_DIR,
prefix="valid_au",
)