distilled_vision_transformer.py 5.1 KB
Newer Older
jm_12138's avatar
jm_12138 已提交
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
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import paddle
import paddle.nn as nn
from .vision_transformer import VisionTransformer, Identity, trunc_normal_, zeros_

__all__ = [
    'DeiT_tiny_patch16_224', 'DeiT_small_patch16_224', 'DeiT_base_patch16_224',
    'DeiT_tiny_distilled_patch16_224', 'DeiT_small_distilled_patch16_224',
    'DeiT_base_distilled_patch16_224', 'DeiT_base_patch16_384',
    'DeiT_base_distilled_patch16_384'
]


class DistilledVisionTransformer(VisionTransformer):
28 29 30 31 32 33 34 35 36 37 38
    def __init__(self,
                 img_size=224,
                 patch_size=16,
                 class_dim=1000,
                 embed_dim=768,
                 depth=12,
                 num_heads=12,
                 mlp_ratio=4,
                 qkv_bias=False,
                 norm_layer='nn.LayerNorm',
                 epsilon=1e-5,
jm_12138's avatar
jm_12138 已提交
39
                 **kwargs):
40 41 42 43 44 45 46 47 48 49 50 51
        super().__init__(
            img_size=img_size,
            patch_size=patch_size,
            class_dim=class_dim,
            embed_dim=embed_dim,
            depth=depth,
            num_heads=num_heads,
            mlp_ratio=mlp_ratio,
            qkv_bias=qkv_bias,
            norm_layer=norm_layer,
            epsilon=epsilon,
            **kwargs)
jm_12138's avatar
jm_12138 已提交
52
        self.pos_embed = self.create_parameter(
53 54
            shape=(1, self.patch_embed.num_patches + 2, self.embed_dim),
            default_initializer=zeros_)
jm_12138's avatar
jm_12138 已提交
55 56 57 58 59 60 61
        self.add_parameter("pos_embed", self.pos_embed)

        self.dist_token = self.create_parameter(
            shape=(1, 1, self.embed_dim), default_initializer=zeros_)
        self.add_parameter("cls_token", self.cls_token)

        self.head_dist = nn.Linear(
62 63
            self.embed_dim,
            self.class_dim) if self.class_dim > 0 else Identity()
jm_12138's avatar
jm_12138 已提交
64 65 66 67 68 69

        trunc_normal_(self.dist_token)
        trunc_normal_(self.pos_embed)
        self.head_dist.apply(self._init_weights)

    def forward_features(self, x):
70
        B = paddle.shape(x)[0]
jm_12138's avatar
jm_12138 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        x = self.patch_embed(x)

        cls_tokens = self.cls_token.expand((B, -1, -1))
        dist_token = self.dist_token.expand((B, -1, -1))
        x = paddle.concat((cls_tokens, dist_token, x), axis=1)

        x = x + self.pos_embed
        x = self.pos_drop(x)

        for blk in self.blocks:
            x = blk(x)

        x = self.norm(x)
        return x[:, 0], x[:, 1]

    def forward(self, x):
        x, x_dist = self.forward_features(x)
        x = self.head(x)
        x_dist = self.head_dist(x_dist)
        return (x + x_dist) / 2


def DeiT_tiny_patch16_224(**kwargs):
    model = VisionTransformer(
95 96 97 98 99 100 101 102
        patch_size=16,
        embed_dim=192,
        depth=12,
        num_heads=3,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
103 104 105 106 107
    return model


def DeiT_small_patch16_224(**kwargs):
    model = VisionTransformer(
108 109 110 111 112 113 114 115
        patch_size=16,
        embed_dim=384,
        depth=12,
        num_heads=6,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
116 117 118 119 120
    return model


def DeiT_base_patch16_224(**kwargs):
    model = VisionTransformer(
121 122 123 124 125 126 127 128
        patch_size=16,
        embed_dim=768,
        depth=12,
        num_heads=12,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
129 130 131 132 133
    return model


def DeiT_tiny_distilled_patch16_224(**kwargs):
    model = DistilledVisionTransformer(
134 135 136 137 138 139 140 141
        patch_size=16,
        embed_dim=192,
        depth=12,
        num_heads=3,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
142 143 144 145 146
    return model


def DeiT_small_distilled_patch16_224(**kwargs):
    model = DistilledVisionTransformer(
147 148 149 150 151 152 153 154
        patch_size=16,
        embed_dim=384,
        depth=12,
        num_heads=6,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
155 156 157 158 159
    return model


def DeiT_base_distilled_patch16_224(**kwargs):
    model = DistilledVisionTransformer(
160 161 162 163 164 165 166 167
        patch_size=16,
        embed_dim=768,
        depth=12,
        num_heads=12,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
168 169 170 171 172
    return model


def DeiT_base_patch16_384(**kwargs):
    model = VisionTransformer(
173 174 175 176 177 178 179 180 181
        img_size=384,
        patch_size=16,
        embed_dim=768,
        depth=12,
        num_heads=12,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
182 183 184 185 186
    return model


def DeiT_base_distilled_patch16_384(**kwargs):
    model = DistilledVisionTransformer(
187 188 189 190 191 192 193 194 195
        img_size=384,
        patch_size=16,
        embed_dim=768,
        depth=12,
        num_heads=12,
        mlp_ratio=4,
        qkv_bias=True,
        epsilon=1e-6,
        **kwargs)
jm_12138's avatar
jm_12138 已提交
196
    return model