| 684 | |
| 685 | |
| 686 | class EncoderUNetModel(nn.Module): |
| 687 | """ |
| 688 | The half UNet model with attention and timestep embedding. |
| 689 | |
| 690 | For usage, see UNet. |
| 691 | """ |
| 692 | |
| 693 | def __init__( |
| 694 | self, |
| 695 | image_size, |
| 696 | in_channels, |
| 697 | model_channels, |
| 698 | out_channels, |
| 699 | num_res_blocks, |
| 700 | attention_resolutions, |
| 701 | dropout=0, |
| 702 | channel_mult=(1, 2, 4, 8), |
| 703 | conv_resample=True, |
| 704 | dims=2, |
| 705 | use_checkpoint=False, |
| 706 | use_fp16=False, |
| 707 | num_heads=1, |
| 708 | num_head_channels=-1, |
| 709 | num_heads_upsample=-1, |
| 710 | use_scale_shift_norm=False, |
| 711 | resblock_updown=False, |
| 712 | use_new_attention_order=False, |
| 713 | pool="adaptive", |
| 714 | ): |
| 715 | super().__init__() |
| 716 | |
| 717 | if num_heads_upsample == -1: |
| 718 | num_heads_upsample = num_heads |
| 719 | |
| 720 | self.in_channels = in_channels |
| 721 | self.model_channels = model_channels |
| 722 | self.out_channels = out_channels |
| 723 | self.num_res_blocks = num_res_blocks |
| 724 | self.attention_resolutions = attention_resolutions |
| 725 | self.dropout = dropout |
| 726 | self.channel_mult = channel_mult |
| 727 | self.conv_resample = conv_resample |
| 728 | self.use_checkpoint = use_checkpoint |
| 729 | self.dtype = th.float16 if use_fp16 else th.float32 |
| 730 | self.num_heads = num_heads |
| 731 | self.num_head_channels = num_head_channels |
| 732 | self.num_heads_upsample = num_heads_upsample |
| 733 | |
| 734 | time_embed_dim = model_channels * 4 |
| 735 | self.time_embed = nn.Sequential( |
| 736 | linear(model_channels, time_embed_dim), |
| 737 | nn.SiLU(), |
| 738 | linear(time_embed_dim, time_embed_dim), |
| 739 | ) |
| 740 | |
| 741 | self.input_blocks = nn.ModuleList( |
| 742 | [ |
| 743 | TimestepEmbedSequential( |