def save_model(model, model_path, model_name): torch.save(ModelSerializer.convert_param_to_cpu(model.state_dict()), os.path.join(model_path, '{}.mdl'.format(model_name))) ModelSerializer.save_config( os.path.join(model_path, '{}.cfg'.format(model_name)), model.get_config()) json.dump(opts, open(os.path.join(opts['result'], 'script.opts'), 'w'), indent=2)
def save_model(model, name, epoch) : torch.save(ModelSerializer.convert_param_to_cpu(model.state_dict()), os.path.join(opts['result'], 'model_{}_e{}.mdl'.format(name, epoch))) ModelSerializer.save_config(os.path.join(opts['result'], 'model_{}_e{}.cfg'.format(name, epoch)), model.get_config()) pass
feat_iterator = DataLoader.load_feat(data_cfg, data_type=LoaderDataType.TYPE_NP, in_memory=opts['mem']) text_iterator = DataLoader.load_text(data_cfg) feat_stat = pickle.load(open(data_cfg['feat']['stat'], 'rb')) feat_sil = feat_sil_from_stat(feat_stat) print("Finish loading dataset ...") NDIM = feat_iterator['train'].get_feat_dim() * group NVOCAB = len(text_iterator['train'].get_map_text2idx()) if opts['model_pt'] is not None: model = ModelSerializer.load_config( os.path.join(opts['model_pt'], 'model.cfg')) model.load_state_dict( torch.load(os.path.join(opts['model_pt'], 'model.mdl'))) assert model.TYPE in [ TacotronType.SINGLE_SPEAKER, TacotronType.MULTI_SPEAKER ] print('[info] load pretrained model') else: _model_cfg = opts['model_cfg'] _model_cfg['enc_in_size'] = NVOCAB _model_cfg['dec_in_size'] = NDIM _model_cfg['dec_out_size'] = NDIM model = ModelSerializer.load_config(_model_cfg) assert model.TYPE in [ TacotronType.SINGLE_SPEAKER, TacotronType.MULTI_SPEAKER ]
def invert_mel_to_linear(model, feat_mat, feat_len, group=1): # model.reset() model.eval() pred_out = model(feat_mat, feat_len) return pred_out if __name__ == '__main__': opts = vars(parse()) feat_cfg = json.load(open(opts['feat_cfg'])) # load model structure and weight # if opts['config'] is None: opts['config'] = os.path.splitext(opts['model'])[0] + '.cfg' model = ModelSerializer.load_config(opts['config']) model.train(False) model.load_state_dict(torch.load(opts['model'])) # tacotron util helper # helper = TacotronHelper(feat_cfg) if opts['gpu'] >= 0: torch.cuda.set_device(opts['gpu']) model.cuda() data_cfg_target = yaml.load(open(opts['data_cfg_tgt'])) data_iterator_target = DataLoader.load_feat_single( feat_path=data_cfg_target['feat']['all']) data_cfg_source = yaml.load(open(opts['data_cfg_src']))
data_cfg = yaml.load(open(opts['data_cfg'])) feat_iterator = DataLoader.load_feat(data_cfg, data_type=LoaderDataType.TYPE_NP, in_memory=opts['mem']) text_iterator = DataLoader.load_text(data_cfg) print("Finish loading dataset ...") NDIM = feat_iterator['train'].get_feat_dim() NVOCAB = len(text_iterator['train'].get_map_text2idx()) if opts['model_pt'] is not None: assert opts['model_pt'].endswith( '.mdl'), 'model pretrained should have *.mdl file extension' _model_state_path = opts['model_pt'] _model_cfg_path = os.path.splitext(opts['model_pt'])[0] + '.cfg' model = ModelSerializer.load_config(_model_cfg_path) model.load_state_dict(torch.load(_model_state_path)) print('[info] load pretrained model') # additional # if opts['result'].startswith('+'): opts['result'] = os.path.dirname( opts['model_pt']) + opts['result'][1:] print('[info] append pretrained folder name to result') else: _model_cfg = opts['model_cfg'] _model_cfg['enc_in_size'] = NDIM _model_cfg['dec_in_size'] = NVOCAB _model_cfg['dec_out_size'] = NVOCAB model = ModelSerializer.load_config(_model_cfg)
) return parser.parse_args() if __name__ == '__main__': args = parse() assert (args.chunk is None) != (args.chunkblk is None) # init logger # logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # load model structure and weight # if args.config is None: args.config = os.path.splitext(args.model)[0] + '.cfg' model = ModelSerializer.load_config(args.config) model.train(False) model.load_state_dict(torch.load(args.model)) if args.gpu >= 0: torch.cuda.set_device(args.gpu) model.cuda() # laod dict and feature scaler # data_cfg = yaml.load(open(args.data_cfg)) map_text2idx = json.load(open(data_cfg['text']['vocab'])) map_idx2text = dict([(y, x) for (x, y) in map_text2idx.items()]) scaler = pickle.load(open( data_cfg['feat']['scaler'], 'rb')) if data_cfg['feat'].get(
vocab=map_text2idx) DataLoader._check_intersect(list(feat_iterator.values())) DataLoader._check_intersect(list(text_iterator.values())) assert text_iterator['train'].get_map_text2idx( ) == text_iterator['unpair'].get_map_text2idx() feat_stat = pickle.load(open(data_pair_cfg['feat']['stat'], 'rb')) feat_sil = feat_sil_from_stat(feat_stat) NVOCAB = len(text_iterator['train'].get_map_text2idx()) NDIM_FEAT = feat_iterator['train'].get_feat_dim() printe("Finish loading dataset ...") # load model # model_asr = ModelSerializer.load_config_and_state( filename_state=opts['asr_model_pt']) model_tts = ModelSerializer.load_config_and_state( filename_state=opts['tts_model_pt']) if opts['tts_loss_spk_emb'] is not None: model_spkrec = ModelSerializer.load_config_and_state( filename_state=opts['spkrec_model_pt']) else: model_spkrec = None if opts['gpu'] >= 0: model_asr.cuda(opts['gpu']) model_tts.cuda(opts['gpu']) if opts['tts_loss_spk_emb'] is not None: model_spkrec.cuda(opts['gpu'])