-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconv.py
114 lines (105 loc) · 3.45 KB
/
conv.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
from torch import nn,square
import torch.nn as nn
from torch.autograd import Function
from typing import Any, Optional, Tuple
import torch.nn as nn
import torch
import torch.nn.functional as F
class GradientReverseFunction(Function):
"""
重写自定义的梯度计算方式
"""
@staticmethod
def forward(ctx: Any, input: torch.Tensor, coeff: Optional[float] = 1.) -> torch.Tensor:
ctx.coeff = coeff
output = input * 1.0
return output
@staticmethod
def backward(ctx: Any, grad_output: torch.Tensor) -> Tuple[torch.Tensor, Any]:
return grad_output.neg() * ctx.coeff, None
class GRL_Layer(nn.Module):
def __init__(self):
super(GRL_Layer, self).__init__()
def forward(self, *input):
return GradientReverseFunction.apply(*input)
class model(nn.Module):
def __init__(self):
super(model, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=10, kernel_size=(1,23), stride=1, padding=0),
nn.BatchNorm2d(10),
nn.ELU(),
nn.Dropout(0.38),
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=10, out_channels=30, kernel_size=(22,1), stride=1, padding=0),
nn.BatchNorm2d(30),
nn.ELU(),
nn.Dropout(0.38),
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=30, out_channels=30, kernel_size=(1,17), stride=1, padding=0),
nn.BatchNorm2d(30),
nn.ELU(),
nn.Dropout(0.38),
)
self.maxpool1 = nn.Sequential(
nn.MaxPool2d(kernel_size=(1,6), stride=6,padding=0),
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=30, out_channels=30, kernel_size=(1,7), stride=1, padding=0),
nn.BatchNorm2d(30),
nn.ELU(),
nn.Dropout(0.38),
)
self.maxpool2 = nn.Sequential(
nn.MaxPool2d(kernel_size=(1,6), stride=6,padding=0),
)
self.grl = GRL_Layer()
self.getfeature = nn.Sequential(
nn.Flatten()
)
self.cls = nn.Sequential(
nn.Flatten(),
nn.LayerNorm(540),
nn.Linear(540,4),
nn.Dropout(0.2),
)
self.adv = nn.Sequential(
nn.Flatten(),
nn.Linear(768,9),
nn.Dropout(0.2),
)
self.out = nn.Sequential(
nn.Softmax(dim=0)
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal(m.weight.data,std=0.01)
m.bias.data.fill_(0.01)
# nn.init.xavier_normal(m.weight.data)
# nn.init.kaiming_normal(m.weight.data)
elif isinstance(m, nn.Linear):
nn.init.normal(m.weight.data,std=0.01)
m.bias.data.fill_(0.01)
def forward(self, x):
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
out = self.maxpool1(out)
out = self.conv4(out)
out = self.maxpool2(out)
out = self.cls(out)
out = self.out(out)
return out
def grl_forward(self,x):
out = self.conv1(x)
out = self.conv2(out)
out = self.conv3(out)
out = self.maxpool1(out)
out = self.conv4(out)
out = self.maxpool2(out)
out = self.grl(out)
out = self.adv(out)
out = self.out(out)
return out