(
self,
*,
dim,
image_size,
channels=3,
max_dim=512,
attn_heads=8,
attn_dim_head=32,
linear_attn_dim_head=8,
linear_attn_heads=16,
ff_mult=4,
antialiased_downsample=False,
)
| 549 | class Discriminator(Module): |
| 550 | @beartype |
| 551 | def __init__( |
| 552 | self, |
| 553 | *, |
| 554 | dim, |
| 555 | image_size, |
| 556 | channels=3, |
| 557 | max_dim=512, |
| 558 | attn_heads=8, |
| 559 | attn_dim_head=32, |
| 560 | linear_attn_dim_head=8, |
| 561 | linear_attn_heads=16, |
| 562 | ff_mult=4, |
| 563 | antialiased_downsample=False, |
| 564 | ): |
| 565 | super().__init__() |
| 566 | image_size = pair(image_size) |
| 567 | min_image_resolution = min(image_size) |
| 568 | |
| 569 | num_layers = int(log2(min_image_resolution) - 2) |
| 570 | |
| 571 | blocks = [] |
| 572 | |
| 573 | layer_dims = [channels] + [(dim * 4) * (2**i) for i in range(num_layers + 1)] |
| 574 | layer_dims = [min(layer_dim, max_dim) for layer_dim in layer_dims] |
| 575 | layer_dims_in_out = tuple(zip(layer_dims[:-1], layer_dims[1:])) |
| 576 | |
| 577 | blocks = [] |
| 578 | attn_blocks = [] |
| 579 | |
| 580 | image_resolution = min_image_resolution |
| 581 | |
| 582 | for ind, (in_chan, out_chan) in enumerate(layer_dims_in_out): |
| 583 | num_layer = ind + 1 |
| 584 | is_not_last = ind != (len(layer_dims_in_out) - 1) |
| 585 | |
| 586 | block = DiscriminatorBlock( |
| 587 | in_chan, out_chan, downsample=is_not_last, antialiased_downsample=antialiased_downsample |
| 588 | ) |
| 589 | |
| 590 | attn_block = Sequential( |
| 591 | Residual(LinearSpaceAttention(dim=out_chan, heads=linear_attn_heads, dim_head=linear_attn_dim_head)), |
| 592 | Residual(FeedForward(dim=out_chan, mult=ff_mult, images=True)), |
| 593 | ) |
| 594 | |
| 595 | blocks.append(ModuleList([block, attn_block])) |
| 596 | |
| 597 | image_resolution //= 2 |
| 598 | |
| 599 | self.blocks = ModuleList(blocks) |
| 600 | |
| 601 | dim_last = layer_dims[-1] |
| 602 | |
| 603 | downsample_factor = 2**num_layers |
| 604 | last_fmap_size = tuple(map(lambda n: n // downsample_factor, image_size)) |
| 605 | |
| 606 | latent_dim = last_fmap_size[0] * last_fmap_size[1] * dim_last |
| 607 | |
| 608 | self.to_logits = Sequential( |
nothing calls this directly
no test coverage detected