forked from brunnergino/JamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chord_model.py
executable file
·142 lines (91 loc) · 3.96 KB
/
chord_model.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from settings import *
from keras.models import load_model
import keras
import numpy as np
from numpy import array
import _pickle as pickle
from keras import backend as K
from data_processing import get_chord_dict
class Chord_Model:
def __init__(self,
model_path,
prediction_mode='sampling',
first_chords=[1,3,2,1,1,3,2,1],
resample='none',
dim_factor=2,
temperature=1.0):
print('loading chord model ...')
self.model = keras.models.load_model(model_path)
self.model.reset_states()
self.embed_layer_output = K.function([self.model.layers[0].input], [self.model.layers[0].output])
self.embed_model = keras.models.Model(inputs=self.model.input,outputs=self.model.get_layer(name="embedding").output)
self.chord_to_index, self.index_to_chords = get_chord_dict()
self.prediction_mode = prediction_mode
self.temperature = temperature
self.resample = resample
self.dim_factor = dim_factor
self.song = []
for chord in first_chords[:-1]:
# print(chord)
self.model.predict(array([[chord]]))
self.song.append(chord)
chord = first_chords[-1]
self.song.append(chord)
self.current_chord = array([[chord]])
def predict_next(self):
prediction = self.model.predict(self.current_chord)[0]
if self.resample=='hard':
prediction[self.current_chord] = 0
prediction = prediction/np.sum(prediction)
elif self.resample=='soft':
prediction[self.current_chord] /= self.dim_factor
prediction = prediction/np.sum(prediction)
# print(prediction)
prediction = np.log(prediction) / self.temperature
prediction = np.exp(prediction) / np.sum(np.exp(prediction))
if self.prediction_mode == 'argmax':
# print('argmax')
while True:
next_chord = np.argmax(prediction)
if next_chord !=0:
break
# print(next_chord)
elif self.prediction_mode == 'sampling':
while True:
next_chord = np.random.choice(len(prediction), p=prediction)
# print(next_chord)
if next_chord !=0:
break
# print(next_chord)
self.song.append(next_chord)
self.current_chord = np.array([next_chord])
return self.current_chord[0]
def embed_chord(self, chord):
return self.embed_layer_output([[[chord]]])[0][0][0]
def embed_chords_song(self, chords):
embeded_chords = []
for chord in chords:
embeded_chords.append(self.embed_chord(chord))
return embeded_chords
class Embed_Chord_Model:
def __init__(self, model_path):
print('loading chord model ...')
model = keras.models.load_model(model_path)
model.reset_states()
self.embed_layer_output = K.function([model.layers[0].input], [model.layers[0].output])
self.chord_to_index, self.index_to_chords = get_chord_dict()
def embed_chord(self, chord):
return self.embed_layer_output([[[chord]]])[0][0][0]
def embed_chords_song(self, chords):
embeded_chords = []
for chord in chords:
embeded_chords.append(self.embed_chord(chord))
return embeded_chords
if __name__=="__main__":
# Paths:
model_folder = 'models/chords/standart_lr_0.00003/'
model_name = 'modelEpoch10'
model = Chord_Model(model_folder + model_name + '.h5', prediction_mode='sampling')
for i in range(0, 16):
model.predict_next()
print(model.song)