-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprepare.py
105 lines (85 loc) · 3.19 KB
/
prepare.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
import os
from shutil import copyfile
######################################################################
# Prepare dataset for training
# You only need to change this line to your dataset download path
# --------------------------------------------------------------------
download_path = 'data/market'
if 'cuhk' in download_path:
suffix = 'png'
else:
suffix = 'jpg'
if not os.path.isdir(download_path):
print('please change the download_path')
save_path = download_path + '/pytorch'
if not os.path.isdir(save_path):
os.mkdir(save_path)
# -----------------------------------------
# query
query_path = download_path + '/query'
query_save_path = download_path + '/pytorch/query'
if not os.path.isdir(query_save_path):
os.mkdir(query_save_path)
for root, dirs, files in os.walk(query_path, topdown=True):
for name in files:
if not name[-3:] == suffix:
continue
ID = name.split('_')
src_path = query_path + '/' + name
dst_path = query_save_path + '/' + ID[0]
if not os.path.isdir(dst_path):
os.mkdir(dst_path)
copyfile(src_path, dst_path + '/' + name)
# -----------------------------------------
# gallery
gallery_path = download_path + '/bounding_box_test'
gallery_save_path = download_path + '/pytorch/gallery'
if not os.path.isdir(gallery_save_path):
os.mkdir(gallery_save_path)
for root, dirs, files in os.walk(gallery_path, topdown=True):
for name in files:
if not name[-3:] == suffix:
continue
ID = name.split('_')
src_path = gallery_path + '/' + name
dst_path = gallery_save_path + '/' + ID[0]
if not os.path.isdir(dst_path):
os.mkdir(dst_path)
copyfile(src_path, dst_path + '/' + name)
# ---------------------------------------
# train_all
train_path = download_path + '/bounding_box_train'
train_save_path = download_path + '/pytorch/train_all'
if not os.path.isdir(train_save_path):
os.mkdir(train_save_path)
for root, dirs, files in os.walk(train_path, topdown=True):
for name in files:
if not name[-3:] == suffix:
continue
ID = name.split('_')
src_path = train_path + '/' + name
dst_path = train_save_path + '/' + ID[0]
if not os.path.isdir(dst_path):
os.mkdir(dst_path)
copyfile(src_path, dst_path + '/' + name)
# ---------------------------------------
######################################################################
# change name of the folder(e.g. 0002,0007,0010,0011... to 0,1,2,3)
# --------------------------------------------------------------------
original_path = save_path
# copy folder tree from source to destination
def copyfolder(src, dst):
files = os.listdir(src)
if not os.path.isdir(dst):
os.mkdir(dst)
for tt in files:
copyfile(src + '/' + tt, dst + '/' + tt)
train_save_path = original_path + '/train_all_new'
data_path = original_path + '/train_all'
if not os.path.isdir(train_save_path):
os.mkdir(train_save_path)
reid_index = 0
folders = os.listdir(data_path)
for foldernames in folders:
copyfolder(data_path + '/' + foldernames, train_save_path + '/' + str(reid_index).zfill(4))
reid_index = reid_index + 1