-
Notifications
You must be signed in to change notification settings - Fork 11
/
relation.py
365 lines (297 loc) · 9.05 KB
/
relation.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
Relations for sampling part positions. Relations, together with parts, make up
concepts.
"""
from abc import ABCMeta, abstractmethod
import torch
from .part import PartToken
from ..splines import bspline_eval, bspline_gen_s
categories_allowed = ['unihist', 'start', 'end', 'mid']
class RelationToken(object):
"""
RelationToken instances hold all of the token-level information for a
relation
Parameters
----------
rtype : Relation
relation type
eval_spot_token : tensor
Optional parameter. Token-level evaluation spot for RelationAttachAlong
"""
def __init__(self, rtype, **kwargs):
self.rtype = rtype
if rtype.category in ['unihist', 'start', 'end']:
assert kwargs == {}
else:
assert set(kwargs.keys()) == {'eval_spot_token'}
self.eval_spot_token = kwargs['eval_spot_token']
def get_attach_point(self, prev_parts):
"""
Get the mean attachment point of where the start of the next part
should be, given the previous part tokens.
Parameters
----------
prev_parts : list of PartToken
previous part tokens
Returns
-------
loc : (2,) tensor
attach point (location); x-y coordinates
"""
if self.rtype.category == 'unihist':
loc = self.rtype.gpos
else:
prev = prev_parts[self.rtype.attach_ix]
if self.rtype.category == 'start':
subtraj = prev.motor[0]
loc = subtraj[0]
elif self.rtype.category == 'end':
subtraj = prev.motor[-1]
loc = subtraj[-1]
else:
assert self.rtype.category == 'mid'
bspline = prev.motor_spline[:, :, self.rtype.attach_subix]
loc = bspline_eval(self.eval_spot_token, bspline)
# convert (1,2) tensor -> (2,) tensor
loc = torch.squeeze(loc, dim=0)
return loc
def parameters(self):
"""
Returns a list of parameters that can be optimized via gradient descent.
Parameters
----------
eps : float
tolerance for constrained optimization
Returns
-------
parameters : list
optimizable parameters
"""
if self.rtype.category == 'mid':
parameters = [self.eval_spot_token]
else:
parameters = []
return parameters
def lbs(self, eps=1e-4):
"""
Returns a list of lower bounds for each of the optimizable parameters.
Parameters
----------
eps : float
tolerance for constrained optimization
Returns
-------
lbs : list
lower bound for each parameter
"""
if self.rtype.category == 'mid':
_, lb, _ = bspline_gen_s(self.rtype.ncpt, 1)
lbs = [lb+eps]
else:
lbs = []
return lbs
def ubs(self, eps=1e-4):
"""
Returns a list of upper bounds for each of the optimizable parameters.
Parameters
----------
eps : float
tolerance for constrained optimization
Returns
-------
ubs : list
upper bound for each parameter
"""
if self.rtype.category == 'mid':
_, _, ub = bspline_gen_s(self.rtype.ncpt, 1)
ubs = [ub-eps]
else:
ubs = []
return ubs
def train(self):
"""
makes params require grad
"""
for param in self.parameters():
param.requires_grad_(True)
def eval(self):
"""
makes params require no grad
"""
for param in self.parameters():
param.requires_grad_(False)
def to(self, device):
"""
moves parameters to device
TODO
"""
pass
class RelationType(object):
"""
Relations define the relationship between the current part and all previous
parts. They fall into 4 categories: ['unihist','start','end','mid'].
RelationType holds all type-level parameters of the relation.
his is an abstract base class that must be inherited from to build specific
categories of relations.
Parameters
----------
category : string
relation category
"""
__metaclass__ = ABCMeta
def __init__(self, category):
# make sure type is valid
assert category in categories_allowed
self.category = category
@abstractmethod
def parameters(self):
"""
Returns a list of parameters that can be optimized via gradient descent.
Parameters
----------
eps : float
tolerance for constrained optimization
Returns
-------
parameters : list
optimizable parameters
"""
pass
@abstractmethod
def lbs(self, eps=1e-4):
"""
Returns a list of lower bounds for each of the optimizable parameters.
Parameters
----------
eps : float
tolerance for constrained optimization
Returns
-------
lbs : list
lower bound for each parameter
"""
pass
@abstractmethod
def ubs(self, eps=1e-4):
"""
Returns a list of upper bounds for each of the optimizable parameters.
Parameters
----------
eps : float
tolerance for constrained optimization
Returns
-------
ubs : list
upper bound for each parameter
"""
pass
def train(self):
"""
makes params require grad
"""
for param in self.parameters():
param.requires_grad_(True)
def eval(self):
"""
makes params require no grad
"""
for param in self.parameters():
param.requires_grad_(False)
def to(self, device):
"""
moves parameters to device
TODO
"""
pass
class RelationIndependent(RelationType):
"""
RelationIndependent (or 'unihist' relations) are assigned when the part's
location is independent of all previous parts. The global position (gpos)
of the part is sampled at random from the prior on positions
Parameters
----------
category : string
relation category
gpos : (2,) tensor
position; x-y coordinates
xlim : (2,) tensor
[lower, upper]; bounds for the x direction
ylim : (2,) tensor
[lower, upper]; bounds for the y direction
"""
def __init__(self, category, gpos, xlim, ylim):
super(RelationIndependent, self).__init__(category)
assert category == 'unihist'
assert gpos.shape == torch.Size([2])
self.gpos = gpos
self.xlim = xlim
self.ylim = ylim
def parameters(self):
parameters = [self.gpos]
return parameters
def lbs(self, eps=1e-4):
bounds = torch.stack([self.xlim, self.ylim])
lbs = [bounds[:,0]+eps]
return lbs
def ubs(self, eps=1e-4):
bounds = torch.stack([self.xlim, self.ylim])
ubs = [bounds[:,1]-eps]
return ubs
class RelationAttach(RelationType):
"""
RelationAttach is assigned when the part will attach to a previous part
Parameters
----------
category : string
relation category
attach_ix : int
index of previous part to which this part will attach
"""
def __init__(self, category, attach_ix):
super(RelationAttach, self).__init__(category)
assert category in ['start', 'end', 'mid']
self.attach_ix = attach_ix
def parameters(self):
parameters = []
return parameters
def lbs(self, eps=1e-4):
lbs = []
return lbs
def ubs(self, eps=1e-4):
ubs = []
return ubs
class RelationAttachAlong(RelationAttach):
"""
RelationAttachAlong is assigned when the part will attach to a previous
part somewhere in the middle of that part (as opposed to the start or end)
Parameters
----------
category : string
relation category
attach_ix : int
index of previous part to which this part will attach
attach_subix : int
index of sub-stroke from the selected previous part to which
this part will attach
eval_spot : tensor
type-level spline coordinate
ncpt : int
number of control points
"""
def __init__(self, category, attach_ix, attach_subix, eval_spot, ncpt):
super(RelationAttachAlong, self).__init__(category, attach_ix)
assert category == 'mid'
self.attach_subix = attach_subix
self.eval_spot = eval_spot
self.ncpt = ncpt
def parameters(self):
parameters = [self.eval_spot]
return parameters
def lbs(self, eps=1e-4):
_, lb, _ = bspline_gen_s(self.ncpt, 1)
lbs = [lb+eps]
return lbs
def ubs(self, eps=1e-4):
_, _, ub = bspline_gen_s(self.ncpt, 1)
ubs = [ub-eps]
return ubs