test_arcface.py 3.59 KB
Newer Older
limm's avatar
limm committed
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright (c) OpenMMLab. All rights reserved.
from copy import deepcopy

import pytest
import torch

from mmgen.models.architectures import IDLossModel
# yapf:disable
from mmgen.models.architectures.arcface.model_irse import Backbone

# yapf:enable


class TestArcFace:

    @classmethod
    def setup_class(cls):
        cls.default_cfg = dict(
            input_size=224,
            num_layers=50,
            mode='ir',
            drop_ratio=0.4,
            affine=True)

    def test_arcface_cpu(self):
        model = Backbone(**self.default_cfg)
        x = torch.randn((2, 3, 224, 224))
        y = model(x)
        assert y.shape == (2, 512)

        # test different input size
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(input_size=112))
        model = Backbone(**cfg)
        x = torch.randn((2, 3, 112, 112))
        y = model(x)
        assert y.shape == (2, 512)

        # test different num_layers
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(num_layers=50))
        model = Backbone(**cfg)
        x = torch.randn((2, 3, 224, 224))
        y = model(x)
        assert y.shape == (2, 512)

        # test different mode
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(mode='ir_se'))
        model = Backbone(**cfg)
        x = torch.randn((2, 3, 224, 224))
        y = model(x)
        assert y.shape == (2, 512)

        # test different drop ratio
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(drop_ratio=0.8))
        model = Backbone(**cfg)
        x = torch.randn((2, 3, 224, 224))
        y = model(x)
        assert y.shape == (2, 512)

        # test affine=False
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(affine=False))
        model = Backbone(**cfg)
        x = torch.randn((2, 3, 224, 224))
        y = model(x)
        assert y.shape == (2, 512)

    @pytest.mark.skipif(not torch.cuda.is_available(), reason='requires cuda')
    def test_arcface_cuda(self):
        model = Backbone(**self.default_cfg).cuda()
        x = torch.randn((2, 3, 224, 224)).cuda()
        y = model(x)
        assert y.shape == (2, 512)

        # test different input size
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(input_size=112))
        model = Backbone(**cfg).cuda()
        x = torch.randn((2, 3, 112, 112)).cuda()
        y = model(x)
        assert y.shape == (2, 512)

        # test different num_layers
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(num_layers=50))
        model = Backbone(**cfg).cuda()
        x = torch.randn((2, 3, 224, 224)).cuda()
        y = model(x)
        assert y.shape == (2, 512)

        # test different mode
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(mode='ir_se'))
        model = Backbone(**cfg).cuda()
        x = torch.randn((2, 3, 224, 224)).cuda()
        y = model(x)
        assert y.shape == (2, 512)

        # test different drop ratio
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(drop_ratio=0.8))
        model = Backbone(**cfg).cuda()
        x = torch.randn((2, 3, 224, 224)).cuda()
        y = model(x)
        assert y.shape == (2, 512)

        # test affine=False
        cfg = deepcopy(self.default_cfg)
        cfg.update(dict(affine=False))
        model = Backbone(**cfg).cuda()
        x = torch.randn((2, 3, 224, 224)).cuda()
        y = model(x)
        assert y.shape == (2, 512)

        # test loss model
        id_loss_model = IDLossModel()
        x1 = torch.randn((2, 3, 224, 224)).cuda()
        x2 = torch.randn((2, 3, 224, 224)).cuda()
        y, _ = id_loss_model(pred=x1, gt=x2)
        assert y >= 0