praca-magisterska/project/generate.py

37 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-05-28 12:40:26 +02:00
import numpy as np
import midi
2019-05-28 12:40:26 +02:00
import tensorflow as tf
from keras.layers import Input, Dense, Conv2D
from keras.models import Model
from tensorflow.keras import layers
from keras.layers import Input, Dense, Conv2D, Flatten, LSTM, Dropout, TimeDistributed, RepeatVector
from keras.models import Model, Sequential
import matplotlib.pyplot as plt
2019-05-28 12:40:26 +02:00
import settings
import pickle
import sys
trained_model_path = sys.argv[1]
output_path = sys.argv[2]
treshold = float(sys.argv[3])
#random seed
generate_seed = np.random.rand(12288).reshape(1,96,128)
# load and predict
model = pickle.load(open(trained_model_path, 'rb'))
generated_sample = model.predict(generate_seed)
generated_sample = generated_sample.reshape(96,128)
# binarize generated music
generated_sample = generated_sample > treshold * generated_sample.max()
2019-05-28 12:40:26 +02:00
#save to midi
midi.to_midi(generated_sample, output_path='{}.mid'.format(output_path) )
2019-05-28 12:40:26 +02:00
#save piano roll to png
plt.imshow(generated_sample, cmap = plt.get_cmap('gray'))
plt.savefig('{}.png'.format(output_path))