|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
from scipy.io import wavfile |
|
import torch |
|
import librosa |
|
from torch.nn import functional as F |
|
|
|
|
|
def save_curve_plot(pred, midi, gt, savepath): |
|
plt.style.use('default') |
|
fig, ax = plt.subplots(figsize=(12, 3)) |
|
|
|
pred[pred == 0] = np.nan |
|
midi[midi == 0] = np.nan |
|
gt[gt == 0] = np.nan |
|
|
|
|
|
ax.plot(range(len(pred)), pred, color='tab:green', label='pred') |
|
ax.plot(range(len(midi)), midi, color='tab:blue', label='midi') |
|
ax.plot(range(len(gt)), gt, color='grey', label='gt') |
|
|
|
plt.tight_layout() |
|
fig.canvas.draw() |
|
plt.legend() |
|
plt.savefig(savepath) |
|
plt.close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def minmax_norm_diff(tensor: torch.Tensor, vmax: float = librosa.note_to_hz('C6'), |
|
vmin: float = 0) -> torch.Tensor: |
|
tensor = torch.clip(tensor, vmin, vmax) |
|
tensor = 2 * (tensor - vmin) / (vmax - vmin) - 1 |
|
return tensor |
|
|
|
|
|
def reverse_minmax_norm_diff(tensor: torch.Tensor, vmax: float = librosa.note_to_hz('C6'), |
|
vmin: float = 0) -> torch.Tensor: |
|
tensor = torch.clip(tensor, -1.0, 1.0) |
|
tensor = (tensor + 1) / 2 |
|
tensor = tensor * (vmax - vmin) + vmin |
|
return tensor |