File size: 1,807 Bytes
fcd8cdd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# Copyright (c) Facebook, Inc. and its affiliates.
import unittest
import torch
from torch import nn
from detectron2.layers import ASPP, DepthwiseSeparableConv2d, FrozenBatchNorm2d
from detectron2.modeling.backbone.resnet import BasicStem, ResNet
"""
Test for misc layers.
"""
class TestBlocks(unittest.TestCase):
def test_separable_conv(self):
DepthwiseSeparableConv2d(3, 10, norm1="BN", activation1=nn.PReLU())
def test_aspp(self):
m = ASPP(3, 10, [2, 3, 4], norm="", activation=nn.PReLU())
self.assertIsNot(m.convs[0].activation.weight, m.convs[1].activation.weight)
self.assertIsNot(m.convs[0].activation.weight, m.project.activation.weight)
@unittest.skipIf(not torch.cuda.is_available(), "CUDA not available")
def test_frozen_batchnorm_fp16(self):
from torch.cuda.amp import autocast
C = 10
input = torch.rand(1, C, 10, 10).cuda()
m = FrozenBatchNorm2d(C).cuda()
with autocast():
output = m(input.half())
self.assertEqual(output.dtype, torch.float16)
# requires_grad triggers a different codepath
input.requires_grad_()
with autocast():
output = m(input.half())
self.assertEqual(output.dtype, torch.float16)
def test_resnet_unused_stages(self):
resnet = ResNet(BasicStem(), ResNet.make_default_stages(18), out_features=["res2"])
self.assertTrue(hasattr(resnet, "res2"))
self.assertFalse(hasattr(resnet, "res3"))
self.assertFalse(hasattr(resnet, "res5"))
resnet = ResNet(BasicStem(), ResNet.make_default_stages(18), out_features=["res2", "res5"])
self.assertTrue(hasattr(resnet, "res2"))
self.assertTrue(hasattr(resnet, "res4"))
self.assertTrue(hasattr(resnet, "res5"))
|