-
Notifications
You must be signed in to change notification settings - Fork 2
/
process_creativebirds.py
176 lines (147 loc) · 5.86 KB
/
process_creativebirds.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
import json
import numpy as np
import matplotlib.pyplot as plt
from simplification.cutil import simplify_coords
from drawing import Drawing
from joblib import Parallel, delayed
class CreativeItem:
def __init__(self, all_strokes: list):
self.all_strokes = all_strokes
@classmethod
def from_json_item(cls, item: dict):
return cls(all_strokes=item['all_strokes'])
def plot(self):
for bodypart in self.all_strokes:
for stroke in bodypart:
for line in stroke:
plt.plot([line[0], line[2]], [line[1], line[3]], c='k')
plt.show()
class LineStringList:
def __init__(self, linestring_list):
self.linestring_list = linestring_list
@classmethod
def fromCreativeItem(cls, itm: CreativeItem):
linestring_list = list()
for bodypart in itm.all_strokes:
if len(bodypart)==0:
continue
for stroke in bodypart:
if len(stroke)<2:
continue
line = stroke[0]
linestring = [[line[0], line[1]]]
for line in stroke:
linestring.append([line[2], line[3]])
linestring_list.append(linestring)
return cls(linestring_list)
def simplify(self):
new_list = list()
for linestring in self.linestring_list:
if len(linestring) > 2:
new_list.append(simplify_coords(linestring, epsilon=1.0))
self.linestring_list = new_list
def plot(self):
for linestring in self.linestring_list:
plt.plot([p[0] for p in linestring], [p[1] for p in linestring], c='b')
plt.show()
def to_zero(self):
x_min = min([min([p[0] for p in linesting]) for linesting in self.linestring_list])
y_min = min([min([p[1] for p in linesting]) for linesting in self.linestring_list])
new_list = [[[p[0] - x_min, p[1] - y_min] for p in linesting]for linesting in self.linestring_list]
self.linestring_list = new_list
def to_bbox(self, bmax=255):
x_max = max([max([p[0] for p in linesting]) for linesting in self.linestring_list])
y_max = max([max([p[1] for p in linesting]) for linesting in self.linestring_list])
maxdim = max(x_max, y_max)
coeff = bmax / maxdim
new_list = [[[p[0]*coeff, p[1]*coeff] for p in linesting] for linesting in self.linestring_list]
self.linestring_list = new_list
def normalize(self):
self.to_zero()
self.to_bbox()
class DatasetItem:
def __init__(self, emb):
self.embedding = emb
@classmethod
def fromLineStringList(cls, lsl: LineStringList):
pen_sequence = list()
pen_position = [0, 0]
for linestring in lsl.linestring_list:
for point in linestring:
pen_sequence.append([point[0] - pen_position[0], point[1] - pen_position[1], 0])
pen_position = point
pen_sequence[-1][2] = 1
return cls(np.array(pen_sequence[1:], dtype=np.int16))
def plot(self):
firstpoint = self.embedding[0]
plt.plot([0,firstpoint[0]], [0, firstpoint[1]], c='r')
position = [0, 0]
for i_point in range(len(self.embedding)-1):
point = self.embedding[i_point]
position = [position[0] + point[0], position[1] + point[1]]
if point[2]==0:
nextpoint = self.embedding[i_point + 1]
nextposition = [position[0] + nextpoint[0], position[1] + nextpoint[1]]
plt.plot([position[0], nextposition[0]], [position[1], nextposition[1]], c='r')
plt.show()
def process_item(item):
cc = CreativeItem.from_json_item(item)
ll = LineStringList.fromCreativeItem(cc)
ll.simplify()
ll.normalize()
dd = DatasetItem.fromLineStringList(ll)
return dd.embedding
def process_creativebirds_json(
in_path='raw_data/test.json',
out_path="raw_data/test.npz",
):
print("-- LOADING --")
data = json.loads(open(in_path).read())
resultlist = list()
print(" -- PROCESSING --")
resultlist = Parallel(n_jobs=4, verbose=2)(delayed(process_item)(item) for item in data)
print("-- FINISHED --")
n_items = len(resultlist)
val_start = int(n_items*0.85)
test_start = int(n_items*0.95)
print(f"We have {n_items} items, splitting to train [:{val_start}], val[{val_start}:{test_start}], valid[{test_start}:]")
np_result = np.array(resultlist, dtype=object)
np.savez_compressed(out_path,
train=np_result[:val_start],
valid=np_result[val_start:test_start],
test=np_result[test_start:])
def test_one(idx=0, in_path='raw_data/test.json'):
data = json.loads(open(in_path).read())
# print(data)
cc = CreativeItem.from_json_item(data[idx])
cc.plot()
ll = LineStringList.fromCreativeItem(cc)
ll.simplify()
ll.normalize()
ll.plot()
print(ll.linestring_list[:5])
dd = DatasetItem.fromLineStringList(ll)
print(dd.embedding[:5])
dd.plot()
print(dd.embedding.shape)
print(dd.embedding.dtype)
drawing = Drawing.from_npz_data(dd.embedding)
drawing.plot()
def test_processing():
inp = 'raw_data/test.json'
outp = "raw_data/test.npz"
process_creativebirds_json(in_path=inp, out_path=outp)
a = np.load(outp, allow_pickle=True, encoding='latin1')
drawing = Drawing.from_npz_data(a['train'][1])
drawing.plot()
def main():
process_creativebirds_json(in_path="raw_data/creative_birds_json.txt",
out_path="data/creativebirds.npz")
if __name__=="__main__":
# test_processing()
main()
# a = np.load("data/creativebirds.npz", encoding='latin1', allow_pickle=True)
# print(a['test'].shape)
# print(a['test'][0].shape)
# drawing = Drawing.from_npz_data(a['test'][0])
# drawing.plot()