File size: 4,124 Bytes
a39be45 |
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 |
"""
Copyright 2023 LINE Corporation
LINE Corporation licenses this file to you under the Apache License,
version 2.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at:
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
"""
import numpy as np
import dutils
import pandas as pd
from collections import Counter
from tqdm import tqdm
import os
from pandas.core.common import flatten
import argparse
MAX_LEN = 1000
N_CLASS = 4
parser = argparse.ArgumentParser(
description="Spatial Temporal Graph Convolution Network"
)
parser.add_argument(
"--data-root",
default="dataset/babel_v1.0_sequence/",
help="the root path of the dataset",
type=str
)
parser.add_argument(
"--split",
default=1,
help="the split of the dataset",
type=int
)
parser.add_argument(
"--output-folder",
default="dataset/processed_data",
help="the output folder of the generated data",
type=str
)
args = parser.parse_args()
os.makedirs(args.output_folder, exist_ok=True)
def main(data_root):
train_data = dutils.read_pkl(os.path.join(data_root, "babel_v1.0_train_ntu_sk_ntu-style_preprocessed.pkl"))
test_data = dutils.read_pkl(os.path.join(data_root, "babel_v1.0_val_ntu_sk_ntu-style_preprocessed.pkl"))
act2idx = dutils.read_json(f"./prepare/configs/action_label_split{args.split}.json")
label_train_data(data_root, train_data, act2idx)
label_val_data(data_root, test_data, act2idx)
def label_train_data(data_root, train_data, act2idx):
sid = []
x = []
y = []
loc = []
for i, seq_labels in enumerate(tqdm(train_data["Y"])):
if len(seq_labels) > MAX_LEN:
continue
y_ = []
loc_ = []
flag = False
for frame, labels in seq_labels.items():
label_set = set(labels) & set(act2idx.keys())
label_list = list(label_set)
if len(label_list) > 0:
flag = True
loc_.append(act2idx[label_list[0]])
y_.append(act2idx[label_list[0]])
else:
loc_.append(N_CLASS)
max_t = len(loc_)
loc_ = np.array(loc_)
y_ = list(set(y_))
if flag:
# print (train_data["X"][i].shape, len(loc_))
loc.append(loc_)
sid.append(train_data["sid"][i])
x.append(train_data["X"][i][:,:max_t,...])
y.append(y_)
data = {"sid": sid, "X": x, "Y": y, "L":loc}
dutils.write_pkl(data, os.path.join(args.output_folder, f"train_split{args.split}.pkl"))
print (f"#Train sequence: {len(x)}")
def label_val_data(data_root, test_data, act2idx):
sid = []
x = []
y = []
loc = []
for i, seq_labels in enumerate(tqdm(test_data["Y"])):
if len(seq_labels) > MAX_LEN:
continue
y_ = []
loc_ = []
flag = False
for frame, labels in seq_labels.items():
label_set = set(labels) & set(act2idx.keys())
label_list = list(label_set)
if len(label_list) > 0:
flag = True
loc_.append(act2idx[label_list[0]])
y_.append(act2idx[label_list[0]])
else:
loc_.append(N_CLASS)
max_t = len(loc_)
loc_ = np.array(loc_)
y_ = list(set(y_))
if flag:
loc.append(loc_)
sid.append(test_data["sid"][i])
x.append(test_data["X"][i][:,:max_t,...])
y.append(y_)
data = {"sid": sid, "X": x, "Y": y, "L":loc}
dutils.write_pkl(data, os.path.join(args.output_folder, f"val_split{args.split}.pkl"))
print (f"#Test sequence: {len(x)}")
if __name__ == "__main__":
main(args.data_root)
|