MCPcopy Create free account
hub / github.com/ChunmingHe/WS-SAM / SlotAttentionModule

Class SlotAttentionModule

lib/Modules.py:379–407  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

377
378
379class SlotAttentionModule(nn.Module):
380 def __init__(self, encoder_dims, resolution, num_slots, iters):
381 super(SlotAttentionModule, self).__init__()
382 self.resolution = resolution
383 self.encoder_pos = SoftPositionEmbed(encoder_dims, ((int(resolution), int(resolution))))
384 self.layer_norm = nn.LayerNorm(encoder_dims)
385 self.mlp = nn.Sequential(
386 nn.Linear(encoder_dims, encoder_dims),
387 nn.ReLU(inplace=True),
388 nn.Linear(encoder_dims, encoder_dims)
389 )
390 self.slot_attention = SlotAttention(iters=iters,
391 num_slots=num_slots,
392 encoder_dims=encoder_dims,
393 hidden_dim=encoder_dims)
394 self.decoder_pos = SoftPositionEmbed(encoder_dims, (int(resolution), int(resolution)))
395 self.conv = nn.Conv2d(encoder_dims*num_slots, encoder_dims, kernel_size=1, padding=0, stride=1)
396
397 def forward(self, x):
398 x = einops.rearrange(x, 'b c h w -> b h w c')
399 x = self.encoder_pos(x)
400 x = spatial_flatten(x)
401 x = self.mlp(self.layer_norm(x))
402 slots = self.slot_attention(x)
403 x = spatial_broadcast(slots, (int(self.resolution), int(self.resolution)))
404 x = self.decoder_pos(x)
405 x = einops.rearrange(x, 'b n h w c -> b (n c) h w')
406 out = self.conv(x)
407 return out
408
409
410class SlotAttentionModule2(nn.Module):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected