Perform forward pass separately on each resolution input. The inputs corresponding to a single resolution are clubbed and single forward is run on the same resolution inputs. Hence we do several forward passes = number of different resolutions used. We then concatenate all the o
| 572 | |
| 573 | |
| 574 | class MultiCropWrapper(nn.Module): |
| 575 | """ |
| 576 | Perform forward pass separately on each resolution input. |
| 577 | The inputs corresponding to a single resolution are clubbed and single |
| 578 | forward is run on the same resolution inputs. Hence we do several |
| 579 | forward passes = number of different resolutions used. We then |
| 580 | concatenate all the output features and run the head forward on these |
| 581 | concatenated features. |
| 582 | """ |
| 583 | def __init__(self, backbone, head): |
| 584 | super(MultiCropWrapper, self).__init__() |
| 585 | # disable layers dedicated to ImageNet labels classification |
| 586 | backbone.fc, backbone.head = nn.Identity(), nn.Identity() |
| 587 | self.backbone = backbone |
| 588 | self.head = head |
| 589 | |
| 590 | def forward(self, x): |
| 591 | # convert to list |
| 592 | if not isinstance(x, list): |
| 593 | x = [x] |
| 594 | idx_crops = torch.cumsum(torch.unique_consecutive( |
| 595 | torch.tensor([inp.shape[-1] for inp in x]), |
| 596 | return_counts=True, |
| 597 | )[1], 0) |
| 598 | start_idx, output = 0, torch.empty(0).to(x[0].device) |
| 599 | for end_idx in idx_crops: |
| 600 | _out = self.backbone(torch.cat(x[start_idx: end_idx])) |
| 601 | # The output is a tuple with XCiT model. See: |
| 602 | # https://github.com/facebookresearch/xcit/blob/master/xcit.py#L404-L405 |
| 603 | if isinstance(_out, tuple): |
| 604 | _out = _out[0] |
| 605 | # accumulate outputs |
| 606 | output = torch.cat((output, _out)) |
| 607 | start_idx = end_idx |
| 608 | # Run the head forward on the concatenated features. |
| 609 | return self.head(output) |
| 610 | |
| 611 | |
| 612 | def get_params_groups(model): |
nothing calls this directly
no outgoing calls
no test coverage detected