r""" A `LightAdapterBlock` is a helper model that contains multiple `LightAdapterResnetBlocks`. It is used in the `LightAdapter` model. Parameters: in_channels (`int`): Number of channels of LightAdapterBlock's input. out_channels (`int`): Number
| 514 | |
| 515 | |
| 516 | class LightAdapterBlock(nn.Module): |
| 517 | r""" |
| 518 | A `LightAdapterBlock` is a helper model that contains multiple `LightAdapterResnetBlocks`. It is used in the |
| 519 | `LightAdapter` model. |
| 520 | |
| 521 | Parameters: |
| 522 | in_channels (`int`): |
| 523 | Number of channels of LightAdapterBlock's input. |
| 524 | out_channels (`int`): |
| 525 | Number of channels of LightAdapterBlock's output. |
| 526 | num_res_blocks (`int`): |
| 527 | Number of LightAdapterResnetBlocks in the LightAdapterBlock. |
| 528 | down (`bool`, *optional*, defaults to `False`): |
| 529 | Whether to perform downsampling on LightAdapterBlock's input. |
| 530 | """ |
| 531 | |
| 532 | def __init__(self, in_channels: int, out_channels: int, num_res_blocks: int, down: bool = False): |
| 533 | super().__init__() |
| 534 | mid_channels = out_channels // 4 |
| 535 | |
| 536 | self.downsample = None |
| 537 | if down: |
| 538 | self.downsample = nn.AvgPool2d(kernel_size=2, stride=2, ceil_mode=True) |
| 539 | |
| 540 | self.in_conv = nn.Conv2d(in_channels, mid_channels, kernel_size=1) |
| 541 | self.resnets = nn.Sequential(*[LightAdapterResnetBlock(mid_channels) for _ in range(num_res_blocks)]) |
| 542 | self.out_conv = nn.Conv2d(mid_channels, out_channels, kernel_size=1) |
| 543 | |
| 544 | def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 545 | r""" |
| 546 | This method takes tensor x as input and performs downsampling if required. Then it applies in convolution |
| 547 | layer, a sequence of residual blocks, and out convolutional layer. |
| 548 | """ |
| 549 | if self.downsample is not None: |
| 550 | x = self.downsample(x) |
| 551 | |
| 552 | x = self.in_conv(x) |
| 553 | x = self.resnets(x) |
| 554 | x = self.out_conv(x) |
| 555 | |
| 556 | return x |
| 557 | |
| 558 | |
| 559 | class LightAdapterResnetBlock(nn.Module): |