| 377 | return z_q, loss, (perplexity, min_encodings, min_encoding_indices) |
| 378 | |
| 379 | def get_codebook_entry(self, indices, shape): |
| 380 | # shape specifying (batch, height, width, channel) |
| 381 | if self.remap is not None: |
| 382 | indices = indices.reshape(shape[0], -1) # add batch axis |
| 383 | indices = self.unmap_to_all(indices) |
| 384 | indices = indices.reshape(-1) # flatten again |
| 385 | |
| 386 | # get quantized latent vectors |
| 387 | z_q = self.embedding(indices) |
| 388 | |
| 389 | if shape is not None: |
| 390 | z_q = z_q.view(shape) |
| 391 | # reshape back to match original input shape |
| 392 | z_q = z_q.permute(0, 3, 1, 2).contiguous() |
| 393 | |
| 394 | return z_q |
| 395 | |
| 396 | |
| 397 | class DiagonalGaussianDistribution(object): |