Spaces:
Running
Running
Update src/infer_pack/predictor/RMVPE.py
Browse files333 unmodified lines3343353363373383393403413423433443453463473483493503513523533543553563573583593603618 unmodified lines3703713723733743753767 unmodified lines384385386387388389390391392393394395396397398399400401402403404405406333 unmodified linesckpt = torch.load(model_path, map_location="cpu")model.load_state_dict(ckpt)model.eval()if is_half == True:model = model.half()self.model = modelself.resample_kernel = {}self.is_half = is_halfif device is None:device = "cuda" if torch.cuda.is_available() else "cpu"self.device = deviceself.mel_extractor = MelSpectrogram(is_half, 128, 16000, 1024, 160, None, 30, 8000).to(device)self.model = self.model.to(device)cents_mapping = 20 * np.arange(360) + 1997.3794084376191self.cents_mapping = np.pad(cents_mapping, (4, 4)) # 368def mel2hidden(self, mel):with torch.no_grad():n_frames = mel.shape[-1]mel = mel.float() # Приведение к float32 перед паддингомmel = F.pad(mel, (0, 32 * ((n_frames - 1) // 32 + 1) - n_frames), mode="reflect")hidden = self.model(mel)return hidden[:, :n_frames]8 unmodified linesmel = self.mel_extractor(audio, center=True)hidden = self.mel2hidden(mel)hidden = hidden.squeeze(0).cpu().numpy()if self.is_half == True:hidden = hidden.astype("float32")f0 = self.decode(hidden, thred=thred)return f07 unmodified linesstarts = center - 4ends = center + 5for idx in range(salience.shape[0]):todo_salience.append(salience[:, starts[idx] : ends[idx]][idx])todo_cents_mapping.append(self.cents_mapping[starts[idx] : ends[idx]])todo_salience = np.array(todo_salience)todo_cents_mapping = np.array(todo_cents_mapping)product_sum = np.sum(todo_salience * todo_cents_mapping, 1)weight_sum = np.sum(todo_salience, 1)devided = product_sum / weight_summaxx = np.max(salience, axis=1)devided[maxx <= thred] = 0return devideddef infer_from_audio_with_pitch(self, audio, thred=0.03, f0_min=50, f0_max=1100):audio = torch.from_numpy(audio).float().to(self.device).unsqueeze(0)mel = self.mel_extractor(audio, center=True)hidden = self.mel2hidden(mel)hidden = hidden.squeeze(0).cpu().numpy()if self.is_half == True:hidden = hidden.astype("float32")f0 = self.decode(hidden, thred=thred)f0[(f0 < f0_min) | (f0 > f0_max)] = 0333 unmodified lines3343353363373383393403413423433443453463473483493503513523533543553568 unmodified lines3653663673683693703717 unmodified lines379380381382383384385386387388389390391392393394395396397398399400401333 unmodified linesckpt = torch.load(model_path, map_location="cpu")model.load_state_dict(ckpt)model.eval()if is_half:model = model.half()self.model = modelself.is_half = is_halfself.device = device if device else "cuda" if torch.cuda.is_available() else "cpu"self.mel_extractor = MelSpectrogram(is_half, 128, 16000, 1024, 160, None, 30, 8000).to(self.device)self.model = self.model.to(self.device)cents_mapping = 20 * np.arange(360) + 1997.3794084376191self.cents_mapping = np.pad(cents_mapping, (4, 4))def mel2hidden(self, mel):with torch.no_grad():n_frames = mel.shape[-1]mel = mel.float()mel = F.pad(mel, (0, 32 * ((n_frames - 1) // 32 + 1) - n_frames), mode="reflect")if self.is_half:mel = mel.half()hidden = self.model(mel)return hidden[:, :n_frames]8 unmodified linesmel = self.mel_extractor(audio, center=True)hidden = self.mel2hidden(mel)hidden = hidden.squeeze(0).cpu().numpy()if self.is_half:hidden = hidden.astype("float32")f0 = self.decode(hidden, thred=thred)return f07 unmodified linesstarts = center - 4ends = center + 5for idx in range(salience.shape[0]):todo_salience.append(salience[:, starts[idx]:ends[idx]][idx])todo_cents_mapping.append(self.cents_mapping[starts[idx]:ends[idx]])todo_salience = np.array(todo_salience)todo_cents_mapping = np.array(todo_cents_mapping)product_sum = np.sum(todo_salience * todo_cents_mapping, 1)weight_sum = np.sum(todo_salience, 1)divided = product_sum / weight_summaxx = np.max(salience, axis=1)divided[maxx <= thred] = 0return divideddef infer_from_audio_with_pitch(self, audio, thred=0.03, f0_min=50, f0_max=1100):audio = torch.from_numpy(audio).float().to(self.device).unsqueeze(0)mel = self.mel_extractor(audio, center=True)hidden = self.mel2hidden(mel)hidden = hidden.squeeze(0).cpu().numpy()if self.is_half:hidden = hidden.astype("float32")f0 = self.decode(hidden, thred=thred)f0[(f0 < f0_min) | (f0 > f0_max)] = 0
src/infer_pack/predictor/RMVPE.py+16-21