how can I convert fine-tuned ckpt to huggingface whisper model? #830
Replies: 8 comments 33 replies
-
I had a similar problem but in the reverse direction (hf -> whisper), but you should be able to reverse this process and apply it to your model.
|
Beta Was this translation helpful? Give feedback.
-
here is a script for converting openai to hf: https://github.com/huggingface/transformers/blob/main/src/transformers/models/whisper/convert_openai_to_hf.py |
Beta Was this translation helpful? Give feedback.
-
This was quite helpful, thank you. Do you have a code to do the reverse, i.e. HF Whisper saved model (.bin file) to Whisper? I fine-tuned a whisper model with HF Whisper and exported it, and I want to use it in Whisper. It fails to load because it cannot get the "dims" key:
|
Beta Was this translation helpful? Give feedback.
-
HarikalarKutusu, if you happen to finally get it to work, please do share the code you use. I want to convert this model to use with faster whisper but none of the script I search on the Net would work https://huggingface.co/simonl0909/whisper-large-v2-cantonese Sorry that I am not a programmer so I would have to find some script that i could use....I can't write it on my own. thanks. |
Beta Was this translation helpful? Give feedback.
-
I don't get a ...and I get these files out of it. I want to convert to a Whisper-compatible .pt file but I'm not yet sure how.
|
Beta Was this translation helpful? Give feedback.
-
Hi |
Beta Was this translation helpful? Give feedback.
-
Complete script for converting HF model to Whisper: #!/bin/env python3
import whisper
import re
import torch
def hf_to_whisper_states(text):
text = re.sub('.layers.', '.blocks.', text)
text = re.sub('.self_attn.', '.attn.', text)
text = re.sub('.q_proj.', '.query.', text)
text = re.sub('.k_proj.', '.key.', text)
text = re.sub('.v_proj.', '.value.', text)
text = re.sub('.out_proj.', '.out.', text)
text = re.sub('.fc1.', '.mlp.0.', text)
text = re.sub('.fc2.', '.mlp.2.', text)
text = re.sub('.fc3.', '.mlp.3.', text)
text = re.sub('.fc3.', '.mlp.3.', text)
text = re.sub('.encoder_attn.', '.cross_attn.', text)
text = re.sub('.cross_attn.ln.', '.cross_attn_ln.', text)
text = re.sub('.embed_positions.weight', '.positional_embedding', text)
text = re.sub('.embed_tokens.', '.token_embedding.', text)
text = re.sub('model.', '', text)
text = re.sub('attn.layer_norm.', 'attn_ln.', text)
text = re.sub('.final_layer_norm.', '.mlp_ln.', text)
text = re.sub('encoder.layer_norm.', 'encoder.ln_post.', text)
text = re.sub('decoder.layer_norm.', 'decoder.ln.', text)
text = re.sub('proj_out.weight', 'decoder.token_embedding.weight', text)
return text
# Load HF Model
hf_state_dict = torch.load("whisper-medium-id.bin", map_location=torch.device('cpu')) # pytorch_model.bin file
# Rename layers
for key in list(hf_state_dict.keys())[:]:
new_key = hf_to_whisper_states(key)
hf_state_dict[new_key] = hf_state_dict.pop(key)
model = whisper.load_model('medium')
dims = model.dims
# Save it
torch.save({
"dims": model.dims.__dict__,
"model_state_dict": hf_state_dict
}, "whisper-model.bin") |
Beta Was this translation helpful? Give feedback.
-
how to ['proj_out.weight'] ? |
Beta Was this translation helpful? Give feedback.
-
hello. I trained whipser base model for inferencing korean adult speech. (.ckpt)
I wish to convert that model to huggingface whisper model for convenient, but I couldn't find the method for progressing that.
Who one know that solution?
Beta Was this translation helpful? Give feedback.
All reactions