wav2vec2-2-rnd-grid-search / create_model.py
sanchit-gandhi's picture
Training in progress, step 500
6c4a6af
raw
history blame
1.97 kB
from transformers import SpeechEncoderDecoderModel, AutoFeatureExtractor, AutoTokenizer, Wav2Vec2Processor, BertConfig, BertLMHeadModel
import torch
# checkpoints to leverage
encoder_id = "facebook/wav2vec2-large-lv60"
decoder_id = "bert-large-uncased"
feature_extractor = AutoFeatureExtractor.from_pretrained(encoder_id)
feature_extractor.save_pretrained("./")
tokenizer = AutoTokenizer.from_pretrained(decoder_id)
tokenizer.save_pretrained("./")
decoder_config = BertConfig.from_pretrained(decoder_id, is_decoder=True)
decoder = BertLMHeadModel(decoder_config)
decoder.save_pretrained("decoder") # save the decoder
model = SpeechEncoderDecoderModel.from_encoder_decoder_pretrained(encoder_id, "decoder", encoder_add_adapter=True)
# verify the decoder
model.decoder.config.is_decoder
model.decoder.config.add_cross_attention
# set all encoder regularisation to zero
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
# set all decoder regularisation to zero
model.config.decoder.attention_probs_dropout_prob = 0.0
# set special token ids
model.config.decoder_start_token_id = tokenizer.cls_token_id
model.config.pad_token_id = tokenizer.pad_token_id
model.config.eos_token_id = tokenizer.sep_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"
# check if generation works
out = model.generate(torch.ones((1, 2000)))
model.save_pretrained("./")