Пример #1
0
    def rmac(self, x):
        y = []
        m_max = AdaptiveMaxPool2d((1, 1))
        m_mean = AdaptiveAvgPool2d((1, 1))
        for r in self.regions:
            x_sliced = x[:, :, r[1]:r[3], r[0]:r[2]]
            if self.power is None:
                x_maxed = m_max(x_sliced)  # x_maxed [B,K]
            else:
                x_maxed = m_mean((x_sliced**self.power))**(1.0 / self.power)
                x_maxed = torch.pow(m_mean((torch.pow(x_sliced, self.power))),
                                    (1.0 / self.power))
            y.append(x_maxed.squeeze(-1).squeeze(-1))
        # y list(N) N [B,K]
        y = torch.stack(y, dim=0)  # y [N,B,K]
        y = y.transpose(0, 1)  # y [B,N,K]

        if self.norm:
            y = F.normalize(y, p=2, dim=-1)  # y [B,N,K]

        m_max = AdaptiveMaxPool2d((1, None))
        if self.sum_fm:
            y = AdaptiveMaxPool2d((1, None))(y)  # y [B,K]
            y = y.squeeze(1)
        return y
Пример #2
0
    def __init__(self, params):
        super(Decoder, self).__init__()

        self.vocab_size = params["vocab_size"]

        self.ada_pool = AdaptiveMaxPool2d((1, None))
        self.end_conv = Conv2d(in_channels=256, out_channels=self.vocab_size+1, kernel_size=(1, 1))
Пример #3
0
 def forward(self, input):
     torch.set_printoptions(threshold=np.inf)
     avg_feat = AdaptiveAvgPool2d(1)(input)
     avg_feat = avg_feat.view(avg_feat.shape[0], -1)
     max_feat = AdaptiveMaxPool2d(1)(input)
     max_feat = max_feat.view(max_feat.shape[0], -1)
     feat = torch.div(torch.add(avg_feat, max_feat), 2.0)
     return feat
Пример #4
0
    def __init__(self, in_planes, ratio=16):
        super(ChannelAttention, self).__init__()
        self.avg_pool = AdaptiveAvgPool2d(1)
        self.max_pool = AdaptiveMaxPool2d(1)

        self.fc1   = Conv2d(in_planes, in_planes // 16, 1, bias=False)
        self.relu1 = ReLU()
        self.fc2   = Conv2d(in_planes // 16, in_planes, 1, bias=False)
        self.sigmoid = Sigmoid()
Пример #5
0
 def __init__(self, in_channels):
     super(CBAM, self).__init__()
     self.in_channels = in_channels
     self.avgpool = AdaptiveAvgPool2d(output_size=(1, 1))
     self.maxpool = AdaptiveMaxPool2d(output_size=(1, 1))
     self.linear = nn.Sequential(Linear(in_channels, in_channels // 2),
                                 Linear(in_channels // 2, in_channels))
     self.relu = ReLU(inplace=True)
     self.avgpool1d = AdaptiveAvgPool1d(output_size=1)
     self.maxpool1d = AdaptiveMaxPool1d(output_size=1)
     self.conv = Conv2d(2, 1, 3, 1, 1)
     self.sigmoid = Sigmoid()
    def __init__(self, params):
        super(VerticalAttention, self).__init__()
        self.att_fc_size = params["att_fc_size"]
        self.features_size = params["features_size"]
        self.use_location = params["use_location"]
        self.use_coverage_vector = params["use_coverage_vector"]
        self.coverage_mode = params["coverage_mode"]
        self.use_hidden = params["use_hidden"]
        self.min_height = params["min_height_feat"]
        self.min_width = params["min_width_feat"]
        self.stop_mode = params["stop_mode"]

        self.ada_pool = AdaptiveMaxPool2d((None, self.min_width))
        self.dense_width = Linear(self.min_width, 1)

        self.dense_enc = Linear(self.features_size, self.att_fc_size)
        self.dense_align = Linear(self.att_fc_size, 1)

        if self.stop_mode == "learned":
            self.ada_pool_height = AdaptiveMaxPool1d(self.min_height)
            self.conv_decision = Conv1d(self.att_fc_size,
                                        self.att_fc_size,
                                        kernel_size=5,
                                        padding=2)
            self.dense_height = Linear(self.min_height, 1)
            if self.use_hidden:
                self.dense_decision = Linear(
                    params["hidden_size"] + self.att_fc_size, 2)
            else:
                self.dense_decision = Linear(self.att_fc_size, 2)
        in_ = 0
        if self.use_location:
            in_ += 1
        if self.use_coverage_vector:
            in_ += 1

        self.norm = InstanceNorm1d(in_, track_running_stats=False)
        self.conv_block = Conv1d(in_, 16, kernel_size=15, padding=7)
        self.dense_conv_block = Linear(16, self.att_fc_size)

        if self.use_hidden:
            self.hidden_size = params["hidden_size"]
            self.dense_hidden = Linear(self.hidden_size, self.att_fc_size)

        self.dropout = Dropout(params["att_dropout"])

        self.h_features = None
Пример #7
0
    def __init__(self, input_n_channels=2):
        super(IcebergSqueezeNetMax, self).__init__()

        self.features = Sequential(*get_squeezenet_features(input_n_channels))

        # Final convolution is initialized differently form the rest
        final_conv = Conv2d(512, 2, kernel_size=1)
        self.classifier = Sequential(Dropout(p=0.5), final_conv,
                                     ReLU(inplace=True), AdaptiveMaxPool2d(1),
                                     Flatten())
        # init weights
        for m in self.modules():
            if isinstance(m, Conv2d):
                if m is final_conv:
                    torch.nn.init.normal(m.weight.data, mean=0.0, std=0.01)
                else:
                    torch.nn.init.xavier_uniform(m.weight.data)
                if m.bias is not None:
                    m.bias.data.zero_()
Пример #8
0
    def __init__(self,
                 version,
                 classnames,
                 freeze_pretrained=0,
                 frozen_bn=False,
                 pretrained=False,
                 attention=False,
                 oc_net=False,
                 cls_add=False):

        basenet = {
            'efficientdet-d1': 'efficientnet-b1',
            'efficientdet-d2': 'efficientnet-b2',
            'efficientdet-d3': 'efficientnet-b3',
            'efficientdet-d4': 'efficientnet-b4',
            'efficientdet-d5': 'efficientnet-b5',
        }
        basenet = basenet[version]

        super(SingleShotDetectorWithClassifier,
              self).__init__(classnames=classnames,
                             basenet=basenet,
                             version=version,
                             pretrained=pretrained,
                             frozen_bn=frozen_bn)
        classifier_in_channels = self.backbone[-1].out_channels

        classifier = [
            DANetHead(classifier_in_channels, classifier_in_channels)
            if attention else None,
            BaseOC(classifier_in_channels, classifier_in_channels,
                   classifier_in_channels, classifier_in_channels)
            if oc_net else None,
            AdaptiveMaxPool2d(1),
            Flatten(),
            Dropout(p=0.2, inplace=True),
            Linear(classifier_in_channels, 1)
        ]
        classifier = [m for m in classifier if m]
        self.classifier = Sequential(*classifier)
        self.set_pretrained_frozen(freeze_pretrained)
        self.cls_add = cls_add
Пример #9
0
    def __init__(self, *, n_classes: int, n_channels: int = 3):
        super().__init__(n_classes)

        self.featurizer = Sequential(
            Conv2d(n_channels, 32, kernel_size=3, stride=1, padding=1),
            ReLU(inplace=True),
            Conv2d(32, 32, kernel_size=3, padding=0),
            ReLU(inplace=True),
            # MaxPool2d(kernel_size=2, stride=2),
            # Dropout(p=0.25),
            # Conv2d(32, 64, kernel_size=3, padding=1),
            # ReLU(inplace=True),
            # Conv2d(64, 64, kernel_size=3, padding=0),
            # ReLU(inplace=True),
            # MaxPool2d(kernel_size=2, stride=2),
            # Dropout(p=0.25),
            # Conv2d(64, 64, kernel_size=1, padding=0),
            # ReLU(inplace=True),
            AdaptiveMaxPool2d(1),
            Dropout(p=0.25),
            Flatten(),
        )
        self.classifier = Sequential(Linear(32, n_classes))
Пример #10
0
def adaptive_max_pool(input, size):
    return AdaptiveMaxPool2d(size[0], size[1])(input)