| 53 | return x |
| 54 | |
| 55 | class Block(nn.Module): |
| 56 | |
| 57 | def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0., |
| 58 | drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm): |
| 59 | super().__init__() |
| 60 | self.norm1 = norm_layer(dim) |
| 61 | self.attn = Attention( |
| 62 | dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop) |
| 63 | # NOTE: drop path for stochastic depth, we shall see if this is better than dropout here |
| 64 | self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() |
| 65 | self.norm2 = norm_layer(dim) |
| 66 | mlp_hidden_dim = int(dim * mlp_ratio) |
| 67 | self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) |
| 68 | |
| 69 | def forward(self, x, return_attention=False): |
| 70 | if return_attention: |
| 71 | y, attn = self.attn(self.norm1(x), return_attention=return_attention) |
| 72 | x = x + self.drop_path(y) |
| 73 | x = x + self.drop_path(self.mlp(self.norm2(x))) |
| 74 | return x, attn |
| 75 | else: |
| 76 | x = x + self.drop_path(self.attn(self.norm1(x))) |
| 77 | x = x + self.drop_path(self.mlp(self.norm2(x))) |
| 78 | return x |
| 79 | |
| 80 | |
| 81 | class PatchEmbed(nn.Module): |