WS_DAN算法代码研读之wsdan.py(三)

本文详细解析了See Better Before Looking Closer论文的PyTorch实现,特别是wsdan.py代码。主要内容包括选择基础网络、加载网络参数,以及如何生成预测概率p、特征矩阵feature_matrix和注意力图attention_map。作为初学者,作者诚邀读者指出可能存在的理解偏差。
摘要由CSDN通过智能技术生成

近期在学习See Better Before Looking Closer: Weakly Supervised Data Augmentation Network for Fine-Grained Visual Classification的pytorch版本代码地址
本文对wsdan.py进行解读,由于本人是小白,理解错误的地方请批评指正。主要功能是选择一种骨干网络,加载对应的参数,返回预测的 p, feature_matrix, attention_map。

"""
WS-DAN models

Hu et al.,
"See Better Before Looking Closer: Weakly Supervised Data Augmentation Network for Fine-Grained Visual Classification",
arXiv:1901.09891

Created: May 04,2019 - Yuchong Gu
Revised: Dec 03,2019 - Yuchong Gu
"""
import logging
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

import models.vgg as vgg
import models.resnet as resnet
from models.inception import inception_v3, BasicConv2d

__all__ = ['WSDAN']
EPSILON = 1e-12

#在WSDAN调用
# Bilinear Attention Pooling###################################################核心函数,实现BAP操作
class BAP(nn.Module):
    def __init__(self, pool='GAP'):
        super(BAP, self).__init__()
        assert pool in ['GAP', 'GMP']#pool有两种选择,一种是GAP,另外一种是GMP
        if pool == 'GAP':
            self.pool = None
        else:
            self.pool = nn.AdaptiveMaxPool2d(1)#自适应平均池化

    def forward(self, features, attentions):
        B, C, H, W = features.size()
        _, M, AH, AW = attentions.size()

        # match size把attentions放大到features大小
        if AH != H or AW != W:
            attentions = F.upsample_bilinear(attentions, size=(H, W))
#************************************************************************************************************************
        # feature_matrix: (B, M, C) -> (B, M * C) 没有使用pool                                                           #*
        if self.pool is None:#(B,C,H,W)*(B,C,H,W)->(i,m,j,k)*(i,m,j,k)-->(i,m*j*k)                                      #*
            fea
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值