| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import os |
| import sys |
| import pdb |
| import torch |
| from torch import nn |
| from torch.nn import functional as F |
|
|
| from lib.models.tools.module_helper import ModuleHelper |
|
|
|
|
| class _SelfAttentionBlock(nn.Module): |
| ''' |
| The basic implementation for self-attention block/non-local block |
| Input: |
| N X C X H X W |
| Parameters: |
| in_channels : the dimension of the input feature map |
| key_channels : the dimension after the key/query transform |
| value_channels : the dimension after the value transform |
| scale : choose the scale to downsample the input feature maps (save memory cost) |
| Return: |
| N X C X H X W |
| position-aware context features.(w/o concate or add with the input) |
| ''' |
| def __init__(self, |
| in_channels, |
| key_channels, |
| value_channels, |
| out_channels=None, |
| scale=1, |
| bn_type=None): |
| super(_SelfAttentionBlock, self).__init__() |
| self.scale = scale |
| self.in_channels = in_channels |
| self.out_channels = out_channels |
| self.key_channels = key_channels |
| self.value_channels = value_channels |
| if out_channels == None: |
| self.out_channels = in_channels |
| self.pool = nn.MaxPool2d(kernel_size=(scale, scale)) |
| self.f_key = nn.Sequential( |
| nn.Conv2d(in_channels=self.in_channels, out_channels=self.key_channels, |
| kernel_size=1, stride=1, padding=0), |
| ModuleHelper.BNReLU(self.key_channels, bn_type=bn_type), |
| nn.Conv2d(in_channels=self.key_channels, out_channels=self.key_channels, |
| kernel_size=1, stride=1, padding=0), |
| ModuleHelper.BNReLU(self.key_channels, bn_type=bn_type), |
| ) |
| self.f_query = nn.Sequential( |
| nn.Conv2d(in_channels=self.in_channels, out_channels=self.key_channels, |
| kernel_size=1, stride=1, padding=0), |
| ModuleHelper.BNReLU(self.key_channels, bn_type=bn_type), |
| nn.Conv2d(in_channels=self.key_channels, out_channels=self.key_channels, |
| kernel_size=1, stride=1, padding=0), |
| ModuleHelper.BNReLU(self.key_channels, bn_type=bn_type), |
| ) |
|
|
| self.f_value = nn.Conv2d(in_channels=self.in_channels, out_channels=self.value_channels, |
| kernel_size=1, stride=1, padding=0) |
| self.W = nn.Conv2d(in_channels=self.value_channels, out_channels=self.out_channels, |
| kernel_size=1, stride=1, padding=0) |
| nn.init.constant_(self.W.weight, 0) |
| nn.init.constant_(self.W.bias, 0) |
|
|
| def forward(self, x): |
| batch_size, h, w = x.size(0), x.size(2), x.size(3) |
| if self.scale > 1: |
| x = self.pool(x) |
| value = self.f_value(x).view(batch_size, self.value_channels, -1) |
| value = value.permute(0, 2, 1) |
| query = self.f_query(x).view(batch_size, self.key_channels, -1) |
| query = query.permute(0, 2, 1) |
| key = self.f_key(x).view(batch_size, self.key_channels, -1) |
|
|
| sim_map = torch.matmul(query, key) |
| sim_map = (self.key_channels**-.5) * sim_map |
| sim_map = F.softmax(sim_map, dim=-1) |
|
|
| context = torch.matmul(sim_map, value) |
| context = context.permute(0, 2, 1).contiguous() |
| context = context.view(batch_size, self.value_channels, *x.size()[2:]) |
| context = self.W(context) |
| if self.scale > 1: |
| context = F.interpolate(input=context, size=(h, w), mode='bilinear', align_corners=True) |
| return context |
|
|
|
|
| class SelfAttentionBlock2D(_SelfAttentionBlock): |
| def __init__(self, |
| in_channels, |
| key_channels, |
| value_channels, |
| out_channels=None, |
| scale=1, |
| bn_type=None): |
| super(SelfAttentionBlock2D, self).__init__(in_channels, |
| key_channels, |
| value_channels, |
| out_channels, |
| scale, bn_type) |
|
|
|
|
| class BaseOC_Module(nn.Module): |
| """ |
| Implementation of the BaseOC module |
| Parameters: |
| in_features / out_features: the channels of the input / output feature maps. |
| dropout: we choose 0.05 as the default value. |
| size: you can apply multiple sizes. Here we only use one size. |
| Return: |
| features fused with Object context information. |
| """ |
| def __init__(self, |
| in_channels, |
| out_channels, |
| key_channels, |
| value_channels, |
| dropout, |
| sizes=([1]), |
| bn_type=None): |
| super(BaseOC_Module, self).__init__() |
| self.stages = [] |
| self.stages = nn.ModuleList([self._make_stage(in_channels, in_channels, |
| key_channels, value_channels, size, bn_type) for size in sizes]) |
| self.conv_bn_dropout = nn.Sequential( |
| nn.Conv2d(2*in_channels, out_channels, kernel_size=1, padding=0), |
| ModuleHelper.BNReLU(out_channels, bn_type=bn_type), |
| nn.Dropout2d(dropout) |
| ) |
|
|
| def _make_stage(self, in_channels, output_channels, key_channels, value_channels, size, bn_type): |
| return SelfAttentionBlock2D(in_channels, |
| key_channels, |
| value_channels, |
| output_channels, |
| size, |
| bn_type=bn_type) |
| |
| def forward(self, feats): |
| priors = [stage(feats) for stage in self.stages] |
| context = priors[0] |
| for i in range(1, len(priors)): |
| context += priors[i] |
| output = self.conv_bn_dropout(torch.cat([context, feats], 1)) |
| return output |
|
|
|
|
| class BaseOC_Context_Module(nn.Module): |
| """ |
| Output only the context features. |
| Parameters: |
| in_features / out_features: the channels of the input / output feature maps. |
| dropout: specify the dropout ratio |
| fusion: We provide two different fusion method, "concat" or "add" |
| size: we find that directly learn the attention weights on even 1/8 feature maps is hard. |
| Return: |
| features after "concat" or "add" |
| """ |
| def __init__(self, in_channels, out_channels, key_channels, value_channels, dropout=0, sizes=([1]), bn_type=None): |
| super(BaseOC_Context_Module, self).__init__() |
| self.stages = [] |
| self.stages = nn.ModuleList([self._make_stage(in_channels, out_channels, |
| key_channels, value_channels, size, bn_type) for size in sizes]) |
| self.conv_bn_dropout = nn.Sequential( |
| ModuleHelper.BNReLU(out_channels, bn_type=bn_type), |
| nn.Dropout2d(dropout), |
| ) |
|
|
| def _make_stage(self, in_channels, output_channels, key_channels, value_channels, size, bn_type): |
| return SelfAttentionBlock2D(in_channels, |
| key_channels, |
| value_channels, |
| output_channels, |
| size, bn_type=bn_type) |
| |
| def forward(self, feats): |
| priors = [stage(feats) for stage in self.stages] |
| context = priors[0] |
| for i in range(1, len(priors)): |
| context += priors[i] |
| output = self.conv_bn_dropout(context) |
| return output |
|
|
| if __name__ == "__main__": |
| os.environ["CUDA_VISIBLE_DEVICES"] = '0' |
| feats = torch.randn((1, 2048, 128, 128)).cuda() |
|
|
| conv_3x3 = nn.Sequential( |
| nn.Conv2d(2048, 512, kernel_size=3, stride=1, padding=1), |
| ModuleHelper.BNReLU(512, bn_type='torchsyncbn'), |
| ) |
| baseoc_infer = BaseOC_Module(in_channels=512, |
| out_channels=512, |
| key_channels=256, |
| value_channels=256, |
| sizes=([1]), |
| dropout=0, |
| bn_type='torchsyncbn') |
| baseoc_infer.eval() |
| conv_3x3.eval() |
| baseoc_infer.cuda() |
| conv_3x3.cuda() |
| |
| def count_parameters(model): |
| return sum(p.numel() for p in model.parameters() if p.requires_grad) |
|
|
| avg_time = 0 |
| avg_mem = 0 |
| import time |
| with torch.no_grad(): |
| for i in range(100): |
| start_time = time.time() |
| outputs = conv_3x3(feats) |
| outputs = baseoc_infer(outputs) |
| torch.cuda.synchronize() |
| avg_time += (time.time() - start_time) |
| avg_mem += (torch.cuda.max_memory_allocated()-feats.element_size() * feats.nelement()) |
|
|
| print("Average Parameters : {}".format(count_parameters(baseoc_infer)+count_parameters(conv_3x3))) |
| print("Average Running Time: {}".format(avg_time/100)) |
| print("Average GPU Memory: {:.2f} MB".format(avg_mem / 100 / 2**20)) |