-
Notifications
You must be signed in to change notification settings - Fork 93
/
heads.py
527 lines (444 loc) · 17.8 KB
/
heads.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
"""Model head definitions for defining model output types."""
import tensorflow as tf
import attr
from typing import Optional, Text, List, Sequence, Tuple, Union
from abc import ABC, abstractmethod
from sleap.nn.config import (
CentroidsHeadConfig,
SingleInstanceConfmapsHeadConfig,
CenteredInstanceConfmapsHeadConfig,
MultiInstanceConfmapsHeadConfig,
PartAffinityFieldsHeadConfig,
ClassMapsHeadConfig,
ClassVectorsHeadConfig,
)
@attr.s(auto_attribs=True)
class Head(ABC):
"""Base class for model output heads."""
output_stride: int = 1
loss_weight: float = 1.0
@property
@abstractmethod
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
pass
@property
def activation(self) -> str:
"""Return the activation function of the head output layer."""
return "linear"
@property
def loss_function(self) -> str:
"""Return the name of the loss function to use for this head."""
return "mse"
def make_head(self, x_in: tf.Tensor, name: Optional[Text] = None) -> tf.Tensor:
"""Make head output tensor from input feature tensor.
Args:
x_in: An input `tf.Tensor`.
name: If provided, specifies the name of the output layer. If not (the
default), uses the name of the head as the layer name.
Returns:
A `tf.Tensor` with the correct shape for the head.
"""
if name is None:
name = f"{type(self).__name__}"
return tf.keras.layers.Conv2D(
filters=self.channels,
kernel_size=1,
strides=1,
padding="same",
activation=self.activation,
name=name,
)(x_in)
@attr.s(auto_attribs=True)
class SingleInstanceConfmapsHead(Head):
"""Head for specifying single instance confidence maps.
Attributes:
part_names: List of strings specifying the part names associated with channels.
sigma: Spread of the confidence maps.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
part_names: List[Text]
sigma: float = 5.0
output_stride: int = 1
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return len(self.part_names)
@classmethod
def from_config(
cls,
config: SingleInstanceConfmapsHeadConfig,
part_names: Optional[List[Text]] = None,
) -> "SingleInstanceConfmapsHead":
"""Create this head from a set of configurations.
Attributes:
config: A `SingleInstanceConfmapsHeadConfig` instance specifying the head
parameters.
part_names: Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the `part_names`
attribute of the configuration is not set.
Returns:
The instantiated head with the specified configuration options.
"""
if config.part_names is not None:
part_names = config.part_names
return cls(
part_names=part_names,
sigma=config.sigma,
output_stride=config.output_stride,
loss_weight=config.loss_weight,
)
@attr.s(auto_attribs=True)
class CentroidConfmapsHead(Head):
"""Head for specifying instance centroid confidence maps.
Attributes:
anchor_part: Name of the part to use as an anchor node. If not specified, the
bounding box centroid will be used.
sigma: Spread of the confidence maps.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
anchor_part: Optional[Text] = None
sigma: float = 5.0
output_stride: int = 1
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return 1
@classmethod
def from_config(cls, config: CentroidsHeadConfig) -> "CentroidConfmapsHead":
"""Create this head from a set of configurations.
Attributes:
config: A `CentroidsHeadConfig` instance specifying the head parameters.
Returns:
The instantiated head with the specified configuration options.
"""
return cls(
anchor_part=config.anchor_part,
sigma=config.sigma,
output_stride=config.output_stride,
loss_weight=config.loss_weight,
)
@attr.s(auto_attribs=True)
class CenteredInstanceConfmapsHead(Head):
"""Head for specifying centered instance confidence maps.
Attributes:
part_names: List of strings specifying the part names associated with channels.
anchor_part: Name of the part to use as an anchor node. If not specified, the
bounding box centroid will be used.
sigma: Spread of the confidence maps.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
part_names: List[Text]
anchor_part: Optional[Text] = None
sigma: float = 5.0
output_stride: int = 1
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return len(self.part_names)
@classmethod
def from_config(
cls,
config: CenteredInstanceConfmapsHeadConfig,
part_names: Optional[List[Text]] = None,
) -> "CenteredInstanceConfmapsHead":
"""Create this head from a set of configurations.
Attributes:
config: A `CenteredInstanceConfmapsHeadConfig` instance specifying the head
parameters.
part_names: Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the `part_names`
attribute of the configuration is not set.
Returns:
The instantiated head with the specified configuration options.
"""
if config.part_names is not None:
part_names = config.part_names
return cls(
part_names=part_names,
anchor_part=config.anchor_part,
sigma=config.sigma,
output_stride=config.output_stride,
loss_weight=config.loss_weight,
)
@attr.s(auto_attribs=True)
class MultiInstanceConfmapsHead(Head):
"""Head for specifying multi-instance confidence maps.
Attributes:
part_names: List of strings specifying the part names associated with channels.
sigma: Spread of the confidence maps.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
part_names: List[Text]
sigma: float = 5.0
output_stride: int = 1
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return len(self.part_names)
@classmethod
def from_config(
cls,
config: MultiInstanceConfmapsHeadConfig,
part_names: Optional[List[Text]] = None,
) -> "MultiInstanceConfmapsHead":
"""Create this head from a set of configurations.
Attributes:
config: A `MultiInstanceConfmapsHeadConfig` instance specifying the head
parameters.
part_names: Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the `part_names`
attribute of the configuration is not set.
Returns:
The instantiated head with the specified configuration options.
"""
if config.part_names is not None:
part_names = config.part_names
return cls(
part_names=part_names,
sigma=config.sigma,
output_stride=config.output_stride,
loss_weight=config.loss_weight,
)
@attr.s(auto_attribs=True)
class PartAffinityFieldsHead(Head):
"""Head for specifying multi-instance part affinity fields.
Attributes:
edges: List of tuples of `(source, destination)` node names.
sigma: Spread of the part affinity fields.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
edges: Sequence[Tuple[Text, Text]]
sigma: float = 15.0
output_stride: int = 1
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return int(len(self.edges) * 2)
@classmethod
def from_config(
cls,
config: PartAffinityFieldsHeadConfig,
edges: Optional[Sequence[Tuple[Text, Text]]] = None,
) -> "PartAffinityFieldsHead":
"""Create this head from a set of configurations.
Attributes:
config: A `PartAffinityFieldsHeadConfig` instance specifying the head
parameters.
edges: List of 2-tuples of the form `(source_node, destination_node)` that
define pairs of text names of the directed edges of the graph. This must
be set if the `edges` attribute of the configuration is not set.
Returns:
The instantiated head with the specified configuration options.
"""
if config.edges is not None:
edges = config.edges
return cls(
edges=edges,
sigma=config.sigma,
output_stride=config.output_stride,
loss_weight=config.loss_weight,
)
@attr.s(auto_attribs=True)
class ClassMapsHead(Head):
"""Head for specifying class identity maps.
Attributes:
classes: List of string names of the classes.
sigma: Spread of the class maps around each node.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
classes: List[Text]
sigma: float = 5.0
output_stride: int = 1
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return len(self.classes)
@property
def activation(self) -> str:
"""Return the activation function of the head output layer."""
return "sigmoid"
@classmethod
def from_config(
cls,
config: ClassMapsHeadConfig,
classes: Optional[List[Text]] = None,
) -> "ClassMapsHead":
"""Create this head from a set of configurations.
Attributes:
config: A `ClassMapsHeadConfig` instance specifying the head parameters.
classes: List of string names of the classes that this head will predict.
This must be set if the `classes` attribute of the configuration is not
set.
Returns:
The instantiated head with the specified configuration options.
"""
if config.classes is not None:
classes = config.classes
return cls(
classes=classes,
sigma=config.sigma,
output_stride=config.output_stride,
loss_weight=config.loss_weight,
)
@attr.s(auto_attribs=True)
class ClassVectorsHead(Head):
"""Head for specifying classification heads.
Attributes:
classes: List of string names of the classes.
num_fc_layers: Number of fully connected layers after flattening input features.
num_fc_units: Number of units (dimensions) in fully connected layers prior to
classification output.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
classes: List[Text]
num_fc_layers: int = 1
num_fc_units: int = 64
global_pool: bool = True
output_stride: int = 1
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return len(self.classes)
@property
def activation(self) -> str:
"""Return the activation function of the head output layer."""
return "softmax"
@property
def loss_function(self) -> str:
"""Return the name of the loss function to use for this head."""
return "categorical_crossentropy"
@classmethod
def from_config(
cls,
config: ClassVectorsHeadConfig,
classes: Optional[List[Text]] = None,
) -> "ClassVectorsHead":
"""Create this head from a set of configurations.
Attributes:
config: A `ClassVectorsHeadConfig` instance specifying the head parameters.
classes: List of string names of the classes that this head will predict.
This must be set if the `classes` attribute of the configuration is not
set.
Returns:
The instantiated head with the specified configuration options.
"""
if config.classes is not None:
classes = config.classes
return cls(
classes=classes,
num_fc_layers=config.num_fc_layers,
num_fc_units=config.num_fc_units,
global_pool=config.global_pool,
output_stride=config.output_stride,
loss_weight=config.loss_weight,
)
def make_head(self, x_in: tf.Tensor, name: Optional[Text] = None) -> tf.Tensor:
"""Make head output tensor from input feature tensor.
Args:
x_in: An input `tf.Tensor`.
name: If provided, specifies the name of the output layer. If not (the
default), uses the name of the head as the layer name.
Returns:
A `tf.Tensor` with the correct shape for the head.
"""
if name is None:
name = f"{type(self).__name__}"
x = x_in
if self.global_pool:
x = tf.keras.layers.GlobalMaxPool2D(name="pre_classification_global_pool")(
x
)
x = tf.keras.layers.Flatten(name="pre_classification_flatten")(x)
for i in range(self.num_fc_layers):
x = tf.keras.layers.Dense(
self.num_fc_units, name=f"pre_classification{i}_fc"
)(x)
x = tf.keras.layers.Activation("relu", name=f"pre_classification{i}_relu")(
x
)
x = tf.keras.layers.Dense(self.channels, activation=self.activation, name=name)(
x
)
return x
ConfmapConfig = Union[
CentroidsHeadConfig,
SingleInstanceConfmapsHeadConfig,
CenteredInstanceConfmapsHeadConfig,
MultiInstanceConfmapsHeadConfig,
]
@attr.s(auto_attribs=True)
class OffsetRefinementHead(Head):
"""Head for specifying offset refinement maps.
Attributes:
part_names: List of strings specifying the part names associated with channels.
sigma_threshold: Threshold of confidence map values to use for defining the
boundary of the offset maps.
output_stride: Stride of the output head tensor. The input tensor is expected to
be at the same stride.
loss_weight: Weight of the loss term for this head during optimization.
"""
part_names: List[Text]
output_stride: int = 1
sigma_threshold: float = 0.2
loss_weight: float = 1.0
@property
def channels(self) -> int:
"""Return the number of channels in the tensor output by this head."""
return int(len(self.part_names) * 2)
@classmethod
def from_config(
cls,
config: ConfmapConfig,
part_names: Optional[List[Text]] = None,
sigma_threshold: float = 0.2,
loss_weight: float = 1.0,
) -> "OffsetRefinementHead":
"""Create this head from a set of configurations.
Attributes:
config: A `ConfmapConfig` instance specifying the head parameters.
part_names: Text name of the body parts (nodes) that the head will be
configured to produce. The number of parts determines the number of
channels in the output. This must be provided if the `part_names`
attribute of the configuration is not set.
sigma_threshold: Minimum confidence map value below which offsets will be
replaced with zeros.
loss_weight: Weight of the loss associated with this head.
Returns:
The instantiated head with the specified configuration options.
"""
if hasattr(config, "part_names"):
if config.part_names is not None:
part_names = config.part_names
elif hasattr(config, "anchor_part"):
part_names = [config.anchor_part]
return cls(
part_names=part_names,
output_stride=config.output_stride,
sigma_threshold=sigma_threshold,
loss_weight=loss_weight,
)