-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnet.py
67 lines (52 loc) · 1.91 KB
/
net.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
59
60
61
62
63
64
65
66
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,feature_name):
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.feature_name=feature_name
if self.feature_name=='ramac':
self.ramac=Ramac_Pooling()
if self.feature_name=='Grmac':
self.Grmac=Grmac_Pooling(p=3.5)
if self.feature_name=='AMAC':
self.AMAC=AMAC_Pooling()
def forward(self,data):
feature=self.backbone(data)
if self.feature_name=='ramac':
feature=self.ramac(feature)
if self.feature_name=='Grmac':
feature=self.Grmac(feature)
if self.feature_name=='AMAC':
feature=self.AMAC(feature)
feature=self.norm(feature)
return feature