-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathmodel.py
executable file
·161 lines (131 loc) · 5.84 KB
/
model.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
import torch
from torch import nn
import os
import json
import torch
class WavEncoder(nn.Module):
"""Handles only the encoding part of WavTokenizer"""
def __init__(self, feature_extractor):
super().__init__()
self.feature_extractor = feature_extractor
def _create_config(self):
config = {
'feature_extractor_class': 'decoder.feature_extractors.EncodecFeatures',
'feature_extractor_config': {
'encodec_model': 'encodec_24khz',
'bandwidths': [6.6, 6.6, 6.6, 6.6],
'train_codebooks': True,
'num_quantizers': 1,
'dowmsamples': [8, 5, 4, 2],
'vq_bins': 4096,
'vq_kmeans': 200
}
}
return config
def save_pretrained(self, save_directory):
"""Save the model and its configuration to a directory"""
os.makedirs(save_directory, exist_ok=True)
model_path = os.path.join(save_directory, "encoder_model.pt")
torch.save(self.state_dict(), model_path)
config = self._create_config()
config_path = os.path.join(save_directory, "config.json")
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
@classmethod
def from_pretrained(cls, model_directory):
"""Load a model and its configuration from a directory"""
config_path = os.path.join(model_directory, "config.json")
with open(config_path, 'r') as f:
config = json.load(f)
# Import feature extractor class
module_name, class_name = config['feature_extractor_class'].rsplit('.', 1)
module = __import__(module_name, fromlist=[class_name])
feature_extractor_cls = getattr(module, class_name)
# Create feature extractor
feature_extractor = feature_extractor_cls(**config['feature_extractor_config'])
model = cls(feature_extractor)
model_path = os.path.join(model_directory, "encoder_model.pt")
state_dict = torch.load(model_path, map_location='cpu')
model.load_state_dict(state_dict)
return model
@torch.inference_mode()
def forward(self, audio_input, **kwargs):
"""Encode audio into discrete codes"""
features, discrete_codes, _ = self.feature_extractor.infer(audio_input, **kwargs)
return features, discrete_codes
class WavDecoder(nn.Module):
"""Handles only the decoding part of WavTokenizer"""
def __init__(self, backbone, head, codebook_weights):
super().__init__()
self.backbone = backbone
self.head = head
self.register_buffer('codebook_weights', codebook_weights)
def _create_config(self):
config = {
'backbone_class': 'decoder.models.VocosBackbone',
'head_class': 'decoder.heads.ISTFTHead',
'backbone_config': {
'input_channels': 512,
'dim': 768,
'intermediate_dim': 2304,
'num_layers': 12,
'adanorm_num_embeddings': 4
},
'head_config': {
'dim': 768,
'n_fft': 1280,
'hop_length': 320,
'padding': 'same'
}
}
return config
def save_pretrained(self, save_directory):
"""Save the model and its configuration to a directory"""
os.makedirs(save_directory, exist_ok=True)
model_path = os.path.join(save_directory, "decoder_model.pt")
save_dict = {
'model_state_dict': self.state_dict(),
'codebook_weights': self.codebook_weights
}
torch.save(save_dict, model_path)
config = self._create_config()
config_path = os.path.join(save_directory, "config.json")
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
@classmethod
def from_pretrained(cls, model_directory):
"""Load a model and its configuration from a directory"""
config_path = os.path.join(model_directory, "config.json")
with open(config_path, 'r') as f:
config = json.load(f)
# Import and create backbone
module_name, class_name = config['backbone_class'].rsplit('.', 1)
module = __import__(module_name, fromlist=[class_name])
backbone_cls = getattr(module, class_name)
backbone = backbone_cls(**config['backbone_config'])
# Import and create head
module_name, class_name = config['head_class'].rsplit('.', 1)
module = __import__(module_name, fromlist=[class_name])
head_cls = getattr(module, class_name)
head = head_cls(**config['head_config'])
model_path = os.path.join(model_directory, "decoder_model.pt")
checkpoint = torch.load(model_path, map_location='cpu')
model = cls(backbone, head, checkpoint['codebook_weights'])
model.load_state_dict(checkpoint['model_state_dict'])
return model
def codes_to_features(self, codes):
"""Convert discrete codes to features using codebook"""
if codes.dim() == 2:
codes = codes.unsqueeze(1)
n_bins = self.codebook_weights.size(0) // len(codes)
offsets = torch.arange(0, n_bins * len(codes), n_bins, device=codes.device)
embeddings_idxs = codes + offsets.view(-1, 1, 1)
features = torch.nn.functional.embedding(embeddings_idxs, self.codebook_weights).sum(dim=0)
features = features.transpose(1, 2)
return features
@torch.inference_mode()
def forward(self, features_input, **kwargs):
"""Decode features to audio"""
x = self.backbone(features_input, **kwargs)
audio_output = self.head(x)
return audio_output