forked from xyd22/Sentence-recognition-based-on-mpu6500
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare.py
181 lines (161 loc) · 7.01 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
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
import re
import os
import json
import random
import torch
def read_txt_to_tensor(txt_path):
data_all = []
with open(txt_path, "r") as f:
cnt = 0
count_line = 0
for line in f:
count_line += 1
with open(txt_path, "r") as f:
count_line_36 = 0
for line in f:
line_data = line.strip()
count_line_36 += 1
if count_line == 42 and len(line_data) <= 30:
cnt = cnt + 1
continue
if count_line == 42 and cnt == 3:
continue
if count_line == 36 and count_line_36 >= 13 and count_line_36 <= 18:
continue
data_num = re.findall(r"(-?\d+)", line_data[0 : len(line_data)])
data_num = [float(data_num[i]) for i in range(len(data_num))]
data_all.append(data_num)
return torch.tensor(data_all, dtype=torch.float32)
def augment(
annot_json, target_data_num, max_seq_length, min_seq_length, more_than_one_word, READY_PATH
):
seq_range = [i for i in range(min_seq_length, max_seq_length)]
READY_AUGMENTED_PATH = os.path.join(READY_PATH, "augment")
os.makedirs(READY_AUGMENTED_PATH, exist_ok=True)
augmented_annot_json = []
for i in range(target_data_num):
seq_length = random.choice(seq_range)
patch = random.sample(annot_json, seq_length)
data = torch.hstack([torch.load(j["path"]) for j in patch])
save_path = os.path.join(READY_AUGMENTED_PATH, f"{i}.pt")
torch.save(data, save_path)
label = []
for j in patch:
label += j["label"]
augmented_annot_json.append(
{"path": save_path, "label": label, "cls_label": more_than_one_word, "length": seq_length}
)
return augmented_annot_json
def split_by_random(data):
N = len(data)
random.shuffle(data)
return data[: int(N * 0.7)], data[int(N * 0.7) : int(N * 0.85)], data[int(N * 0.85) :]
def prepare(ROOT_PATH, TRAIN_FOLDER):
RAW_PATH = os.path.join(ROOT_PATH, r"train-data", TRAIN_FOLDER)
READY_PATH = os.path.join(ROOT_PATH, r"train-data\ready")
os.makedirs(READY_PATH, exist_ok=True)
random.seed(42)
word_list = [
i
for i in os.listdir(RAW_PATH)
if len(i.split(" ")) == 1 and os.path.isdir(os.path.join(RAW_PATH, i))
]
sentence_list = [
i
for i in os.listdir(RAW_PATH)
if len(i.split(" ")) > 1 and os.path.isdir(os.path.join(RAW_PATH, i))
]
word2num_dict = {word: i + 1 for i, word in enumerate(word_list)}
word2num_dict["<blank>"] = 0
word2num_dict["<more_than_one_word>"] = len(word2num_dict)
num2word_dict = {i + 1:word for i, word in enumerate(word_list)}
num2word_dict[0] = "<blank>"
num2word_dict[len(num2word_dict)] = "<more_than_one_word>"
with open(os.path.join(READY_PATH, "word2num.json"), "w") as f:
json.dump(word2num_dict, f)
with open(os.path.join(READY_PATH, "num2word.json"), "w") as f:
json.dump(num2word_dict, f)
word_annot_json = []
for word in word_list:
RAW_WORD_PATH = os.path.join(RAW_PATH, word)
READY_WORD_PATH = os.path.join(READY_PATH, "word", word)
os.makedirs(READY_WORD_PATH, exist_ok=True)
counter = 0
for filename in os.listdir(RAW_WORD_PATH):
if "raw.txt" in filename:
data = read_txt_to_tensor(os.path.join(RAW_WORD_PATH, filename))
save_path = os.path.join(READY_WORD_PATH, f"{counter}.pt")
torch.save(data, save_path)
word_annot_json.append(
{
"path": save_path,
"label": [word2num_dict[word]],
"cls_label": word2num_dict[word],
"length": 1
}
)
counter += 1
# print(counter)
# 对于没有句子的数据库要删掉
sentence_annot_json = []
for sentence in sentence_list:
RAW_SENTENCE_PATH = os.path.join(RAW_PATH, sentence)
READY_SENTENCE_PATH = os.path.join(READY_PATH, "sentence", sentence)
os.makedirs(READY_SENTENCE_PATH, exist_ok=True)
counter = 0
for filename in os.listdir(RAW_SENTENCE_PATH):
if "raw.txt" in filename:
data = read_txt_to_tensor(os.path.join(RAW_SENTENCE_PATH, filename))
save_path = os.path.join(READY_SENTENCE_PATH, f"{counter}.pt")
torch.save(data, save_path)
label = [word2num_dict[word.lower()]
for word in re.split(r",|\s", sentence)
if word
]
sentence_annot_json.append(
{
"path": save_path,
"label": label,
"cls_label": word2num_dict["<more_than_one_word>"],
"length": len(label)
}
)
counter += 1
augmented_annot_json = augment(
word_annot_json + sentence_annot_json,
# word_annot_json,
target_data_num=3000,
max_seq_length=20,
min_seq_length=4,
more_than_one_word=word2num_dict["<more_than_one_word>"],
READY_PATH=READY_PATH,
)
word_train,word_valid,word_test=split_by_random(word_annot_json)
with open(os.path.join(READY_PATH,'word','train.json'),'w') as f:
json.dump(word_train,f)
with open(os.path.join(READY_PATH,'word','valid.json'),'w') as f:
json.dump(word_valid,f)
with open(os.path.join(READY_PATH,'word','test.json'),'w') as f:
json.dump(word_test,f)
sent_train,sent_test,sent_valid=split_by_random(sentence_annot_json)
with open(os.path.join(READY_PATH,'sentence','train.json'),'w') as f:
json.dump(sent_train,f)
with open(os.path.join(READY_PATH,'sentence','valid.json'),'w') as f:
json.dump(sent_valid,f)
with open(os.path.join(READY_PATH,'sentence','test.json'),'w') as f:
json.dump(sent_test,f)
augment_train,augment_valid,augment_test=split_by_random(augmented_annot_json)
with open(os.path.join(READY_PATH,'augment','train.json'),'w') as f:
json.dump(augment_train,f)
with open(os.path.join(READY_PATH,'augment','valid.json'),'w') as f:
json.dump(augment_valid,f)
with open(os.path.join(READY_PATH,'augment','test.json'),'w') as f:
json.dump(augment_test,f)
with open(os.path.join(READY_PATH,'train.json'),'w') as f:
json.dump(augment_train+word_train+sent_train,f)
# json.dump(word_train,f)
with open(os.path.join(READY_PATH,'valid.json'),'w') as f:
json.dump(augment_valid+word_valid+sent_valid,f)
# json.dump(word_test,f)
with open(os.path.join(READY_PATH,'test.json'),'w') as f:
json.dump(augment_test+word_test+sent_test,f)