| 92 | |
| 93 | |
| 94 | class Block(nn.Module): |
| 95 | def __init__(self, dim, num_heads, mlp_ratio=4., qkv_bias=False, qk_scale=None, drop=0., attn_drop=0., |
| 96 | drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm): |
| 97 | super().__init__() |
| 98 | self.norm1 = norm_layer(dim) |
| 99 | self.attn = Attention( |
| 100 | dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop) |
| 101 | self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() |
| 102 | self.norm2 = norm_layer(dim) |
| 103 | mlp_hidden_dim = int(dim * mlp_ratio) |
| 104 | self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) |
| 105 | |
| 106 | def forward(self, x, return_attention=False): |
| 107 | y, attn = self.attn(self.norm1(x)) |
| 108 | x = x + self.drop_path(y) |
| 109 | x = x + self.drop_path(self.mlp(self.norm2(x))) |
| 110 | |
| 111 | if return_attention: |
| 112 | return x, attn |
| 113 | else: |
| 114 | return x |
| 115 | |
| 116 | |
| 117 | class PatchEmbed(nn.Module): |