Esempio n. 1
0
def get_model(model_name, model_config, to_cuda):
    """ Code chooses a model based on name"""
    model = None
    if model_name == 'Tacotron2':
        model = Tacotron2(**model_config)
    elif model_name == 'WaveGlow':
        model = WaveGlow(**model_config)
    else:
        raise NotImplementedError(model_name)
    if to_cuda:
        model = model.cuda()
    return model
Esempio n. 2
0
def get_model(model_name, model_config, cpu_run,
              uniform_initialize_bn_weight=False, forward_is_infer=False):
    """ Code chooses a model based on name"""
    model = None
    if model_name == 'Tacotron2':
        if forward_is_infer:
            class Tacotron2__forward_is_infer(Tacotron2):
                def forward(self, inputs, input_lengths):
                    return self.infer(inputs, input_lengths)
            model = Tacotron2__forward_is_infer(**model_config)
        else:
            model = Tacotron2(**model_config)
    elif model_name == 'WaveGlow':
        if forward_is_infer:
            class WaveGlow__forward_is_infer(WaveGlow):
                def forward(self, spect, sigma=1.0):
                    return self.infer(spect, sigma)
            model = WaveGlow__forward_is_infer(**model_config)
        else:
            model = WaveGlow(**model_config)
    else:
        raise NotImplementedError(model_name)

    if uniform_initialize_bn_weight:
        init_bn(model)

    if not cpu_run:
        model = model.cuda()
    return model
Esempio n. 3
0
    def __init__(self, ckpt_file, device='cuda', use_fp16=False, use_denoiser=False):
        self.ckpt_file = ckpt_file
        self.device = device
        self.use_fp16 = use_fp16
        self.use_denoiser = use_denoiser

        # model
        # sys.path.append('waveglow')

        from waveglow.arg_parser import parse_waveglow_args
        parser = parser = argparse.ArgumentParser()
        model_parser= parse_waveglow_args(parser)
        args, _ = model_parser.parse_known_args()
        model_config = dict(
            n_mel_channels=args.n_mel_channels,
            n_flows=args.flows,
            n_group=args.groups,
            n_early_every=args.early_every,
            n_early_size=args.early_size,
            WN_config=dict(
                n_layers=args.wn_layers,
                kernel_size=args.wn_kernel_size,
                n_channels=args.wn_channels
            )
        )        
        self.model = WaveGlow(**model_config)

        state_dict = torch.load(self.ckpt_file, map_location=self.device)['state_dict']
        state_dict = unwrap_distributed(state_dict)
        self.model.load_state_dict(state_dict)

        self.model = to_device_async(self.model, self.device)

        self.model = self.model.remove_weightnorm(self.model)

        self.model.eval()

        if self.use_fp16:
            self.model = self.model.half()
        self.model = self.model

        if self.use_denoiser:
            self.denoiser = Denoiser(self.model, device=device)
            self.denoiser = to_device_async(self.denoiser, self.device)

            tprint('Using WaveGlow denoiser.')
Esempio n. 4
0
def get_model(model_name, model_config, to_cuda,
              uniform_initialize_bn_weight=False):
    """ Code chooses a model based on name"""
    model = None
    if model_name == 'Tacotron2':
        model = Tacotron2(**model_config)
    elif model_name == 'WaveGlow':
        model = WaveGlow(**model_config)
    else:
        raise NotImplementedError(model_name)

    if uniform_initialize_bn_weight:
        init_bn(model)

    if to_cuda:
        model = model.cuda()
    return model
Esempio n. 5
0
def load_waveglow(filename, waveglow_config):
    class RenamingUnpickler(pickle.Unpickler):
        def find_class(self, module, name):
            if module == 'glow':
                module = 'waveglow.model'
            return super().find_class(module, name)

    class RenamingPickleModule:
        def load(self, f, *args, **kw_args):
            return self.Unpickler(f, *args, **kw_args).load()

        def Unpickler(self, f, **pickle_load_args):
            return RenamingUnpickler(f, **pickle_load_args)

    pickle_module = RenamingPickleModule()
    blob = torch.load(filename, pickle_module=pickle_module)

    if 'state_dict' in blob:
        waveglow = WaveGlow(**waveglow_config).cuda()
        state_dict = {}
        for key, value in blob["state_dict"].items():
            newKey = key
            if key.startswith("module."):
                newKey = key[len("module."):]
            state_dict[newKey] = value
        waveglow.load_state_dict(state_dict)
    else:
        waveglow = blob['model']

    waveglow = split_cond_layers(waveglow)
    waveglow = waveglow.remove_weightnorm(waveglow)
    waveglow.cuda().eval()

    return waveglow
Esempio n. 6
0
def get_model(model_name, model_config, to_fp16, to_cuda, training=True):
    """ Code chooses a model based on name"""
    model = None
    if model_name == 'Tacotron2':
        model = Tacotron2(**model_config)
    elif model_name == 'WaveGlow':
        model = WaveGlow(**model_config)
    else:
        raise NotImplementedError(model_name)
    if to_fp16:
        model = batchnorm_to_float(model.half())
        model = lstmcell_to_float(model)
        if model_name == "WaveGlow":
            for k in model.convinv:
                k.float()
    if to_cuda:
        model = model.cuda()
    return model
Esempio n. 7
0
def get_model(model_name, model_config, device,
              uniform_initialize_bn_weight=False, forward_is_infer=False,
              jitable=False):
    """ Code chooses a model based on name"""
    model = None
    if model_name == 'Tacotron2':
        if forward_is_infer:
            class Tacotron2__forward_is_infer(Tacotron2):
                def forward(self, inputs, input_lengths):
                    return self.infer(inputs, input_lengths)
            model = Tacotron2__forward_is_infer(**model_config)
        else:
            model = Tacotron2(**model_config)

    elif model_name == 'WaveGlow':
        if forward_is_infer:
            class WaveGlow__forward_is_infer(WaveGlow):
                def forward(self, spect, sigma=1.0):
                    return self.infer(spect, sigma)
            model = WaveGlow__forward_is_infer(**model_config)
        else:
            model = WaveGlow(**model_config)

    elif model_name == 'FastPitch':

        if forward_is_infer:

            if jitable:
                class FastPitch__forward_is_infer(_FastPitchJIT):
                    def forward(self, inputs, input_lengths, pace: float = 1.0,
                                dur_tgt: Optional[torch.Tensor] = None,
                                pitch_tgt: Optional[torch.Tensor] = None,
                                pitch_transform: Optional[bool] = None):
                        return self.infer(inputs, input_lengths, pace=pace,
                                          dur_tgt=dur_tgt, pitch_tgt=pitch_tgt)
            else:
                class FastPitch__forward_is_infer(_FastPitch):
                    def forward(self, inputs, input_lengths, pace: float = 1.0,
                                dur_tgt: Optional[torch.Tensor] = None,
                                pitch_tgt: Optional[torch.Tensor] = None,
                                pitch_transform=None):
                        return self.infer(inputs, input_lengths, pace=pace,
                                          dur_tgt=dur_tgt, pitch_tgt=pitch_tgt,
                                          pitch_transform=pitch_transform)

            model = FastPitch__forward_is_infer(**model_config)
        else:
            model = _FastPitch(**model_config)

    else:
        raise NotImplementedError(model_name)

    if uniform_initialize_bn_weight:
        init_bn(model)

    return model.to(device)
Esempio n. 8
0
def get_model(model_name: str,
              model_config: dict,
              cpu_run: bool,
              uniform_initialize_bn_weight: bool = False,
              forward_is_infer: bool = False):
    """Return a model based on `model_name` defined by `model_config`.

    Args:
        model_name (str): One of the 'Tacotron2' or 'WaveGlow'.
        model_config (dict): [description]
        cpu_run (bool): Run on CPU (True) or GPU (False).
        uniform_initialize_bn_weight (bool, optional): [description]. Defaults to False.
        forward_is_infer (bool, optional): [description]. Defaults to False.

    Raises:
        NotImplementedError: [description]

    Returns:
        [type]: [description]
    """

    model = None
    if model_name == 'Tacotron2':
        if forward_is_infer:

            class Tacotron2__forward_is_infer(Tacotron2):
                def forward(self, inputs, input_lengths):
                    print(input_lengths)  #FIXME
                    return self.infer(inputs, input_lengths)

            model = Tacotron2__forward_is_infer(**model_config)
        else:
            model = Tacotron2(**model_config)
    elif model_name == 'WaveGlow':
        if forward_is_infer:

            class WaveGlow__forward_is_infer(WaveGlow):
                def forward(self, spect, sigma=1.0):
                    return self.infer(spect, sigma)

            model = WaveGlow__forward_is_infer(**model_config)
        else:
            model = WaveGlow(**model_config)
    else:
        raise NotImplementedError(model_name)

    if uniform_initialize_bn_weight:
        init_bn(model)

    if cpu_run == False:
        model = model.cuda()
    return model
Esempio n. 9
0
def get_model(model_name, model_config, to_cuda,
              uniform_initialize_bn_weight=False, rename=False):
    """ Code chooses a model based on name"""
    model = None
    if model_name == 'Tacotron2':
        if rename:
            class Tacotron2_extra(Tacotron2):
                def forward(self, inputs, input_lengths):
                    return self.infer(inputs, input_lengths)
            model = Tacotron2_extra(**model_config)
        else:
            model = Tacotron2(**model_config)
    elif model_name == 'WaveGlow':
        model = WaveGlow(**model_config)
    else:
        raise NotImplementedError(model_name)

    if uniform_initialize_bn_weight:
        init_bn(model)

    if to_cuda:
        model = model.cuda()
    return model
Esempio n. 10
0
def build_model(params):
    upsample_net = UpsampleNet(
        upsample_factor=params['upsample_net']['upsample_factor'],
        upsample_method=params['upsample_net']['upsample_method'],
        squeeze_factor=params['waveglow']['squeeze_factor'])

    input_channels = params['upsample_net']['input_channels']
    local_condition_channels = input_channels * params['waveglow']['squeeze_factor']
    model = WaveGlow(
        squeeze_factor=params['waveglow']['squeeze_factor'],
        num_layers=params['waveglow']['num_layers'],
        wn_filter_width=params['waveglow']['wn_filter_width'],
        wn_dilation_layers=params['waveglow']['wn_dilation_layers'],
        wn_residual_channels=params['waveglow']['wn_residual_channels'],
        wn_dilation_channels=params['waveglow']['wn_dilation_channels'],
        wn_skip_channels=params['waveglow']['wn_skip_channels'],
        local_condition_channels=local_condition_channels)

    return upsample_net, model
Esempio n. 11
0
def get_model(model_name, model_config, device,
              uniform_initialize_bn_weight=False, forward_is_infer=False,
              jitable=False):

    if model_name == 'WaveGlow':
        model = WaveGlow(**model_config)

    elif model_name == 'FastPitch':
        if jitable:
            model = FastPitchJIT(**model_config)
        else:
            model = FastPitch(**model_config)

    else:
        raise NotImplementedError(model_name)

    if forward_is_infer:
        model.forward = model.infer

    if uniform_initialize_bn_weight:
        init_bn(model)

    return model.to(device)
Esempio n. 12
0
class WaveGlowInferencer(object):

    def __init__(self, ckpt_file, device='cuda', use_fp16=False, use_denoiser=False):
        self.ckpt_file = ckpt_file
        self.device = device
        self.use_fp16 = use_fp16
        self.use_denoiser = use_denoiser

        # model
        # sys.path.append('waveglow')

        from waveglow.arg_parser import parse_waveglow_args
        parser = parser = argparse.ArgumentParser()
        model_parser= parse_waveglow_args(parser)
        args, _ = model_parser.parse_known_args()
        model_config = dict(
            n_mel_channels=args.n_mel_channels,
            n_flows=args.flows,
            n_group=args.groups,
            n_early_every=args.early_every,
            n_early_size=args.early_size,
            WN_config=dict(
                n_layers=args.wn_layers,
                kernel_size=args.wn_kernel_size,
                n_channels=args.wn_channels
            )
        )        
        self.model = WaveGlow(**model_config)

        state_dict = torch.load(self.ckpt_file, map_location=self.device)['state_dict']
        state_dict = unwrap_distributed(state_dict)
        self.model.load_state_dict(state_dict)

        self.model = to_device_async(self.model, self.device)

        self.model = self.model.remove_weightnorm(self.model)

        self.model.eval()

        if self.use_fp16:
            self.model = self.model.half()
        self.model = self.model

        if self.use_denoiser:
            self.denoiser = Denoiser(self.model, device=device)
            self.denoiser = to_device_async(self.denoiser, self.device)

            tprint('Using WaveGlow denoiser.')

    def __enter__(self):
        pass

    def __exit__(self, exception_type, exception_value, traceback):
        pass

    def infer(self, mels):
        if self.use_fp16:
            mels = mels.half()
        mels = to_device_async(mels, self.device)
        wavs = self.model.infer(mels, sigma=0.6)

        if self.use_denoiser:
            wavs = self.denoiser(wavs, strength=0.01)

        return wavs.float()