-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
166 lines (142 loc) · 4.98 KB
/
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# *-* coding:utf-8 *-*
import random
import os
import sys
# 我只有A卡,所以用了pml后端
import plaidml.keras
plaidml.keras.install_backend()
import keras
import numpy as np
from keras.models import Sequential
from keras.callbacks import LambdaCallback
from keras.models import Input, Model, load_model
from keras.layers import LSTM, Dropout, Dense, Flatten, Bidirectional, Embedding, GRU, Conv1D, MaxPool1D
from keras.optimizers import Adam
class midiModel(object):
def __init__(self,path):
self.model = None
self.model_path = path
if os.path.exists(path):
print("load model")
self.model = load_model(path)
self.model.summary()
else:
print("build model")
self.build_model()
def build_model(self):
self.model = Sequential()
self.model.add(GRU(128 , return_sequences=True , input_shape=(32,128)))
self.model.add(Dropout(0.6))
self.model.add(GRU(128 , return_sequences=True))
self.model.add(Dense(129, activation='softmax'))
optimizer = Adam(lr=0.001)
self.model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
self.model.summary()
def load_file(self,path):
notes = []
num = 0
with open(path, 'r', encoding='utf-8') as f:
print("open file:"+path)
for line in f:
out_arr = line.split("=",1)
if len(out_arr)>=1 :
if len(out_arr)==2 :
out = int(out_arr[1])
else:
out = -1
inp_arr = out_arr[0].split(",")
inp = []
if len(inp_arr)>0 :
for inp_str in inp_arr:
inp.append(inp_str)
notes.append([inp,out])
return num,notes
def train(self,path):
# 加载文件
data_len , notes = self.load_file(path)
count = 0;
x = np.zeros(
shape=(32 , 128)
)
y = np.zeros(
shape=(32 , 129)
)
print("start")
for note in notes:
note_inp = note[0]
note_out = note[1]
if note_out>=0 and note_out<128:
y[count][note_out] = 1
else:
y[count][128]=1;
for ps in note_inp:
posi = int(ps)
if posi>=0 and posi<128:
x[count][posi] = 1
if count >=31 :
count = 0
#print ("===========================================================")
#print (x)
#print (y)
#print ("===========================================================")
# 开始训练
status = self.model.fit(
[[x]],[[y]], # 我也不知道为什么要这样写。这是试出来的
verbose=True,
steps_per_epoch=32,
epochs=1,
callbacks=[
keras.callbacks.ModelCheckpoint(self.model_path, save_weights_only=False)
]
)
loss = abs(float(status.history['loss'][0]))
print("loss:",loss)
x = np.zeros(
shape=(32 , 128)
)
y = np.zeros(
shape=(32 , 129)
)
else:
count = count+1
def predict(self,notes):
x = np.zeros(
shape=(32 , 128)
)
count = 0
for ser in notes:
for posi in ser:
#print(count , posi)
x[count][int(posi)] = 1
count = count+1
if count>=32 :
break
res_vec = self.model.predict([[x]])[0]
res=[]
count = 0
noteNum = len(notes)
for it in res_vec:
mp = -1
mpvl = 0
if noteNum>count :
hn = notes[count]
for posi in hn :
if posi<128 and posi>=0 :
value = it[int(posi)]
# 获取最大的位置
if value>mpvl :
mp = int(posi)
mpvl = value
# 检验是否静音
if it[128]>mpvl:
mp = 128
if mp>=128 or mp<0 :
mp = -1
res.append(mp)
count = count + 1
return res
if __name__ == '__main__' :
model = midiModel("model.h5")
model.train("data.txt")
print("test:")
print(model.predict([[1,3,5],[3,5,7]]))