<filename>model/models/feat.py
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F

from model.models import FewShotModel


def conv3x3(in_channels, out_channels, stride=1):
    return nn.Conv1d(in_channels, out_channels, kernel_size=3,
                     stride=stride, padding=1, bias=False)


class ChannelAttention(nn.Module):
    def __init__(self, in_planes, rotio=3):
        super(ChannelAttention, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool1d(1)
        self.max_pool = nn.AdaptiveMaxPool1d(1)

        self.sharedMLP = nn.Sequential(
            nn.Conv1d(in_planes, in_planes // rotio, 1, bias=False), nn.ReLU(),
            nn.Conv1d(in_planes // rotio, in_planes, 1, bias=False))
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avgout = self.sharedMLP(self.avg_pool(x))
        maxout = self.sharedMLP(self.max_pool(x))
        return self.sigmoid(avgout + maxout)


class SpatialAttention(nn.Module):
    def __init__(self, kernel_size=7):
        super(SpatialAttention, self).__init__()
        assert kernel_size in (3, 7), "kernel size must be 3 or 7"
        padding = 3 if kernel_size == 7 else 1

        self.conv = nn.Conv1d(2, 1, kernel_size,
                              padding=padding, bias=False)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avgout = torch.mean(x, dim=1, keepdim=True)
        maxout, _ = torch.max(x, dim=1, keepdim=True)
        x = torch.cat([avgout, maxout], dim=1)
        x = self.conv(x)
        return self.sigmoid(x)


class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm1d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm1d(planes)
        self.ca = ChannelAttention(planes)
        self.sa = SpatialAttention()
        self.downsample = downsample
        self.stride = stride

    def forward(self, x, y=0, z=0):
        x = x
        residual = x
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        out = self.ca(out) * out  # 广播机制
        out = self.sa(out) * out  # 广播机制
        if self.downsample is not None:
            residual = self.downsample(x)
        out += residual
        out = self.relu(out)
        return out


class ScaledDotProductAttention(nn.Module):
    ''' Scaled Dot-Product Attention '''

    def __init__(self, temperature, attn_dropout=0.1):
        super().__init__()
        self.temperature = temperature
        self.dropout = nn.Dropout(attn_dropout)
        self.softmax = nn.Softmax(dim=2)

    def forward(self, q, k, v):

        attn = torch.bmm(q, k.transpose(1, 2))
        attn = attn / self.temperature
        log_attn = F.log_softmax(attn, 2)
        attn = self.softmax(attn)
        attn = self.dropout(attn)
        output = torch.bmm(attn, v)
        return output, attn, log_attn


class MultiHeadAttention(nn.Module):
    ''' Multi-Head Attention module '''

    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
        super().__init__()
        self.n_head = n_head
        self.d_k = d_k
        self.d_v = d_v

        self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False)
        self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False)
        self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False)
        nn.init.normal_(self.w_qs.weight, mean=0,
                        std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_ks.weight, mean=0,
                        std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_vs.weight, mean=0,
                        std=np.sqrt(2.0 / (d_model + d_v)))

        self.attention = ScaledDotProductAttention(
            temperature=np.power(d_k, 0.5))

        self.slf_attn = BasicBlock(3, 3)

        self.layer_norm = nn.LayerNorm(d_model)

        self.fc = nn.Linear(n_head * d_v, d_model)
        nn.init.xavier_normal_(self.fc.weight)
        self.dropout = nn.Dropout(dropout)

    def forward(self, q, k, v):
        d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
        sz_b, len_q, _ = q.size()
        sz_b, len_k, _ = k.size()
        sz_b, len_v, _ = v.size()

        residual = q
        q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
        k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
        v = self.w_vs(v).view(sz_b, len_v, n_head, d_v)

        q = q.permute(2, 0, 1, 3).contiguous().view(-1,
                                                    len_q, d_k)  # (n*b) x lq x dk
        k = k.permute(2, 0, 1, 3).contiguous().view(-1,
                                                    len_k, d_k)  # (n*b) x lk x dk
        v = v.permute(2, 0, 1, 3).contiguous().view(-1,
                                                    len_v, d_v)  # (n*b) x lv x dv

        q, k, v = self.slf_attn(torch.cat([
            q.view(*(-1,)+(1, d_k)),
            k.view(*(-1,)+(1, d_k)),
            v.view(*(-1,)+(1, d_k))], dim=1)).chunk(3, dim=1)

        q = q.contiguous().view(-1,
                                len_q, d_k)  # (n*b) x lq x dk
        k = k.contiguous().view(-1,
                                len_k, d_k)  # (n*b) x lk x dk
        v = v.contiguous().view(-1,
                                len_v, d_v)  # (n*b) x lv x dv

        output, attn, log_attn = self.attention(q, k, v)

        output = output.view(n_head, sz_b, len_q, d_v)
        output = output.permute(1, 2, 0, 3).contiguous().view(
            sz_b, len_q, -1)  # b x lq x (n*dv)

        output = self.dropout(self.fc(output))
        output = self.layer_norm(output + residual)

        return output


class FEAT(FewShotModel):
    def __init__(self, args):
        super().__init__(args)
        if args.backbone_class == 'ConvNet':
            hdim = 64
        elif args.backbone_class == 'Res12':
            hdim = 640
        elif args.backbone_class == 'Res18':
            hdim = 512
        elif args.backbone_class == 'WRN':
            hdim = 640
        else:
            raise ValueError('')

        self.slf_attn = MultiHeadAttention(1, hdim, hdim, hdim, dropout=0.5)
        # self.slf_attn = BasicBlock(hdim, hdim)
        # self.slf_attn2 = BasicBlock(20, 20)

    def _forward(self, instance_embs, support_idx, query_idx):
        emb_dim = instance_embs.size(-1)
        # print("{}\n{}\n{}\n".format(instance_embs.shape,
        # support_idx.shape, query_idx.shape))
        # organize support/query data
        support = instance_embs[support_idx.contiguous(
        ).view(-1)].contiguous().view(*(support_idx.shape + (-1,)))
        query = instance_embs[query_idx.contiguous(
        ).view(-1)].contiguous().view(*(query_idx.shape + (-1,)))

        # print("support:{} query:{}".format(support.shape, query.shape))

        # get mean of the support
        proto = support.mean(dim=1)  # Ntask x NK x d
        num_batch = proto.shape[0]
        num_proto = proto.shape[1]
        num_query = np.prod(query_idx.shape[-2:])
        # print("proto:")
        # print(proto.shape)
        # query: (num_batch, num_query, num_proto, num_emb)
        # proto: (num_batch, num_proto, num_emb)
        proto = self.slf_attn(proto, proto, proto)
        if self.args.use_euclidean:
            # (Nbatch*Nq*Nw, 1, d)
            query = query.view(-1, emb_dim).unsqueeze(1)
            proto = proto.unsqueeze(1).expand(
                num_batch, num_query, num_proto, emb_dim).contiguous()
            proto = proto.view(num_batch*num_query, num_proto,
                               emb_dim)  # (Nbatch x Nq, Nk, d)

            logits = - torch.sum((proto - query) ** 2, 2) / \
                self.args.temperature
        else:
            proto = F.normalize(proto, dim=-1)  # normalize for cosine distance
            query = query.view(num_batch, -1, emb_dim)  # (Nbatch,  Nq*Nw, d)

            logits = torch.bmm(query, proto.permute(
                [0, 2, 1])) / self.args.temperature
            logits = logits.view(-1, num_proto)

        # for regularization
        if self.training:
            aux_task = torch.cat([support.view(1, self.args.shot, self.args.way, emb_dim),
                                  query.view(1, self.args.query, self.args.way, emb_dim)], 1)  # T x (K+Kq) x N x d
            num_query = np.prod(aux_task.shape[1: 3])
            aux_task = aux_task.permute([0, 2, 1, 3])
            aux_task = aux_task.contiguous().view(-1, self.args.shot + self.args.query, emb_dim)
            # apply the transformation over the Aug Task
            # print("{} {}".format(self.args.shot, self.args.query))
            # print("aux_task")
            # print(aux_task.shape)
            aux_emb = self.slf_attn(
                aux_task, aux_task, aux_task)  # T x N x (K+Kq) x d
            # compute class mean
            aux_emb = aux_emb.view(
                num_batch, self.args.way, self.args.shot + self.args.query, emb_dim)
            aux_center = torch.mean(aux_emb, 2)  # T x N x d

            if self.args.use_euclidean:
                aux_task = aux_task.permute([1, 0, 2]).contiguous(
                ).view(-1, emb_dim).unsqueeze(1)  # (Nbatch*Nq*Nw, 1, d)
                aux_center = aux_center.unsqueeze(1).expand(
                    num_batch, num_query, num_proto, emb_dim).contiguous()
                aux_center = aux_center.view(
                    num_batch*num_query, num_proto, emb_dim)  # (Nbatch x Nq, Nk, d)

                logits_reg = - \
                    torch.sum((aux_center - aux_task) ** 2, 2) / \
                    self.args.temperature2
            else:
                # normalize for cosine distance
                aux_center = F.normalize(aux_center, dim=-1)
                aux_task = aux_task.permute([1, 0, 2]).contiguous().view(
                    num_batch, -1, emb_dim)  # (Nbatch,  Nq*Nw, d)

                logits_reg = torch.bmm(aux_task, aux_center.permute(
                    [0, 2, 1])) / self.args.temperature2
                logits_reg = logits_reg.view(-1, num_proto)

            return logits, logits_reg
        else:
            return logits

<filename>model/models/feat.py
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F

from model.models import FewShotModel


def conv3x3(in_channels, out_channels, stride=1):
    return nn.Conv1d(in_channels, out_channels, kernel_size=3,
                     stride=stride, padding=1, bias=False)


class ChannelAttention(nn.Module):
    def __init__(self, in_planes, rotio=3):
        super(ChannelAttention, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool1d(1)
        self.max_pool = nn.AdaptiveMaxPool1d(1)

        self.sharedMLP = nn.Sequential(
            nn.Conv1d(in_planes, in_planes // rotio, 1, bias=False), nn.ReLU(),
            nn.Conv1d(in_planes // rotio, in_planes, 1, bias=False))
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avgout = self.sharedMLP(self.avg_pool(x))
        maxout = self.sharedMLP(self.max_pool(x))
        return self.sigmoid(avgout + maxout)


class SpatialAttention(nn.Module):
    def __init__(self, kernel_size=7):
        super(SpatialAttention, self).__init__()
        assert kernel_size in (3, 7), "kernel size must be 3 or 7"
        padding = 3 if kernel_size == 7 else 1

        self.conv = nn.Conv1d(2, 1, kernel_size,
                              padding=padding, bias=False)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avgout = torch.mean(x, dim=1, keepdim=True)
        maxout, _ = torch.max(x, dim=1, keepdim=True)
        x = torch.cat([avgout, maxout], dim=1)
        x = self.conv(x)
        return self.sigmoid(x)


class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm1d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm1d(planes)
        self.ca = ChannelAttention(planes)
        self.sa = SpatialAttention()
        self.downsample = downsample
        self.stride = stride

    def forward(self, x, y=0, z=0):
        x = x
        residual = x
        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        out = self.ca(out) * out  # 广播机制
        out = self.sa(out) * out  # 广播机制
        if self.downsample is not None:
            residual = self.downsample(x)
        out += residual
        out = self.relu(out)
        return out


class ScaledDotProductAttention(nn.Module):
    ''' Scaled Dot-Product Attention '''

    def __init__(self, temperature, attn_dropout=0.1):
        super().__init__()
        self.temperature = temperature
        self.dropout = nn.Dropout(attn_dropout)
        self.softmax = nn.Softmax(dim=2)

    def forward(self, q, k, v):

        attn = torch.bmm(q, k.transpose(1, 2))
        attn = attn / self.temperature
        log_attn = F.log_softmax(attn, 2)
        attn = self.softmax(attn)
        attn = self.dropout(attn)
        output = torch.bmm(attn, v)
        return output, attn, log_attn


class MultiHeadAttention(nn.Module):
    ''' Multi-Head Attention module '''

    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
        super().__init__()
        self.n_head = n_head
        self.d_k = d_k
        self.d_v = d_v

        self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False)
        self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False)
        self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False)
        nn.init.normal_(self.w_qs.weight, mean=0,
                        std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_ks.weight, mean=0,
                        std=np.sqrt(2.0 / (d_model + d_k)))
        nn.init.normal_(self.w_vs.weight, mean=0,
                        std=np.sqrt(2.0 / (d_model + d_v)))

        self.attention = ScaledDotProductAttention(
            temperature=np.power(d_k, 0.5))

        self.slf_attn = BasicBlock(3, 3)

        self.layer_norm = nn.LayerNorm(d_model)

        self.fc = nn.Linear(n_head * d_v, d_model)
        nn.init.xavier_normal_(self.fc.weight)
        self.dropout = nn.Dropout(dropout)

    def forward(self, q, k, v):
        d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
        sz_b, len_q, _ = q.size()
        sz_b, len_k, _ = k.size()
        sz_b, len_v, _ = v.size()

        residual = q
        q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
        k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
        v = self.w_vs(v).view(sz_b, len_v, n_head, d_v)

        q = q.permute(2, 0, 1, 3).contiguous().view(-1,
                                                    len_q, d_k)  # (n*b) x lq x dk
        k = k.permute(2, 0, 1, 3).contiguous().view(-1,
                                                    len_k, d_k)  # (n*b) x lk x dk
        v = v.permute(2, 0, 1, 3).contiguous().view(-1,
                                                    len_v, d_v)  # (n*b) x lv x dv

        q, k, v = self.slf_attn(torch.cat([
            q.view(*(-1,)+(1, d_k)),
            k.view(*(-1,)+(1, d_k)),
            v.view(*(-1,)+(1, d_k))], dim=1)).chunk(3, dim=1)

        q = q.contiguous().view(-1,
                                len_q, d_k)  # (n*b) x lq x dk
        k = k.contiguous().view(-1,
                                len_k, d_k)  # (n*b) x lk x dk
        v = v.contiguous().view(-1,
                                len_v, d_v)  # (n*b) x lv x dv

        output, attn, log_attn = self.attention(q, k, v)

        output = output.view(n_head, sz_b, len_q, d_v)
        output = output.permute(1, 2, 0, 3).contiguous().view(
            sz_b, len_q, -1)  # b x lq x (n*dv)

        output = self.dropout(self.fc(output))
        output = self.layer_norm(output + residual)

        return output


class FEAT(FewShotModel):
    def __init__(self, args):
        super().__init__(args)
        if args.backbone_class == 'ConvNet':
            hdim = 64
        elif args.backbone_class == 'Res12':
            hdim = 640
        elif args.backbone_class == 'Res18':
            hdim = 512
        elif args.backbone_class == 'WRN':
            hdim = 640
        else:
            raise ValueError('')

        self.slf_attn = MultiHeadAttention(1, hdim, hdim, hdim, dropout=0.5)
        # self.slf_attn = BasicBlock(hdim, hdim)
        # self.slf_attn2 = BasicBlock(20, 20)

    def _forward(self, instance_embs, support_idx, query_idx):
        emb_dim = instance_embs.size(-1)
        # print("{}\n{}\n{}\n".format(instance_embs.shape,
        # support_idx.shape, query_idx.shape))
        # organize support/query data
        support = instance_embs[support_idx.contiguous(
        ).view(-1)].contiguous().view(*(support_idx.shape + (-1,)))
        query = instance_embs[query_idx.contiguous(
        ).view(-1)].contiguous().view(*(query_idx.shape + (-1,)))

        # print("support:{} query:{}".format(support.shape, query.shape))

        # get mean of the support
        proto = support.mean(dim=1)  # Ntask x NK x d
        num_batch = proto.shape[0]
        num_proto = proto.shape[1]
        num_query = np.prod(query_idx.shape[-2:])
        # print("proto:")
        # print(proto.shape)
        # query: (num_batch, num_query, num_proto, num_emb)
        # proto: (num_batch, num_proto, num_emb)
        proto = self.slf_attn(proto, proto, proto)
        if self.args.use_euclidean:
            # (Nbatch*Nq*Nw, 1, d)
            query = query.view(-1, emb_dim).unsqueeze(1)
            proto = proto.unsqueeze(1).expand(
                num_batch, num_query, num_proto, emb_dim).contiguous()
            proto = proto.view(num_batch*num_query, num_proto,
                               emb_dim)  # (Nbatch x Nq, Nk, d)

            logits = - torch.sum((proto - query) ** 2, 2) / \
                self.args.temperature
        else:
            proto = F.normalize(proto, dim=-1)  # normalize for cosine distance
            query = query.view(num_batch, -1, emb_dim)  # (Nbatch,  Nq*Nw, d)

            logits = torch.bmm(query, proto.permute(
                [0, 2, 1])) / self.args.temperature
            logits = logits.view(-1, num_proto)

        # for regularization
        if self.training:
            aux_task = torch.cat([support.view(1, self.args.shot, self.args.way, emb_dim),
                                  query.view(1, self.args.query, self.args.way, emb_dim)], 1)  # T x (K+Kq) x N x d
            num_query = np.prod(aux_task.shape[1: 3])
            aux_task = aux_task.permute([0, 2, 1, 3])
            aux_task = aux_task.contiguous().view(-1, self.args.shot + self.args.query, emb_dim)
            # apply the transformation over the Aug Task
            # print("{} {}".format(self.args.shot, self.args.query))
            # print("aux_task")
            # print(aux_task.shape)
            aux_emb = self.slf_attn(
                aux_task, aux_task, aux_task)  # T x N x (K+Kq) x d
            # compute class mean
            aux_emb = aux_emb.view(
                num_batch, self.args.way, self.args.shot + self.args.query, emb_dim)
            aux_center = torch.mean(aux_emb, 2)  # T x N x d

            if self.args.use_euclidean:
                aux_task = aux_task.permute([1, 0, 2]).contiguous(
                ).view(-1, emb_dim).unsqueeze(1)  # (Nbatch*Nq*Nw, 1, d)
                aux_center = aux_center.unsqueeze(1).expand(
                    num_batch, num_query, num_proto, emb_dim).contiguous()
                aux_center = aux_center.view(
                    num_batch*num_query, num_proto, emb_dim)  # (Nbatch x Nq, Nk, d)

                logits_reg = - \
                    torch.sum((aux_center - aux_task) ** 2, 2) / \
                    self.args.temperature2
            else:
                # normalize for cosine distance
                aux_center = F.normalize(aux_center, dim=-1)
                aux_task = aux_task.permute([1, 0, 2]).contiguous().view(
                    num_batch, -1, emb_dim)  # (Nbatch,  Nq*Nw, d)

                logits_reg = torch.bmm(aux_task, aux_center.permute(
                    [0, 2, 1])) / self.args.temperature2
                logits_reg = logits_reg.view(-1, num_proto)

            return logits, logits_reg
        else:
            return logits

# 广播机制 # 广播机制 Scaled Dot-Product Attention Multi-Head Attention module # (n*b) x lq x dk # (n*b) x lk x dk # (n*b) x lv x dv # (n*b) x lq x dk # (n*b) x lk x dk # (n*b) x lv x dv # b x lq x (n*dv) # self.slf_attn = BasicBlock(hdim, hdim) # self.slf_attn2 = BasicBlock(20, 20) # print("{}\n{}\n{}\n".format(instance_embs.shape, # support_idx.shape, query_idx.shape)) # organize support/query data # print("support:{} query:{}".format(support.shape, query.shape)) # get mean of the support # Ntask x NK x d # print("proto:") # print(proto.shape) # query: (num_batch, num_query, num_proto, num_emb) # proto: (num_batch, num_proto, num_emb) # (Nbatch*Nq*Nw, 1, d) # (Nbatch x Nq, Nk, d) # normalize for cosine distance # (Nbatch,  Nq*Nw, d) # for regularization # T x (K+Kq) x N x d # apply the transformation over the Aug Task # print("{} {}".format(self.args.shot, self.args.query)) # print("aux_task") # print(aux_task.shape) # T x N x (K+Kq) x d # compute class mean # T x N x d # (Nbatch*Nq*Nw, 1, d) # (Nbatch x Nq, Nk, d) # normalize for cosine distance # (Nbatch,  Nq*Nw, d)