-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopm-rnn-generate.py
67 lines (50 loc) · 1.86 KB
/
opm-rnn-generate.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
import numpy as np
from keras.engine.saving import model_from_json
import argparse
import sys
import utils
def generate(dataset, weights, json_path):
raw_text = open(dataset).read()
pre_text = utils.pre_process(raw_text)
char_map = utils.map_chars_to_int(pre_text)
int_map = utils.map_int_to_char(pre_text)
params = {
"seq_length":80,
"n_chars" : len(pre_text),
"n_vocab" : len(char_map)
}
testX, _ = utils.prepare_dset(pre_text, char_map, params)
params["n_patterns"] = len(testX)
with open(json_path, 'r') as json_file:
json_model = json_file.read()
model = model_from_json(json_model)
model.load_weights(weights)
model.compile(loss="categorical_crossentropy", optimizer="adam")
start = np.random.randint(0 , len(testX) - 1)
sentence = testX[start]
output = []
for i in range(1500):
x = np.reshape(sentence, (1, len(sentence), 1))
x = x / float(params["n_vocab"])
prediction = model.predict(x, verbose=0)
index = np.argmax(prediction)
result = int_map[index]
output.append(result)
sentence.append(index)
sentence = sentence[1:len(sentence)]
print("".join(output))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--dataset",
help="Path of the training dataset", default="opm-lyrics.txt")
parser.add_argument("--weights",
help="Path of the saved weights to load",
default="weights/model-opm-weights.hdf5")
parser.add_argument("--json",
help="Path of the json model to load",
default="model-opm.json")
args = parser.parse_args()
dataset = args.dataset
weights = args.weights
json_path = args.json
generate(dataset, weights, json_path)