|
from transformers import SpeechEncoderDecoderModel, AutoFeatureExtractor, GPT2Tokenizer |
|
import torch |
|
|
|
|
|
encoder_id = "facebook/wav2vec2-large-lv60" |
|
decoder_id = "gpt2-medium" |
|
|
|
model = SpeechEncoderDecoderModel.from_encoder_decoder_pretrained(encoder_id, decoder_id, encoder_add_adapter=True) |
|
|
|
|
|
model.config.encoder.feat_proj_dropout = 0.0 |
|
model.config.encoder.final_dropout = 0.0 |
|
model.config.encoder.activation_dropout = 0.0 |
|
model.config.encoder.apply_spec_augment = False |
|
model.config.encoder.attention_dropout = 0.0 |
|
model.config.encoder.feat_extract_dropout = 0.0 |
|
model.config.encoder.feat_proj_dropout = 0.0 |
|
model.config.encoder.hidden_dropout = 0.0 |
|
model.config.encoder.hidden_dropout_prob = 0.0 |
|
model.config.encoder.layerdrop = 0.0 |
|
model.config.encoder.mask_feature_prob = 0.0 |
|
model.config.encoder.mask_time_prob = 0.0 |
|
|
|
|
|
model.config.decoder.attn_pdrop = 0.0 |
|
model.config.decoder.embd_pdrop = 0.0 |
|
model.config.decoder.resid_pdrop = 0.0 |
|
model.config.decoder.summary_first_dropout = 0.0 |
|
|
|
|
|
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None): |
|
outputs = [self.bos_token_id] + token_ids_0 + [self.eos_token_id] |
|
return outputs |
|
|
|
GPT2Tokenizer.build_inputs_with_special_tokens = build_inputs_with_special_tokens |
|
gpt2_tokenizer = GPT2Tokenizer.from_pretrained(decoder_id) |
|
|
|
gpt2_tokenizer.pad_token = gpt2_tokenizer.unk_token |
|
gpt2_tokenizer.save_pretrained("./") |
|
|
|
model.config.pad_token_id = gpt2_tokenizer.pad_token_id |
|
model.config.decoder_start_token_id = model.decoder.config.bos_token_id |
|
model.config.eos_token_id = model.decoder.config.eos_token_id |
|
model.config.max_length = 50 |
|
model.config.num_beams = 1 |
|
|
|
model.config.use_cache = False |
|
model.config.decoder.use_cache = False |
|
model.config.processor_class = "Wav2Vec2Processor" |
|
|
|
|
|
out = model.generate(torch.ones((1, 2000))) |
|
|
|
model.save_pretrained("./") |
|
|
|
feature_extractor = AutoFeatureExtractor.from_pretrained(encoder_id) |
|
feature_extractor.save_pretrained("./") |
|
|
|
|