|
| 1 | +# Copyright (c) OpenMMLab. All rights reserved. |
| 2 | +import pytest |
| 3 | +import torch |
| 4 | + |
| 5 | +from mmseg.models.backbones.mae import MAE |
| 6 | +from .utils import check_norm_state |
| 7 | + |
| 8 | + |
| 9 | +def test_mae_backbone(): |
| 10 | + with pytest.raises(TypeError): |
| 11 | + # pretrained must be a string path |
| 12 | + model = MAE() |
| 13 | + model.init_weights(pretrained=0) |
| 14 | + |
| 15 | + with pytest.raises(TypeError): |
| 16 | + # img_size must be int or tuple |
| 17 | + model = MAE(img_size=512.0) |
| 18 | + |
| 19 | + with pytest.raises(TypeError): |
| 20 | + # out_indices must be int ,list or tuple |
| 21 | + model = MAE(out_indices=1.) |
| 22 | + |
| 23 | + with pytest.raises(AssertionError): |
| 24 | + # The length of img_size tuple must be lower than 3. |
| 25 | + MAE(img_size=(224, 224, 224)) |
| 26 | + |
| 27 | + with pytest.raises(TypeError): |
| 28 | + # Pretrained must be None or Str. |
| 29 | + MAE(pretrained=123) |
| 30 | + |
| 31 | + # Test img_size isinstance tuple |
| 32 | + imgs = torch.randn(1, 3, 224, 224) |
| 33 | + model = MAE(img_size=(224, )) |
| 34 | + model.init_weights() |
| 35 | + model(imgs) |
| 36 | + |
| 37 | + # Test img_size isinstance tuple |
| 38 | + imgs = torch.randn(1, 3, 224, 224) |
| 39 | + model = MAE(img_size=(224, 224)) |
| 40 | + model(imgs) |
| 41 | + |
| 42 | + # Test norm_eval = True |
| 43 | + model = MAE(norm_eval=True) |
| 44 | + model.train() |
| 45 | + |
| 46 | + # Test BEiT backbone with input size of 224 and patch size of 16 |
| 47 | + model = MAE() |
| 48 | + model.init_weights() |
| 49 | + model.train() |
| 50 | + |
| 51 | + # Test qv_bias |
| 52 | + model = MAE(qv_bias=False) |
| 53 | + model.train() |
| 54 | + |
| 55 | + # Test out_indices = list |
| 56 | + model = MAE(out_indices=[2, 4, 8, 12]) |
| 57 | + model.train() |
| 58 | + |
| 59 | + assert check_norm_state(model.modules(), True) |
| 60 | + |
| 61 | + # Test image size = (224, 224) |
| 62 | + imgs = torch.randn(1, 3, 224, 224) |
| 63 | + feat = model(imgs) |
| 64 | + assert feat[-1].shape == (1, 768, 14, 14) |
| 65 | + |
| 66 | + # Test MAE backbone with input size of 256 and patch size of 16 |
| 67 | + model = MAE(img_size=(256, 256)) |
| 68 | + model.init_weights() |
| 69 | + model.train() |
| 70 | + imgs = torch.randn(1, 3, 256, 256) |
| 71 | + feat = model(imgs) |
| 72 | + assert feat[-1].shape == (1, 768, 16, 16) |
| 73 | + |
| 74 | + # Test MAE backbone with input size of 32 and patch size of 16 |
| 75 | + model = MAE(img_size=(32, 32)) |
| 76 | + model.init_weights() |
| 77 | + model.train() |
| 78 | + imgs = torch.randn(1, 3, 32, 32) |
| 79 | + feat = model(imgs) |
| 80 | + assert feat[-1].shape == (1, 768, 2, 2) |
| 81 | + |
| 82 | + # Test unbalanced size input image |
| 83 | + model = MAE(img_size=(112, 224)) |
| 84 | + model.init_weights() |
| 85 | + model.train() |
| 86 | + imgs = torch.randn(1, 3, 112, 224) |
| 87 | + feat = model(imgs) |
| 88 | + assert feat[-1].shape == (1, 768, 7, 14) |
| 89 | + |
| 90 | + # Test irregular input image |
| 91 | + model = MAE(img_size=(234, 345)) |
| 92 | + model.init_weights() |
| 93 | + model.train() |
| 94 | + imgs = torch.randn(1, 3, 234, 345) |
| 95 | + feat = model(imgs) |
| 96 | + assert feat[-1].shape == (1, 768, 14, 21) |
| 97 | + |
| 98 | + # Test init_values=0 |
| 99 | + model = MAE(init_values=0) |
| 100 | + imgs = torch.randn(1, 3, 224, 224) |
| 101 | + feat = model(imgs) |
| 102 | + assert feat[-1].shape == (1, 768, 14, 14) |
| 103 | + |
| 104 | + # Test final norm |
| 105 | + model = MAE(final_norm=True) |
| 106 | + imgs = torch.randn(1, 3, 224, 224) |
| 107 | + feat = model(imgs) |
| 108 | + assert feat[-1].shape == (1, 768, 14, 14) |
| 109 | + |
| 110 | + # Test patch norm |
| 111 | + model = MAE(patch_norm=True) |
| 112 | + imgs = torch.randn(1, 3, 224, 224) |
| 113 | + feat = model(imgs) |
| 114 | + assert feat[-1].shape == (1, 768, 14, 14) |
| 115 | + |
| 116 | + |
| 117 | +def test_beit_init(): |
| 118 | + path = 'PATH_THAT_DO_NOT_EXIST' |
| 119 | + # Test all combinations of pretrained and init_cfg |
| 120 | + # pretrained=None, init_cfg=None |
| 121 | + model = MAE(pretrained=None, init_cfg=None) |
| 122 | + assert model.init_cfg is None |
| 123 | + model.init_weights() |
| 124 | + |
| 125 | + # pretrained=None |
| 126 | + # init_cfg loads pretrain from an non-existent file |
| 127 | + model = MAE( |
| 128 | + pretrained=None, init_cfg=dict(type='Pretrained', checkpoint=path)) |
| 129 | + assert model.init_cfg == dict(type='Pretrained', checkpoint=path) |
| 130 | + # Test loading a checkpoint from an non-existent file |
| 131 | + with pytest.raises(OSError): |
| 132 | + model.init_weights() |
| 133 | + |
| 134 | + # test resize_rel_pos_embed |
| 135 | + value = torch.randn(732, 16) |
| 136 | + ckpt = { |
| 137 | + 'state_dict': { |
| 138 | + 'layers.0.attn.relative_position_index': 0, |
| 139 | + 'layers.0.attn.relative_position_bias_table': value |
| 140 | + } |
| 141 | + } |
| 142 | + model = MAE(img_size=(512, 512)) |
| 143 | + with pytest.raises(AttributeError): |
| 144 | + model.resize_rel_pos_embed(ckpt) |
| 145 | + |
| 146 | + # pretrained=None |
| 147 | + # init_cfg=123, whose type is unsupported |
| 148 | + model = MAE(pretrained=None, init_cfg=123) |
| 149 | + with pytest.raises(TypeError): |
| 150 | + model.init_weights() |
| 151 | + |
| 152 | + # pretrained loads pretrain from an non-existent file |
| 153 | + # init_cfg=None |
| 154 | + model = MAE(pretrained=path, init_cfg=None) |
| 155 | + assert model.init_cfg == dict(type='Pretrained', checkpoint=path) |
| 156 | + # Test loading a checkpoint from an non-existent file |
| 157 | + with pytest.raises(OSError): |
| 158 | + model.init_weights() |
| 159 | + |
| 160 | + # pretrained loads pretrain from an non-existent file |
| 161 | + # init_cfg loads pretrain from an non-existent file |
| 162 | + with pytest.raises(AssertionError): |
| 163 | + model = MAE( |
| 164 | + pretrained=path, init_cfg=dict(type='Pretrained', checkpoint=path)) |
| 165 | + with pytest.raises(AssertionError): |
| 166 | + model = MAE(pretrained=path, init_cfg=123) |
| 167 | + |
| 168 | + # pretrain=123, whose type is unsupported |
| 169 | + # init_cfg=None |
| 170 | + with pytest.raises(TypeError): |
| 171 | + model = MAE(pretrained=123, init_cfg=None) |
| 172 | + |
| 173 | + # pretrain=123, whose type is unsupported |
| 174 | + # init_cfg loads pretrain from an non-existent file |
| 175 | + with pytest.raises(AssertionError): |
| 176 | + model = MAE( |
| 177 | + pretrained=123, init_cfg=dict(type='Pretrained', checkpoint=path)) |
| 178 | + |
| 179 | + # pretrain=123, whose type is unsupported |
| 180 | + # init_cfg=123, whose type is unsupported |
| 181 | + with pytest.raises(AssertionError): |
| 182 | + model = MAE(pretrained=123, init_cfg=123) |
0 commit comments