近期在学习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