-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_music_genaration.py
186 lines (128 loc) · 6.09 KB
/
lstm_music_genaration.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
"""lstm_music_genaration.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1G2X0360Ml-inE-5YA77i2Mijwd1EwJkh
"""
import os
import json
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import LSTM, Dropout, TimeDistributed, Dense, Activation, Embedding
data_directory = "/content/my_proj"
data_file = "Data_Tunes.txt"
charIndex_json = "char_to_index.json"
model_weights_directory = "/content/my_proj/Model_Weights"
BATCH_SIZE = 16
SEQ_LENGTH = 64
def read_batches(all_chars, unique_chars):
length = all_chars.shape[0]
batch_chars = int(length / BATCH_SIZE)
print(length)
for start in range(0, batch_chars - SEQ_LENGTH, 64):
X = np.zeros((BATCH_SIZE, SEQ_LENGTH))
Y = np.zeros((BATCH_SIZE, SEQ_LENGTH, unique_chars))
for batch_index in range(0, 16):
for i in range(0, 64):
X[batch_index, i] = all_chars[batch_index * batch_chars + start + i]
Y[batch_index, i, all_chars[batch_index * batch_chars + start + i + 1]] = 1
yield X, Y
def built_model(batch_size, seq_length, unique_chars):
model = Sequential()
model.add(Embedding(input_dim = unique_chars, output_dim = 512, batch_input_shape = (batch_size, seq_length)))
model.add(LSTM(256, return_sequences = True, stateful = True))
model.add(Dropout(0.2))
model.add(LSTM(256, return_sequences = True, stateful = True))
model.add(Dropout(0.2))
model.add(LSTM(256, return_sequences = True, stateful = True))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(unique_chars)))
model.add(Activation("softmax"))
return model
def training_model(data, epochs = 5):
char_to_index = {ch: i for (i, ch) in enumerate(sorted(list(set(data))))}
print("Number of unique characters in our whole tunes database = {}".format(len(char_to_index)))
with open(os.path.join(data_directory, charIndex_json), mode = "w") as f:
json.dump(char_to_index, f)
index_to_char = {i: ch for (ch, i) in char_to_index.items()}
unique_chars = len(char_to_index)
model = built_model(BATCH_SIZE, SEQ_LENGTH, unique_chars)
model.summary()
model.compile(loss = "categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"])
all_characters = np.asarray([char_to_index[c] for c in data], dtype = np.int32)
print("Total number of characters = "+str(all_characters.shape[0]))
epoch_number, loss, accuracy = [], [], []
for epoch in range(epochs):
print("Epoch {}/{}".format(epoch+1, epochs))
final_epoch_loss, final_epoch_accuracy = 0, 0
epoch_number.append(epoch+1)
for i, (x, y) in enumerate(read_batches(all_characters, unique_chars)):
final_epoch_loss, final_epoch_accuracy = model.train_on_batch(x, y)
print("Batch: {}, Loss: {}, Accuracy: {}".format(i+1, final_epoch_loss, final_epoch_accuracy))
loss.append(final_epoch_loss)
accuracy.append(final_epoch_accuracy)
if (epoch + 1) % 10 == 0:
if not os.path.exists(model_weights_directory):
os.makedirs(model_weights_directory)
model.save_weights(os.path.join(model_weights_directory, "Weights_{}.h5".format(epoch+1)))
print('Saved Weights at epoch {} to file Weights_{}.h5'.format(epoch+1, epoch+1))
log_frame = pd.DataFrame(columns = ["Epoch", "Loss", "Accuracy"])
log_frame["Epoch"] = epoch_number
log_frame["Loss"] = loss
log_frame["Accuracy"] = accuracy
log_frame.to_csv("/content/my_proj/log.csv", index = False)
file = open(os.path.join(data_directory, data_file), mode = 'r')
data = file.read()
file.close()
if __name__ == "__main__":
training_model(data)
log = pd.read_csv(os.path.join(data_directory, "log.csv"))
log
length
def make_model(unique_chars):
model = Sequential()
model.add(Embedding(input_dim = unique_chars, output_dim = 512, batch_input_shape = (1, 1)))
model.add(LSTM(256, return_sequences = True, stateful = True))
model.add(Dropout(0.2))
model.add(LSTM(256, return_sequences = True, stateful = True))
model.add(Dropout(0.2))
model.add(LSTM(256, stateful = True))
model.add(Dropout(0.2))
model.add((Dense(unique_chars)))
model.add(Activation("softmax"))
return model
def generate_sequence(epoch_num, initial_index, seq_length):
with open(os.path.join(data_directory, charIndex_json)) as f:
char_to_index = json.load(f)
index_to_char = {i:ch for ch, i in char_to_index.items()}
unique_chars = len(index_to_char)
model = make_model(unique_chars)
model.load_weights(os.path.join(model_weights_directory , "Weights_{}.h5".format(epoch_num)))
sequence_index = [initial_index]
for _ in range(seq_length):
batch = np.zeros((1, 1))
batch[0, 0] = sequence_index[-1]
predicted_probs = model.predict_on_batch(batch).ravel()
sample = np.random.choice(range(unique_chars), size = 1, p = predicted_probs)
sequence_index.append(sample[0])
seq = ''.join(index_to_char[c] for c in sequence_index)
cnt = 0
for i in seq:
cnt += 1
if i == "\n":
break
seq1 = seq[cnt:]
cnt = 0
for i in seq1:
cnt += 1
if i == "\n" and seq1[cnt] == "\n":
break
seq2 = seq1[:cnt]
return seq2
ep = int(input("1. Which epoch number weight you want to load into the model(10, 20, 30, ..., 90). Small number will generate more errors in music: "))
ar = int(input("\n2. Enter any number between 0 to 86 which will be given as initial charcter to model for generating sequence: "))
ln = int(input("\n3. Enter the length of music sequence you want to generate. Typical number is between 300-600. Too small number will generate hardly generate any sequence: "))
music = generate_sequence(ep, ar, ln)
print("\nMUSIC SEQUENCE GENERATED: \n")
print(music)