mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-04 17:38:10 +00:00
51 KiB
51 KiB
In [ ]:
"""
You can run either this notebook locally (if you have all the dependencies and a GPU) or on Google Colab.
Instructions for setting up Colab are as follows:
1. Open a new Python 3 notebook.
2. Import this notebook from GitHub (File -> Upload Notebook -> "GITHUB" tab -> copy/paste GitHub URL)
3. Connect to an instance with a GPU (Runtime -> Change runtime type -> select "GPU" for hardware accelerator)
4. Run this cell to set up dependencies.
"""
# If you're using Google Colab and not running locally, run this cell.
## Install dependencies
!pip install wget
!apt-get install sox libsndfile1 ffmpeg
!pip install unidecode
# ## Install NeMo
BRANCH = 'v1.0.2'
!python -m pip install git+https://github.com/NVIDIA/NeMo.git@$BRANCH#egg=nemo_toolkit[all]
## Install TorchAudio
!pip install torchaudio>=0.6.0 -f https://download.pytorch.org/whl/torch_stable.html
## Grab the config we'll use in this example
!mkdir configsIn [ ]:
import nemo
nemo.__version__In [ ]:
import nemo.collections.asr as nemo_asr
import nemo.collections.nlp as nemo_nlp
import nemo.collections.tts as nemo_ttsIn [ ]:
asr_models = [model for model in dir(nemo_asr.models) if model.endswith("Model")]
asr_modelsIn [ ]:
nlp_models = [model for model in dir(nemo_nlp.models) if model.endswith("Model")]
nlp_modelsIn [ ]:
tts_models = [model for model in dir(nemo_tts.models) if model.endswith("Model")]
tts_modelsIn [ ]:
citrinet = nemo_asr.models.EncDecCTCModelBPE.from_pretrained('stt_en_citrinet_512')In [ ]:
citrinet.summarize();In [ ]:
from omegaconf import OmegaConfIn [ ]:
import copyIn [ ]:
cfg = copy.deepcopy(citrinet.cfg)
print(OmegaConf.to_yaml(cfg))In [ ]:
# OmegaConf won't allow you to add new config items, so we temporarily disable this safeguard.
OmegaConf.set_struct(cfg, False)
# Let's see the old optim config
print("Old Config: ")
print(OmegaConf.to_yaml(cfg.optim))
sched = {'name': 'CosineAnnealing', 'warmup_steps': 1000, 'min_lr': 1e-6}
sched = OmegaConf.create(sched) # Convert it into a DictConfig
# Assign it to cfg.optim.sched namespace
cfg.optim.sched = sched
# Let's see the new optim config
print("New Config: ")
print(OmegaConf.to_yaml(cfg.optim))
# Here, we restore the safeguards so no more additions can be made to the config
OmegaConf.set_struct(cfg, True)In [ ]:
new_preprocessor_config = copy.deepcopy(cfg.preprocessor)
new_preprocessor = citrinet.from_config_dict(new_preprocessor_config)
print(new_preprocessor)In [ ]:
citrinet.preprocessor = new_preprocessorIn [ ]:
citrinet.summarize();In [ ]:
# Update the config copy
cfg.preprocessor = new_preprocessor_config
# Update the model config
citrinet.cfg = cfgIn [ ]:
# Let's print out the current optimizer
print(OmegaConf.to_yaml(citrinet.cfg.optim))In [ ]:
# Now let's update the config
citrinet.setup_optimization(cfg.optim);In [ ]:
OmegaConf.set_struct(cfg.optim.sched, False)
cfg.optim.sched.max_steps = 100
OmegaConf.set_struct(cfg.optim.sched, True)In [ ]:
# Now let's update the config and try again
citrinet.setup_optimization(cfg.optim);In [ ]:
# Save the model
citrinet.save_to('citrinet_512.nemo')In [ ]:
!ls -d -- *.nemo In [ ]:
# Restore the model
temp_cn = nemo_asr.models.EncDecCTCModelBPE.restore_from('citrinet_512.nemo')In [ ]:
temp_cn.summarize();In [ ]:
# Note that the preprocessor + optimizer config have been preserved after the changes we made !
print(OmegaConf.to_yaml(temp_cn.cfg))In [ ]:
!cp citrinet_512.nemo citrinet_512.tar.gz
!tar -xvf citrinet_512.tar.gzIn [ ]:
import torchIn [ ]:
state_dict = temp_cn.extract_state_dict_from('citrinet_512.nemo', save_dir='./pt_ckpt/')
!ls ./pt_ckpt/In [ ]:
temp_cn.load_state_dict(torch.load('./pt_ckpt/model_weights.ckpt'))In [ ]:
state_dict = temp_cn.extract_state_dict_from('citrinet_512.nemo', save_dir='./pt_module_ckpt/', split_by_module=True)
!ls ./pt_module_ckpt/In [ ]:
temp_cn.preprocessor.load_state_dict(torch.load('./pt_module_ckpt/preprocessor.ckpt'))
temp_cn.encoder.load_state_dict(torch.load('./pt_module_ckpt/encoder.ckpt'))
temp_cn.decoder.load_state_dict(torch.load('./pt_module_ckpt/decoder.ckpt'))