-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoder.py
174 lines (137 loc) · 6 KB
/
Decoder.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# %%
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import channel, instance
class ResidualBlock(nn.Module):
def __init__(self, input_dims, kernel_size=3, stride=1,
channel_norm=True, activation='relu'):
"""
input_dims: Dimension of input tensor (B,C,H,W)
"""
super(ResidualBlock, self).__init__()
self.activation = getattr(F, activation)
in_channels = input_dims[1]
norm_kwargs = dict(momentum=0.1, affine=True, track_running_stats=False)
if channel_norm is True:
self.interlayer_norm = channel.ChannelNorm2D_wrap
else:
self.interlayer_norm = instance.InstanceNorm2D_wrap
pad_size = int((kernel_size-1)/2)
self.pad = nn.ReflectionPad2d(pad_size)
self.conv1 = nn.Conv2d(in_channels, in_channels, kernel_size, stride=stride)
self.conv2 = nn.Conv2d(in_channels, in_channels, kernel_size, stride=stride)
self.norm1 = self.interlayer_norm(in_channels, **norm_kwargs)
self.norm2 = self.interlayer_norm(in_channels, **norm_kwargs)
def forward(self, x):
identity_map = x
res = self.pad(x)
res = self.conv1(res)
res = self.norm1(res)
res = self.activation(res)
res = self.pad(res)
res = self.conv2(res)
res = self.norm2(res)
return torch.add(res, identity_map)
class Generator(nn.Module):
def __init__(self, input_dims, batch_size, C=16, activation='relu',
n_residual_blocks=8, channel_norm=True, sample_noise=False,
noise_dim=32):
super(Generator, self).__init__()
kernel_dim = 3
filters = [960, 480, 240, 120, 60]
self.n_residual_blocks = n_residual_blocks
self.sample_noise = sample_noise
self.noise_dim = noise_dim
# Layer / normalization options
cnn_kwargs = dict(stride=2, padding=1, output_padding=1)
norm_kwargs = dict(momentum=0.1, affine=True, track_running_stats=False)
activation_d = dict(relu='ReLU', elu='ELU', leaky_relu='LeakyReLU')
self.activation = getattr(nn, activation_d[activation]) # (leaky_relu, relu, elu)
self.n_upsampling_layers = 4
if channel_norm is True:
self.interlayer_norm = channel.ChannelNorm2D_wrap
else:
self.interlayer_norm = instance.InstanceNorm2D_wrap
self.pre_pad = nn.ReflectionPad2d(1)
self.asymmetric_pad = nn.ReflectionPad2d((0,1,1,0)) # Slower than tensorflow?
self.post_pad = nn.ReflectionPad2d(3)
H0, W0 = input_dims[1:]
heights = [2**i for i in range(5,9)]
widths = heights
H1, H2, H3, H4 = heights
W1, W2, W3, W4 = widths
# (16,16) -> (16,16), with implicit padding
self.conv_block_init = nn.Sequential(
self.interlayer_norm(C, **norm_kwargs),
self.pre_pad,
nn.Conv2d(C, filters[0], kernel_size=(3,3), stride=1),
self.interlayer_norm(filters[0], **norm_kwargs),
)
if sample_noise is True:
# Concat noise with latent representation
filters[0] += self.noise_dim
for m in range(n_residual_blocks):
resblock_m = ResidualBlock(input_dims=(batch_size, 960, H0, W0),
channel_norm=channel_norm, activation=activation)
self.add_module(f'resblock_{str(m)}', resblock_m)
# (16,16) -> (32,32)
self.upconv_block1 = nn.Sequential(
nn.ConvTranspose2d(960, filters[1], kernel_dim, **cnn_kwargs),
self.interlayer_norm(filters[1], **norm_kwargs),
self.activation(),
)
self.upconv_block2 = nn.Sequential(
nn.ConvTranspose2d(filters[1], filters[2], kernel_dim, **cnn_kwargs),
self.interlayer_norm(filters[2], **norm_kwargs),
self.activation(),
)
self.upconv_block3 = nn.Sequential(
nn.ConvTranspose2d(filters[2], filters[3], kernel_dim, **cnn_kwargs),
self.interlayer_norm(filters[3], **norm_kwargs),
self.activation(),
)
self.upconv_block4 = nn.Sequential(
nn.ConvTranspose2d(filters[3], filters[4], kernel_dim, **cnn_kwargs),
self.interlayer_norm(filters[4], **norm_kwargs),
self.activation(),
)
self.conv_block_out = nn.Sequential(
self.post_pad,
nn.Conv2d(filters[-1], 3, kernel_size=(7,7), stride=1),
)
def forward(self, x):
head = self.conv_block_init(x)
if self.sample_noise is True:
B, C, H, W = tuple(head.size())
z = torch.randn((B, self.noise_dim, H, W)).to(head)
head = torch.cat((head,z), dim=1)
for m in range(self.n_residual_blocks):
resblock_m = getattr(self, f'resblock_{str(m)}')
if m == 0:
x = resblock_m(head)
else:
x = resblock_m(x)
x += head
x = self.upconv_block1(x)
x = self.upconv_block2(x)
x = self.upconv_block3(x)
x = self.upconv_block4(x)
out = self.conv_block_out(x)
return out
# %%
if __name__ == "__main__":
load = torch.load(r"C:\Users\TEMMMAR\Desktop\Hifi_local\Chekpoint\hific-high.pt")
new_state_dict = {}
for name, weight in load['model_state_dict'].items():
if 'Generator' in name:
new_state_dict[name] = weight
new_state_dict1 = {}
for key, value in new_state_dict.items():
new_key = key.replace("Generator.", "")
new_state_dict1[new_key] = value
y = torch.randn([0,220,4,4])
j_dims = y.size()
G = Generator(j_dims[1:], j_dims[0], C=220, n_residual_blocks=9, sample_noise=False)
G.load_state_dict(new_state_dict1,strict=False)