1. Computer Vision
1.1
aps
Este clase implementa la capa APS de este paper: https://arxiv.org/abs/2011.14214
1.1.1
APS(norm=2)
Initializes the class with normalization option.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
norm
|
int | float | Literal['fro', 'nuc', 'inf', '-inf'] | None
|
Normalization type or value, defaults to 2. |
2
|
Source code in src/layers/cv/aps.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
1.1.1.1
forward(input_tensor, return_index=False)
Processes input tensor to extract dominant polyphase component.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Tensor with shape (B, C, H, W). |
required |
return_index
|
bool
|
If True, returns index of dominant component. |
False
|
Returns:
Type | Description |
---|---|
Tensor | tuple[Tensor, Tensor]
|
Output tensor, optionally with index if return_index is True. |
Source code in src/layers/cv/aps.py
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 |
|
1.2
lps
Este clase implementa la capa APS de este paper: https://arxiv.org/abs/2210.08001
1.2.1
LPS(channel_size, hidden_size)
Initializes the model with specified channel and hidden sizes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel_size
|
int
|
Number of input channels for the Conv2D layer. |
required |
hidden_size
|
int
|
Number of hidden units for the Conv2D layer. |
required |
Source code in src/layers/cv/lps.py
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 |
|
1.2.1.1
forward(input_tensor, return_index=False)
Processes input to extract dominant polyphase component.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Tensor with shape (B, C, H, W). |
required |
return_index
|
bool
|
If True, returns index of dominant component. |
False
|
Returns:
Type | Description |
---|---|
Tensor | tuple[Tensor, Tensor]
|
Tensor of dominant component, optionally with index. |
Source code in src/layers/cv/lps.py
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 |
|
1.3
se
Este clase implementa la capa SE de este paper: https://arxiv.org/abs/1709.01507
1.3.1
SqueezeExcitation(channel_size, ratio)
Implements Squeeze-and-Excitation (SE) block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channel_size
|
int
|
Number of channels in the input tensor. |
required |
ratio
|
int
|
Reduction factor for the compression layer. |
required |
Source code in src/layers/cv/se.py
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 |
|
1.3.1.1
forward(input_tensor)
Applies attention mechanism to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Input tensor with shape (B, C, H, W). |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tensor with attention applied, same shape as input. |
Source code in src/layers/cv/se.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
1.4
vit
1.4.1
EncoderBlock(d_model, d_ff, h, dropout_rate)
Initialize encoder block module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d_model
|
int
|
Number of features in input. |
required |
d_ff
|
int
|
Hidden layer feature dimensions. |
required |
h
|
int
|
Number of attention heads. |
required |
dropout_rate
|
float
|
Dropout rate for layers. |
required |
Source code in src/layers/cv/vit.py
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 |
|
1.4.1.1
forward(input_tensor, mask=None)
Process input tensor through encoder block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Batch of input tensors. |
required |
mask
|
Tensor | None
|
Mask tensor, optional. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor after encoder block processing. |
Source code in src/layers/cv/vit.py
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 |
|
1.4.2
FeedForward(d_model, d_ff, dropout_rate)
Initialize feed-forward neural network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d_model
|
int
|
Input and output feature dimensions. |
required |
d_ff
|
int
|
Hidden layer feature dimensions. |
required |
dropout_rate
|
float
|
Dropout rate applied on layers. |
required |
Source code in src/layers/cv/vit.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
1.4.2.1
forward(input_tensor)
Process input tensor through feed-forward layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Batch of input tensors. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor after feed-forward processing. |
Source code in src/layers/cv/vit.py
251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
1.4.3
LayerNormalization(features, eps=1e-06)
Initialize layer normalization module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features
|
int
|
Number of features in input. |
required |
eps
|
float
|
Small value to avoid division by zero. |
1e-06
|
Source code in src/layers/cv/vit.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
1.4.3.1
forward(input_embedding)
Apply layer normalization to input embeddings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_embedding
|
Tensor
|
Batch of input embeddings. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Normalized embeddings. |
Source code in src/layers/cv/vit.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
1.4.4
MultiHeadAttention(d_model, h, dropout_rate)
Initialize multi-head attention module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d_model
|
int
|
Number of features in input. |
required |
h
|
int
|
Number of attention heads. |
required |
dropout_rate
|
float
|
Dropout rate applied on scores. |
required |
Source code in src/layers/cv/vit.py
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 |
|
1.4.4.1
attention(k, q, v, mask=None, dropout=None)
staticmethod
Compute attention scores and output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k
|
Tensor
|
Key tensor. |
required |
q
|
Tensor
|
Query tensor. |
required |
v
|
Tensor
|
Value tensor. |
required |
mask
|
Tensor | None
|
Mask tensor, optional. |
None
|
dropout
|
Dropout | None
|
Dropout layer, optional. |
None
|
Returns:
Type | Description |
---|---|
Tuple of attention output and scores. |
Source code in src/layers/cv/vit.py
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 |
|
1.4.4.2
forward(k, q, v, mask=None)
Process input tensors through multi-head attention.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
k
|
Tensor
|
Key tensor. |
required |
q
|
Tensor
|
Query tensor. |
required |
v
|
Tensor
|
Value tensor. |
required |
mask
|
Tensor | None
|
Mask tensor, optional. |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
Output tensor after attention processing. |
Source code in src/layers/cv/vit.py
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 |
|
1.4.5
PatchEmbedding(patch_size_height, patch_size_width, in_channels, d_model)
Initialize patch embedding module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch_size_height
|
int
|
Height of each patch. |
required |
patch_size_width
|
int
|
Width of each patch. |
required |
in_channels
|
int
|
Number of input channels. |
required |
d_model
|
int
|
Dimension of the model. |
required |
Source code in src/layers/cv/vit.py
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 |
|
1.4.5.1
forward(input_tensor)
Apply linear projection to input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Batch of image patches as a tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tensor after linear projection of patches. |
Source code in src/layers/cv/vit.py
105 106 107 108 109 110 111 112 113 114 115 116 |
|
1.4.6
Patches(patch_size_height, patch_size_width, img_height, img_width)
Initialize patch extraction module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch_size_height
|
int
|
Height of each patch. |
required |
patch_size_width
|
int
|
Width of each patch. |
required |
img_height
|
int
|
Height of the input image. |
required |
img_width
|
int
|
Width of the input image. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If img_height not divisible by patch height. |
ValueError
|
If img_width not divisible by patch width. |
Source code in src/layers/cv/vit.py
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 |
|
1.4.6.1
forward(input_tensor)
Extract patches from input tensor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Batch of images as a tensor. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tensor with patches from input images. |
Source code in src/layers/cv/vit.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|
1.4.7
PositionalEncoding(d_model, sequence_length, dropout_rate)
Initialize positional encoding module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
d_model
|
int
|
Dimension of the model. |
required |
sequence_length
|
int
|
Max length of input sequences. |
required |
dropout_rate
|
float
|
Dropout rate applied on outputs. |
required |
Source code in src/layers/cv/vit.py
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 |
|
1.4.7.1
forward(input_embedding)
Add positional encoding to input embeddings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_embedding
|
Tensor
|
Batch of input embeddings. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Embeddings with added positional encoding. |
Source code in src/layers/cv/vit.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
1.4.8
ResidualConnection(features, dropout_rate)
Initialize residual connection module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features
|
int
|
Number of features in input. |
required |
dropout_rate
|
float
|
Dropout rate for sublayer output. |
required |
Source code in src/layers/cv/vit.py
419 420 421 422 423 424 425 426 427 428 429 430 431 |
|
1.4.8.1
forward(input_tensor, sublayer)
Apply residual connection to sublayer output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Original input tensor. |
required |
sublayer
|
Module
|
Sublayer module to apply. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Tensor with residual connection applied. |
Source code in src/layers/cv/vit.py
433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
1.4.9
VIT(patch_size_height, patch_size_width, img_height, img_width, in_channels, num_encoders, d_model, d_ff, h, num_classes, dropout_rate)
Initialize Vision Transformer (VIT).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patch_size_height
|
int
|
Height of each patch. |
required |
patch_size_width
|
int
|
Width of each patch. |
required |
img_height
|
int
|
Height of input images. |
required |
img_width
|
int
|
Width of input images. |
required |
in_channels
|
int
|
Number of input channels. |
required |
num_encoders
|
int
|
Number of encoder blocks. |
required |
d_model
|
int
|
Dimension of the model. |
required |
d_ff
|
int
|
Dimension of feed-forward layers. |
required |
h
|
int
|
Number of attention heads. |
required |
num_classes
|
int
|
Number of output classes. |
required |
dropout_rate
|
float
|
Dropout rate for layers. |
required |
Source code in src/layers/cv/vit.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
|
1.4.9.1
forward(input_tensor)
Process input tensor through VIT model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Batch of input images. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
Classification output tensor. |
Source code in src/layers/cv/vit.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 |
|
1.5
vq_vae
1.5.1
Decoder(in_channels, num_residuals, out_channels=3, hidden_size=256, kernel_size=4, stride=2)
Initializes a decoder with residual blocks and transpose convolutional layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_channels
|
int
|
Number of input channels to the decoder. |
required |
num_residuals
|
int
|
Number of residual blocks in the decoder. |
required |
out_channels
|
int
|
Number of output channels, e.g., RGB. |
3
|
hidden_size
|
int
|
Number of channels in hidden layers. |
256
|
kernel_size
|
int
|
Size of the convolutional kernels. |
4
|
stride
|
int
|
Stride of the convolutional kernels. |
2
|
Source code in src/layers/cv/vq_vae.py
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 |
|
1.5.1.1
forward(input_tensor)
Forward pass through the decoder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
The input tensor to the decoder. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor processed by residual blocks and transpose |
Tensor
|
convolutional layers. |
Source code in src/layers/cv/vq_vae.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
1.5.2
Encoder(in_channels, num_residuals, hidden_size=256, kernel_size=4, stride=2)
Initializes an encoder with convolutional layers and residual blocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_channels
|
int
|
Number of input channels to the encoder. |
required |
num_residuals
|
int
|
Number of residual blocks in the encoder. |
required |
hidden_size
|
int
|
Number of channels in hidden layers. |
256
|
kernel_size
|
int
|
Size of the convolutional kernels. |
4
|
stride
|
int
|
Stride of the convolutional kernels. |
2
|
Source code in src/layers/cv/vq_vae.py
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 |
|
1.5.2.1
forward(input_tensor)
Forward pass through the encoder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
The input tensor to the encoder. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor processed by convolutional layers and residual |
Tensor
|
blocks. |
Source code in src/layers/cv/vq_vae.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
1.5.3
ResidualBlock(in_channels, hidden_size=256)
Initializes a residual block that applies two convolutional layers and ReLU activations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_channels
|
int
|
Number of input channels for the block. |
required |
hidden_size
|
int
|
Number of channels in the hidden layer. |
256
|
Source code in src/layers/cv/vq_vae.py
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 |
|
1.5.3.1
forward(input_tensor)
Forward pass through the residual block.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
The input tensor to the block. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor that is the sum of the input tensor and the |
Tensor
|
block's output. |
Source code in src/layers/cv/vq_vae.py
43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
1.5.4
VQVAE(in_channels, size_discrete_space, size_embeddings, num_residuals, hidden_size, kernel_size, stride, beta=0.25)
Initializes a VQ-VAE model with encoder, decoder, and quantizer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_channels
|
int
|
Number of input channels for the model. |
required |
size_discrete_space
|
int
|
Number of discrete embeddings. |
required |
size_embeddings
|
int
|
Size of each embedding vector. |
required |
num_residuals
|
int
|
Number of residual blocks in encoder/decoder. |
required |
hidden_size
|
int
|
Number of channels in hidden layers. |
required |
kernel_size
|
int
|
Size of convolutional kernels. |
required |
stride
|
int
|
Stride of convolutional kernels. |
required |
beta
|
float
|
Weighting factor for the commitment loss. |
0.25
|
Source code in src/layers/cv/vq_vae.py
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 |
|
1.5.4.1
forward(input_tensor)
Forward pass through VQ-VAE model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_tensor
|
Tensor
|
Input tensor to the model. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tuple containing VQ loss, reconstructed tensor, |
Tensor
|
and perplexity. |
Source code in src/layers/cv/vq_vae.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
1.5.5
VectorQuantizer(size_discrete_space, size_embeddings, beta=0.25)
Initializes a vector quantizer with a learnable codebook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size_discrete_space
|
int
|
Number of discrete embeddings. |
required |
size_embeddings
|
int
|
Size of each embedding vector. |
required |
beta
|
float
|
Weighting factor for the commitment loss. |
0.25
|
Source code in src/layers/cv/vq_vae.py
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 |
|
1.5.5.1
forward(encoder_output)
Quantizes the encoder output using the codebook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
encoder_output
|
Tensor
|
Tensor of encoder outputs. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tuple containing VQ loss, quantized tensor, perplexity, |
Tensor
|
and encodings. |
Source code in src/layers/cv/vq_vae.py
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 |
|