-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_dataset.py
168 lines (145 loc) · 6.06 KB
/
generate_dataset.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
import numpy as np
import pandas as pd
# import matplotlib.pyplot as plt
from scipy.io import loadmat
from scipy.io import savemat
import time
import random
import os
import math
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
import shutil
src_path = 'data/filter_data/train_all_2027'
dst_base_path = 'data/filter_data/'
def generate_dataset():
dirs = os.listdir(src_path)
print(len(dirs))
dst_sub_dir1 = 'real'
dst_sub_dir2 = 'fake'
if not os.path.exists(os.path.join(dst_base_path, dst_sub_dir1)):
os.makedirs(os.path.join(dst_base_path, dst_sub_dir1))
if not os.path.exists(os.path.join(dst_base_path, dst_sub_dir2)):
os.makedirs(os.path.join(dst_base_path, dst_sub_dir2))
file_num = 0
real_num = 0
fake_num = 0
for dir in dirs:
files = os.listdir(os.path.join(src_path, dir))
# print(len(files))
if len(dir) == 4:
real_num += len(files)
for file in files:
shutil.copy(os.path.join(src_path, dir, file), os.path.join(dst_base_path, dst_sub_dir1, file))
elif len(dir) == 8:
fake_num += len(files)
for file in files:
shutil.copy(os.path.join(src_path, dir, file), os.path.join(dst_base_path, dst_sub_dir2, file))
file_num += len(files)
print('real = %4d fake = %4d sum = %4d' % (real_num, fake_num, file_num))
return real_num, fake_num, file_num
def sample(path='data/filter_data', num=5000):
files = os.listdir(os.path.join(path, 'real'))
# files = np.sort(files)
np.random.shuffle(files)
dst_sub_dir = 'real_sample'
if not os.path.exists(os.path.join('data/filter_data', dst_sub_dir)):
os.makedirs(os.path.join('data/filter_data', dst_sub_dir))
for i in range(num):
shutil.copy(os.path.join(os.path.join(path, 'real'), files[i]), os.path.join('data/filter_data', dst_sub_dir, files[i]))
files = os.listdir(os.path.join(path, 'fake'))
# files = np.sort(files)
np.random.shuffle(files)
dst_sub_dir = 'fake_sample'
if not os.path.exists(os.path.join('data/filter_data', dst_sub_dir)):
os.makedirs(os.path.join('data/filter_data', dst_sub_dir))
for i in range(num):
shutil.copy(os.path.join(os.path.join(path, 'fake'), files[i]), os.path.join('data/filter_data', dst_sub_dir, files[i]))
def redivide(path='data/filter_data', ratio=0.9):
dst_base_path1 = os.path.join(path, 'train_set')
dst_base_path2 = os.path.join(path, 'test_set')
dst_sub_dir1 = 'real'
dst_sub_dir2 = 'fake'
if not os.path.exists(dst_base_path1):
os.makedirs(dst_base_path1)
if not os.path.exists(os.path.join(dst_base_path1, dst_sub_dir1)):
os.makedirs(os.path.join(dst_base_path1, dst_sub_dir1))
if not os.path.exists(os.path.join(dst_base_path1, dst_sub_dir2)):
os.makedirs(os.path.join(dst_base_path1, dst_sub_dir2))
if not os.path.exists(dst_base_path2):
os.makedirs(dst_base_path2)
if not os.path.exists(os.path.join(dst_base_path2, dst_sub_dir1)):
os.makedirs(os.path.join(dst_base_path2, dst_sub_dir1))
if not os.path.exists(os.path.join(dst_base_path2, dst_sub_dir2)):
os.makedirs(os.path.join(dst_base_path2, dst_sub_dir2))
files = os.listdir(os.path.join(path, 'real_sample'))
# files = np.sort(files)
np.random.shuffle(files)
for i in range(len(files)):
if i < int(ratio * len(files)):
shutil.copy(os.path.join(path, 'real_sample', files[i]), os.path.join(dst_base_path1, dst_sub_dir1, files[i]))
else:
shutil.copy(os.path.join(path, 'real_sample', files[i]), os.path.join(dst_base_path2, dst_sub_dir1, files[i]))
files = os.listdir(os.path.join(path, 'fake_sample'))
# files = np.sort(files)
np.random.shuffle(files)
for i in range(len(files)):
if i < int(ratio * len(files)):
shutil.copy(os.path.join(path, 'fake_sample', files[i]), os.path.join(dst_base_path1, dst_sub_dir2, files[i]))
else:
shutil.copy(os.path.join(path, 'fake_sample', files[i]), os.path.join(dst_base_path2, dst_sub_dir2, files[i]))
def for_train():
real_num, fake_num, file_num = generate_dataset()
sample(path='data/filter_data', num=min(real_num, fake_num))
redivide()
def for_test():
dirs = os.listdir(src_path)
dst_path = os.path.join(dst_base_path, 'test_set/fake')
print(len(dirs))
if os.path.exists(dst_path):
shutil.rmtree(dst_path)
os.makedirs(os.path.join(dst_path))
file_num = 0
dir_num = 0
for dir in dirs:
if len(dir) == 8:
files = os.listdir(os.path.join(src_path, dir))
file_num += len(files)
dir_num += 1
for file in files:
shutil.copy(os.path.join(src_path, dir, file), os.path.join(dst_path, file))
print('dir_num = %d file_num = %d' % (dir_num, file_num))
def for_reid():
sample_good = os.path.join(dst_base_path, 'good')
dst_path = os.path.join(dst_base_path, 'augment')
if not os.path.exists(sample_good):
print('src data %s is not exist' % sample_good)
if os.path.exists(dst_path):
shutil.rmtree(dst_path)
os.mkdir(dst_path)
files = os.listdir(sample_good)
file_num = 0
dir_num = 0
for file in files:
dir = file.split('_')[0]
if len(dir) == 8:
if not os.path.exists(os.path.join(dst_path, dir)):
os.mkdir(os.path.join(dst_path, dir))
dir_num += 1
shutil.copy(os.path.join(sample_good, file), os.path.join(dst_path, dir, file))
file_num += 1
print('after first filter total file_num = %d dir_num = %d' % (file_num, dir_num))
dirs = os.listdir(dst_path)
for dir in dirs:
files = os.listdir(os.path.join(dst_path, dir))
if len(files) < 4:
file_num -= len(files)
dir_num -= 1
shutil.rmtree(os.path.join(dst_path, dir))
print('after second filter total file_num = %d dir_num = %d' % (file_num, dir_num))
if __name__ == '__main__':
# for_train()
# for_test()
for_reid()