-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel.py
More file actions
180 lines (154 loc) · 6.53 KB
/
model.py
File metadata and controls
180 lines (154 loc) · 6.53 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import torch
import torch.nn as nn
from channel import Channel
""" def _image_normalization(norm_type):
def _inner(tensor: torch.Tensor):
if norm_type == 'nomalization':
return tensor / 255.0
elif norm_type == 'denormalization':
return (tensor * 255.0).type(torch.FloatTensor)
else:
raise Exception('Unknown type of normalization')
return _inner """
def ratio2filtersize(x: torch.Tensor, ratio):
if x.dim() == 4:
# before_size = np.prod(x.size()[1:])
before_size = torch.prod(torch.tensor(x.size()[1:]))
elif x.dim() == 3:
# before_size = np.prod(x.size())
before_size = torch.prod(torch.tensor(x.size()))
else:
raise Exception('Unknown size of input')
encoder_temp = _Encoder(is_temp=True)
z_temp = encoder_temp(x)
# c = before_size * ratio / np.prod(z_temp.size()[-2:])
c = before_size * ratio / torch.prod(torch.tensor(z_temp.size()[-2:]))
return int(c)
class _ConvWithPReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0):
super(_ConvWithPReLU, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding)
self.prelu = nn.PReLU()
nn.init.kaiming_normal_(self.conv.weight, mode='fan_out', nonlinearity='leaky_relu')
def forward(self, x):
x = self.conv(x)
x = self.prelu(x)
return x
class _TransConvWithPReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, activate=nn.PReLU(), padding=0, output_padding=0):
super(_TransConvWithPReLU, self).__init__()
self.transconv = nn.ConvTranspose2d(
in_channels, out_channels, kernel_size, stride, padding, output_padding)
self.activate = activate
if activate == nn.PReLU():
nn.init.kaiming_normal_(self.transconv.weight, mode='fan_out',
nonlinearity='leaky_relu')
else:
nn.init.xavier_normal_(self.transconv.weight)
def forward(self, x):
x = self.transconv(x)
x = self.activate(x)
return x
class _Encoder(nn.Module):
def __init__(self, c=1, is_temp=False, P=1):
super(_Encoder, self).__init__()
self.is_temp = is_temp
# self.imgae_normalization = _image_normalization(norm_type='nomalization')
self.conv1 = _ConvWithPReLU(in_channels=3, out_channels=16, kernel_size=5, stride=2, padding=2)
self.conv2 = _ConvWithPReLU(in_channels=16, out_channels=32, kernel_size=5, stride=2, padding=2)
self.conv3 = _ConvWithPReLU(in_channels=32, out_channels=32,
kernel_size=5, padding=2) # padding size could be changed here
self.conv4 = _ConvWithPReLU(in_channels=32, out_channels=32, kernel_size=5, padding=2)
self.conv5 = _ConvWithPReLU(in_channels=32, out_channels=2*c, kernel_size=5, padding=2)
self.norm = self._normlizationLayer(P=P)
@staticmethod
def _normlizationLayer(P=1):
def _inner(z_hat: torch.Tensor):
if z_hat.dim() == 4:
batch_size = z_hat.size()[0]
# k = np.prod(z_hat.size()[1:])
k = torch.prod(torch.tensor(z_hat.size()[1:]))
elif z_hat.dim() == 3:
batch_size = 1
# k = np.prod(z_hat.size())
k = torch.prod(torch.tensor(z_hat.size()))
else:
raise Exception('Unknown size of input')
# k = torch.tensor(k)
z_temp = z_hat.reshape(batch_size, 1, 1, -1)
z_trans = z_hat.reshape(batch_size, 1, -1, 1)
tensor = torch.sqrt(P * k) * z_hat / torch.sqrt((z_temp @ z_trans))
if batch_size == 1:
return tensor.squeeze(0)
return tensor
return _inner
def forward(self, x):
# x = self.imgae_normalization(x)
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
if not self.is_temp:
x = self.conv5(x)
x = self.norm(x)
return x
class _Decoder(nn.Module):
def __init__(self, c=1):
super(_Decoder, self).__init__()
# self.imgae_normalization = _image_normalization(norm_type='denormalization')
self.tconv1 = _TransConvWithPReLU(
in_channels=2*c, out_channels=32, kernel_size=5, stride=1, padding=2)
self.tconv2 = _TransConvWithPReLU(
in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2)
self.tconv3 = _TransConvWithPReLU(
in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2)
self.tconv4 = _TransConvWithPReLU(in_channels=32, out_channels=16, kernel_size=5, stride=2, padding=2, output_padding=1)
self.tconv5 = _TransConvWithPReLU(
in_channels=16, out_channels=3, kernel_size=5, stride=2, padding=2, output_padding=1,activate=nn.Sigmoid())
# may be some problems in tconv4 and tconv5, the kernal_size is not the same as the paper which is 5
def forward(self, x):
x = self.tconv1(x)
x = self.tconv2(x)
x = self.tconv3(x)
x = self.tconv4(x)
x = self.tconv5(x)
# x = self.imgae_normalization(x)
return x
class DeepJSCC(nn.Module):
def __init__(self, c, channel_type='AWGN', snr=None):
super(DeepJSCC, self).__init__()
self.encoder = _Encoder(c=c)
self.snr = snr
if self.snr is not None:
self.channel = Channel(channel_type, snr)
self.decoder = _Decoder(c=c)
def forward(self, x):
z = self.encoder(x)
if hasattr(self, 'channel') and self.channel is not None:
z = self.channel(z)
x_hat = self.decoder(z)
return x_hat
def change_channel(self, channel_type='AWGN', snr=None):
if snr is None:
self.channel = None
else:
self.channel = Channel(channel_type, snr)
def get_channel(self):
if hasattr(self, 'channel') and self.channel is not None:
return self.channel.get_channel()
return None
def loss(self, prd, gt):
criterion = nn.MSELoss(reduction='mean')
loss = criterion(prd, gt)
return loss
if __name__ == '__main__':
model = DeepJSCC(c=20)
print(model)
x = torch.rand(1, 3, 128, 128)
y = model(x)
print(y.size())
print(y)
print(model.encoder.norm)
print(model.encoder.norm(y))
print(model.encoder.norm(y).size())
print(model.encoder.norm(y).size()[1:])