forked from teticio/Deej-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt_to_tf.py
204 lines (190 loc) · 6.37 KB
/
pt_to_tf.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import argparse
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
import numpy as np
import torch
from audiodiffusion.audio_encoder import AudioEncoder
from keras.models import load_model, save_model
from tensorflow.compat.v1.keras.losses import cosine_proximity # type: ignore
from tensorflow.keras.layers import Dropout # type: ignore
from tensorflow.keras.layers import ( # type: ignore
BatchNormalization,
Dense,
Flatten,
Input,
LeakyReLU,
MaxPooling2D,
SeparableConv2D,
)
from tensorflow.keras.models import Sequential # type: ignore
if __name__ == "__main__":
"""
Entry point for the pt_to_tf script.
Converts a PyTorch MP3ToVec model to a TensorFlow MP3ToVec model.
Args:
--pt_model_file (str): Path to the PyTorch model file. Default is "models/mp3tovec.ckpt".
--tf_model_file (str): Path to the TensorFlow model file. Default is "models/speccymodel".
Returns:
None
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--pt_model_file",
type=str,
default="models/mp3tovec.ckpt",
help="PyTorch model path",
)
parser.add_argument(
"--tf_model_file",
type=str,
default="models/speccy_model",
help="TensorFlow model path",
)
args = parser.parse_args()
pytorch_model = AudioEncoder()
pytorch_model.eval()
pytorch_model.load_state_dict(
{
k.replace("model.", ""): v
for k, v in torch.load(
args.pt_model_file, map_location=torch.device("cpu")
)["state_dict"].items()
},
)
input_shape = (96, 216, 1)
model_input = Input(shape=input_shape, name="input")
model = Sequential(
[
Input(shape=input_shape, name="input"),
SeparableConv2D(
32,
3,
padding="same",
activation=LeakyReLU(0.2),
name=f"separable_conv2d_1",
),
BatchNormalization(name=f"batch_normalization_1"),
MaxPooling2D((2, 2)),
Dropout(rate=0.2),
SeparableConv2D(
64,
3,
padding="same",
activation=LeakyReLU(0.2),
name=f"separable_conv2d_2",
),
BatchNormalization(name=f"batch_normalization_2"),
MaxPooling2D((2, 2)),
Dropout(rate=0.3),
SeparableConv2D(
128,
3,
padding="same",
activation=LeakyReLU(0.2),
name=f"separable_conv2d_3",
),
BatchNormalization(name=f"batch_normalization_3"),
MaxPooling2D((2, 2)),
Dropout(rate=0.4),
Flatten(),
Dense(1024, activation=LeakyReLU(0.2), name="dense_1"),
BatchNormalization(name=f"batch_normalization_4"),
Dropout(rate=0.5),
Dense(100, name="dense_2"),
]
)
for conv_block in range(3):
model.get_layer(name=f"separable_conv2d_{conv_block+1}").set_weights(
[
np.transpose(
pytorch_model.state_dict()[
f"conv_blocks.{conv_block}.sep_conv.depthwise.weight"
]
.float()
.numpy(),
(2, 3, 0, 1),
),
np.transpose(
pytorch_model.state_dict()[
f"conv_blocks.{conv_block}.sep_conv.pointwise.weight"
]
.float()
.numpy(),
(2, 3, 1, 0),
),
pytorch_model.state_dict()[
f"conv_blocks.{conv_block}.sep_conv.pointwise.bias"
]
.float()
.numpy(),
]
)
model.get_layer(name=f"batch_normalization_{conv_block+1}").set_weights(
[
pytorch_model.state_dict()[
f"conv_blocks.{conv_block}.batch_norm.weight"
]
.float()
.numpy(),
pytorch_model.state_dict()[f"conv_blocks.{conv_block}.batch_norm.bias"]
.float()
.numpy(),
pytorch_model.state_dict()[
f"conv_blocks.{conv_block}.batch_norm.running_mean"
]
.float()
.numpy(),
pytorch_model.state_dict()[
f"conv_blocks.{conv_block}.batch_norm.running_var"
]
.float()
.numpy(),
]
)
model.get_layer(name=f"batch_normalization_{conv_block+2}").set_weights( # type: ignore
[
pytorch_model.state_dict()[f"dense_block.batch_norm.weight"]
.float()
.numpy(),
pytorch_model.state_dict()[f"dense_block.batch_norm.bias"].float().numpy(),
pytorch_model.state_dict()[f"dense_block.batch_norm.running_mean"]
.float()
.numpy(),
pytorch_model.state_dict()[f"dense_block.batch_norm.running_var"]
.float()
.numpy(),
]
)
model.get_layer(name=f"dense_1").set_weights(
[
np.transpose(
pytorch_model.state_dict()[f"dense_block.dense.weight"].float().numpy(),
(1, 0),
),
pytorch_model.state_dict()[f"dense_block.dense.bias"].float().numpy(),
]
)
model.get_layer(name=f"dense_2").set_weights(
[
np.transpose(
pytorch_model.state_dict()[f"embedding.weight"].float().numpy(), (1, 0)
),
pytorch_model.state_dict()[f"embedding.bias"].float().numpy(),
]
)
model.compile(loss=cosine_proximity, optimizer="adam")
save_model(model=model, filepath=args.tf_model_file, save_format="h5")
model = load_model(args.tf_model_file)
if model is None:
raise ValueError("Failed to load model")
# test
np.random.seed(42)
example = np.random.random_sample((1, 96, 216, 1)).astype(np.float32)
with torch.no_grad():
assert (
np.abs(
model.predict(example)
- pytorch_model(torch.from_numpy(example).permute(0, 3, 1, 2)).numpy()
).max()
< 2e-3
)