├── __init__.py ├── README.md ├── nodes.py ├── seecoder_utils.py ├── seecoder.py ├── seet_tdecoder.py ├── swin.py └── LICENSE /__init__.py: -------------------------------------------------------------------------------- 1 | from .nodes import NODE_CLASS_MAPPINGS 2 | 3 | __all__ = ['NODE_CLASS_MAPPINGS'] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ComfyUI SeeCoder nodes 2 | 3 | This repo contains 2 experimental WIP nodes for [ComfyUI](https://github.com/comfyanonymous/ComfyUI) that let's you use [SeeCoders](https://github.com/SHI-Labs/Prompt-Free-Diffusion). 4 | 5 | ## getting SeeCoders 6 | You can find the seecoders [here](https://huggingface.co/shi-labs/prompt-free-diffusion). They have to be placed at `models/seecoders` 7 | 8 | ## nodes: 9 | 10 | ### SEECoderImageEncode 11 | 12 | this node can be used to create an embedding from an image 13 | 14 | - **image**: the image to encode 15 | - **seecoder_name**: the name of the seecoder 16 | 17 | ### ConcatConditioning 18 | 19 | this node can be used to concat different embeddings together, so you can e.g. create both a text and a visual embedding and concat them together. 20 | 21 | - **conditioning_to**: a set of embeddings to concat something to 22 | - **conditioning_from**: the embedding to concat behind those in **conditioning_to** 23 | 24 | ## TODO: 25 | 26 | - [ ] support for non safetensor formats 27 | - [ ] bring attention layers in line with ones used in comfy -------------------------------------------------------------------------------- /nodes.py: -------------------------------------------------------------------------------- 1 | 2 | import folder_paths 3 | import torch 4 | from .seecoder import SemanticExtractionEncoder, QueryTransformer, Decoder 5 | from .swin import SwinTransformer 6 | import safetensors.torch 7 | import os 8 | import sys 9 | 10 | sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), "comfy")) 11 | 12 | import comfy.model_management 13 | 14 | folder_paths.folder_names_and_paths["seecoder"] = ([os.path.join(folder_paths.models_dir, "seecoders")], folder_paths.supported_pt_extensions) 15 | 16 | _swine_config = { 17 | "embed_dim" : 192, 18 | "depths" : [ 2, 2, 18, 2 ], 19 | "num_heads" : [ 6, 12, 24, 48 ], 20 | "window_size" : 12, 21 | "ape" : False, 22 | "drop_path_rate" : 0.3, 23 | "patch_norm" : True, 24 | } 25 | 26 | _decoder_config = { 27 | "inchannels" : {'res3' : 384, 'res4' : 768, 'res5' : 1536}, 28 | "trans_input_tags" : ['res3', 'res4', 'res5'], 29 | "trans_dim" : 768, 30 | "trans_dropout" : 0.1, 31 | "trans_nheads" : 8, 32 | "trans_feedforward_dim" : 1024, 33 | "trans_num_layers" : 6, 34 | } 35 | 36 | _qt_config = { 37 | "in_channels":768, 38 | "hidden_dim":768, 39 | "num_queries":[4, 144], 40 | "nheads":8, 41 | "num_layers":9, 42 | "feedforward_dim":2048, 43 | "pre_norm":False, 44 | "num_feature_levels":3, 45 | "enforce_input_project":False, 46 | "with_fea2d_pos":False 47 | } 48 | 49 | class SEECoderImageEncode: 50 | @classmethod 51 | def INPUT_TYPES(s): 52 | return {"required": { 53 | "seecoder_name": (folder_paths.get_filename_list("seecoder"), ), 54 | "image": ("IMAGE",), 55 | }} 56 | RETURN_TYPES = ("CONDITIONING",) 57 | FUNCTION = "SEECoderEncode" 58 | 59 | CATEGORY = "conditioning" 60 | 61 | def SEECoderEncode(self, seecoder_name, image): 62 | device = comfy.model_management.get_torch_device() 63 | path = folder_paths.get_full_path("seecoder", seecoder_name) 64 | sd = safetensors.torch.load_file(path, device="cpu") 65 | sd = {k[10:] if k.startswith('ctx.image.') else k: v for k,v in sd.items()} 66 | is_pa = any([x.startswith("qtransformer.pe_layer") for x in sd.keys()]) 67 | 68 | swine_config = _swine_config.copy() 69 | decoder_config = _decoder_config.copy() 70 | qt_config = _qt_config.copy() 71 | if is_pa: 72 | qt_config['with_fea2d_pos'] = True 73 | 74 | swine = SwinTransformer(**swine_config) 75 | decoder = Decoder(**decoder_config) 76 | queryTransformer = QueryTransformer(**qt_config) 77 | 78 | SEE_encoder = SemanticExtractionEncoder(swine, decoder, queryTransformer) 79 | SEE_encoder.load_state_dict(sd) 80 | SEE_encoder = SEE_encoder.to(device) 81 | SEE_encoder.eval() 82 | encoding = SEE_encoder(image.movedim(-1,1).to(device)).cpu() 83 | 84 | return ([[encoding, {}]], ) 85 | 86 | class ConcatConditioning: 87 | @classmethod 88 | def INPUT_TYPES(s): 89 | return {"required": { 90 | "conditioning_to": ("CONDITIONING",), 91 | "conditioning_from": ("CONDITIONING",), 92 | }} 93 | RETURN_TYPES = ("CONDITIONING",) 94 | FUNCTION = "SEECoderEncode" 95 | 96 | CATEGORY = "_for_testing" 97 | 98 | def SEECoderEncode(self, conditioning_to, conditioning_from): 99 | out = [] 100 | 101 | if len(conditioning_from) > 1: 102 | print("Warning: ConditioningAverage conditioning_from contains more than 1 cond, only the first one will actually be applied to conditioning_to.") 103 | 104 | cond_from = conditioning_from[0][0] 105 | 106 | for i in range(len(conditioning_to)): 107 | t1 = conditioning_to[i][0] 108 | tw = torch.cat((t1, cond_from),1) 109 | n = [tw, conditioning_to[i][1].copy()] 110 | out.append(n) 111 | 112 | return (out, ) 113 | 114 | NODE_CLASS_MAPPINGS = { 115 | "SEECoderImageEncode": SEECoderImageEncode, 116 | "ConcatConditioning": ConcatConditioning, 117 | } -------------------------------------------------------------------------------- /seecoder_utils.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | from torch.nn.init import xavier_uniform_, constant_, uniform_, normal_ 5 | import math 6 | import copy 7 | 8 | def _get_clones(module, N): 9 | return nn.ModuleList([copy.deepcopy(module) for i in range(N)]) 10 | 11 | def _get_activation_fn(activation): 12 | """Return an activation function given a string""" 13 | if activation == "relu": 14 | return F.relu 15 | if activation == "gelu": 16 | return F.gelu 17 | if activation == "glu": 18 | return F.glu 19 | raise RuntimeError(f"activation should be relu/gelu, not {activation}.") 20 | 21 | def _is_power_of_2(n): 22 | if (not isinstance(n, int)) or (n < 0): 23 | raise ValueError("invalid input for _is_power_of_2: {} (type: {})".format(n, type(n))) 24 | return (n & (n-1) == 0) and n != 0 25 | 26 | def c2_xavier_fill(module): 27 | # Caffe2 implementation of XavierFill in fact 28 | nn.init.kaiming_uniform_(module.weight, a=1) 29 | if module.bias is not None: 30 | nn.init.constant_(module.bias, 0) 31 | 32 | def with_pos_embed(x, pos): 33 | return x if pos is None else x + pos 34 | 35 | class PositionEmbeddingSine(nn.Module): 36 | def __init__(self, num_pos_feats=64, temperature=256, normalize=False, scale=None): 37 | super().__init__() 38 | self.num_pos_feats = num_pos_feats 39 | self.temperature = temperature 40 | self.normalize = normalize 41 | if scale is not None and normalize is False: 42 | raise ValueError("normalize should be True if scale is passed") 43 | if scale is None: 44 | scale = 2 * math.pi 45 | self.scale = scale 46 | 47 | def forward(self, x, mask=None): 48 | if mask is None: 49 | mask = torch.zeros((x.size(0), x.size(2), x.size(3)), device=x.device, dtype=torch.bool) 50 | not_mask = ~mask 51 | h, w = not_mask.shape[-2:] 52 | minlen = min(h, w) 53 | h_embed = not_mask.cumsum(1, dtype=torch.float32) 54 | w_embed = not_mask.cumsum(2, dtype=torch.float32) 55 | if self.normalize: 56 | eps = 1e-6 57 | h_embed = (h_embed - h/2) / (minlen + eps) * self.scale 58 | w_embed = (w_embed - w/2) / (minlen + eps) * self.scale 59 | 60 | dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32, device=x.device) 61 | dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats) 62 | 63 | pos_w = w_embed[:, :, :, None] / dim_t 64 | pos_h = h_embed[:, :, :, None] / dim_t 65 | pos_w = torch.stack( 66 | (pos_w[:, :, :, 0::2].sin(), pos_w[:, :, :, 1::2].cos()), dim=4 67 | ).flatten(3) 68 | pos_h = torch.stack( 69 | (pos_h[:, :, :, 0::2].sin(), pos_h[:, :, :, 1::2].cos()), dim=4 70 | ).flatten(3) 71 | pos = torch.cat((pos_h, pos_w), dim=3).permute(0, 3, 1, 2) 72 | return pos 73 | 74 | def __repr__(self, _repr_indent=4): 75 | head = "Positional encoding " + self.__class__.__name__ 76 | body = [ 77 | "num_pos_feats: {}".format(self.num_pos_feats), 78 | "temperature: {}".format(self.temperature), 79 | "normalize: {}".format(self.normalize), 80 | "scale: {}".format(self.scale), 81 | ] 82 | # _repr_indent = 4 83 | lines = [head] + [" " * _repr_indent + line for line in body] 84 | return "\n".join(lines) 85 | 86 | class Conv2d_Convenience(nn.Conv2d): 87 | def __init__(self, *args, **kwargs): 88 | norm = kwargs.pop("norm", None) 89 | activation = kwargs.pop("activation", None) 90 | super().__init__(*args, **kwargs) 91 | self.norm = norm 92 | self.activation = activation 93 | 94 | def forward(self, x): 95 | if not torch.jit.is_scripting(): 96 | if x.numel() == 0 and self.training: 97 | assert not isinstance( 98 | self.norm, torch.nn.SyncBatchNorm 99 | ), "SyncBatchNorm does not support empty inputs!" 100 | x = F.conv2d( 101 | x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups 102 | ) 103 | if self.norm is not None: 104 | x = self.norm(x) 105 | if self.activation is not None: 106 | x = self.activation(x) 107 | return x 108 | 109 | -------------------------------------------------------------------------------- /seecoder.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | import copy 5 | 6 | from .seecoder_utils import with_pos_embed 7 | 8 | ########### 9 | # helpers # 10 | ########### 11 | 12 | def _get_clones(module, N): 13 | return nn.ModuleList([copy.deepcopy(module) for i in range(N)]) 14 | 15 | def _get_activation_fn(activation): 16 | """Return an activation function given a string""" 17 | if activation == "relu": 18 | return F.relu 19 | if activation == "gelu": 20 | return F.gelu 21 | if activation == "glu": 22 | return F.glu 23 | raise RuntimeError(f"activation should be relu/gelu, not {activation}.") 24 | 25 | def c2_xavier_fill(module): 26 | # Caffe2 implementation of XavierFill in fact 27 | nn.init.kaiming_uniform_(module.weight, a=1) 28 | if module.bias is not None: 29 | nn.init.constant_(module.bias, 0) 30 | 31 | def with_pos_embed(x, pos): 32 | return x if pos is None else x + pos 33 | 34 | ########### 35 | # Modules # 36 | ########### 37 | 38 | class Conv2d_Convenience(nn.Conv2d): 39 | def __init__(self, *args, **kwargs): 40 | norm = kwargs.pop("norm", None) 41 | activation = kwargs.pop("activation", None) 42 | super().__init__(*args, **kwargs) 43 | self.norm = norm 44 | self.activation = activation 45 | 46 | def forward(self, x): 47 | x = F.conv2d( 48 | x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups) 49 | if self.norm is not None: 50 | x = self.norm(x) 51 | if self.activation is not None: 52 | x = self.activation(x) 53 | return x 54 | 55 | class DecoderLayer(nn.Module): 56 | def __init__(self, 57 | dim=256, 58 | feedforward_dim=1024, 59 | dropout=0.1, 60 | activation="relu", 61 | n_heads=8,): 62 | 63 | super().__init__() 64 | 65 | self.self_attn = nn.MultiheadAttention(dim, n_heads, dropout=dropout) 66 | self.dropout1 = nn.Dropout(dropout) 67 | self.norm1 = nn.LayerNorm(dim) 68 | 69 | self.linear1 = nn.Linear(dim, feedforward_dim) 70 | self.activation = _get_activation_fn(activation) 71 | self.dropout2 = nn.Dropout(dropout) 72 | self.linear2 = nn.Linear(feedforward_dim, dim) 73 | self.dropout3 = nn.Dropout(dropout) 74 | self.norm2 = nn.LayerNorm(dim) 75 | 76 | def forward(self, x): 77 | h = x 78 | h1 = self.self_attn(x, x, x, attn_mask=None)[0] 79 | h = h + self.dropout1(h1) 80 | h = self.norm1(h) 81 | 82 | h2 = self.linear2(self.dropout2(self.activation(self.linear1(h)))) 83 | h = h + self.dropout3(h2) 84 | h = self.norm2(h) 85 | return h 86 | 87 | class DecoderLayerStacked(nn.Module): 88 | def __init__(self, layer, num_layers, norm=None): 89 | super().__init__() 90 | self.layers = _get_clones(layer, num_layers) 91 | self.num_layers = num_layers 92 | self.norm = norm 93 | 94 | def forward(self, x): 95 | h = x 96 | for _, layer in enumerate(self.layers): 97 | h = layer(h) 98 | if self.norm is not None: 99 | h = self.norm(h) 100 | return h 101 | 102 | class SelfAttentionLayer(nn.Module): 103 | def __init__(self, channels, nhead, dropout=0.0, 104 | activation="relu", normalize_before=False): 105 | super().__init__() 106 | self.self_attn = nn.MultiheadAttention(channels, nhead, dropout=dropout) 107 | 108 | self.norm = nn.LayerNorm(channels) 109 | self.dropout = nn.Dropout(dropout) 110 | 111 | self.activation = _get_activation_fn(activation) 112 | self.normalize_before = normalize_before 113 | 114 | self._reset_parameters() 115 | 116 | def _reset_parameters(self): 117 | for p in self.parameters(): 118 | if p.dim() > 1: 119 | nn.init.xavier_uniform_(p) 120 | 121 | def forward_post(self, 122 | qkv, 123 | qk_pos = None, 124 | mask = None,): 125 | h = qkv 126 | qk = with_pos_embed(qkv, qk_pos).transpose(0, 1) 127 | v = qkv.transpose(0, 1) 128 | h1 = self.self_attn(qk, qk, v, attn_mask=mask)[0] 129 | h1 = h1.transpose(0, 1) 130 | h = h + self.dropout(h1) 131 | h = self.norm(h) 132 | return h 133 | 134 | def forward_pre(self, tgt, 135 | tgt_mask = None, 136 | tgt_key_padding_mask = None, 137 | query_pos = None): 138 | # deprecated 139 | assert False 140 | tgt2 = self.norm(tgt) 141 | q = k = self.with_pos_embed(tgt2, query_pos) 142 | tgt2 = self.self_attn(q, k, value=tgt2, attn_mask=tgt_mask, 143 | key_padding_mask=tgt_key_padding_mask)[0] 144 | tgt = tgt + self.dropout(tgt2) 145 | return tgt 146 | 147 | def forward(self, *args, **kwargs): 148 | if self.normalize_before: 149 | return self.forward_pre(*args, **kwargs) 150 | return self.forward_post(*args, **kwargs) 151 | 152 | class CrossAttentionLayer(nn.Module): 153 | def __init__(self, channels, nhead, dropout=0.0, 154 | activation="relu", normalize_before=False): 155 | super().__init__() 156 | self.multihead_attn = nn.MultiheadAttention(channels, nhead, dropout=dropout) 157 | 158 | self.norm = nn.LayerNorm(channels) 159 | self.dropout = nn.Dropout(dropout) 160 | 161 | self.activation = _get_activation_fn(activation) 162 | self.normalize_before = normalize_before 163 | 164 | self._reset_parameters() 165 | 166 | def _reset_parameters(self): 167 | for p in self.parameters(): 168 | if p.dim() > 1: 169 | nn.init.xavier_uniform_(p) 170 | 171 | def forward_post(self, 172 | q, 173 | kv, 174 | q_pos = None, 175 | k_pos = None, 176 | mask = None,): 177 | h = q 178 | q = with_pos_embed(q, q_pos).transpose(0, 1) 179 | k = with_pos_embed(kv, k_pos).transpose(0, 1) 180 | v = kv.transpose(0, 1) 181 | h1 = self.multihead_attn(q, k, v, attn_mask=mask)[0] 182 | h1 = h1.transpose(0, 1) 183 | h = h + self.dropout(h1) 184 | h = self.norm(h) 185 | return h 186 | 187 | def forward_pre(self, tgt, memory, 188 | memory_mask = None, 189 | memory_key_padding_mask = None, 190 | pos = None, 191 | query_pos = None): 192 | # Deprecated 193 | assert False 194 | tgt2 = self.norm(tgt) 195 | tgt2 = self.multihead_attn(query=self.with_pos_embed(tgt2, query_pos), 196 | key=self.with_pos_embed(memory, pos), 197 | value=memory, attn_mask=memory_mask, 198 | key_padding_mask=memory_key_padding_mask)[0] 199 | tgt = tgt + self.dropout(tgt2) 200 | return tgt 201 | 202 | def forward(self, *args, **kwargs): 203 | if self.normalize_before: 204 | return self.forward_pre(*args, **kwargs) 205 | return self.forward_post(*args, **kwargs) 206 | 207 | class FeedForwardLayer(nn.Module): 208 | def __init__(self, channels, hidden_channels=2048, dropout=0.0, 209 | activation="relu", normalize_before=False): 210 | super().__init__() 211 | self.linear1 = nn.Linear(channels, hidden_channels) 212 | self.dropout = nn.Dropout(dropout) 213 | self.linear2 = nn.Linear(hidden_channels, channels) 214 | self.norm = nn.LayerNorm(channels) 215 | self.activation = _get_activation_fn(activation) 216 | self.normalize_before = normalize_before 217 | self._reset_parameters() 218 | 219 | def _reset_parameters(self): 220 | for p in self.parameters(): 221 | if p.dim() > 1: 222 | nn.init.xavier_uniform_(p) 223 | 224 | def forward_post(self, x): 225 | h = x 226 | h1 = self.linear2(self.dropout(self.activation(self.linear1(h)))) 227 | h = h + self.dropout(h1) 228 | h = self.norm(h) 229 | return h 230 | 231 | def forward_pre(self, x): 232 | xn = self.norm(x) 233 | h = x 234 | h1 = self.linear2(self.dropout(self.activation(self.linear1(xn)))) 235 | h = h + self.dropout(h1) 236 | return h 237 | 238 | def forward(self, *args, **kwargs): 239 | if self.normalize_before: 240 | return self.forward_pre(*args, **kwargs) 241 | return self.forward_post(*args, **kwargs) 242 | 243 | class MLP(nn.Module): 244 | def __init__(self, in_channels, channels, out_channels, num_layers): 245 | super().__init__() 246 | self.num_layers = num_layers 247 | h = [channels] * (num_layers - 1) 248 | self.layers = nn.ModuleList( 249 | nn.Linear(n, k) 250 | for n, k in zip([in_channels]+h, h+[out_channels])) 251 | 252 | def forward(self, x): 253 | for i, layer in enumerate(self.layers): 254 | x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x) 255 | return x 256 | 257 | class PPE_MLP(nn.Module): 258 | def __init__(self, freq_num=20, freq_max=None, out_channel=768, mlp_layer=3): 259 | import math 260 | super().__init__() 261 | self.freq_num = freq_num 262 | self.freq_max = freq_max 263 | self.out_channel = out_channel 264 | self.mlp_layer = mlp_layer 265 | self.twopi = 2 * math.pi 266 | 267 | mlp = [] 268 | in_channel = freq_num*4 269 | for idx in range(mlp_layer): 270 | linear = nn.Linear(in_channel, out_channel, bias=True) 271 | nn.init.xavier_normal_(linear.weight) 272 | nn.init.constant_(linear.bias, 0) 273 | mlp.append(linear) 274 | if idx != mlp_layer-1: 275 | mlp.append(nn.SiLU()) 276 | in_channel = out_channel 277 | self.mlp = nn.Sequential(*mlp) 278 | nn.init.constant_(self.mlp[-1].weight, 0) 279 | 280 | def forward(self, x, mask=None): 281 | assert mask is None, "Mask not implemented" 282 | h, w = x.shape[-2:] 283 | minlen = min(h, w) 284 | 285 | h_embed, w_embed = torch.meshgrid(torch.arange(h), torch.arange(w), indexing='ij') 286 | if self.training: 287 | import numpy.random as npr 288 | pertube_h, pertube_w = npr.uniform(-0.5, 0.5), npr.uniform(-0.5, 0.5) 289 | else: 290 | pertube_h, pertube_w = 0, 0 291 | 292 | h_embed = (h_embed+0.5 - h/2 + pertube_h) / (minlen) * self.twopi 293 | w_embed = (w_embed+0.5 - w/2 + pertube_w) / (minlen) * self.twopi 294 | h_embed, w_embed = h_embed.to(x.device).to(x.dtype), w_embed.to(x.device).to(x.dtype) 295 | 296 | dim_t = torch.linspace(0, 1, self.freq_num, dtype=torch.float32, device=x.device) 297 | freq_max = self.freq_max if self.freq_max is not None else minlen/2 298 | dim_t = freq_max ** dim_t.to(x.dtype) 299 | 300 | pos_h = h_embed[:, :, None] * dim_t 301 | pos_w = w_embed[:, :, None] * dim_t 302 | pos = torch.cat((pos_h.sin(), pos_h.cos(), pos_w.sin(), pos_w.cos()), dim=-1) 303 | pos = self.mlp(pos) 304 | pos = pos.permute(2, 0, 1)[None] 305 | return pos 306 | 307 | def __repr__(self, _repr_indent=4): 308 | head = "Positional encoding " + self.__class__.__name__ 309 | body = [ 310 | "num_pos_feats: {}".format(self.num_pos_feats), 311 | "temperature: {}".format(self.temperature), 312 | "normalize: {}".format(self.normalize), 313 | "scale: {}".format(self.scale), 314 | ] 315 | # _repr_indent = 4 316 | lines = [head] + [" " * _repr_indent + line for line in body] 317 | return "\n".join(lines) 318 | 319 | ########### 320 | # Decoder # 321 | ########### 322 | 323 | class Decoder(nn.Module): 324 | def __init__( 325 | self, 326 | inchannels, 327 | trans_input_tags, 328 | trans_num_layers, 329 | trans_dim, 330 | trans_nheads, 331 | trans_dropout, 332 | trans_feedforward_dim,): 333 | 334 | super().__init__() 335 | trans_inchannels = { 336 | k: v for k, v in inchannels.items() if k in trans_input_tags} 337 | fpn_inchannels = { 338 | k: v for k, v in inchannels.items() if k not in trans_input_tags} 339 | 340 | self.trans_tags = sorted(list(trans_inchannels.keys())) 341 | self.fpn_tags = sorted(list(fpn_inchannels.keys())) 342 | self.all_tags = sorted(list(inchannels.keys())) 343 | 344 | if len(self.trans_tags)==0: 345 | assert False # Not allowed 346 | 347 | self.num_trans_lvls = len(self.trans_tags) 348 | 349 | self.inproj_layers = nn.ModuleDict() 350 | for tagi in self.trans_tags: 351 | layeri = nn.Sequential( 352 | nn.Conv2d(trans_inchannels[tagi], trans_dim, kernel_size=1), 353 | nn.GroupNorm(32, trans_dim),) 354 | nn.init.xavier_uniform_(layeri[0].weight, gain=1) 355 | nn.init.constant_(layeri[0].bias, 0) 356 | self.inproj_layers[tagi] = layeri 357 | 358 | tlayer = DecoderLayer( 359 | dim = trans_dim, 360 | n_heads = trans_nheads, 361 | dropout = trans_dropout, 362 | feedforward_dim = trans_feedforward_dim, 363 | activation = 'relu',) 364 | 365 | self.transformer = DecoderLayerStacked(tlayer, trans_num_layers) 366 | for p in self.transformer.parameters(): 367 | if p.dim() > 1: 368 | nn.init.xavier_uniform_(p) 369 | self.level_embed = nn.Parameter(torch.Tensor(len(self.trans_tags), trans_dim)) 370 | nn.init.normal_(self.level_embed) 371 | 372 | self.lateral_layers = nn.ModuleDict() 373 | self.output_layers = nn.ModuleDict() 374 | for tagi in self.all_tags: 375 | lateral_conv = Conv2d_Convenience( 376 | inchannels[tagi], trans_dim, kernel_size=1, 377 | bias=False, norm=nn.GroupNorm(32, trans_dim)) 378 | c2_xavier_fill(lateral_conv) 379 | self.lateral_layers[tagi] = lateral_conv 380 | 381 | for tagi in self.fpn_tags: 382 | output_conv = Conv2d_Convenience( 383 | trans_dim, trans_dim, kernel_size=3, stride=1, padding=1, 384 | bias=False, norm=nn.GroupNorm(32, trans_dim), activation=F.relu,) 385 | c2_xavier_fill(output_conv) 386 | self.output_layers[tagi] = output_conv 387 | 388 | def forward(self, features): 389 | x = [] 390 | spatial_shapes = {} 391 | for idx, tagi in enumerate(self.trans_tags[::-1]): 392 | xi = features[tagi] 393 | xi = self.inproj_layers[tagi](xi) 394 | bs, _, h, w = xi.shape 395 | spatial_shapes[tagi] = (h, w) 396 | xi = xi.flatten(2).transpose(1, 2) + self.level_embed[idx].view(1, 1, -1) 397 | x.append(xi) 398 | 399 | x_length = [xi.shape[1] for xi in x] 400 | x_concat = torch.cat(x, 1) 401 | y_concat = self.transformer(x_concat) 402 | y = torch.split(y_concat, x_length, dim=1) 403 | 404 | out = {} 405 | for idx, tagi in enumerate(self.trans_tags[::-1]): 406 | h, w = spatial_shapes[tagi] 407 | yi = y[idx].transpose(1, 2).view(bs, -1, h, w) 408 | out[tagi] = yi 409 | 410 | for idx, tagi in enumerate(self.all_tags[::-1]): 411 | lconv = self.lateral_layers[tagi] 412 | if tagi in self.trans_tags: 413 | out[tagi] = out[tagi] + lconv(features[tagi]) 414 | tag_save = tagi 415 | else: 416 | oconv = self.output_layers[tagi] 417 | h = lconv(features[tagi]) 418 | oprev = out[tag_save] 419 | h = h + F.interpolate(oconv(oprev), size=h.shape[-2:], mode="bilinear", align_corners=False) 420 | out[tagi] = h 421 | 422 | return out 423 | 424 | ##################### 425 | # Query Transformer # 426 | ##################### 427 | 428 | class QueryTransformer(nn.Module): 429 | def __init__(self, 430 | in_channels, 431 | hidden_dim, 432 | num_queries = [8, 144], 433 | nheads = 8, 434 | num_layers = 9, 435 | feedforward_dim = 2048, 436 | mask_dim = 256, 437 | pre_norm = False, 438 | num_feature_levels = 3, 439 | enforce_input_project = False, 440 | with_fea2d_pos = True): 441 | 442 | super().__init__() 443 | 444 | if with_fea2d_pos: 445 | self.pe_layer = PPE_MLP(freq_num=20, freq_max=None, out_channel=hidden_dim, mlp_layer=3) 446 | else: 447 | self.pe_layer = None 448 | 449 | if in_channels!=hidden_dim or enforce_input_project: 450 | self.input_proj = nn.ModuleList() 451 | for _ in range(num_feature_levels): 452 | self.input_proj.append(nn.Conv2d(in_channels, hidden_dim, kernel_size=1)) 453 | c2_xavier_fill(self.input_proj[-1]) 454 | else: 455 | self.input_proj = None 456 | 457 | self.num_heads = nheads 458 | self.num_layers = num_layers 459 | self.transformer_selfatt_layers = nn.ModuleList() 460 | self.transformer_crossatt_layers = nn.ModuleList() 461 | self.transformer_feedforward_layers = nn.ModuleList() 462 | 463 | for _ in range(self.num_layers): 464 | self.transformer_selfatt_layers.append( 465 | SelfAttentionLayer( 466 | channels=hidden_dim, 467 | nhead=nheads, 468 | dropout=0.0, 469 | normalize_before=pre_norm, )) 470 | 471 | self.transformer_crossatt_layers.append( 472 | CrossAttentionLayer( 473 | channels=hidden_dim, 474 | nhead=nheads, 475 | dropout=0.0, 476 | normalize_before=pre_norm, )) 477 | 478 | self.transformer_feedforward_layers.append( 479 | FeedForwardLayer( 480 | channels=hidden_dim, 481 | hidden_channels=feedforward_dim, 482 | dropout=0.0, 483 | normalize_before=pre_norm, )) 484 | 485 | self.num_queries = num_queries 486 | num_gq, num_lq = self.num_queries 487 | self.init_query = nn.Embedding(num_gq+num_lq, hidden_dim) 488 | self.query_pos_embedding = nn.Embedding(num_gq+num_lq, hidden_dim) 489 | 490 | self.num_feature_levels = num_feature_levels 491 | self.level_embed = nn.Embedding(num_feature_levels, hidden_dim) 492 | 493 | def forward(self, x): 494 | # x is a list of multi-scale feature 495 | assert len(x) == self.num_feature_levels 496 | fea2d = [] 497 | fea2d_pos = [] 498 | size_list = [] 499 | 500 | for i in range(self.num_feature_levels): 501 | size_list.append(x[i].shape[-2:]) 502 | if self.pe_layer is not None: 503 | pi = self.pe_layer(x[i], None).flatten(2) 504 | pi = pi.transpose(1, 2) 505 | else: 506 | pi = None 507 | xi = self.input_proj[i](x[i]) if self.input_proj is not None else x[i] 508 | xi = xi.flatten(2) + self.level_embed.weight[i][None, :, None] 509 | xi = xi.transpose(1, 2) 510 | fea2d.append(xi) 511 | fea2d_pos.append(pi) 512 | 513 | bs, _, _ = fea2d[0].shape 514 | num_gq, num_lq = self.num_queries 515 | gquery = self.init_query.weight[:num_gq].unsqueeze(0).repeat(bs, 1, 1) 516 | lquery = self.init_query.weight[num_gq:].unsqueeze(0).repeat(bs, 1, 1) 517 | 518 | gquery_pos = self.query_pos_embedding.weight[:num_gq].unsqueeze(0).repeat(bs, 1, 1) 519 | lquery_pos = self.query_pos_embedding.weight[num_gq:].unsqueeze(0).repeat(bs, 1, 1) 520 | 521 | for i in range(self.num_layers): 522 | level_index = i % self.num_feature_levels 523 | 524 | qout = self.transformer_crossatt_layers[i]( 525 | q = lquery, 526 | kv = fea2d[level_index], 527 | q_pos = lquery_pos, 528 | k_pos = fea2d_pos[level_index], 529 | mask = None,) 530 | lquery = qout 531 | 532 | qout = self.transformer_selfatt_layers[i]( 533 | qkv = torch.cat([gquery, lquery], dim=1), 534 | qk_pos = torch.cat([gquery_pos, lquery_pos], dim=1),) 535 | 536 | qout = self.transformer_feedforward_layers[i](qout) 537 | 538 | gquery = qout[:, :num_gq] 539 | lquery = qout[:, num_gq:] 540 | 541 | output = torch.cat([gquery, lquery], dim=1) 542 | 543 | return output 544 | 545 | ################## 546 | # Main structure # 547 | ################## 548 | 549 | class SemanticExtractionEncoder(nn.Module): 550 | def __init__(self, 551 | imencoder_cfg, 552 | imdecoder_cfg, 553 | qtransformer_cfg): 554 | super().__init__() 555 | self.imencoder = imencoder_cfg 556 | self.imdecoder = imdecoder_cfg 557 | self.qtransformer = qtransformer_cfg 558 | 559 | def forward(self, x): 560 | fea = self.imencoder(x) 561 | hs = {'res3' : fea['res3'], 562 | 'res4' : fea['res4'], 563 | 'res5' : fea['res5'], } 564 | hs = self.imdecoder(hs) 565 | hs = [hs['res3'], hs['res4'], hs['res5']] 566 | q = self.qtransformer(hs) 567 | return q 568 | 569 | def encode(self, x): 570 | return self(x) 571 | -------------------------------------------------------------------------------- /seet_tdecoder.py: -------------------------------------------------------------------------------- 1 | #import fvcore.nn.weight_init as weight_init #dependency only needed for training? 2 | from typing import Optional 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | 7 | from .seecoder_utils import PositionEmbeddingSine, _get_clones, _get_activation_fn 8 | 9 | ########## 10 | # helper # 11 | ########## 12 | 13 | def with_pos_embed(x, pos): 14 | return x if pos is None else x + pos 15 | 16 | ############## 17 | # One Former # 18 | ############## 19 | 20 | class Transformer(nn.Module): 21 | def __init__(self, 22 | d_model=512, 23 | nhead=8, 24 | num_encoder_layers=6, 25 | num_decoder_layers=6, 26 | dim_feedforward=2048, 27 | dropout=0.1, 28 | activation="relu", 29 | normalize_before=False, 30 | return_intermediate_dec=False,): 31 | 32 | super().__init__() 33 | encoder_layer = TransformerEncoderLayer( 34 | d_model, nhead, dim_feedforward, dropout, activation, normalize_before) 35 | encoder_norm = nn.LayerNorm(d_model) if normalize_before else None 36 | self.encoder = TransformerEncoder(encoder_layer, num_encoder_layers, encoder_norm) 37 | 38 | decoder_layer = TransformerDecoderLayer( 39 | d_model, nhead, dim_feedforward, dropout, activation, normalize_before) 40 | decoder_norm = nn.LayerNorm(d_model) 41 | self.decoder = TransformerDecoder( 42 | decoder_layer, 43 | num_decoder_layers, 44 | decoder_norm, 45 | return_intermediate=return_intermediate_dec,) 46 | 47 | self._reset_parameters() 48 | 49 | self.d_model = d_model 50 | self.nhead = nhead 51 | 52 | def _reset_parameters(self): 53 | for p in self.parameters(): 54 | if p.dim() > 1: 55 | nn.init.xavier_uniform_(p) 56 | 57 | def forward(self, src, mask, query_embed, pos_embed, task_token=None): 58 | # flatten NxCxHxW to HWxNxC 59 | bs, c, h, w = src.shape 60 | src = src.flatten(2).permute(2, 0, 1) 61 | pos_embed = pos_embed.flatten(2).permute(2, 0, 1) 62 | query_embed = query_embed.unsqueeze(1).repeat(1, bs, 1) 63 | if mask is not None: 64 | mask = mask.flatten(1) 65 | 66 | if task_token is None: 67 | tgt = torch.zeros_like(query_embed) 68 | else: 69 | tgt = task_token.repeat(query_embed.shape[0], 1, 1) 70 | 71 | memory = self.encoder(src, src_key_padding_mask=mask, pos=pos_embed) # src = memory 72 | hs = self.decoder( 73 | tgt, memory, memory_key_padding_mask=mask, pos=pos_embed, query_pos=query_embed 74 | ) 75 | return hs.transpose(1, 2), memory.permute(1, 2, 0).view(bs, c, h, w) 76 | 77 | class TransformerEncoder(nn.Module): 78 | def __init__(self, encoder_layer, num_layers, norm=None): 79 | super().__init__() 80 | self.layers = _get_clones(encoder_layer, num_layers) 81 | self.num_layers = num_layers 82 | self.norm = norm 83 | 84 | def forward(self, src, mask=None, src_key_padding_mask=None, pos=None,): 85 | output = src 86 | for layer in self.layers: 87 | output = layer( 88 | output, src_mask=mask, src_key_padding_mask=src_key_padding_mask, pos=pos 89 | ) 90 | if self.norm is not None: 91 | output = self.norm(output) 92 | return output 93 | 94 | class TransformerDecoder(nn.Module): 95 | def __init__(self, decoder_layer, num_layers, norm=None, return_intermediate=False): 96 | super().__init__() 97 | self.layers = _get_clones(decoder_layer, num_layers) 98 | self.num_layers = num_layers 99 | self.norm = norm 100 | self.return_intermediate = return_intermediate 101 | 102 | def forward( 103 | self, 104 | tgt, 105 | memory, 106 | tgt_mask=None, 107 | memory_mask=None, 108 | tgt_key_padding_mask=None, 109 | memory_key_padding_mask=None, 110 | pos=None, 111 | query_pos=None,): 112 | 113 | output = tgt 114 | intermediate = [] 115 | for layer in self.layers: 116 | output = layer( 117 | output, 118 | memory, 119 | tgt_mask=tgt_mask, 120 | memory_mask=memory_mask, 121 | tgt_key_padding_mask=tgt_key_padding_mask, 122 | memory_key_padding_mask=memory_key_padding_mask, 123 | pos=pos, 124 | query_pos=query_pos, 125 | ) 126 | if self.return_intermediate: 127 | intermediate.append(self.norm(output)) 128 | 129 | if self.norm is not None: 130 | output = self.norm(output) 131 | if self.return_intermediate: 132 | intermediate.pop() 133 | intermediate.append(output) 134 | 135 | if self.return_intermediate: 136 | return torch.stack(intermediate) 137 | 138 | return output.unsqueeze(0) 139 | 140 | class TransformerEncoderLayer(nn.Module): 141 | def __init__( 142 | self, 143 | d_model, 144 | nhead, 145 | dim_feedforward=2048, 146 | dropout=0.1, 147 | activation="relu", 148 | normalize_before=False, ): 149 | 150 | super().__init__() 151 | self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) 152 | # Implementation of Feedforward model 153 | self.linear1 = nn.Linear(d_model, dim_feedforward) 154 | self.dropout = nn.Dropout(dropout) 155 | self.linear2 = nn.Linear(dim_feedforward, d_model) 156 | 157 | self.norm1 = nn.LayerNorm(d_model) 158 | self.norm2 = nn.LayerNorm(d_model) 159 | self.dropout1 = nn.Dropout(dropout) 160 | self.dropout2 = nn.Dropout(dropout) 161 | 162 | self.activation = _get_activation_fn(activation) 163 | self.normalize_before = normalize_before 164 | 165 | def with_pos_embed(self, x, pos): 166 | return x if pos is None else x + pos 167 | 168 | def forward_post( 169 | self, 170 | src, 171 | src_mask = None, 172 | src_key_padding_mask = None, 173 | pos = None,): 174 | 175 | q = k = self.with_pos_embed(src, pos) 176 | src2 = self.self_attn( 177 | q, k, value=src, attn_mask=src_mask, key_padding_mask=src_key_padding_mask 178 | )[0] 179 | src = src + self.dropout1(src2) 180 | src = self.norm1(src) 181 | src2 = self.linear2(self.dropout(self.activation(self.linear1(src)))) 182 | src = src + self.dropout2(src2) 183 | src = self.norm2(src) 184 | return src 185 | 186 | def forward_pre( 187 | self, 188 | src, 189 | src_mask = None, 190 | src_key_padding_mask = None, 191 | pos = None,): 192 | 193 | src2 = self.norm1(src) 194 | q = k = self.with_pos_embed(src2, pos) 195 | src2 = self.self_attn( 196 | q, k, value=src2, attn_mask=src_mask, key_padding_mask=src_key_padding_mask 197 | )[0] 198 | src = src + self.dropout1(src2) 199 | src2 = self.norm2(src) 200 | src2 = self.linear2(self.dropout(self.activation(self.linear1(src2)))) 201 | src = src + self.dropout2(src2) 202 | return src 203 | 204 | def forward( 205 | self, 206 | src, 207 | src_mask = None, 208 | src_key_padding_mask = None, 209 | pos = None,): 210 | if self.normalize_before: 211 | return self.forward_pre(src, src_mask, src_key_padding_mask, pos) 212 | return self.forward_post(src, src_mask, src_key_padding_mask, pos) 213 | 214 | class TransformerDecoderLayer(nn.Module): 215 | def __init__( 216 | self, 217 | d_model, 218 | nhead, 219 | dim_feedforward=2048, 220 | dropout=0.1, 221 | activation="relu", 222 | normalize_before=False,): 223 | 224 | super().__init__() 225 | self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) 226 | self.multihead_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) 227 | # Implementation of Feedforward model 228 | self.linear1 = nn.Linear(d_model, dim_feedforward) 229 | self.dropout = nn.Dropout(dropout) 230 | self.linear2 = nn.Linear(dim_feedforward, d_model) 231 | 232 | self.norm1 = nn.LayerNorm(d_model) 233 | self.norm2 = nn.LayerNorm(d_model) 234 | self.norm3 = nn.LayerNorm(d_model) 235 | self.dropout1 = nn.Dropout(dropout) 236 | self.dropout2 = nn.Dropout(dropout) 237 | self.dropout3 = nn.Dropout(dropout) 238 | 239 | self.activation = _get_activation_fn(activation) 240 | self.normalize_before = normalize_before 241 | 242 | def with_pos_embed(self, x, pos): 243 | return x if pos is None else x + pos 244 | 245 | def forward_post( 246 | self, 247 | tgt, 248 | memory, 249 | tgt_mask = None, 250 | memory_mask = None, 251 | tgt_key_padding_mask = None, 252 | memory_key_padding_mask = None, 253 | pos = None, 254 | query_pos = None,): 255 | 256 | q = k = self.with_pos_embed(tgt, query_pos) 257 | tgt2 = self.self_attn( 258 | q, k, value=tgt, attn_mask=tgt_mask, key_padding_mask=tgt_key_padding_mask)[0] 259 | tgt = tgt + self.dropout1(tgt2) 260 | tgt = self.norm1(tgt) 261 | tgt2 = self.multihead_attn( 262 | query=self.with_pos_embed(tgt, query_pos), 263 | key=self.with_pos_embed(memory, pos), 264 | value=memory, 265 | attn_mask=memory_mask, 266 | key_padding_mask=memory_key_padding_mask,)[0] 267 | tgt = tgt + self.dropout2(tgt2) 268 | tgt = self.norm2(tgt) 269 | tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt)))) 270 | tgt = tgt + self.dropout3(tgt2) 271 | tgt = self.norm3(tgt) 272 | return tgt 273 | 274 | def forward_pre( 275 | self, 276 | tgt, 277 | memory, 278 | tgt_mask = None, 279 | memory_mask = None, 280 | tgt_key_padding_mask = None, 281 | memory_key_padding_mask = None, 282 | pos = None, 283 | query_pos = None,): 284 | 285 | tgt2 = self.norm1(tgt) 286 | q = k = self.with_pos_embed(tgt2, query_pos) 287 | tgt2 = self.self_attn( 288 | q, k, value=tgt2, attn_mask=tgt_mask, key_padding_mask=tgt_key_padding_mask 289 | )[0] 290 | tgt = tgt + self.dropout1(tgt2) 291 | tgt2 = self.norm2(tgt) 292 | tgt2 = self.multihead_attn( 293 | query=self.with_pos_embed(tgt2, query_pos), 294 | key=self.with_pos_embed(memory, pos), 295 | value=memory, 296 | attn_mask=memory_mask, 297 | key_padding_mask=memory_key_padding_mask, 298 | )[0] 299 | tgt = tgt + self.dropout2(tgt2) 300 | tgt2 = self.norm3(tgt) 301 | tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2)))) 302 | tgt = tgt + self.dropout3(tgt2) 303 | return tgt 304 | 305 | def forward( 306 | self, 307 | tgt, 308 | memory, 309 | tgt_mask = None, 310 | memory_mask = None, 311 | tgt_key_padding_mask = None, 312 | memory_key_padding_mask = None, 313 | pos = None, 314 | query_pos = None, ): 315 | 316 | if self.normalize_before: 317 | return self.forward_pre( 318 | tgt, 319 | memory, 320 | tgt_mask, 321 | memory_mask, 322 | tgt_key_padding_mask, 323 | memory_key_padding_mask, 324 | pos, 325 | query_pos,) 326 | return self.forward_post( 327 | tgt, 328 | memory, 329 | tgt_mask, 330 | memory_mask, 331 | tgt_key_padding_mask, 332 | memory_key_padding_mask, 333 | pos, 334 | query_pos,) 335 | 336 | class SelfAttentionLayer(nn.Module): 337 | 338 | def __init__(self, d_model, nhead, dropout=0.0, 339 | activation="relu", normalize_before=False): 340 | super().__init__() 341 | self.self_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) 342 | 343 | self.norm = nn.LayerNorm(d_model) 344 | self.dropout = nn.Dropout(dropout) 345 | 346 | self.activation = _get_activation_fn(activation) 347 | self.normalize_before = normalize_before 348 | 349 | self._reset_parameters() 350 | 351 | def _reset_parameters(self): 352 | for p in self.parameters(): 353 | if p.dim() > 1: 354 | nn.init.xavier_uniform_(p) 355 | 356 | def with_pos_embed(self, tensor, pos): 357 | return tensor if pos is None else tensor + pos 358 | 359 | def forward_post(self, tgt, 360 | tgt_mask = None, 361 | tgt_key_padding_mask = None, 362 | query_pos = None): 363 | q = k = self.with_pos_embed(tgt, query_pos).transpose(0 ,1) 364 | tgt2 = self.self_attn(q, k, value=tgt.transpose(0 ,1), attn_mask=tgt_mask, 365 | key_padding_mask=tgt_key_padding_mask)[0] 366 | tgt = tgt + self.dropout(tgt2.transpose(0 ,1)) 367 | tgt = self.norm(tgt) 368 | 369 | return tgt 370 | 371 | def forward_pre(self, tgt, 372 | tgt_mask = None, 373 | tgt_key_padding_mask = None, 374 | query_pos = None): 375 | tgt2 = self.norm(tgt) 376 | q = k = self.with_pos_embed(tgt2, query_pos) 377 | tgt2 = self.self_attn(q, k, value=tgt2, attn_mask=tgt_mask, 378 | key_padding_mask=tgt_key_padding_mask)[0] 379 | tgt = tgt + self.dropout(tgt2) 380 | 381 | return tgt 382 | 383 | def forward(self, tgt, 384 | tgt_mask = None, 385 | tgt_key_padding_mask = None, 386 | query_pos = None): 387 | if self.normalize_before: 388 | return self.forward_pre(tgt, tgt_mask, 389 | tgt_key_padding_mask, query_pos) 390 | return self.forward_post(tgt, tgt_mask, 391 | tgt_key_padding_mask, query_pos) 392 | 393 | class CrossAttentionLayer(nn.Module): 394 | 395 | def __init__(self, d_model, nhead, dropout=0.0, 396 | activation="relu", normalize_before=False): 397 | super().__init__() 398 | self.multihead_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout) 399 | 400 | self.norm = nn.LayerNorm(d_model) 401 | self.dropout = nn.Dropout(dropout) 402 | 403 | self.activation = _get_activation_fn(activation) 404 | self.normalize_before = normalize_before 405 | 406 | self._reset_parameters() 407 | 408 | def _reset_parameters(self): 409 | for p in self.parameters(): 410 | if p.dim() > 1: 411 | nn.init.xavier_uniform_(p) 412 | 413 | def with_pos_embed(self, tensor, pos): 414 | return tensor if pos is None else tensor + pos 415 | 416 | def forward_post(self, tgt, memory, 417 | memory_mask = None, 418 | memory_key_padding_mask = None, 419 | pos = None, 420 | query_pos = None): 421 | tgt2 = self.multihead_attn(query=self.with_pos_embed(tgt, query_pos).transpose(0, 1), 422 | key=self.with_pos_embed(memory, pos).transpose(0, 1), 423 | value=memory.transpose(0, 1), attn_mask=memory_mask, 424 | key_padding_mask=memory_key_padding_mask)[0] 425 | tgt = tgt + self.dropout(tgt2.transpose(0, 1)) 426 | tgt = self.norm(tgt) 427 | 428 | return tgt 429 | 430 | def forward_pre(self, tgt, memory, 431 | memory_mask = None, 432 | memory_key_padding_mask = None, 433 | pos = None, 434 | query_pos = None): 435 | tgt2 = self.norm(tgt) 436 | tgt2 = self.multihead_attn(query=self.with_pos_embed(tgt2, query_pos), 437 | key=self.with_pos_embed(memory, pos), 438 | value=memory, attn_mask=memory_mask, 439 | key_padding_mask=memory_key_padding_mask)[0] 440 | tgt = tgt + self.dropout(tgt2) 441 | 442 | return tgt 443 | 444 | def forward(self, tgt, memory, 445 | memory_mask = None, 446 | memory_key_padding_mask = None, 447 | pos = None, 448 | query_pos = None): 449 | if self.normalize_before: 450 | return self.forward_pre(tgt, memory, memory_mask, 451 | memory_key_padding_mask, pos, query_pos) 452 | return self.forward_post(tgt, memory, memory_mask, 453 | memory_key_padding_mask, pos, query_pos) 454 | 455 | class FFNLayer(nn.Module): 456 | 457 | def __init__(self, d_model, dim_feedforward=2048, dropout=0.0, 458 | activation="relu", normalize_before=False): 459 | super().__init__() 460 | # Implementation of Feedforward model 461 | self.linear1 = nn.Linear(d_model, dim_feedforward) 462 | self.dropout = nn.Dropout(dropout) 463 | self.linear2 = nn.Linear(dim_feedforward, d_model) 464 | 465 | self.norm = nn.LayerNorm(d_model) 466 | 467 | self.activation = _get_activation_fn(activation) 468 | self.normalize_before = normalize_before 469 | 470 | self._reset_parameters() 471 | 472 | def _reset_parameters(self): 473 | for p in self.parameters(): 474 | if p.dim() > 1: 475 | nn.init.xavier_uniform_(p) 476 | 477 | def with_pos_embed(self, tensor, pos): 478 | return tensor if pos is None else tensor + pos 479 | 480 | def forward_post(self, tgt): 481 | tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt)))) 482 | tgt = tgt + self.dropout(tgt2) 483 | tgt = self.norm(tgt) 484 | return tgt 485 | 486 | def forward_pre(self, tgt): 487 | tgt2 = self.norm(tgt) 488 | tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2)))) 489 | tgt = tgt + self.dropout(tgt2) 490 | return tgt 491 | 492 | def forward(self, tgt): 493 | if self.normalize_before: 494 | return self.forward_pre(tgt) 495 | return self.forward_post(tgt) 496 | 497 | class MLP(nn.Module): 498 | """ Very simple multi-layer perceptron (also called FFN)""" 499 | def __init__(self, input_dim, hidden_dim, output_dim, num_layers): 500 | super().__init__() 501 | self.num_layers = num_layers 502 | h = [hidden_dim] * (num_layers - 1) 503 | self.layers = nn.ModuleList(nn.Linear(n, k) for n, k in zip([input_dim] + h, h + [output_dim])) 504 | 505 | def forward(self, x): 506 | for i, layer in enumerate(self.layers): 507 | x = F.relu(layer(x)) if i < self.num_layers - 1 else layer(x) 508 | return x 509 | 510 | class Seet_OneFormer_TDecoder(nn.Module): 511 | def __init__( 512 | self, 513 | in_channels, 514 | mask_classification, 515 | num_classes, 516 | hidden_dim, 517 | num_queries, 518 | nheads, 519 | dropout, 520 | dim_feedforward, 521 | enc_layers, 522 | is_train, 523 | dec_layers, 524 | class_dec_layers, 525 | pre_norm, 526 | mask_dim, 527 | enforce_input_project, 528 | use_task_norm,): 529 | 530 | super().__init__() 531 | 532 | assert mask_classification, "Only support mask classification model" 533 | self.mask_classification = mask_classification 534 | self.is_train = is_train 535 | self.use_task_norm = use_task_norm 536 | 537 | # positional encoding 538 | N_steps = hidden_dim // 2 539 | self.pe_layer = PositionEmbeddingSine(N_steps, normalize=True) 540 | 541 | self.class_transformer = Transformer( 542 | d_model=hidden_dim, 543 | dropout=dropout, 544 | nhead=nheads, 545 | dim_feedforward=dim_feedforward, 546 | num_encoder_layers=enc_layers, 547 | num_decoder_layers=class_dec_layers, 548 | normalize_before=pre_norm, 549 | return_intermediate_dec=False, 550 | ) 551 | 552 | # define Transformer decoder here 553 | self.num_heads = nheads 554 | self.num_layers = dec_layers 555 | self.transformer_self_attention_layers = nn.ModuleList() 556 | self.transformer_cross_attention_layers = nn.ModuleList() 557 | self.transformer_ffn_layers = nn.ModuleList() 558 | 559 | for _ in range(self.num_layers): 560 | self.transformer_self_attention_layers.append( 561 | SelfAttentionLayer( 562 | d_model=hidden_dim, 563 | nhead=nheads, 564 | dropout=0.0, 565 | normalize_before=pre_norm, 566 | ) 567 | ) 568 | 569 | self.transformer_cross_attention_layers.append( 570 | CrossAttentionLayer( 571 | d_model=hidden_dim, 572 | nhead=nheads, 573 | dropout=0.0, 574 | normalize_before=pre_norm, 575 | ) 576 | ) 577 | 578 | self.transformer_ffn_layers.append( 579 | FFNLayer( 580 | d_model=hidden_dim, 581 | dim_feedforward=dim_feedforward, 582 | dropout=0.0, 583 | normalize_before=pre_norm, 584 | ) 585 | ) 586 | 587 | self.decoder_norm = nn.LayerNorm(hidden_dim) 588 | 589 | self.num_queries = num_queries 590 | # learnable query p.e. 591 | self.query_embed = nn.Embedding(num_queries, hidden_dim) 592 | 593 | # level embedding (we always use 3 scales) 594 | self.num_feature_levels = 3 595 | self.level_embed = nn.Embedding(self.num_feature_levels, hidden_dim) 596 | self.input_proj = nn.ModuleList() 597 | for _ in range(self.num_feature_levels): 598 | if in_channels != hidden_dim or enforce_input_project: 599 | self.input_proj.append(nn.Conv2d(in_channels, hidden_dim, kernel_size=1)) 600 | #weight_init.c2_xavier_fill(self.input_proj[-1]) 601 | else: 602 | self.input_proj.append(nn.Sequential()) 603 | 604 | self.class_input_proj = nn.Conv2d(in_channels, hidden_dim, kernel_size=1) 605 | #weight_init.c2_xavier_fill(self.class_input_proj) 606 | 607 | # output FFNs 608 | if self.mask_classification: 609 | self.class_embed = nn.Linear(hidden_dim, num_classes + 1) 610 | self.mask_embed = MLP(hidden_dim, hidden_dim, mask_dim, 3) 611 | 612 | def forward(self, x, mask_features, tasks): 613 | # x is a list of multi-scale feature 614 | assert len(x) == self.num_feature_levels 615 | src = [] 616 | pos = [] 617 | size_list = [] 618 | 619 | for i in range(self.num_feature_levels): 620 | size_list.append(x[i].shape[-2:]) 621 | pos.append(self.pe_layer(x[i], None).flatten(2)) 622 | src.append(self.input_proj[i](x[i]).flatten(2) + self.level_embed.weight[i][None, :, None]) 623 | pos[-1] = pos[-1].transpose(1, 2) 624 | src[-1] = src[-1].transpose(1, 2) 625 | 626 | bs, _, _ = src[0].shape 627 | 628 | query_embed = self.query_embed.weight.unsqueeze(0).repeat(bs, 1, 1) 629 | 630 | tasks = tasks.unsqueeze(0) 631 | if self.use_task_norm: 632 | tasks = self.decoder_norm(tasks) 633 | 634 | feats = self.pe_layer(mask_features, None) 635 | 636 | out_t, _ = self.class_transformer( 637 | feats, None, 638 | self.query_embed.weight[:-1], 639 | self.class_input_proj(mask_features), 640 | tasks if self.use_task_norm else None) 641 | out_t = out_t[0] 642 | 643 | out = torch.cat([out_t, tasks], dim=1) 644 | 645 | output = out.clone() 646 | 647 | predictions_class = [] 648 | predictions_mask = [] 649 | 650 | # prediction heads on learnable query features 651 | outputs_class, outputs_mask, attn_mask = self.forward_prediction_heads( 652 | output, mask_features, attn_mask_target_size=size_list[0]) 653 | predictions_class.append(outputs_class) 654 | predictions_mask.append(outputs_mask) 655 | 656 | for i in range(self.num_layers): 657 | level_index = i % self.num_feature_levels 658 | attn_mask[torch.where(attn_mask.sum(-1) == attn_mask.shape[-1])] = False 659 | 660 | output = self.transformer_cross_attention_layers[i]( 661 | output, src[level_index], 662 | memory_mask=attn_mask, 663 | memory_key_padding_mask=None, 664 | pos=pos[level_index], query_pos=query_embed, ) 665 | 666 | output = self.transformer_self_attention_layers[i]( 667 | output, tgt_mask=None, 668 | tgt_key_padding_mask=None, 669 | query_pos=query_embed, ) 670 | 671 | # FFN 672 | output = self.transformer_ffn_layers[i](output) 673 | 674 | outputs_class, outputs_mask, attn_mask = self.forward_prediction_heads( 675 | output, mask_features, attn_mask_target_size=size_list[(i + 1) % self.num_feature_levels]) 676 | predictions_class.append(outputs_class) 677 | predictions_mask.append(outputs_mask) 678 | 679 | assert len(predictions_class) == self.num_layers + 1 680 | 681 | out = { 682 | 'pred_logits': predictions_class[-1], 683 | 'pred_masks': predictions_mask[-1],} 684 | 685 | return out 686 | 687 | def forward_prediction_heads(self, output, mask_features, attn_mask_target_size): 688 | decoder_output = self.decoder_norm(output) 689 | outputs_class = self.class_embed(decoder_output) 690 | mask_embed = self.mask_embed(decoder_output) 691 | outputs_mask = torch.einsum("bqc,bchw->bqhw", mask_embed, mask_features) 692 | 693 | attn_mask = F.interpolate(outputs_mask, size=attn_mask_target_size, mode="bilinear", align_corners=False) 694 | attn_mask = (attn_mask.sigmoid().flatten(2).unsqueeze(1).repeat(1, self.num_heads, 1, 1).flatten(0, 1) < 0.5).bool() 695 | attn_mask = attn_mask.detach() 696 | 697 | return outputs_class, outputs_mask, attn_mask 698 | -------------------------------------------------------------------------------- /swin.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | import torch.utils.checkpoint as checkpoint 5 | import numpy as np 6 | 7 | 8 | ############################## 9 | # timm.models.layers helpers # 10 | ############################## 11 | 12 | def drop_path(x, drop_prob: float = 0., training: bool = False, scale_by_keep: bool = True): 13 | if drop_prob == 0. or not training: 14 | return x 15 | keep_prob = 1 - drop_prob 16 | shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets 17 | random_tensor = x.new_empty(shape).bernoulli_(keep_prob) 18 | if keep_prob > 0.0 and scale_by_keep: 19 | random_tensor.div_(keep_prob) 20 | return x * random_tensor 21 | 22 | class DropPath(nn.Module): 23 | """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). 24 | """ 25 | def __init__(self, drop_prob: float = 0., scale_by_keep: bool = True): 26 | super(DropPath, self).__init__() 27 | self.drop_prob = drop_prob 28 | self.scale_by_keep = scale_by_keep 29 | 30 | def forward(self, x): 31 | return drop_path(x, self.drop_prob, self.training, self.scale_by_keep) 32 | 33 | def extra_repr(self): 34 | return f'drop_prob={round(self.drop_prob,3):0.3f}' 35 | 36 | def _ntuple(n): 37 | def parse(x): 38 | from itertools import repeat 39 | import collections.abc 40 | if isinstance(x, collections.abc.Iterable) and not isinstance(x, str): 41 | return tuple(x) 42 | return tuple(repeat(x, n)) 43 | return parse 44 | 45 | to_1tuple = _ntuple(1) 46 | to_2tuple = _ntuple(2) 47 | to_3tuple = _ntuple(3) 48 | to_4tuple = _ntuple(4) 49 | to_ntuple = _ntuple 50 | 51 | def _trunc_normal_(tensor, mean, std, a, b): 52 | import warnings 53 | import math 54 | 55 | def norm_cdf(x): 56 | return (1. + math.erf(x / math.sqrt(2.))) / 2. 57 | 58 | if (mean < a - 2 * std) or (mean > b + 2 * std): 59 | warnings.warn("mean is more than 2 std from [a, b] in nn.init.trunc_normal_. " 60 | "The distribution of values may be incorrect.", 61 | stacklevel=2) 62 | 63 | l = norm_cdf((a - mean) / std) 64 | u = norm_cdf((b - mean) / std) 65 | tensor.uniform_(2 * l - 1, 2 * u - 1) 66 | tensor.erfinv_() 67 | tensor.mul_(std * math.sqrt(2.)) 68 | tensor.add_(mean) 69 | tensor.clamp_(min=a, max=b) 70 | return tensor 71 | 72 | def trunc_normal_(tensor, mean=0., std=1., a=-2., b=2.): 73 | with torch.no_grad(): 74 | return _trunc_normal_(tensor, mean, std, a, b) 75 | 76 | ############# 77 | # main swin # 78 | ############# 79 | 80 | class Mlp(nn.Module): 81 | """ Multilayer perceptron.""" 82 | 83 | def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.): 84 | super().__init__() 85 | out_features = out_features or in_features 86 | hidden_features = hidden_features or in_features 87 | self.fc1 = nn.Linear(in_features, hidden_features) 88 | self.act = act_layer() 89 | self.fc2 = nn.Linear(hidden_features, out_features) 90 | self.drop = nn.Dropout(drop) 91 | 92 | def forward(self, x): 93 | x = self.fc1(x) 94 | x = self.act(x) 95 | x = self.drop(x) 96 | x = self.fc2(x) 97 | x = self.drop(x) 98 | return x 99 | 100 | 101 | def window_partition(x, window_size): 102 | """ 103 | Args: 104 | x: (B, H, W, C) 105 | window_size (int): window size 106 | Returns: 107 | windows: (num_windows*B, window_size, window_size, C) 108 | """ 109 | B, H, W, C = x.shape 110 | x = x.view(B, H // window_size, window_size, W // window_size, window_size, C) 111 | windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C) 112 | return windows 113 | 114 | 115 | def window_reverse(windows, window_size, H, W): 116 | """ 117 | Args: 118 | windows: (num_windows*B, window_size, window_size, C) 119 | window_size (int): Window size 120 | H (int): Height of image 121 | W (int): Width of image 122 | Returns: 123 | x: (B, H, W, C) 124 | """ 125 | B = int(windows.shape[0] / (H * W / window_size / window_size)) 126 | x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1) 127 | x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1) 128 | return x 129 | 130 | 131 | class WindowAttention(nn.Module): 132 | """ Window based multi-head self attention (W-MSA) module with relative position bias. 133 | It supports both of shifted and non-shifted window. 134 | Args: 135 | dim (int): Number of input channels. 136 | window_size (tuple[int]): The height and width of the window. 137 | num_heads (int): Number of attention heads. 138 | qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True 139 | qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set 140 | attn_drop (float, optional): Dropout ratio of attention weight. Default: 0.0 141 | proj_drop (float, optional): Dropout ratio of output. Default: 0.0 142 | """ 143 | 144 | def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.): 145 | 146 | super().__init__() 147 | self.dim = dim 148 | self.window_size = window_size # Wh, Ww 149 | self.num_heads = num_heads 150 | head_dim = dim // num_heads 151 | self.scale = qk_scale or head_dim ** -0.5 152 | 153 | # define a parameter table of relative position bias 154 | self.relative_position_bias_table = nn.Parameter( 155 | torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads)) # 2*Wh-1 * 2*Ww-1, nH 156 | 157 | # get pair-wise relative position index for each token inside the window 158 | coords_h = torch.arange(self.window_size[0]) 159 | coords_w = torch.arange(self.window_size[1]) 160 | coords = torch.stack(torch.meshgrid([coords_h, coords_w], indexing='ij')) # 2, Wh, Ww 161 | coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww 162 | relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww 163 | relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2 164 | relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0 165 | relative_coords[:, :, 1] += self.window_size[1] - 1 166 | relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1 167 | relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww 168 | self.register_buffer("relative_position_index", relative_position_index) 169 | 170 | self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) 171 | self.attn_drop = nn.Dropout(attn_drop) 172 | self.proj = nn.Linear(dim, dim) 173 | self.proj_drop = nn.Dropout(proj_drop) 174 | 175 | trunc_normal_(self.relative_position_bias_table, std=.02) 176 | self.softmax = nn.Softmax(dim=-1) 177 | 178 | def forward(self, x, mask=None): 179 | """ Forward function. 180 | Args: 181 | x: input features with shape of (num_windows*B, N, C) 182 | mask: (0/-inf) mask with shape of (num_windows, Wh*Ww, Wh*Ww) or None 183 | """ 184 | B_, N, C = x.shape 185 | qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4) 186 | q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple) 187 | 188 | q = q * self.scale 189 | attn = (q @ k.transpose(-2, -1)) 190 | 191 | relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view( 192 | self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH 193 | relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww 194 | attn = attn + relative_position_bias.unsqueeze(0) 195 | 196 | if mask is not None: 197 | nW = mask.shape[0] 198 | attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0) 199 | attn = attn.view(-1, self.num_heads, N, N) 200 | attn = self.softmax(attn) 201 | else: 202 | attn = self.softmax(attn) 203 | 204 | attn = self.attn_drop(attn) 205 | 206 | x = (attn @ v).transpose(1, 2).reshape(B_, N, C) 207 | x = self.proj(x) 208 | x = self.proj_drop(x) 209 | return x 210 | 211 | 212 | class SwinTransformerBlock(nn.Module): 213 | """ Swin Transformer Block. 214 | Args: 215 | dim (int): Number of input channels. 216 | num_heads (int): Number of attention heads. 217 | window_size (int): Window size. 218 | shift_size (int): Shift size for SW-MSA. 219 | mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. 220 | qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True 221 | qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set. 222 | drop (float, optional): Dropout rate. Default: 0.0 223 | attn_drop (float, optional): Attention dropout rate. Default: 0.0 224 | drop_path (float, optional): Stochastic depth rate. Default: 0.0 225 | act_layer (nn.Module, optional): Activation layer. Default: nn.GELU 226 | norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm 227 | """ 228 | 229 | def __init__(self, dim, num_heads, window_size=7, shift_size=0, 230 | mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0., 231 | act_layer=nn.GELU, norm_layer=nn.LayerNorm): 232 | super().__init__() 233 | self.dim = dim 234 | self.num_heads = num_heads 235 | self.window_size = window_size 236 | self.shift_size = shift_size 237 | self.mlp_ratio = mlp_ratio 238 | assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size" 239 | 240 | self.norm1 = norm_layer(dim) 241 | self.attn = WindowAttention( 242 | dim, window_size=to_2tuple(self.window_size), num_heads=num_heads, 243 | qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop) 244 | 245 | self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity() 246 | self.norm2 = norm_layer(dim) 247 | mlp_hidden_dim = int(dim * mlp_ratio) 248 | self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop) 249 | 250 | self.H = None 251 | self.W = None 252 | 253 | def forward(self, x, mask_matrix): 254 | """ Forward function. 255 | Args: 256 | x: Input feature, tensor size (B, H*W, C). 257 | H, W: Spatial resolution of the input feature. 258 | mask_matrix: Attention mask for cyclic shift. 259 | """ 260 | B, L, C = x.shape 261 | H, W = self.H, self.W 262 | assert L == H * W, "input feature has wrong size" 263 | 264 | shortcut = x 265 | x = self.norm1(x) 266 | x = x.view(B, H, W, C) 267 | 268 | # pad feature maps to multiples of window size 269 | pad_l = pad_t = 0 270 | pad_r = (self.window_size - W % self.window_size) % self.window_size 271 | pad_b = (self.window_size - H % self.window_size) % self.window_size 272 | x = F.pad(x, (0, 0, pad_l, pad_r, pad_t, pad_b)) 273 | _, Hp, Wp, _ = x.shape 274 | 275 | # cyclic shift 276 | if self.shift_size > 0: 277 | shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2)) 278 | attn_mask = mask_matrix 279 | else: 280 | shifted_x = x 281 | attn_mask = None 282 | 283 | # partition windows 284 | x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C 285 | x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C 286 | 287 | # W-MSA/SW-MSA 288 | attn_windows = self.attn(x_windows, mask=attn_mask) # nW*B, window_size*window_size, C 289 | 290 | # merge windows 291 | attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C) 292 | shifted_x = window_reverse(attn_windows, self.window_size, Hp, Wp) # B H' W' C 293 | 294 | # reverse cyclic shift 295 | if self.shift_size > 0: 296 | x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2)) 297 | else: 298 | x = shifted_x 299 | 300 | if pad_r > 0 or pad_b > 0: 301 | x = x[:, :H, :W, :].contiguous() 302 | 303 | x = x.view(B, H * W, C) 304 | 305 | # FFN 306 | x = shortcut + self.drop_path(x) 307 | x = x + self.drop_path(self.mlp(self.norm2(x))) 308 | 309 | return x 310 | 311 | 312 | class PatchMerging(nn.Module): 313 | """ Patch Merging Layer 314 | Args: 315 | dim (int): Number of input channels. 316 | norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm 317 | """ 318 | def __init__(self, dim, norm_layer=nn.LayerNorm): 319 | super().__init__() 320 | self.dim = dim 321 | self.reduction = nn.Linear(4 * dim, 2 * dim, bias=False) 322 | self.norm = norm_layer(4 * dim) 323 | 324 | def forward(self, x, H, W): 325 | """ Forward function. 326 | Args: 327 | x: Input feature, tensor size (B, H*W, C). 328 | H, W: Spatial resolution of the input feature. 329 | """ 330 | B, L, C = x.shape 331 | assert L == H * W, "input feature has wrong size" 332 | 333 | x = x.view(B, H, W, C) 334 | 335 | # padding 336 | pad_input = (H % 2 == 1) or (W % 2 == 1) 337 | if pad_input: 338 | x = F.pad(x, (0, 0, 0, W % 2, 0, H % 2)) 339 | 340 | x0 = x[:, 0::2, 0::2, :] # B H/2 W/2 C 341 | x1 = x[:, 1::2, 0::2, :] # B H/2 W/2 C 342 | x2 = x[:, 0::2, 1::2, :] # B H/2 W/2 C 343 | x3 = x[:, 1::2, 1::2, :] # B H/2 W/2 C 344 | x = torch.cat([x0, x1, x2, x3], -1) # B H/2 W/2 4*C 345 | x = x.view(B, -1, 4 * C) # B H/2*W/2 4*C 346 | 347 | x = self.norm(x) 348 | x = self.reduction(x) 349 | 350 | return x 351 | 352 | 353 | class BasicLayer(nn.Module): 354 | """ A basic Swin Transformer layer for one stage. 355 | Args: 356 | dim (int): Number of feature channels 357 | depth (int): Depths of this stage. 358 | num_heads (int): Number of attention head. 359 | window_size (int): Local window size. Default: 7. 360 | mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4. 361 | qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True 362 | qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set. 363 | drop (float, optional): Dropout rate. Default: 0.0 364 | attn_drop (float, optional): Attention dropout rate. Default: 0.0 365 | drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0 366 | norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm 367 | downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None 368 | use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False. 369 | """ 370 | 371 | def __init__(self, 372 | dim, 373 | depth, 374 | num_heads, 375 | window_size=7, 376 | mlp_ratio=4., 377 | qkv_bias=True, 378 | qk_scale=None, 379 | drop=0., 380 | attn_drop=0., 381 | drop_path=0., 382 | norm_layer=nn.LayerNorm, 383 | downsample=None, 384 | use_checkpoint=False): 385 | super().__init__() 386 | self.window_size = window_size 387 | self.shift_size = window_size // 2 388 | self.depth = depth 389 | self.use_checkpoint = use_checkpoint 390 | 391 | # build blocks 392 | self.blocks = nn.ModuleList([ 393 | SwinTransformerBlock( 394 | dim=dim, 395 | num_heads=num_heads, 396 | window_size=window_size, 397 | shift_size=0 if (i % 2 == 0) else window_size // 2, 398 | mlp_ratio=mlp_ratio, 399 | qkv_bias=qkv_bias, 400 | qk_scale=qk_scale, 401 | drop=drop, 402 | attn_drop=attn_drop, 403 | drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path, 404 | norm_layer=norm_layer) 405 | for i in range(depth)]) 406 | 407 | # patch merging layer 408 | if downsample is not None: 409 | self.downsample = downsample(dim=dim, norm_layer=norm_layer) 410 | else: 411 | self.downsample = None 412 | 413 | def forward(self, x, H, W): 414 | """ Forward function. 415 | Args: 416 | x: Input feature, tensor size (B, H*W, C). 417 | H, W: Spatial resolution of the input feature. 418 | """ 419 | 420 | # calculate attention mask for SW-MSA 421 | Hp = int(np.ceil(H / self.window_size)) * self.window_size 422 | Wp = int(np.ceil(W / self.window_size)) * self.window_size 423 | img_mask = torch.zeros((1, Hp, Wp, 1), device=x.device, dtype=x.dtype) # 1 Hp Wp 1 424 | h_slices = (slice(0, -self.window_size), 425 | slice(-self.window_size, -self.shift_size), 426 | slice(-self.shift_size, None)) 427 | w_slices = (slice(0, -self.window_size), 428 | slice(-self.window_size, -self.shift_size), 429 | slice(-self.shift_size, None)) 430 | cnt = 0 431 | for h in h_slices: 432 | for w in w_slices: 433 | img_mask[:, h, w, :] = cnt 434 | cnt += 1 435 | 436 | mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1 437 | mask_windows = mask_windows.view(-1, self.window_size * self.window_size) 438 | attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) 439 | attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0)) 440 | 441 | for blk in self.blocks: 442 | blk.H, blk.W = H, W 443 | if self.use_checkpoint: 444 | x = checkpoint.checkpoint(blk, x, attn_mask) 445 | else: 446 | x = blk(x, attn_mask) 447 | if self.downsample is not None: 448 | x_down = self.downsample(x, H, W) 449 | Wh, Ww = (H + 1) // 2, (W + 1) // 2 450 | return x, H, W, x_down, Wh, Ww 451 | else: 452 | return x, H, W, x, H, W 453 | 454 | 455 | class PatchEmbed(nn.Module): 456 | """ Image to Patch Embedding 457 | Args: 458 | patch_size (int): Patch token size. Default: 4. 459 | in_chans (int): Number of input image channels. Default: 3. 460 | embed_dim (int): Number of linear projection output channels. Default: 96. 461 | norm_layer (nn.Module, optional): Normalization layer. Default: None 462 | """ 463 | 464 | def __init__(self, patch_size=4, in_chans=3, embed_dim=96, norm_layer=None): 465 | super().__init__() 466 | patch_size = to_2tuple(patch_size) 467 | self.patch_size = patch_size 468 | 469 | self.in_chans = in_chans 470 | self.embed_dim = embed_dim 471 | 472 | self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size) 473 | if norm_layer is not None: 474 | self.norm = norm_layer(embed_dim) 475 | else: 476 | self.norm = None 477 | 478 | def forward(self, x): 479 | """Forward function.""" 480 | # padding 481 | _, _, H, W = x.size() 482 | if W % self.patch_size[1] != 0: 483 | x = F.pad(x, (0, self.patch_size[1] - W % self.patch_size[1])) 484 | if H % self.patch_size[0] != 0: 485 | x = F.pad(x, (0, 0, 0, self.patch_size[0] - H % self.patch_size[0])) 486 | 487 | x = self.proj(x) # B C Wh Ww 488 | if self.norm is not None: 489 | Wh, Ww = x.size(2), x.size(3) 490 | x = x.flatten(2).transpose(1, 2) 491 | x = self.norm(x) 492 | x = x.transpose(1, 2).view(-1, self.embed_dim, Wh, Ww) 493 | 494 | return x 495 | 496 | 497 | class SwinTransformer(nn.Module): 498 | """ Swin Transformer backbone. 499 | A PyTorch impl of : `Swin Transformer: Hierarchical Vision Transformer using Shifted Windows` - 500 | https://arxiv.org/pdf/2103.14030 501 | Args: 502 | pretrain_img_size (int): Input image size for training the pretrained model, 503 | used in absolute postion embedding. Default 224. 504 | patch_size (int | tuple(int)): Patch size. Default: 4. 505 | in_chans (int): Number of input image channels. Default: 3. 506 | embed_dim (int): Number of linear projection output channels. Default: 96. 507 | depths (tuple[int]): Depths of each Swin Transformer stage. 508 | num_heads (tuple[int]): Number of attention head of each stage. 509 | window_size (int): Window size. Default: 7. 510 | mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4. 511 | qkv_bias (bool): If True, add a learnable bias to query, key, value. Default: True 512 | qk_scale (float): Override default qk scale of head_dim ** -0.5 if set. 513 | drop_rate (float): Dropout rate. 514 | attn_drop_rate (float): Attention dropout rate. Default: 0. 515 | drop_path_rate (float): Stochastic depth rate. Default: 0.2. 516 | norm_layer (nn.Module): Normalization layer. Default: nn.LayerNorm. 517 | ape (bool): If True, add absolute position embedding to the patch embedding. Default: False. 518 | patch_norm (bool): If True, add normalization after patch embedding. Default: True. 519 | out_indices (Sequence[int]): Output from which stages. 520 | frozen_stages (int): Stages to be frozen (stop grad and set eval mode). 521 | -1 means not freezing any parameters. 522 | use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False. 523 | """ 524 | 525 | def __init__(self, 526 | pretrain_img_size=224, 527 | patch_size=4, 528 | in_chans=3, 529 | embed_dim=96, 530 | depths=[2, 2, 6, 2], 531 | num_heads=[3, 6, 12, 24], 532 | window_size=7, 533 | mlp_ratio=4., 534 | qkv_bias=True, 535 | qk_scale=None, 536 | drop_rate=0., 537 | attn_drop_rate=0., 538 | drop_path_rate=0.2, 539 | norm_layer=nn.LayerNorm, 540 | ape=False, 541 | patch_norm=True, 542 | out_indices=(0, 1, 2, 3), 543 | frozen_stages=-1, 544 | use_checkpoint=False): 545 | super().__init__() 546 | 547 | self.pretrain_img_size = pretrain_img_size 548 | self.num_layers = len(depths) 549 | self.embed_dim = embed_dim 550 | self.ape = ape 551 | self.patch_norm = patch_norm 552 | self.out_indices = out_indices 553 | self.frozen_stages = frozen_stages 554 | 555 | # split image into non-overlapping patches 556 | self.patch_embed = PatchEmbed( 557 | patch_size=patch_size, in_chans=in_chans, embed_dim=embed_dim, 558 | norm_layer=norm_layer if self.patch_norm else None) 559 | 560 | # absolute position embedding 561 | if self.ape: 562 | pretrain_img_size = to_2tuple(pretrain_img_size) 563 | patch_size = to_2tuple(patch_size) 564 | patches_resolution = [pretrain_img_size[0] // patch_size[0], pretrain_img_size[1] // patch_size[1]] 565 | 566 | self.absolute_pos_embed = nn.Parameter(torch.zeros(1, embed_dim, patches_resolution[0], patches_resolution[1])) 567 | trunc_normal_(self.absolute_pos_embed, std=.02) 568 | 569 | self.pos_drop = nn.Dropout(p=drop_rate) 570 | 571 | # stochastic depth 572 | dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # stochastic depth decay rule 573 | 574 | # build layers 575 | self.layers = nn.ModuleList() 576 | for i_layer in range(self.num_layers): 577 | layer = BasicLayer( 578 | dim=int(embed_dim * 2 ** i_layer), 579 | depth=depths[i_layer], 580 | num_heads=num_heads[i_layer], 581 | window_size=window_size, 582 | mlp_ratio=mlp_ratio, 583 | qkv_bias=qkv_bias, 584 | qk_scale=qk_scale, 585 | drop=drop_rate, 586 | attn_drop=attn_drop_rate, 587 | drop_path=dpr[sum(depths[:i_layer]):sum(depths[:i_layer + 1])], 588 | norm_layer=norm_layer, 589 | downsample=PatchMerging if (i_layer < self.num_layers - 1) else None, 590 | use_checkpoint=use_checkpoint) 591 | self.layers.append(layer) 592 | 593 | num_features = [int(embed_dim * 2 ** i) for i in range(self.num_layers)] 594 | self.num_features = num_features 595 | 596 | # add a norm layer for each output 597 | for i_layer in out_indices: 598 | layer = norm_layer(num_features[i_layer]) 599 | layer_name = f'norm{i_layer}' 600 | self.add_module(layer_name, layer) 601 | 602 | self._freeze_stages() 603 | 604 | def _freeze_stages(self): 605 | if self.frozen_stages >= 0: 606 | self.patch_embed.eval() 607 | for param in self.patch_embed.parameters(): 608 | param.requires_grad = False 609 | 610 | if self.frozen_stages >= 1 and self.ape: 611 | self.absolute_pos_embed.requires_grad = False 612 | 613 | if self.frozen_stages >= 2: 614 | self.pos_drop.eval() 615 | for i in range(0, self.frozen_stages - 1): 616 | m = self.layers[i] 617 | m.eval() 618 | for param in m.parameters(): 619 | param.requires_grad = False 620 | 621 | def forward(self, x): 622 | """Forward function.""" 623 | x = self.patch_embed(x) 624 | 625 | Wh, Ww = x.size(2), x.size(3) 626 | if self.ape: 627 | # interpolate the position embedding to the corresponding size 628 | absolute_pos_embed = F.interpolate(self.absolute_pos_embed, size=(Wh, Ww), mode='bicubic') 629 | x = (x + absolute_pos_embed).flatten(2).transpose(1, 2) # B Wh*Ww C 630 | else: 631 | x = x.flatten(2).transpose(1, 2) 632 | x = self.pos_drop(x) 633 | 634 | outs = [] 635 | for i in range(self.num_layers): 636 | layer = self.layers[i] 637 | x_out, H, W, x, Wh, Ww = layer(x, Wh, Ww) 638 | 639 | if i in self.out_indices: 640 | norm_layer = getattr(self, f'norm{i}') 641 | x_out = norm_layer(x_out) 642 | 643 | out = x_out.view(-1, H, W, self.num_features[i]).permute(0, 3, 1, 2).contiguous() 644 | outs.append(out) 645 | 646 | outputs = { 647 | 'res2' : outs[0], 648 | 'res3' : outs[1], 649 | 'res4' : outs[2], 650 | 'res5' : outs[3],} 651 | return outputs 652 | 653 | def train(self, mode=True): 654 | """Convert the model into training mode while keep layers freezed.""" 655 | super(SwinTransformer, self).train(mode) 656 | self._freeze_stages() 657 | return self 658 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------