-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.py
62 lines (45 loc) · 1.86 KB
/
launch.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
import librosa, librosa.display
import numpy as np
import matplotlib
import torch
import torch.nn.functional as F
from training.helper import TrackLoader
def file_to_chromagram(file_name):
sr = 44100
x, sr = librosa.load(file_name, sr=sr) # .wav file and its sampling rate
fmin = librosa.midi_to_hz(22) # minimal key on our chromagram will be A0
hop_length = 256 # needed for Constant-Q Transform
amplitude = librosa.cqt(x, sr=sr, fmin=fmin, n_bins=108, hop_length=hop_length)
chromagram = librosa.amplitude_to_db(np.abs(amplitude))
l = x.shape[-1] // sr
return chromagram, l
def classifier_torch(file_name, net, maxlength=None):
chromo, tracklength = file_to_chromagram(file_name)
if maxlength is None:
maxlength = tracklength
maxlength = min(maxlength, tracklength) - 1
instruments = ["accordion", "guitar", "piano", "violin"]
n_inst = len(instruments)
trackloader = TrackLoader(chromo=chromo, maxlength=maxlength)
class_probs = []
net.eval()
with torch.no_grad():
for data in range(maxlength):
images = trackloader.second(data)
images = images.to(torch.float)
outputs = net(images)
class_probs_batch = [F.sigmoid(el) for el in outputs]
class_probs.append(class_probs_batch)
test_probs = np.array(torch.cat([torch.stack(batch) for batch in class_probs]))[:, :n_inst]
im = test_probs.T
if im.shape[-1] < 25:
lacking_secs = 25 - im.shape[-1]
probs = np.concatenate((im, np.zeros((4, lacking_secs))), axis=1)
else:
probs = im[:, :25]
probs = np.repeat(probs, 150, axis=0)
probs = np.repeat(probs, 50, axis=1)
id_ = np.random.randint(low=1, high=1e+10)
matplotlib.image.imsave('static/probs/' + str(id_) + '.png', 100 - probs, cmap='Blues')
print("feature map saved")
return id_