-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcreate_generator_model.py
265 lines (205 loc) · 9.42 KB
/
create_generator_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, Reshape, Dropout, LSTM, Bidirectional
from tensorflow.keras.layers import BatchNormalization, LeakyReLU
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from music21 import converter, instrument, note, chord, stream
from pathlib import Path
import matplotlib.pyplot as plt
SEQUENCE_LENGTH = 100
LATENT_DIMENSION = 1000
BATCH_SIZE = 16
EPOCHS = 100
SAMPLE_INTERVAL = 1
def get_notes():
""" Get all the notes and chords from the midi files """
notes = []
for file in Path("archive").glob("*.mid"):
midi = converter.parse(file)
print("Parsing %s" % file)
notes_to_parse = midi.flat.notes
for element in notes_to_parse:
if isinstance(element, note.Note):
notes.append(str(element.pitch))
elif isinstance(element, chord.Chord):
notes.append('.'.join(str(n) for n in element.normalOrder))
return notes
def prepare_sequences(notes, n_vocab):
""" Prepare the sequences used by the Neural Network """
sequence_length = 100
# Get all pitch names
pitchnames = sorted(set(item for item in notes))
# Create a dictionary to map pitches to integers
note_to_int = dict((note, number) for number, note in enumerate(pitchnames))
network_input = []
network_output = []
# create input sequences and the corresponding outputs
for i in range(0, len(notes) - sequence_length, 1):
sequence_in = notes[i:i + sequence_length]
sequence_out = notes[i + sequence_length]
network_input.append([note_to_int[char] for char in sequence_in])
network_output.append(note_to_int[sequence_out])
n_patterns = len(network_input)
# Reshape the input into a format compatible with LSTM layers
network_input = np.reshape(network_input, (n_patterns, sequence_length, 1))
# Normalize input between -1 and 1
network_input = (network_input - float(n_vocab) / 2) / (float(n_vocab) / 2)
network_output = to_categorical(network_output, num_classes=n_vocab) # Use to_categorical from TensorFlow's Keras
return network_input, network_output # Add this return statement
def create_midi(prediction_output, filename):
""" convert the output from the prediction to notes and create a midi file
from the notes """
offset = 0
output_notes = []
# create note and chord objects based on the values generated by the model
for item in prediction_output:
pattern = item[0]
# pattern is a chord
if ('.' in pattern) or pattern.isdigit():
notes_in_chord = pattern.split('.')
notes = []
for current_note in notes_in_chord:
new_note = note.Note(int(current_note))
new_note.storedInstrument = instrument.Piano()
notes.append(new_note)
new_chord = chord.Chord(notes)
new_chord.offset = offset
output_notes.append(new_chord)
# pattern is a note
else:
new_note = note.Note(pattern)
new_note.offset = offset
new_note.storedInstrument = instrument.Piano()
output_notes.append(new_note)
# increase offset each iteration so that notes do not stack
offset += 0.5
midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='{}.mid'.format(filename))
class GAN():
def __init__(self, rows):
self.seq_length = rows
self.seq_shape = (self.seq_length, 1)
self.latent_dim = 1000
self.disc_loss = []
self.gen_loss =[]
optimizer = Adam(0.0002, 0.5)
# Build and compile the discriminator
self.discriminator = self.build_discriminator()
self.discriminator.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy'])
# Build the generator
self.generator = self.build_generator()
# The generator takes noise as input and generates note sequences
z = Input(shape=(self.latent_dim,))
generated_seq = self.generator(z)
# For the combined model we will only train the generator
self.discriminator.trainable = False
# The discriminator takes generated images as input and determines validity
validity = self.discriminator(generated_seq)
# The combined model (stacked generator and discriminator)
# Trains the generator to fool the discriminator
self.combined = Model(z, validity)
self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
def build_discriminator(self):
model = Sequential()
model.add(LSTM(512, input_shape=self.seq_shape, return_sequences=True))
model.add(Bidirectional(LSTM(512)))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(256))
model.add(LeakyReLU(alpha=0.2))
# Adding Minibatch Discrimination
model.add(Dense(100))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.summary()
seq = Input(shape=self.seq_shape)
validity = model(seq)
return Model(seq, validity)
def build_generator(self):
model = Sequential()
model.add(Dense(256, input_dim=self.latent_dim))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(1024))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(np.prod(self.seq_shape), activation='tanh'))
model.add(Reshape(self.seq_shape))
model.summary()
noise = Input(shape=(self.latent_dim,))
seq = model(noise)
return Model(noise, seq)
def train(self, epochs, batch_size=128, sample_interval=50):
# Load and convert the data
notes = get_notes()
n_vocab = len(set(notes))
X_train, y_train = prepare_sequences(notes, n_vocab)
# Adversarial ground truths
real = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
# Training the model
for epoch in range(epochs):
# Training the discriminator
# Select a random batch of note sequences
idx = np.random.randint(0, X_train.shape[0], batch_size)
real_seqs = X_train[idx]
#noise = np.random.choice(range(484), (batch_size, self.latent_dim))
#noise = (noise-242)/242
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
# Generate a batch of new note sequences
gen_seqs = self.generator.predict(noise)
# Train the discriminator
d_loss_real = self.discriminator.train_on_batch(real_seqs, real)
d_loss_fake = self.discriminator.train_on_batch(gen_seqs, fake)
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
# Training the Generator
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
# Train the generator (to have the discriminator label samples as real)
g_loss = self.combined.train_on_batch(noise, real)
# Print the progress and save into loss lists
if epoch % sample_interval == 0:
print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss))
self.disc_loss.append(d_loss[0])
self.gen_loss.append(g_loss)
self.generate(notes)
self.plot_loss()
def generate(self, input_notes):
# Get pitch names and store in a dictionary
notes = input_notes
pitchnames = sorted(set(item for item in notes))
int_to_note = dict((number, note) for number, note in enumerate(pitchnames))
# Use random noise to generate sequences
noise = np.random.normal(0, 1, (1, self.latent_dim))
predictions = self.generator.predict(noise)
pred_notes = [x*242+242 for x in predictions[0]]
# Map generated integer indices to note names, with error handling
pred_notes_mapped = []
for x in pred_notes:
index = int(x)
if index in int_to_note:
pred_notes_mapped.append(int_to_note[index])
else:
# Fallback mechanism: Choose a default note when the index is out of range
pred_notes_mapped.append('C5') # You can choose any default note here
create_midi(pred_notes_mapped, 'gan_final')
def plot_loss(self):
plt.plot(self.disc_loss, c='red')
plt.plot(self.gen_loss, c='blue')
plt.title("GAN Loss per Epoch")
plt.legend(['Discriminator', 'Generator'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.savefig('GAN_Loss_per_Epoch_final.png', transparent=True)
plt.close()
if __name__ == '__main__':
gan = GAN(rows=SEQUENCE_LENGTH)
gan.train(epochs=EPOCHS, batch_size=BATCH_SIZE, sample_interval=SAMPLE_INTERVAL)
# Save the generator and discriminator models
gan.generator.save("generator_model.h5")
gan.discriminator.save("discriminator_model.h5")