<jupyter_start><jupyter_text>Part 1 Loading and processing dataIn this part we load and process the midi files.<jupyter_code>fpath = "TPD/classical/bach_846_format0.mid"
midi = mido.MidiFile(fpath)
midi_array, velocity_array = midi_util.midi_to_array_one_hot(midi, 4)
print "midi_array shape = %s" % str(midi_array.shape)
print "velocity_array shape = %s" % str(velocity_array.shape)
midi_notes = midi_array[:, ::2]
midi_continuation = midi_array[:, 1::2]
X = np.dstack((midi_notes, midi_continuation))


print "X shape = %s" % str(X.shape)
def load_midis(base_fpath):
    fpaths = []
    iter = 0
    for (root, dirnames, filenames) in os.walk(base_fpath):
        fpaths += [os.path.join(root, filename) for filename in filenames]
    return [mido.MidiFile(fpath) for fpath in fpaths]

def convert_midis(midis):
    X = []
    Y = []
    iter = 0
    for midi in midis:
        try:
            midi_array, velocity_array = midi_util.midi_to_array_one_hot(midi, 4)
            print "midi_array shape = %s" % str(midi_array.shape)
        except:
            continue
            
        velocity_array = velocity_array.reshape(velocity_array.shape[0],velocity_array.shape[1], 1)
        midi_notes = midi_array[:, ::2]
        midi_continuation = midi_array[:, 1::2]
        X_i = np.dstack((midi_notes, midi_continuation))
        X += [X_i]
        Y += [velocity_array]
    return np.stack(X), np.stack(Y)
midis = load_midis("TPD/classical")
%%capture
X, Y = convert_midis(midis)
print(X.shape)
print(Y.shape)
train_percent = 0.8
total = X.shape[0]
train_index = int(total * train_percent)

X_train, Y_train = X[:train_index,:], Y[:train_index, :]
X_test, Y_test = X[train_index:, :], Y[train_index:, :]
print('size of X_train is ' + str(X_train.shape))
print('size of Y_train is ' + str(Y_train.shape))

print('size of X_test is ' + str(X_test.shape))
print('size of Y_test is ' + str(Y_test.shape))

def model(input_shape):
    X_input = Input(input_shape)
    X = Conv2D(filters=50, kernel_size=(3, 3), strides=(1, 1), padding='same', name='Conv0')(X_input)
    X = Activation('relu')(X)
    
    X = keras.layers.Conv2D(filters=1, kernel_size=(3, 3), strides=(1, 1), padding='same', name='Conv1')(X_input)
    model = Model(inputs=X_input, outputs=X, name='basic')
    return model
test_model = model((512, 88, 2))
test_model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
history = test_model.fit(X_train, Y_train, batch_size = 1, epochs = 150)
# summarize history for loss
plt.plot(history.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train lost'], loc='upper left')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1502225519287834, 'fraction_numerical': 0.017062314540059347, 'mean_word_length': 4.361829025844931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '537621', 'license': 'Apache-2.0', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1012, 'n_words': 210, 'path': '2D_CNN.ipynb', 'repo_name': 'LithiumH/CS230-Music-Style-Transfer', 'size': '2696'}