ocl.perceptual_grouping
Implementations of perceptual grouping algorithms.
We denote methods that group input feature together into slots of objects (either unconditionally) or via additional conditioning signals as perceptual grouping modules.
SlotAttention
Implementation of SlotAttention.
Based on the slot attention implementation of Phil Wang available at: https://github.com/lucidrains/slot-attention
Source code in ocl/perceptual_grouping.py
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 |
|
SlotAttentionGrouping
Implementation of SlotAttention for perceptual grouping.
Source code in ocl/perceptual_grouping.py
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 |
|
__init__
Initialize Slot Attention Grouping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_dim |
int
|
Dimensionality of features to slot attention (after positional encoding). |
required |
object_dim |
int
|
Dimensionality of slots. |
required |
kvq_dim |
Optional[int]
|
Dimensionality after projecting to keys, values, and queries. If |
None
|
n_heads |
int
|
Number of heads slot attention uses. |
1
|
iters |
int
|
Number of slot attention iterations. |
3
|
eps |
float
|
Epsilon in slot attention. |
1e-08
|
ff_mlp |
Optional[nn.Module]
|
Optional module applied slot-wise after GRU update. |
None
|
positional_embedding |
Optional[nn.Module]
|
Optional module applied to the features before slot attention, adding positional encoding. |
None
|
use_projection_bias |
bool
|
Whether to use biases in key, value, query projections. |
False
|
use_implicit_differentiation |
bool
|
Whether to use implicit differentiation trick. If true,
performs one more iteration of slot attention that is used for the gradient step
after |
False
|
use_empty_slot_for_masked_slots |
bool
|
Replace slots masked with a learnt empty slot vector. |
False
|
Source code in ocl/perceptual_grouping.py
forward
Apply slot attention based perceptual grouping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
ocl.typing.FeatureExtractorOutput
|
Features used for grouping. |
required |
conditioning |
ocl.typing.ConditioningOutput
|
Initial conditioning vectors for slots. |
required |
slot_mask |
Optional[ocl.typing.EmptyIndicator]
|
Slot mask where true indicates that the slot should be masked. |
None
|
Returns:
Type | Description |
---|---|
ocl.typing.PerceptualGroupingOutput
|
The grouped features. |
Source code in ocl/perceptual_grouping.py
StickBreakingGrouping
Perceptual grouping based on a stick-breaking process.
The idea is to pick a random feature from a yet unexplained part of the feature map, then see which parts of the feature map are "explained" by this feature using a kernel distance. This process is iterated until some termination criterion is reached. In principle, this process allows to extract a variable number of slots per image.
This is based on Engelcke et al, GENESIS-V2: Inferring Unordered Object Representations without Iterative Refinement, http://arxiv.org/abs/2104.09958. Our implementation here differs a bit from the one described there:
- It only implements one kernel distance, the Gaussian kernel
- It does not take features positions into account when computing the kernel distances
- It L2-normalises the input features to get comparable scales of the kernel distance
- It has multiple termination criteria, namely termination based on fraction explained, mean mask value, and min-max mask value. GENESIS-V2 implements termination based on mean mask value, but does not mention it in the paper. Note that by default, all termination criteria are disabled.
Source code in ocl/perceptual_grouping.py
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 |
|
__init__
Initialize stick-breaking-based perceptual grouping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
object_dim |
int
|
Dimensionality of extracted slots. |
required |
feature_dim |
int
|
Dimensionality of features to operate on. |
required |
n_slots |
int
|
Maximum number of slots. |
required |
kernel_var |
float
|
Variance in Gaussian kernel. |
1.0
|
learn_kernel_var |
bool
|
Whether kernel variance should be included as trainable parameter. |
False
|
max_unexplained |
float
|
If fraction of unexplained features drops under this value, drop the slot. |
0.0
|
min_slot_mask |
float
|
If slot mask has lower average value than this value, drop the slot. |
0.0
|
min_max_mask_value |
float
|
If slot mask's maximum value is lower than this value, drop the slot. |
0.0
|
early_termination |
bool
|
If true, all slots after the first dropped slot are also dropped. |
False
|
add_unexplained |
bool
|
If true, add a slot that covers all unexplained parts at the point when the first slot was dropped. |
False
|
eps |
float
|
Minimum value for masks. |
1e-08
|
detach_features |
bool
|
If true, detach input features such that no gradient flows through this operation. |
False
|
use_input_layernorm |
bool
|
Apply layernorm to features prior to grouping. |
False
|
Source code in ocl/perceptual_grouping.py
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 |
|
forward
Apply stick-breaking-based perceptual grouping to input features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
ocl.typing.FeatureExtractorOutput
|
Features that should be grouped. |
required |
Returns:
Type | Description |
---|---|
ocl.typing.PerceptualGroupingOutput
|
Grouped features. |
Source code in ocl/perceptual_grouping.py
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 |
|
KMeansGrouping
Simple K-means clustering based grouping.