2. Models
2.1
vq_vae
2.1.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/cv/models/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 | |
2.1.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/cv/models/vq_vae.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
2.1.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/cv/models/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 | |
2.1.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/cv/models/vq_vae.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
2.1.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/cv/models/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 | |
2.1.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/cv/models/vq_vae.py
43 44 45 46 47 48 49 50 51 52 53 54 55 | |
2.1.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/cv/models/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 | |
2.1.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/cv/models/vq_vae.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
2.1.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/cv/models/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 | |
2.1.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/cv/models/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 | |