Skip to content

Commit 88be209

Browse files
committed
chore: code cleanup by ruff fix
1 parent 30975cd commit 88be209

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+101
-175
lines changed

.ruff.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

.vscode/extensions.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"recommendations": [
3+
"charliermarsh.ruff",
4+
"ms-python.python"
5+
]
6+
}

cluster/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import numpy as np
21
import torch
32
from sklearn.cluster import KMeans
43

cluster/kmeans.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import math,pdb
21
import torch,pynvml
32
from torch.nn.functional import normalize
43
from time import time

cluster/train_cluster.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import time,pdb
1+
import time
22
import tqdm
3-
from time import time as ttime
43
import os
54
from pathlib import Path
65
import logging
@@ -12,8 +11,7 @@
1211

1312
logging.basicConfig(level=logging.INFO)
1413
logger = logging.getLogger(__name__)
15-
from time import time as ttime
16-
import pynvml,torch
14+
import torch
1715

1816
def train_cluster(in_dir, n_clusters, use_minibatch=True, verbose=False,use_gpu=False):#gpu_minibatch真拉,虽然库支持但是也不考虑
1917
logger.info(f"Loading features from {in_dir}")
@@ -29,22 +27,22 @@ def train_cluster(in_dir, n_clusters, use_minibatch=True, verbose=False,use_gpu=
2927
features = features.astype(np.float32)
3028
logger.info(f"Clustering features of shape: {features.shape}")
3129
t = time.time()
32-
if(use_gpu==False):
30+
if(use_gpu is False):
3331
if use_minibatch:
3432
kmeans = MiniBatchKMeans(n_clusters=n_clusters,verbose=verbose, batch_size=4096, max_iter=80).fit(features)
3533
else:
3634
kmeans = KMeans(n_clusters=n_clusters,verbose=verbose).fit(features)
3735
else:
3836
kmeans = KMeansGPU(n_clusters=n_clusters, mode='euclidean', verbose=2 if verbose else 0,max_iter=500,tol=1e-2)#
3937
features=torch.from_numpy(features)#.to(device)
40-
labels = kmeans.fit_predict(features)#
38+
kmeans.fit_predict(features)#
4139

4240
print(time.time()-t, "s")
4341

4442
x = {
45-
"n_features_in_": kmeans.n_features_in_ if use_gpu==False else features.shape[1],
46-
"_n_threads": kmeans._n_threads if use_gpu==False else 4,
47-
"cluster_centers_": kmeans.cluster_centers_ if use_gpu==False else kmeans.centroids.cpu().numpy(),
43+
"n_features_in_": kmeans.n_features_in_ if use_gpu is False else features.shape[1],
44+
"_n_threads": kmeans._n_threads if use_gpu is False else 4,
45+
"cluster_centers_": kmeans.cluster_centers_ if use_gpu is False else kmeans.centroids.cpu().numpy(),
4846
}
4947
print("end")
5048

data_utils.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import time
21
import os
32
import random
43
import numpy as np
54
import torch
65
import torch.utils.data
76

8-
import modules.commons as commons
97
import utils
10-
from modules.mel_processing import spectrogram_torch, spec_to_mel_torch, spectrogram_torch
8+
from modules.mel_processing import spectrogram_torch, spectrogram_torch
119
from utils import load_wav_to_torch, load_filepaths_and_text
1210

1311
# import h5py
@@ -87,7 +85,7 @@ def get_audio(self, filename):
8785
assert abs(audio_norm.shape[1]-lmin * self.hop_length) < 3 * self.hop_length
8886
spec, c, f0, uv = spec[:, :lmin], c[:, :lmin], f0[:lmin], uv[:lmin]
8987
audio_norm = audio_norm[:, :lmin * self.hop_length]
90-
if volume!= None:
88+
if volume is not None:
9189
volume = volume[:lmin]
9290
return c, f0, spec, audio_norm, spk, uv, volume
9391

@@ -96,7 +94,7 @@ def random_slice(self, c, f0, spec, audio_norm, spk, uv, volume):
9694
# print("skip too short audio:", filename)
9795
# return None
9896

99-
if random.choice([True, False]) and self.vol_aug and volume!=None:
97+
if random.choice([True, False]) and self.vol_aug and volume is not None:
10098
max_amp = float(torch.max(torch.abs(audio_norm))) + 1e-5
10199
max_shift = min(1, np.log10(1/max_amp))
102100
log10_vol_shift = random.uniform(-1, max_shift)
@@ -114,7 +112,7 @@ def random_slice(self, c, f0, spec, audio_norm, spk, uv, volume):
114112
end = start + 790
115113
spec, c, f0, uv = spec[:, start:end], c[:, start:end], f0[start:end], uv[start:end]
116114
audio_norm = audio_norm[:, start * self.hop_length : end * self.hop_length]
117-
if volume !=None:
115+
if volume is not None:
118116
volume = volume[start:end]
119117
return c, f0, spec, audio_norm, spk, uv,volume
120118

@@ -178,7 +176,7 @@ def __call__(self, batch):
178176
uv = row[5]
179177
uv_padded[i, :uv.size(0)] = uv
180178
volume = row[6]
181-
if volume != None:
179+
if volume is not None:
182180
volume_padded[i, :volume.size(0)] = volume
183181
else :
184182
volume_padded = None

diffusion/data_loaders.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import random
3-
import re
43
import numpy as np
54
import librosa
65
import torch
@@ -130,7 +129,7 @@ def __init__(
130129
with open(filelists,"r") as f:
131130
self.paths = f.read().splitlines()
132131
for name_ext in tqdm(self.paths, total=len(self.paths)):
133-
name = os.path.splitext(name_ext)[0]
132+
os.path.splitext(name_ext)[0]
134133
path_audio = name_ext
135134
duration = librosa.get_duration(filename = path_audio, sr = self.sample_rate)
136135

diffusion/diffusion.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from functools import partial
33
from inspect import isfunction
44
import torch.nn.functional as F
5-
import librosa.sequence
65
import numpy as np
76
import torch
87
from torch import nn
@@ -26,8 +25,10 @@ def extract(a, t, x_shape):
2625

2726

2827
def noise_like(shape, device, repeat=False):
29-
repeat_noise = lambda: torch.randn((1, *shape[1:]), device=device).repeat(shape[0], *((1,) * (len(shape) - 1)))
30-
noise = lambda: torch.randn(shape, device=device)
28+
def repeat_noise():
29+
return torch.randn((1, *shape[1:]), device=device).repeat(shape[0], *((1,) * (len(shape) - 1)))
30+
def noise():
31+
return torch.randn(shape, device=device)
3132
return repeat_noise() if repeat else noise()
3233

3334

diffusion/diffusion_onnx.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from functools import partial
33
from inspect import isfunction
44
import torch.nn.functional as F
5-
import librosa.sequence
65
import numpy as np
76
from torch.nn import Conv1d
87
from torch.nn import Mish
@@ -27,8 +26,10 @@ def extract(a, t):
2726

2827

2928
def noise_like(shape, device, repeat=False):
30-
repeat_noise = lambda: torch.randn((1, *shape[1:]), device=device).repeat(shape[0], *((1,) * (len(shape) - 1)))
31-
noise = lambda: torch.randn(shape, device=device)
29+
def repeat_noise():
30+
return torch.randn((1, *shape[1:]), device=device).repeat(shape[0], *((1,) * (len(shape) - 1)))
31+
def noise():
32+
return torch.randn(shape, device=device)
3233
return repeat_noise() if repeat else noise()
3334

3435

@@ -577,7 +578,7 @@ def forward(self, condition=None, init_noise=None, pndms=None, k_step=None):
577578
noise_list = torch.zeros((0, 1, 1, self.mel_bins, n_frames), device=device)
578579

579580
ot = step_range[0]
580-
ot_1 = torch.full((1,), ot, device=device, dtype=torch.long)
581+
torch.full((1,), ot, device=device, dtype=torch.long)
581582

582583
for t in step_range:
583584
t_1 = torch.full((1,), t, device=device, dtype=torch.long)

diffusion/dpm_solver_pytorch.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import torch
2-
import torch.nn.functional as F
3-
import math
42

53

64
class NoiseScheduleVP:
@@ -559,7 +557,7 @@ def dpm_solver_first_update(self, x, s, t, model_s=None, return_intermediate=Fal
559557
x_t: A pytorch tensor. The approximated solution at time `t`.
560558
"""
561559
ns = self.noise_schedule
562-
dims = x.dim()
560+
x.dim()
563561
lambda_s, lambda_t = ns.marginal_lambda(s), ns.marginal_lambda(t)
564562
h = lambda_t - lambda_s
565563
log_alpha_s, log_alpha_t = ns.marginal_log_mean_coeff(s), ns.marginal_log_mean_coeff(t)
@@ -984,20 +982,25 @@ def dpm_solver_adaptive(self, x, order, t_T, t_0, h_init=0.05, atol=0.0078, rtol
984982
nfe = 0
985983
if order == 2:
986984
r1 = 0.5
987-
lower_update = lambda x, s, t: self.dpm_solver_first_update(x, s, t, return_intermediate=True)
988-
higher_update = lambda x, s, t, **kwargs: self.singlestep_dpm_solver_second_update(x, s, t, r1=r1, solver_type=solver_type, **kwargs)
985+
def lower_update(x, s, t):
986+
return self.dpm_solver_first_update(x, s, t, return_intermediate=True)
987+
def higher_update(x, s, t, **kwargs):
988+
return self.singlestep_dpm_solver_second_update(x, s, t, r1=r1, solver_type=solver_type, **kwargs)
989989
elif order == 3:
990990
r1, r2 = 1. / 3., 2. / 3.
991-
lower_update = lambda x, s, t: self.singlestep_dpm_solver_second_update(x, s, t, r1=r1, return_intermediate=True, solver_type=solver_type)
992-
higher_update = lambda x, s, t, **kwargs: self.singlestep_dpm_solver_third_update(x, s, t, r1=r1, r2=r2, solver_type=solver_type, **kwargs)
991+
def lower_update(x, s, t):
992+
return self.singlestep_dpm_solver_second_update(x, s, t, r1=r1, return_intermediate=True, solver_type=solver_type)
993+
def higher_update(x, s, t, **kwargs):
994+
return self.singlestep_dpm_solver_third_update(x, s, t, r1=r1, r2=r2, solver_type=solver_type, **kwargs)
993995
else:
994996
raise ValueError("For adaptive step size solver, order must be 2 or 3, got {}".format(order))
995997
while torch.abs((s - t_0)).mean() > t_err:
996998
t = ns.inverse_lambda(lambda_s + h)
997999
x_lower, lower_noise_kwargs = lower_update(x, s, t)
9981000
x_higher = higher_update(x, s, t, **lower_noise_kwargs)
9991001
delta = torch.max(torch.ones_like(x).to(x) * atol, rtol * torch.max(torch.abs(x_lower), torch.abs(x_prev)))
1000-
norm_fn = lambda v: torch.sqrt(torch.square(v.reshape((v.shape[0], -1))).mean(dim=-1, keepdim=True))
1002+
def norm_fn(v):
1003+
return torch.sqrt(torch.square(v.reshape((v.shape[0], -1))).mean(dim=-1, keepdim=True))
10011004
E = norm_fn((x_higher - x_lower) / delta).max()
10021005
if torch.all(E <= 1.):
10031006
x = x_higher

diffusion/infer_gt_mel.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import numpy as np
21
import torch
32
import torch.nn.functional as F
43
from diffusion.unit2mel import load_model_vocoder

diffusion/logger/saver.py

-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
'''
44

55
import os
6-
import json
76
import time
87
import yaml
98
import datetime
109
import torch
1110
import matplotlib.pyplot as plt
12-
from . import utils
1311
from torch.utils.tensorboard import SummaryWriter
1412

1513
class Saver(object):

diffusion/logger/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import yaml
33
import json
4-
import pickle
54
import torch
65

76
def traverse_dir(
@@ -121,6 +120,6 @@ def load_model(
121120
ckpt = torch.load(path_pt, map_location=torch.device(device))
122121
global_step = ckpt['global_step']
123122
model.load_state_dict(ckpt['model'], strict=False)
124-
if ckpt.get('optimizer') != None:
123+
if ckpt.get("optimizer") is not None:
125124
optimizer.load_state_dict(ckpt['optimizer'])
126125
return global_step, model, optimizer

diffusion/onnx_export.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import torch
55
import torch.nn as nn
66
import numpy as np
7-
from wavenet import WaveNet
87
import torch.nn.functional as F
9-
import diffusion
108

119
class DotDict(dict):
1210
def __getattr__(*args):
@@ -147,8 +145,8 @@ def OnnxExport(self, project_name=None, init_noise=None, export_encoder=True, ex
147145
spks.update({i:1.0/float(self.n_spk)})
148146
spk_mix = torch.tensor(spk_mix)
149147
spk_mix = spk_mix.repeat(n_frames, 1)
150-
orgouttt = self.init_spkembed(hubert, f0.unsqueeze(-1), volume.unsqueeze(-1), spk_mix_dict=spks)
151-
outtt = self.forward(hubert, mel2ph, f0, volume, spk_mix)
148+
self.init_spkembed(hubert, f0.unsqueeze(-1), volume.unsqueeze(-1), spk_mix_dict=spks)
149+
self.forward(hubert, mel2ph, f0, volume, spk_mix)
152150
if export_encoder:
153151
torch.onnx.export(
154152
self,
@@ -182,8 +180,8 @@ def ExportOnnx(self, project_name=None):
182180
spk_mix.append(1.0/float(self.n_spk))
183181
spks.update({i:1.0/float(self.n_spk)})
184182
spk_mix = torch.tensor(spk_mix)
185-
orgouttt = self.orgforward(hubert, f0.unsqueeze(-1), volume.unsqueeze(-1), spk_mix_dict=spks)
186-
outtt = self.forward(hubert, mel2ph, f0, volume, spk_mix)
183+
self.orgforward(hubert, f0.unsqueeze(-1), volume.unsqueeze(-1), spk_mix_dict=spks)
184+
self.forward(hubert, mel2ph, f0, volume, spk_mix)
187185

188186
torch.onnx.export(
189187
self,

diffusion/solver.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import time
32
import numpy as np
43
import torch

diffusion/uni_pc.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import torch
2-
import torch.nn.functional as F
32
import math
43

54

@@ -109,7 +108,8 @@ def marginal_log_mean_coeff(self, t):
109108
elif self.schedule == 'linear':
110109
return -0.25 * t ** 2 * (self.beta_1 - self.beta_0) - 0.5 * t * self.beta_0
111110
elif self.schedule == 'cosine':
112-
log_alpha_fn = lambda s: torch.log(torch.cos((s + self.cosine_s) / (1. + self.cosine_s) * math.pi / 2.))
111+
def log_alpha_fn(s):
112+
return torch.log(torch.cos((s + self.cosine_s) / (1.0 + self.cosine_s) * math.pi / 2.0))
113113
log_alpha_t = log_alpha_fn(t) - self.cosine_log_alpha_0
114114
return log_alpha_t
115115

@@ -147,7 +147,8 @@ def inverse_lambda(self, lamb):
147147
return t.reshape((-1,))
148148
else:
149149
log_alpha = -0.5 * torch.logaddexp(-2. * lamb, torch.zeros((1,)).to(lamb))
150-
t_fn = lambda log_alpha_t: torch.arccos(torch.exp(log_alpha_t + self.cosine_log_alpha_0)) * 2. * (1. + self.cosine_s) / math.pi - self.cosine_s
150+
def t_fn(log_alpha_t):
151+
return torch.arccos(torch.exp(log_alpha_t + self.cosine_log_alpha_0)) * 2.0 * (1.0 + self.cosine_s) / math.pi - self.cosine_s
151152
t = t_fn(log_alpha)
152153
return t
153154

diffusion/unit2mel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ def init_spkmix(self, n_spk):
116116
hubert_hidden_size = self.input_channel
117117
n_frames = 10
118118
hubert = torch.randn((1, n_frames, hubert_hidden_size))
119-
mel2ph = torch.arange(end=n_frames).unsqueeze(0).long()
119+
torch.arange(end=n_frames).unsqueeze(0).long()
120120
f0 = torch.randn((1, n_frames))
121121
volume = torch.randn((1, n_frames))
122122
spks = {}
123123
for i in range(n_spk):
124124
spks.update({i:1.0/float(self.n_spk)})
125-
orgouttt = self.init_spkembed(hubert, f0.unsqueeze(-1), volume.unsqueeze(-1), spk_mix_dict=spks)
125+
self.init_spkembed(hubert, f0.unsqueeze(-1), volume.unsqueeze(-1), spk_mix_dict=spks)
126126

127127
def forward(self, units, f0, volume, spk_id = None, spk_mix_dict = None, aug_shift = None,
128128
gt_spec=None, infer=True, infer_speedup=10, method='dpm-solver', k_step=300, use_tqdm=True):

inference/infer_tool.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import pickle
2222

2323
from diffusion.unit2mel import load_model_vocoder
24-
import yaml
2524

2625
logging.getLogger('matplotlib').setLevel(logging.WARNING)
2726

@@ -153,7 +152,7 @@ def __init__(self, net_g_path, config_path,
153152
self.hop_size = self.diffusion_args.data.block_size
154153
self.spk2id = self.diffusion_args.spk
155154
self.speech_encoder = self.diffusion_args.data.encoder
156-
self.unit_interpolate_mode = self.diffusion_args.data.unit_interpolate_mode if self.diffusion_args.data.unit_interpolate_mode!=None else 'left'
155+
self.unit_interpolate_mode = self.diffusion_args.data.unit_interpolate_mode if self.diffusion_args.data.unit_interpolate_mode is not None else 'left'
157156
if spk_mix_enable:
158157
self.diffusion_model.init_spkmix(len(self.spk2id))
159158
else:
@@ -290,7 +289,7 @@ def infer(self, speaker, tran, raw_path,
290289
audio = torch.FloatTensor(wav).to(self.dev)
291290
audio_mel = None
292291
if self.only_diffusion or self.shallow_diffusion:
293-
vol = self.volume_extractor.extract(audio[None,:])[None,:,None].to(self.dev) if vol==None else vol[:,:,None]
292+
vol = self.volume_extractor.extract(audio[None,:])[None,:,None].to(self.dev) if vol is None else vol[:,:,None]
294293
if self.shallow_diffusion and second_encoding:
295294
audio16k = librosa.resample(audio.detach().cpu().numpy(), orig_sr=self.target_sample, target_sr=16000)
296295
audio16k = torch.from_numpy(audio16k).to(self.dev)

0 commit comments

Comments
 (0)