-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
153 lines (119 loc) · 5.14 KB
/
models.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
145
146
147
148
149
150
151
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
class Mapping(nn.Module):
def __init__(self, in_dimension, out_dimension):
super(Mapping, self).__init__()
self.preconv = nn.Conv2d(in_dimension, out_dimension, 1, 1, bias=False)
self.preconv_bn = nn.BatchNorm2d(out_dimension)
def forward(self, x):
x = self.preconv(x)
x = self.preconv_bn(x)
return x
def conv3x3x3(in_channel, out_channel):
layer = nn.Sequential(
nn.Conv3d(in_channels=in_channel, out_channels=out_channel, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm3d(out_channel),
# nn.ReLU(inplace=True)
)
return layer
class residual_block(nn.Module):
def __init__(self, in_channel, out_channel):
super(residual_block, self).__init__()
self.conv1 = conv3x3x3(in_channel, out_channel)
self.conv2 = conv3x3x3(out_channel, out_channel)
self.conv3 = conv3x3x3(out_channel, out_channel)
def forward(self, x): # (1,1,100,9,9)
x1 = F.relu(self.conv1(x), inplace=True) # (1,8,100,9,9) (1,16,25,5,5)
x2 = F.relu(self.conv2(x1), inplace=True) # (1,8,100,9,9) (1,16,25,5,5)
x3 = self.conv3(x2) # (1,8,100,9,9) (1,16,25,5,5)
out = F.relu(x1 + x3, inplace=True) # (1,8,100,9,9) (1,16,25,5,5)
return out
class D_Res_3d_CNN(nn.Module):
def __init__(self, in_channel, out_channel1, out_channel2):
super(D_Res_3d_CNN, self).__init__()
self.block1 = residual_block(in_channel, out_channel1)
self.maxpool1 = nn.MaxPool3d(kernel_size=(4, 2, 2), padding=(0, 1, 1), stride=(4, 2, 2))
self.block2 = residual_block(out_channel1, out_channel2)
self.maxpool2 = nn.MaxPool3d(kernel_size=(4, 2, 2), stride=(4, 2, 2), padding=(2, 1, 1))
self.conv = nn.Conv3d(in_channels=out_channel2, out_channels=32, kernel_size=3, bias=False)
def forward(self, x): # x:(400,100,9,9)
x = x.unsqueeze(1) # (400,1,100,9,9)
x = self.block1(x) # (1,8,100,9,9)
x = self.maxpool1(x) # (1,8,25,5,5)
x = self.block2(x) # (1,16,25,5,5)
x = self.maxpool2(x) # (1,16,7,3,3)
x = self.conv(x) # (1,32,5,1,1)
x = x.view(x.shape[0], -1) # (1,160)
# x = F.relu(x)
# y = self.classifier(x)
return x#, y
#############################################################################################################
def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha*iter_num / max_iter)) - (high - low) + low)
def grl_hook(coeff):
def fun1(grad):
return -coeff*grad.clone()
return fun1
class DomainClassifier1(nn.Module):
def __init__(self):# torch.Size([1, 64, 7, 3, 3])
super(DomainClassifier, self).__init__() #
self.layer = nn.Sequential(
nn.Linear(1024, 1024), #nn.Linear(320, 512), nn.Linear(FEATURE_DIM*CLASS_NUM, 1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(1024, 1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(1024, 1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(1024, 1024),
nn.ReLU(),
nn.Dropout(0.5),
)
self.domain = nn.Linear(1024, 1) # 512
def forward(self, x, iter_num):
coeff = calc_coeff(iter_num, 1.0, 0.0, 10,10000.0)
x.register_hook(grl_hook(coeff))
x = self.layer(x)
domain_y = self.domain(x)
return domain_y
class DomainClassifier(nn.Module):
def __init__(self):# torch.Size([1, 64, 7, 3, 3])
super(DomainClassifier, self).__init__() #
self.layer = nn.Sequential(
nn.Linear(1024, 512), #nn.Linear(320, 512), nn.Linear(FEATURE_DIM*CLASS_NUM, 1024),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, 128),
nn.ReLU(),
nn.Dropout(0.5),
)
self.domain = nn.Linear(128, 1) # 512
def forward(self, x, iter_num):
coeff = calc_coeff(iter_num, 1.0, 0.0, 10,10000.0)
x.register_hook(grl_hook(coeff))
x = self.layer(x)
domain_y = self.domain(x)
return domain_y
class RandomLayer(nn.Module):
def __init__(self, input_dim_list=[], output_dim=1024):
super(RandomLayer, self).__init__()
self.input_num = len(input_dim_list)
self.output_dim = output_dim
self.random_matrix = [torch.randn(input_dim_list[i], output_dim) for i in range(self.input_num)]
def forward(self, input_list):
return_list = [torch.mm(input_list[i], self.random_matrix[i]) for i in range(self.input_num)]
return_tensor = return_list[0] / math.pow(float(self.output_dim), 1.0/len(return_list))
for single in return_list[1:]:
return_tensor = torch.mul(return_tensor, single)
return return_tensor
def cuda(self):
super(RandomLayer, self).cuda()
self.random_matrix = [val.cuda() for val in self.random_matrix]