forked from llSourcell/AI_Composer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rnn_separate.py
183 lines (153 loc) · 7.15 KB
/
rnn_separate.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
import os, sys
import argparse
import time
import itertools
import cPickle
import logging
import random
import string
import pprint
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import midi_util
import nottingham_util
import sampling
import util
from rnn import get_config_name, DefaultConfig
from model import Model, NottinghamSeparate
if __name__ == '__main__':
np.random.seed()
parser = argparse.ArgumentParser(description='Music RNN')
parser.add_argument('--choice', type=str, default='melody',
choices = ['melody', 'harmony'])
parser.add_argument('--dataset', type=str, default='softmax',
choices = ['bach', 'nottingham', 'softmax'])
parser.add_argument('--model_dir', type=str, default='models')
parser.add_argument('--run_name', type=str, default=time.strftime("%m%d_%H%M"))
args = parser.parse_args()
if args.dataset == 'softmax':
resolution = 480
time_step = 120
model_class = NottinghamSeparate
with open(nottingham_util.PICKLE_LOC, 'r') as f:
pickle = cPickle.load(f)
chord_to_idx = pickle['chord_to_idx']
input_dim = pickle["train"][0].shape[1]
print 'Finished loading data, input dim: {}'.format(input_dim)
else:
raise Exception("Other datasets not yet implemented")
initializer = tf.random_uniform_initializer(-0.1, 0.1)
best_config = None
best_valid_loss = None
# set up run dir
run_folder = os.path.join(args.model_dir, args.run_name)
if os.path.exists(run_folder):
raise Exception("Run name {} already exists, choose a different one", format(run_folder))
os.makedirs(run_folder)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.FileHandler(os.path.join(run_folder, "training.log")))
# grid
grid = {
"dropout_prob": [0.65],
"input_dropout_prob": [0.9],
"num_layers": [1],
"hidden_size": [100]
}
# Generate product of hyperparams
runs = list(list(itertools.izip(grid, x)) for x in itertools.product(*grid.itervalues()))
logger.info("{} runs detected".format(len(runs)))
for combination in runs:
config = DefaultConfig()
config.dataset = args.dataset
config.model_name = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(12)) + '.model'
for attr, value in combination:
setattr(config, attr, value)
if config.dataset == 'softmax':
data = util.load_data('', time_step, config.time_batch_len, config.max_time_batches, nottingham=pickle)
config.input_dim = data["input_dim"]
else:
raise Exception("Other datasets not yet implemented")
# cut away unnecessary parts
r = nottingham_util.NOTTINGHAM_MELODY_RANGE
if args.choice == 'melody':
print "Using only melody"
for d in ['train', 'test', 'valid']:
new_data = []
for batch_data, batch_targets in data[d]["data"]:
new_data.append(([tb[:, :, :r] for tb in batch_data],
[tb[:, :, 0] for tb in batch_targets]))
data[d]["data"] = new_data
else:
print "Using only harmony"
for d in ['train', 'test', 'valid']:
new_data = []
for batch_data, batch_targets in data[d]["data"]:
new_data.append(([tb[:, :, r:] for tb in batch_data],
[tb[:, :, 1] for tb in batch_targets]))
data[d]["data"] = new_data
input_dim = data["input_dim"] = data["train"]["data"][0][0][0].shape[2]
config.input_dim = input_dim
print "New input dim: {}".format(input_dim)
logger.info(config)
config_file_path = os.path.join(run_folder, get_config_name(config) + '.config')
with open(config_file_path, 'w') as f:
cPickle.dump(config, f)
with tf.Graph().as_default(), tf.Session() as session:
with tf.variable_scope("model", reuse=None):
train_model = model_class(config, training=True)
with tf.variable_scope("model", reuse=True):
valid_model = model_class(config, training=False)
saver = tf.train.Saver(tf.all_variables())
tf.initialize_all_variables().run()
# training
early_stop_best_loss = None
start_saving = False
saved_flag = False
train_losses, valid_losses = [], []
start_time = time.time()
for i in range(config.num_epochs):
loss = util.run_epoch(session, train_model, data["train"]["data"], training=True, testing=False)
train_losses.append((i, loss))
if i == 0:
continue
valid_loss = util.run_epoch(session, valid_model, data["valid"]["data"], training=False, testing=False)
valid_losses.append((i, valid_loss))
logger.info('Epoch: {}, Train Loss: {}, Valid Loss: {}, Time Per Epoch: {}'.format(\
i, loss, valid_loss, (time.time() - start_time)/i))
# if it's best validation loss so far, save it
if early_stop_best_loss == None:
early_stop_best_loss = valid_loss
elif valid_loss < early_stop_best_loss:
early_stop_best_loss = valid_loss
if start_saving:
logger.info('Best loss so far encountered, saving model.')
saver.save(session, os.path.join(run_folder, config.model_name))
saved_flag = True
elif not start_saving:
start_saving = True
logger.info('Valid loss increased for the first time, will start saving models')
saver.save(session, os.path.join(run_folder, config.model_name))
saved_flag = True
if not saved_flag:
saver.save(session, os.path.join(run_folder, config.model_name))
# set loss axis max to 20
axes = plt.gca()
if config.dataset == 'softmax':
axes.set_ylim([0, 2])
else:
axes.set_ylim([0, 100])
plt.plot([t[0] for t in train_losses], [t[1] for t in train_losses])
plt.plot([t[0] for t in valid_losses], [t[1] for t in valid_losses])
plt.legend(['Train Loss', 'Validation Loss'])
chart_file_path = os.path.join(run_folder, get_config_name(config) + '.png')
plt.savefig(chart_file_path)
plt.clf()
logger.info("Config {}, Loss: {}".format(config, early_stop_best_loss))
if best_valid_loss == None or early_stop_best_loss < best_valid_loss:
logger.info("Found best new model!")
best_valid_loss = early_stop_best_loss
best_config = config
logger.info("Best Config: {}, Loss: {}".format(best_config, best_valid_loss))