-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccm.py
144 lines (129 loc) · 5.82 KB
/
ccm.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import torch
import torch.nn as nn
import torch.nn.functional as F
device = 0
class Downsample(nn.Module):
def __init__(self, pad_type='reflect', filt_size=3, stride=2, channels=None, pad_off=0):
super(Downsample, self).__init__()
self.filt_size = filt_size
self.pad_off = pad_off
self.pad_sizes = [int(1.*(filt_size-1)/2), int(np.ceil(1.*(filt_size-1)/2)), int(1.*(filt_size-1)/2), int(np.ceil(1.*(filt_size-1)/2))]
self.pad_sizes = [pad_size+pad_off for pad_size in self.pad_sizes]
self.stride = stride
self.off = int((self.stride-1)/2.)
self.channels = channels
if(self.filt_size==1):
a = np.array([1.,])
elif(self.filt_size==2):
a = np.array([1., 1.])
elif(self.filt_size==3):
a = np.array([1., 2., 1.])
elif(self.filt_size==4):
a = np.array([1., 3., 3., 1.])
elif(self.filt_size==5):
a = np.array([1., 4., 6., 4., 1.])
elif(self.filt_size==6):
a = np.array([1., 5., 10., 10., 5., 1.])
elif(self.filt_size==7):
a = np.array([1., 6., 15., 20., 15., 6., 1.])
filt = torch.Tensor(a[:,None]*a[None,:])
filt = filt/torch.sum(filt)
self.register_buffer('filt', filt[None,None,:,:].repeat((self.channels,1,1,1)))
self.pad = get_pad_layer(pad_type)(self.pad_sizes)
def forward(self, inp):
if(self.filt_size==1):
if(self.pad_off==0):
return inp[:,:,::self.stride,::self.stride]
else:
return self.pad(inp)[:,:,::self.stride,::self.stride]
else:
return F.conv2d(self.pad(inp), self.filt, stride=self.stride, groups=inp.shape[1])
def get_pad_layer(pad_type):
if(pad_type in ['refl','reflect']):
PadLayer = nn.ReflectionPad2d
elif(pad_type in ['repl','replicate']):
PadLayer = nn.ReplicationPad2d
elif(pad_type=='zero'):
PadLayer = nn.ZeroPad2d
else:
print('Pad type [%s] not recognized'%pad_type)
return PadLayer
class CropAffine(nn.Module):
def __init__(self):
super(CropAffine,self).__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(3,16,3,1,1),nn.BatchNorm2d(16),nn.ReLU(inplace = True),#
nn.Conv2d(16,16,3,2,1),nn.BatchNorm2d(16),nn.ReLU(inplace = True),#
nn.Conv2d(16,32,3,2,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),#
nn.Conv2d(32,32,3,2,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),#
nn.Conv2d(32,32,3,2,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),#
nn.Conv2d(32,32,3,2,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),#
)
self.loc = nn.Sequential(
nn.Linear(32*2*2,16),
nn.Linear(16, 4),
nn.Sigmoid()
)
self.loc[1].weight.data.zero_()
self.loc[1].bias.data.copy_(torch.tensor([-2.,-2., 2.,2.], dtype=torch.float))
#self.crop = AttentionCropLayer()
def forward(self,x,):
n,_,h,w = x.size()
feature = self.feature_extractor(x)
pos = self.loc(feature.view(-1,32*2*2))
pos_return = pos.clone()
#croped = self.crop(x,pos)
#return croped,pos
pos = pos * h
boxW, boxH = pos[:,2] - pos[:,0],pos[:,3] - pos[:,1]
m = torch.zeros((n,2,3),requires_grad = False).cuda(device)
m[:,0,0] = boxW/w
m[:,0,2] = (pos[:,0]/2 + pos[:,2]/2 - boxW/2) / w
m[:,1,1] = boxH/h
m[:,1,2] = (pos[:,1]/2 + pos[:,3]/2 - boxH/2) / h
grid = F.affine_grid(m, x.size(),align_corners = True)
return F.grid_sample(x, grid,align_corners = True),pos_return
class CropAffineAlias(nn.Module):
def __init__(self):
super(CropAffineAlias,self).__init__()
self.feature_extractor = nn.Sequential(
nn.Conv2d(3,16,3,1,1),nn.BatchNorm2d(16),nn.ReLU(inplace = True),# 96
nn.Conv2d(16,16,3,1,1),nn.BatchNorm2d(16),nn.ReLU(inplace = True),# 48
nn.MaxPool2d(kernel_size=2, stride=1),
Downsample(channels=16, filt_size=3, stride=2),
nn.Conv2d(16,32,3,1,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),# 24
nn.MaxPool2d(kernel_size=2, stride=1),
Downsample(channels=32, filt_size=3, stride=2),
nn.Conv2d(32,32,3,1,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),# 12
nn.MaxPool2d(kernel_size=2, stride=1),
Downsample(channels=32, filt_size=3, stride=2),
nn.Conv2d(32,32,3,1,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),# 6
nn.MaxPool2d(kernel_size=2, stride=1),
Downsample(channels=32, filt_size=3, stride=2),
nn.Conv2d(32,32,3,1,1),nn.BatchNorm2d(32),nn.ReLU(inplace = True),# 3
nn.MaxPool2d(kernel_size=2, stride=1),
Downsample(channels=32, filt_size=3, stride=2),
)
self.loc = nn.Sequential(
nn.Linear(32*2*2,16),
nn.Linear(16, 4),
nn.Sigmoid()
)
self.loc[1].weight.data.zero_()
self.loc[1].bias.data.copy_(torch.tensor([-2.,-2., 2.,2.], dtype=torch.float))
#self.crop = AttentionCropLayer()
def forward(self,x,):
n,_,h,w = x.size()
feature = self.feature_extractor(x)
pos = self.loc(feature.view(-1,32*2*2))
pos_return = pos.clone()
#croped = self.crop(x,pos)
#return croped,pos
boxW, boxH = pos[:,2] - pos[:,0],pos[:,3] - pos[:,1]
m = torch.zeros((n,2,3),requires_grad = False)
m[:,0,0] = boxW
m[:,0,2] = (pos[:,0]/2 + pos[:,2]/2 - boxW/2)
m[:,1,1] = boxH
m[:,1,2] = (pos[:,1]/2 + pos[:,3]/2 - boxH/2)
grid = F.affine_grid(m, x.size(),align_corners = True)
return F.grid_sample(x, grid,align_corners = True),pos_return