Fork of https://github.com/alokprasad/fastspeech_squeezewave to also fix denoising in squeezewave
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.8 KiB

  1. import sys
  2. import copy
  3. import torch
  4. def _check_model_old_version(model):
  5. if hasattr(model.WN[0], 'res_layers'):
  6. return True
  7. else:
  8. return False
  9. def update_model(old_model):
  10. if not _check_model_old_version(old_model):
  11. return old_model
  12. new_model = copy.deepcopy(old_model)
  13. for idx in range(0, len(new_model.WN)):
  14. wavenet = new_model.WN[idx]
  15. wavenet.res_skip_layers = torch.nn.ModuleList()
  16. n_channels = wavenet.n_channels
  17. n_layers = wavenet.n_layers
  18. for i in range(0, n_layers):
  19. if i < n_layers - 1:
  20. res_skip_channels = 2*n_channels
  21. else:
  22. res_skip_channels = n_channels
  23. res_skip_layer = torch.nn.Conv1d(n_channels, res_skip_channels, 1)
  24. skip_layer = torch.nn.utils.remove_weight_norm(wavenet.skip_layers[i])
  25. if i < n_layers - 1:
  26. res_layer = torch.nn.utils.remove_weight_norm(wavenet.res_layers[i])
  27. res_skip_layer.weight = torch.nn.Parameter(torch.cat([res_layer.weight, skip_layer.weight]))
  28. res_skip_layer.bias = torch.nn.Parameter(torch.cat([res_layer.bias, skip_layer.bias]))
  29. else:
  30. res_skip_layer.weight = torch.nn.Parameter(skip_layer.weight)
  31. res_skip_layer.bias = torch.nn.Parameter(skip_layer.bias)
  32. res_skip_layer = torch.nn.utils.weight_norm(res_skip_layer, name='weight')
  33. wavenet.res_skip_layers.append(res_skip_layer)
  34. del wavenet.res_layers
  35. del wavenet.skip_layers
  36. return new_model
  37. if __name__ == '__main__':
  38. old_model_path = sys.argv[1]
  39. new_model_path = sys.argv[2]
  40. model = torch.load(old_model_path)
  41. model['model'] = update_model(model['model'])
  42. torch.save(model, new_model_path)