-
Notifications
You must be signed in to change notification settings - Fork 6
/
demo.py
71 lines (61 loc) · 1.85 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from models import *
from scipy import signal
import scipy.io.wavfile
import matplotlib.pyplot as plt
import librosa
import librosa.display
# Load MS-SincResNet model
MODEL_PATH = 'MS-SincResNet.tar'
filename = 'country.00003.wav'
state_dict = torch.load(MODEL_PATH)
model = MS_SincResNet()
model.load_state_dict(state_dict['state_dict'])
model.cuda()
model.eval()
# Read wavefile
_, data = scipy.io.wavfile.read(filename)
data = signal.resample(data, 16000 * 30)
data = data[24000:72000]
# Get spectrogram, harmonic spectrogram,
# percussive spectrogram, and Mel-spectrogram
D = librosa.stft(data, n_fft=512, hop_length=128)
rp = np.max(np.abs(D))
D_harmonic, D_percussive = librosa.decompose.hpss(D)
plt.plot()
librosa.display.specshow(librosa.amplitude_to_db(np.abs(D), ref=rp))
plt.jet()
plt.show()
plt.plot()
librosa.display.specshow(librosa.amplitude_to_db(np.abs(D_harmonic), ref=rp))
plt.jet()
plt.show()
plt.plot()
librosa.display.specshow(librosa.amplitude_to_db(np.abs(D_percussive), ref=rp))
plt.jet()
plt.show()
D = librosa.feature.melspectrogram(y=data, sr=16000, n_mels=160,fmax=8000, n_fft=512,hop_length=128, power=1)
rp = np.max(np.abs(D))
librosa.display.specshow(librosa.amplitude_to_db(np.abs(D), ref=rp))
plt.jet()
plt.show()
# Get the leared 2D representations from MS-SincResNet
data = torch.from_numpy(data).float()
data.unsqueeze_(dim=0)
data.unsqueeze_(dim=0)
data = data.cuda()
_, feat1, feat2, feat3 = model(data)
feat1.squeeze_()
feat2.squeeze_()
feat3.squeeze_()
feat1 = feat1.detach().cpu().numpy()
feat2 = feat2.detach().cpu().numpy()
feat3 = feat3.detach().cpu().numpy()
librosa.display.specshow(librosa.amplitude_to_db(np.abs(feat1), ref=rp))
plt.jet()
plt.show()
librosa.display.specshow(librosa.amplitude_to_db(np.abs(feat2), ref=rp))
plt.jet()
plt.show()
librosa.display.specshow(librosa.amplitude_to_db(np.abs(feat3), ref=rp))
plt.jet()
plt.show()