-
Notifications
You must be signed in to change notification settings - Fork 43
/
s2s_dataset.py
177 lines (137 loc) · 4.43 KB
/
s2s_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
166
167
168
169
170
171
172
173
174
175
176
177
import glob
import random
import json
import numpy as np
import pandas as pd
import torch
from torchtext import data, vocab
from torch.utils.data import DataLoader, Dataset
import rnn_infer
from config import Config
config = Config()
def str_count(string):
'''找出字符串中的中英文、空格、数字、标点符号个数'''
count = {}
count_en = count_dg = count_sp = count_zh = count_pu = 0
try:
for s in string:
# 英文
if s >= 'a' and s <= 'z' or s >= 'A' and s <= 'Z':
count_en += 1
# 数字
elif s.isdigit():
count_dg += 1
# 空格
elif s.isspace():
count_sp += 1
# 中文
elif s.isalpha():
count_zh += 1
# 特殊字符
else:
count_pu += 1
except:
pass
count["EN"] = count_en
count["DG"] = count_dg
count["SP"] = count_sp
count["ZH"] = count_zh
count["PU"] = count_pu
return count
def ifdrop(string):
'''
根据业务情况过滤无用文本框
'''
if len(string)<2:
return False
count = str_count(string)
if count["ZH"]>6:
return False
if count["EN"] == len(string):
return False
return True
def change_num(string):
'''
根据业务需求,随即替换数字
'''
ss = list(string)
for i in range(len(ss)):
if ss[i].isdigit():
n = random.randint(0, 9)
ss[i] = str(n)
return ''.join(ss)
def processing_location(loc_list, h, w):
loc_hidden = []
for points in loc_list:
if len(points) == 2:
p2 = [points[1][0], points[0][1]]
p3 = [points[0][0], points[1][1]]
location = points[0]+p2+points[1]+p3
else:
location = points[0]+points[1]+points[2]+points[3]
noise = [random.randint(-3, 3) for _ in range(8)]
add_noise = list(np.array(location)+np.array(noise))
add_noise = [x/w if i % 2 == 0 else x /
h for i, x in enumerate(add_noise)]
flatten = list(add_noise)*config.expend_box_times
loc_hidden.append(flatten)
return np.array(loc_hidden).T
def processing_text(label_list):
out = rnn_infer.predict(label_list)
return out
def json2embed(path, max_len):
output = np.zeros((config.s2s_emb_dim, max_len))
text_list = []
loc_list = []
label_list = []
with open(path, 'rb') as f:
data = json.loads(f.read())
h = data["imageHeight"]
w = data["imageWidth"]
shapes = data["shapes"]
shapes = sorted(shapes, key=lambda x: (
x["points"][0][1], x["points"][0][1]))
for i, obj in enumerate(shapes):
label = str(obj["label"]).replace("\"", "").replace("\'", "")
if ifdrop(label):
group_id = obj["group_id"]
points = obj["points"]
if len(points) in [2, 4]:
text_list.append(label)
loc_list.append(points)
if group_id:
label_list.append(int(group_id))
else:
label_list.append(0)
length = len(text_list) if len(text_list) < max_len+1 else max_len
out_label = [0]*max_len
out_label[:length] = label_list
text_hidden = processing_text(text_list[:max_len])
loc_hidden = processing_location(loc_list, h, w)
output[:config.rnn_hidden_size, :length] = text_hidden[:, :length]
output[config.rnn_hidden_size:, :length] = loc_hidden[:, :length]
return output.T, out_label
def gen(paths, batch_size, seq_length, device):
num =len(paths)
i=0
while True:
X = np.zeros((batch_size, seq_length, config.s2s_emb_dim))*1.0
Y = np.zeros((batch_size, seq_length))
for j in range(batch_size):
if i>=num:
i=0
np.random.shuffle(paths)
path = paths[i]
i+=1
a,b = json2embed(path, seq_length)
X[j], Y[j] = a,b
outx = torch.from_numpy(X).float().to(device)
outy = torch.from_numpy(Y).to(device)
yield outx, outy
if __name__ == "__main__":
import pandas as pd
paths = glob.glob("data/train/*.json")
device = torch.device('cuda')
train_loader = gen(paths, config.s2s_batch_size, config.max_box_num, device)
for i in train_loader:
print(i[0])