| 339 | |
| 340 | |
| 341 | class SubSample(nn.Module): |
| 342 | def __init__(self, |
| 343 | in_channels, |
| 344 | out_channels, |
| 345 | types='Pool', |
| 346 | stride=[2, 1], |
| 347 | sub_norm='nn.LayerNorm', |
| 348 | act=None): |
| 349 | super().__init__() |
| 350 | self.types = types |
| 351 | if types == 'Pool': |
| 352 | self.avgpool = nn.AvgPool2d( |
| 353 | kernel_size=[3, 5], stride=stride, padding=[1, 2]) |
| 354 | self.maxpool = nn.MaxPool2d( |
| 355 | kernel_size=[3, 5], stride=stride, padding=[1, 2]) |
| 356 | self.proj = nn.Linear(in_channels, out_channels) |
| 357 | else: |
| 358 | self.conv = nn.Conv2d( |
| 359 | in_channels, |
| 360 | out_channels, |
| 361 | kernel_size=3, |
| 362 | stride=stride, |
| 363 | padding=1) |
| 364 | |
| 365 | self.norm = eval(sub_norm)(out_channels) |
| 366 | if act is not None: |
| 367 | self.act = act() |
| 368 | else: |
| 369 | self.act = None |
| 370 | |
| 371 | def forward(self, x): |
| 372 | |
| 373 | if self.types == 'Pool': |
| 374 | x1 = self.avgpool(x) |
| 375 | x2 = self.maxpool(x) |
| 376 | x = (x1 + x2) * 0.5 |
| 377 | out = self.proj(x.flatten(2).permute((0, 2, 1))).contiguous() |
| 378 | else: |
| 379 | x = self.conv(x) |
| 380 | out = x.flatten(2).permute((0, 2, 1)).contiguous() |
| 381 | out = self.norm(out) |
| 382 | if self.act is not None: |
| 383 | out = self.act(out) |
| 384 | |
| 385 | return out |
| 386 | |
| 387 | |
| 388 | class SVTRNet(nn.Module): |