-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_memories.py
65 lines (45 loc) · 1.77 KB
/
generate_memories.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
import network
import data
import preprocessing
import numpy as np
from tqdm import tqdm
import config
import os
conditions = ["arabic", "india", "ashsham", "suomi"]
RUN_NAME = "run4"
WEIGHT_NAME = "weights-0.2561.hdf5"
def init():
""" Train a Neural Network to generate music """
_, _, n_vocab, _, _ = data.prepare_sequences()
network_input, _, _, _, _ = data.prepare_sequences(conditions = conditions)
net = network.NetworkGetStates(n_vocab)
model = net.model
if not os.path.exists("outputs/states"):
os.makedirs("outputs/states")
model.load_weights("results/" + RUN_NAME + "/" + WEIGHT_NAME)
generate_states(model, network_input)
def generate_states(model, network_input):
""" generate states """
states = np.zeros((config.NUMBER_GENERATED_STATES, 512*6))
n = len(network_input)
conditions_string = ""
n = len(conditions)
for i in range(n):
conditions_string += conditions[i]
conditions_string += "_"
if conditions_string == "":
conditions_string = "all"
print("Generate states for: " + conditions_string)
for i in tqdm(range(config.NUMBER_GENERATED_STATES)):
idx = i % n
idx2 = np.random.randint(0, n-1)
idx3 = np.random.randint(0, n-1)
idx4 = np.random.randint(0, n-1)
input = np.append((network_input[idx4], network_input[idx3], network_input[idx2]), network_input[idx])
input = input.reshape((1, input.shape[0], 1))
_, state1, state2, state3, state4, state5, state6 = model.predict(input, verbose=0)
state = np.concatenate((state1.T, state2.T, state3.T, state4.T, state5.T, state6.T))
states[i] = state.T
np.save("outputs/states/" + conditions_string + "states.npy", states)
if __name__ == '__main__':
init()