Download models/backbone.py from ApyHTML19/Faster-RCNN-Vision-ANN18: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/ApyHTML19/Faster-RCNN-Vision-ANN18/resolve/main/models/backbone.py
- Command line
-
hf download hf://ApyHTML19/Faster-RCNN-Vision-ANN18/models/backbone.py
-
curl -L -o backbone.py https://huggingface.co/ApyHTML19/Faster-RCNN-Vision-ANN18/resolve/main/models/backbone.py
1.21 kB
| import torch | |
| import torch.nn as nn | |
| from torchvision.models import resnet50, ResNet50_Weights | |
| from torchvision.models._utils import IntermediateLayerGetter | |
| from torchvision.ops import FeaturePyramidNetwork | |
| from torchvision.ops.feature_pyramid_network import LastLevelMaxPool | |
| class ResNet50FPNBackbone(nn.Module): | |
| def __init__(self, pretrained=True, out_channels=256): | |
| super().__init__() | |
| weights = ResNet50_Weights.DEFAULT if pretrained else None | |
| resnet = resnet50(weights=weights) | |
| self.body = IntermediateLayerGetter( | |
| resnet, | |
| return_layers={ | |
| "layer1": "c2", | |
| "layer2": "c3", | |
| "layer3": "c4", | |
| "layer4": "c5" | |
| } | |
| ) | |
| self.fpn = FeaturePyramidNetwork( | |
| in_channels_list=[ | |
| 256, | |
| 512, | |
| 1024, | |
| 2048 | |
| ], | |
| out_channels=out_channels, | |
| extra_blocks=LastLevelMaxPool() | |
| ) | |
| self.out_channels = out_channels | |
| def forward(self, x): | |
| features = self.body(x) | |
| features = self.fpn(features) | |
| return features |