generated from fastai/nbdev_template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: lgvaz <lucasgouvaz@gmail.com>
- Loading branch information
1 parent
864ffe3
commit 48f6f5d
Showing
6 changed files
with
216 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .torchvision_backbones import * | ||
from torchvision.models.detection.backbone_utils import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# This imports the torchivsion defined backbones | ||
__all__ = ["create_torchvision_backbone"] | ||
|
||
from mantisshrimp.imports import * | ||
|
||
|
||
def create_torchvision_backbone(backbone: str, pretrained: bool): | ||
# These creates models from torchvision directly, it uses imagent pretrained_weights | ||
if backbone == "mobilenet": | ||
mobile_net = torchvision.models.mobilenet_v2(pretrained=pretrained) | ||
ft_backbone = mobile_net.features | ||
ft_backbone.out_channels = 1280 | ||
return ft_backbone | ||
|
||
elif backbone == "vgg11": | ||
vgg_net = torchvision.models.vgg11(pretrained=pretrained) | ||
ft_backbone = vgg_net.features | ||
ft_backbone.out_channels = 512 | ||
return ft_backbone | ||
|
||
elif backbone == "vgg13": | ||
vgg_net = torchvision.models.vgg13(pretrained=pretrained) | ||
ft_backbone = vgg_net.features | ||
ft_backbone.out_channels = 512 | ||
return ft_backbone | ||
|
||
elif backbone == "vgg16": | ||
vgg_net = torchvision.models.vgg16(pretrained=pretrained) | ||
ft_backbone = vgg_net.features | ||
ft_backbone.out_channels = 512 | ||
return ft_backbone | ||
|
||
elif backbone == "vgg19": | ||
vgg_net = torchvision.models.vgg19(pretrained=pretrained) | ||
ft_backbone = vgg_net.features | ||
ft_backbone.out_channels = 512 | ||
return ft_backbone | ||
|
||
elif backbone == "resnet18": | ||
resnet_net = torchvision.models.resnet18(pretrained=pretrained) | ||
modules = list(resnet_net.children())[:-1] | ||
ft_backbone = nn.Sequential(*modules) | ||
ft_backbone.out_channels = 512 | ||
return ft_backbone | ||
|
||
elif backbone == "resnet34": | ||
resnet_net = torchvision.models.resnet34(pretrained=pretrained) | ||
modules = list(resnet_net.children())[:-1] | ||
ft_backbone = nn.Sequential(*modules) | ||
ft_backbone.out_channels = 512 | ||
return ft_backbone | ||
|
||
elif backbone == "resnet50": | ||
resnet_net = torchvision.models.resnet50(pretrained=pretrained) | ||
modules = list(resnet_net.children())[:-1] | ||
ft_backbone = nn.Sequential(*modules) | ||
ft_backbone.out_channels = 2048 | ||
return ft_backbone | ||
|
||
elif backbone == "resnet101": | ||
resnet_net = torchvision.models.resnet101(pretrained=pretrained) | ||
modules = list(resnet_net.children())[:-1] | ||
ft_backbone = nn.Sequential(*modules) | ||
ft_backbone.out_channels = 2048 | ||
return ft_backbone | ||
|
||
elif backbone == "resnet152": | ||
resnet_net = torchvision.models.resnet152(pretrained=pretrained) | ||
modules = list(resnet_net.children())[:-1] | ||
ft_backbone = nn.Sequential(*modules) | ||
ft_backbone.out_channels = 2048 | ||
return ft_backbone | ||
|
||
elif backbone == "resnext101_32x8d": | ||
resnet_net = torchvision.models.resnext101_32x8d(pretrained=pretrained) | ||
modules = list(resnet_net.children())[:-1] | ||
ft_backbone = nn.Sequential(*modules) | ||
ft_backbone.out_channels = 2048 | ||
return ft_backbone | ||
|
||
else: | ||
raise ValueError("No such backbone implemented in mantisshrimp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from mantisshrimp.backbones import * | ||
import pytest | ||
import torch | ||
|
||
|
||
def test_torchvision_backbones(): | ||
supported_backbones = [ | ||
"mobilenet", | ||
"vgg11", | ||
"vgg13", | ||
"vgg16", | ||
"vgg19", | ||
"resnet18", | ||
"resnet34", | ||
"resnet50", | ||
"resnet101", | ||
"resnet152", | ||
"resnext101_32x8d", | ||
] | ||
pretrained_status = [True, False] | ||
|
||
for backbone in supported_backbones: | ||
for is_pretrained in pretrained_status: | ||
model = create_torchvision_backbone( | ||
backbone=backbone, pretrained=is_pretrained | ||
) | ||
assert isinstance(model, torch.nn.modules.container.Sequential) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import pytest, torch | ||
from mantisshrimp import * | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def batch(): | ||
dataset = test_utils.sample_dataset() | ||
dataloader = MantisFasterRCNN.dataloader(dataset, batch_size=2) | ||
xb, yb = next(iter(dataloader)) | ||
return xb, list(yb) | ||
|
||
|
||
@pytest.mark.slow | ||
@pytest.mark.parametrize("pretrained", [False, True]) | ||
@pytest.mark.parametrize( | ||
"backbone, fpn", | ||
[ | ||
(None, True), | ||
("mobilenet", False), | ||
("vgg11", False), | ||
("vgg13", False), | ||
("vgg16", False), | ||
("vgg19", False), | ||
("resnet18", False), | ||
("resnet34", False), | ||
("resnet50", False), | ||
("resnet18", True), | ||
("resnet34", True), | ||
("resnet50", True), | ||
# these models are too big for github runners | ||
# "resnet101", | ||
# "resnet152", | ||
# "resnext101_32x8d", | ||
], | ||
) | ||
def test_faster_rcnn_nonfpn_backbones(batch, backbone, fpn, pretrained): | ||
if backbone is not None: | ||
backbone = MantisFasterRCNN.get_backbone_by_name( | ||
name=backbone, fpn=fpn, pretrained=pretrained | ||
) | ||
model = MantisFasterRCNN(n_class=91, backbone=backbone) | ||
with torch.no_grad(): | ||
preds = model.forward(*batch) | ||
assert set(preds.keys()) == set( | ||
["loss_classifier", "loss_box_reg", "loss_objectness", "loss_rpn_box_reg"] | ||
) |