MCPcopy Create free account
hub / github.com/alinlab/SelfPatch / __init__

Method __init__

detection/backbone/vit_SelfPatch.py:142–218  ·  view source on GitHub ↗

Args: img_size (int, tuple): input image size patch_size (int, tuple): patch size in_chans (int): number of input channels num_classes (int): number of classes for classification head embed_dim (int): embedding dimension

(self, img_size=224, patch_size=16, in_chans=3, num_classes=80, embed_dim=768,
                 depth=12, num_heads=12, mlp_ratio=4., qkv_bias=True, qk_scale=None,
                 drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1, norm_layer=None,
                 cls_attn_layers=2, use_pos=True, eta=None, tokens_norm=False,
                 out_indices=[2, 5, 8, 11])

Source from the content-addressed store, hash-verified

140 """
141
142 def __init__(self, img_size=224, patch_size=16, in_chans=3, num_classes=80, embed_dim=768,
143 depth=12, num_heads=12, mlp_ratio=4., qkv_bias=True, qk_scale=None,
144 drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1, norm_layer=None,
145 cls_attn_layers=2, use_pos=True, eta=None, tokens_norm=False,
146 out_indices=[2, 5, 8, 11]):
147 """
148 Args:
149 img_size (int, tuple): input image size
150 patch_size (int, tuple): patch size
151 in_chans (int): number of input channels
152 num_classes (int): number of classes for classification head
153 embed_dim (int): embedding dimension
154 depth (int): depth of transformer
155 num_heads (int): number of attention heads
156 mlp_ratio (int): ratio of mlp hidden dim to embedding dim
157 qkv_bias (bool): enable bias for qkv if True
158 qk_scale (float): override default qk scale of head_dim ** -0.5 if set
159 drop_rate (float): dropout rate
160 attn_drop_rate (float): attention dropout rate
161 drop_path_rate (float): stochastic depth rate
162 norm_layer: (nn.Module): normalization layer
163 cls_attn_layers: (int) Depth of Class attention layers
164 use_pos: (bool) whether to use positional encoding
165 eta: (float) layerscale initialization value
166 tokens_norm: (bool) Whether to normalize all tokens or just the cls_token in the CA
167 out_indices: (list) Indices of layers from which FPN features are extracted
168 """
169 super().__init__()
170 self.num_classes = num_classes
171 self.num_features = self.embed_dim = embed_dim
172 norm_layer = norm_layer or partial(nn.LayerNorm, eps=1e-6)
173
174 self.patch_embed = PatchEmbed(
175 img_size=img_size, patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim)
176
177 num_patches = self.patch_embed.num_patches
178 self.pos_embed = nn.Parameter(torch.zeros(1, num_patches , embed_dim))
179 self.pos_drop = nn.Dropout(p=drop_rate)
180
181 dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)] # stochastic depth decay rule
182 self.blocks = nn.ModuleList([
183 Block(
184 dim=embed_dim, num_heads=num_heads, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale,
185 drop=drop_rate, attn_drop=attn_drop_rate, drop_path=dpr[i], norm_layer=norm_layer)
186 for i in range(depth)])
187
188 self.out_indices = out_indices
189
190 if patch_size == 16:
191 self.fpn1 = nn.Sequential(
192 nn.ConvTranspose2d(embed_dim, embed_dim, kernel_size=2, stride=2),
193 nn.SyncBatchNorm(embed_dim),
194 nn.GELU(),
195 nn.ConvTranspose2d(embed_dim, embed_dim, kernel_size=2, stride=2),
196 )
197
198 self.fpn2 = nn.Sequential(
199 nn.ConvTranspose2d(embed_dim, embed_dim, kernel_size=2, stride=2),

Callers

nothing calls this directly

Calls 3

PatchEmbedClass · 0.70
BlockClass · 0.70
__init__Method · 0.45

Tested by

no test coverage detected