-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_data.py
161 lines (125 loc) · 6.58 KB
/
preprocess_data.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
import pandas as pd
from glob import glob
from sklearn.model_selection import GroupKFold
from dataclasses import dataclass
@dataclass
class Configuration:
n_folds: int = 10
path: str = "/home/data1/lrd/mmsport/2022-winners-player-reidentification-challenge-master/data_reid"
#----------------------------------------------------------------------------------------------------------------------#
# Config #
#----------------------------------------------------------------------------------------------------------------------#
config = Configuration()
#----------------------------------------------------------------------------------------------------------------------#
# Get image for train, test and challenge #
#----------------------------------------------------------------------------------------------------------------------#
# train
train = glob("{}/reid_training/*.jpeg".format(config.path))
print("Train:{}".format(len(train)))
# test
test_query = glob("{}/reid_test/query/*.jpeg".format(config.path))
test_gallery = glob("{}/reid_test/gallery/*.jpeg".format(config.path))
print("Test Query: {} - Test Gallery: {}".format(len(test_query), len(test_gallery)))
# challenge
challenge_query = glob("{}/reid_challenge/query/*.jpeg".format(config.path))
challenge_gallery = glob("{}/reid_challenge/gallery/*.jpeg".format(config.path))
print("Challenge Query: {} - Challenge Gallery: {}".format(len(challenge_query), len(challenge_gallery)))
# #----------------------------------------------------------------------------------------------------------------------#
# # Train: Query + Gallery #
# #----------------------------------------------------------------------------------------------------------------------#
img_id = []
folder = []
player = []
game = []
split = []
img_type = []
for f in train:
data = f.replace("\\", "/").split("/")[-1].split(".")[0]
img_id.append(data)
data = data.split("_")
p = data[0]
g = data[1]
i = data[2]
folder.append("reid_training")
player.append(p)
game.append("train_{}".format(g))
split.append("train")
if i == "00":
img_type.append("q")
else:
img_type.append("g")
#----------------------------------------------------------------------------------------------------------------------#
# Test: Query #
#----------------------------------------------------------------------------------------------------------------------#
for f in test_query:
data = f.replace("\\", "/").split("/")[-1].split(".")[0]
img_id.append(data)
data = data.split("_")
p = data[0]
g = data[1]
folder.append("reid_test/query")
player.append(p)
game.append("test_{}".format(g))
split.append("test")
img_type.append("q")
#----------------------------------------------------------------------------------------------------------------------#
# Test: Gallery #
#----------------------------------------------------------------------------------------------------------------------#
for f in test_gallery:
data = f.replace("\\", "/").split("/")[-1].split(".")[0]
img_id.append(data)
data = data.split("_")
p = data[0]
g = data[1]
folder.append("reid_test/gallery")
player.append(p)
game.append("test_{}".format(g))
split.append("test")
img_type.append("g")
#----------------------------------------------------------------------------------------------------------------------#
# Dataframe for Train + Test #
#----------------------------------------------------------------------------------------------------------------------#
df_train = pd.DataFrame({"img_id": img_id,
"folder": folder,
"player": player,
"game": game,
"split": split,
"img_type": img_type,
})
df_train["fold"] = -1
# CV splits beside offical split
cv = GroupKFold(n_splits=config.n_folds)
split = list(cv.split(df_train, df_train['player'], df_train['game']))
for i in range(config.n_folds):
train_idx, val_idx = split[i]
df_train.loc[val_idx, "fold"] = i
# save train DataFrame
df_train.to_csv("{}/train_df.csv".format(config.path), index=False)
#----------------------------------------------------------------------------------------------------------------------#
# Challenge: Query #
#----------------------------------------------------------------------------------------------------------------------#
img_id = []
img_type = []
player = []
for f in challenge_query:
data = f.replace("\\", "/")
img_id.append(data)
player.append(data.split("/")[-1].split(".")[0])
img_type.append("q")
#----------------------------------------------------------------------------------------------------------------------#
# Challenge: Gallery #
#----------------------------------------------------------------------------------------------------------------------#
for f in challenge_gallery:
data = f.replace("\\", "/")
img_id.append(data)
player.append(data.split("/")[-1].split(".")[0])
img_type.append("g")
#----------------------------------------------------------------------------------------------------------------------#
# Dataframe for Challenge #
#----------------------------------------------------------------------------------------------------------------------#
df_challenge = pd.DataFrame({"img_id": img_id,
"player": player,
"img_type": img_type,
})
# save challenge DataFrame
df_challenge.to_csv("{}/challenge_df.csv".format(config.path), index=False)