-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnet2.py
58 lines (44 loc) · 1.65 KB
/
net2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from pretrainedmodels import models
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torchvision
import torchvision.transforms as transforms
from pooling import *
__all__=['L2N','resnext101']
#---------------feature extraction------------------#
class L2N(nn.Module):
def __init__(self, eps=1e-6):
super(L2N,self).__init__()
self.eps = eps
def forward(self, x):
return x / (torch.norm(x, p=2, dim=1, keepdim=True) + self.eps).expand_as(x)
def __repr__(self):
return self.__class__.__name__ + '(' + 'eps=' + str(self.eps) + ')'
class model_resnext101(nn.Module):
def __init__(self):
super(model_resnext101,self).__init__()
model=models.resnext101_32x4d()
self.backbone = nn.Sequential(*list(model.children())[0])
def forward(self, data):
features = self.backbone(data)
return features
class resnext101(nn.Module):
def __init__(self,model_path):
super(resnext101, self).__init__()
model = model_resnext101()
self.backbone=model.backbone
checkpoint = torch.load(model_path)['net']
self.backbone.load_state_dict(checkpoint)
self.norm=L2N()
self.ramac=Ramac_Pooling()
self.Grmac=Grmac_Pooling(p=3.5)
self.AMAC=AMAC_Pooling()
def forward(self,data):
feature=self.backbone(data)
feature_ramac=self.norm(self.ramac(feature))
feature_Grmac=self.norm(self.Grmac(feature))
feature_AMAC=self.norm(self.AMAC(feature))
return feature_ramac,feature_Grmac,feature_AMAC