Skip to content

Commit e1cc8ae

Browse files
committed
cog-ified
1 parent fea0db5 commit e1cc8ae

Some content is hidden

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

67 files changed

+181
-47
lines changed

README.md

+6-6

cog.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
build:
2+
python_version: "3.6"
3+
gpu: True
4+
python_packages:
5+
- cython==3.0.0a9
6+
- torch==1.5.0
7+
- miditoolkit==0.1.14
8+
- tqdm==4.62.2
9+
- pylab-sdk==1.3.2
10+
- matplotlib==3.3.4
11+
- midi2audio==0.1.1
12+
system_packages:
13+
- fluidsynth --fix-missing
14+
- ffmpeg
15+
pre_install:
16+
- "pip install madmom"
17+
- "pip install pytorch-fast-transformers"
18+
predict: "predict.py:Predictor"

dataset/Dataset.md

+5-5

dataset/corpus/.DS_Store

4 KB
Binary file not shown.

dataset/midi_analyzed/.DS_Store

0 Bytes
Binary file not shown.

dataset/midi_analyzed/keep

Whitespace-only changes.

dataset/midi_synchronized/.DS_Store

2 KB
Binary file not shown.

dataset/midi_synchronized/keep

Whitespace-only changes.

dataset/midi_transcribed/.DS_Store

0 Bytes
Binary file not shown.

dataset/midi_transcribed/keep

Whitespace-only changes.

dataset/representations/cond-ls2midi/keep

Whitespace-only changes.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

predict.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import datetime
2+
import glob
3+
import json
4+
import math
5+
import os
6+
import pickle
7+
import random
8+
import sys
9+
import tempfile
10+
import time
11+
from pathlib import Path
12+
import subprocess
13+
14+
import cog
15+
import numpy as np
16+
import torch
17+
import torch.nn as nn
18+
import torch.nn.functional as F
19+
import torch.optim as optim
20+
from midi2audio import FluidSynth
21+
from torch.nn.utils import clip_grad_norm_
22+
from torch.utils.data import DataLoader, Dataset
23+
from tqdm.notebook import tqdm
24+
25+
sys.path.append("workspace/uncond/cp-linear")
26+
from main_cp import *
27+
28+
29+
class Predictor(cog.Predictor):
30+
def setup(self):
31+
"""Load model"""
32+
path_data_root = "dataset/representations/uncond/cp/ailab17k_from-scratch_cp"
33+
path_dictionary = os.path.join(path_data_root, "dictionary.pkl")
34+
path_ckpt = "checkpoints" # path to ckpt dir
35+
loss = 25 # loss
36+
name = "loss_" + str(loss)
37+
path_saved_ckpt = os.path.join(path_ckpt, name + "_params.pt")
38+
39+
# load
40+
dictionary = pickle.load(open(path_dictionary, "rb"))
41+
event2word, word2event = dictionary
42+
43+
# config
44+
n_class = []
45+
for key in event2word.keys():
46+
n_class.append(len(dictionary[0][key]))
47+
48+
# init model
49+
net = TransformerModel(n_class, is_training=False)
50+
net.cuda()
51+
net.eval()
52+
53+
# load model
54+
print("[*] load model from:", path_saved_ckpt)
55+
net.load_state_dict(torch.load(path_saved_ckpt))
56+
57+
self.net = net
58+
self.word2event = word2event
59+
self.event2word = event2word
60+
self.dictionary = dictionary
61+
# self.fs = FluidSynth()
62+
63+
@cog.input("seed", type=int, default=-1, help="Random seed, -1 for random")
64+
@cog.input(
65+
"output_type",
66+
type=str,
67+
default="audio",
68+
options=["audio", "midi"],
69+
help="Output file type, can be audio or midi",
70+
)
71+
def predict(self, seed, output_type):
72+
"""Compute prediction"""
73+
if seed >= 0:
74+
random.seed(seed)
75+
np.random.seed(seed)
76+
torch.backends.cudnn.deterministic = True
77+
torch.backends.cudnn.benchmark = False
78+
torch.manual_seed(seed)
79+
80+
output_path_midi = Path(tempfile.mkdtemp()) / "output.mid"
81+
output_path_wav = Path(tempfile.mkdtemp()) / "output.wav"
82+
output_path_mp3 = Path(tempfile.mkdtemp()) / "output.mp3"
83+
res = None
84+
while res is None:
85+
# because sometimes happens: ValueError: probabilities contain NaN
86+
try:
87+
res = self.net.inference_from_scratch(self.dictionary)
88+
except:
89+
print("Generation failed... Re-trying")
90+
91+
write_midi(res, str(output_path_midi), self.word2event)
92+
93+
if output_type == "audio":
94+
command_fs = (
95+
"fluidsynth -ni /usr/share/sounds/sf2/FluidR3_GM.sf2 "
96+
+ str(output_path_midi)
97+
+ " -F "
98+
+ str(output_path_wav)
99+
+ " -r 44100"
100+
)
101+
os.system(command_fs)
102+
# self.fs.midi_to_audio(str(output_path_midi), str(output_path_wav))
103+
subprocess.check_output(
104+
[
105+
"ffmpeg",
106+
"-i",
107+
str(output_path_wav),
108+
"-af",
109+
"silenceremove=1:0:-50dB,aformat=dblp,areverse,silenceremove=1:0:-50dB,aformat=dblp,areverse", # strip silence
110+
str(output_path_mp3),
111+
],
112+
)
113+
114+
return output_path_mp3
115+
116+
elif output_type == "midi":
117+
return output_path_midi
File renamed without changes.
-8.29 KB
Binary file not shown.
-9.49 KB
Binary file not shown.
Binary file not shown.
-7.78 KB
Binary file not shown.
-9.63 KB
Binary file not shown.
-8.21 KB
Binary file not shown.
-7.34 KB
Binary file not shown.
-8.2 KB
Binary file not shown.
-12.3 KB
Binary file not shown.
-6.48 KB
Binary file not shown.
-7.97 KB
Binary file not shown.
-9.76 KB
Binary file not shown.
-10.3 KB
Binary file not shown.
-7.32 KB
Binary file not shown.
-8.15 KB
Binary file not shown.
-7.69 KB
Binary file not shown.
-8.92 KB
Binary file not shown.
-8.92 KB
Binary file not shown.
-8.31 KB
Binary file not shown.
-8.02 KB
Binary file not shown.
-6.14 KB
Binary file not shown.
-8.02 KB
Binary file not shown.
-10.5 KB
Binary file not shown.
-7.25 KB
Binary file not shown.
-6.51 KB
Binary file not shown.
-8.57 KB
Binary file not shown.
-9.25 KB
Binary file not shown.
-7.31 KB
Binary file not shown.
-6.45 KB
Binary file not shown.
-9.24 KB
Binary file not shown.
-9.05 KB
Binary file not shown.
-11.7 KB
Binary file not shown.
-7.82 KB
Binary file not shown.
-6.38 KB
Binary file not shown.
-6.99 KB
Binary file not shown.
-7.74 KB
Binary file not shown.
-7.72 KB
Binary file not shown.
-9.68 KB
Binary file not shown.
-6.73 KB
Binary file not shown.
-6.33 KB
Binary file not shown.
-7.17 KB
Binary file not shown.
-11.9 KB
Binary file not shown.
-7.24 KB
Binary file not shown.
-9.53 KB
Binary file not shown.
-10.2 KB
Binary file not shown.
-5.78 KB
Binary file not shown.
-6.91 KB
Binary file not shown.
-7.72 KB
Binary file not shown.
-7.68 KB
Binary file not shown.
-8.41 KB
Binary file not shown.

0 commit comments

Comments
 (0)