This repository was archived by the owner on Nov 13, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepBlueNote.py
More file actions
executable file
·161 lines (124 loc) · 5.66 KB
/
DeepBlueNote.py
File metadata and controls
executable file
·161 lines (124 loc) · 5.66 KB
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
from simple_esn import SimpleESN
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import RidgeCV
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing
from itertools import groupby
import matplotlib.pyplot as plt
from array import array
import numpy as np
import csv
import random
import sys
import heapq
import Utils
import Classify
training_data_file = sys.argv[1]
test_data_file = sys.argv[2]
output_file = sys.argv[3]
n_components = int(sys.argv[4])
damping = float(sys.argv[5])
weight_scaling = float(sys.argv[6])
n_readout = int(sys.argv[7])
discard = int(sys.argv[8])
alpha = float(sys.argv[9])
lengthPenalty = float(sys.argv[10])
random_seed = int(sys.argv[11])
training_data = Utils.readDataFile(training_data_file)
test_data = Utils.readDataFile(test_data_file)
trainData=Utils.importTrainingData(training_data)
testData= Utils.importTestData(test_data)
trainSongsNotes = trainData[0]
testSongsNotes = testData[0]
trainSongsRhythm = trainData[1]
testSongsRhythm = testData[1]
####################### Reservoir Part
#fixed reservoir
#n_readout = 3
#discard = 40
#esn = SimpleESN(n_readout, n_components = 100, damping = 0.2, weight_scaling = 0.99, random_state = 1, discard_steps = discard)
esn = SimpleESN(n_readout, n_components=n_components, damping = damping, weight_scaling = weight_scaling, random_state = random_seed, discard_steps=discard)
#### feed one or more songs to the reservoir and collect the echoes
####################### Learning Part
possibleComposers = list(np.unique(training_data[:,1]))
possibleInstruments = list(np.unique(training_data[:,3]))
possibleStyles = list(np.unique(training_data[:,4]))
possibleYears = list(np.unique(training_data[:,5]))
SVRs = []
learnedSignals = [] #becomes 2D list
possibleComposers = list(np.unique(training_data[:,1]))
numberOfComposers = 37
colorList = plt.cm.Dark2(np.linspace(0, 1, numberOfComposers))
for s in np.arange(len(training_data)):
#print(s)
trainSong = trainSongsNotes[s]
#trainSongRhythm = trainSongsRhythm[s]
inputToReservoir = np.ndarray(shape=(len(trainSong),1), dtype=float, order='F')
inputToReservoir[:,0] = trainSong
# inputToReservoir[:,1] = trainSongRhythm
echoes = Classify.collectEchoes(esn, inputToReservoir)
training = Classify.trainAtOnce(echoes, trainSong, discard, alpha)
SVRs.append(training[0])
#print(training[0].coef_)
learnedSignals.append([])
learnedSignals[s] = training[1]
if (s < 0):
target = training_data[s, 1]
targetAsInteger = possibleComposers.index(target)
xAxis = np.arange(len(trainSong)-discard)
#print(training[1].shape)
#print(len(xAxis))
#plt.plot(xAxis, trainSong[discard : ], color = colorList[targetAsInteger+6] )
#plt.plot(xAxis, training[1], color = colorList[targetAsInteger] )
#plt.plot(xAxis, echoes, color = colorList[targetAsInteger] )
#plt.show()
outFile = open(output_file, 'w')
for s in np.arange(len(test_data)):
#for s in np.arange(10):
#print("****")
#print(s)
testSong = testSongsNotes[s]
# testSongRhythm = testSongsRhythm[s]
inputToReservoir = np.ndarray(shape=(len(testSong),1), dtype=float, order='F')
inputToReservoir[:,0] = testSong
# inputToReservoir[:,1] = testSongRhythm
echoesNewSong = Classify.collectEchoes(esn, inputToReservoir )
errors = []
if (s < 0):
xAxis = np.arange(len(testSong)-discard)
for i in np.arange(len(training_data)):
#plt.figure()
#xAxis = np.arange(len(testSong) - discard)
#plt.plot(xAxis, testSong[discard:], color='k')
err = Classify.compareNewSong(echoesNewSong, SVRs[i], testSong, discard)
err += abs(len(testSong) - len(trainSongsNotes[s])) / Utils.maxLengthOfSong * lengthPenalty;
#print(err)
errors.append(err)
#if ( 0 <= i ):
# composer = training_data[i, 1]
# composerAsInteger = possibleComposers.index(composer)
#plt.plot(xAxis, np.gradient(SVRs[i].predict(echoesNewSong)), color = 'r' )
#plt.show()
# if i>0:
# plt.close(i-1)
#print composer
indicesOf5best = heapq.nsmallest(5, range(len(errors)), errors.__getitem__)
print(indicesOf5best)
#prediction = classify(training_data, errors)
prediction = training_data[np.argmin(errors)]
print(prediction)
outFile.write(prediction[1]+";" + prediction[3] + ";" + prediction[4] + ";" + prediction[5]+ ";"+ prediction[6] + "\n")
## print(training_data[indicesOf5best[0], :])
## print(training_data[indicesOf5best[1], :])
## print(training_data[indicesOf5best[2], :])
## print(training_data[indicesOf5best[3], :])
## print(training_data[indicesOf5best[4], :])
####################### Classification Part
### use the trained SVR (=signature) belonging to a certain composer/style/... to predict a signal from the echoes resulting from a new song
### compare this signal to the true signal for that composer/style/... and return the error
def dissimilarity( echoesOfNewSong, trainedSVR, trueSignal):
predictedSignal = trainedSVR.predict(echoesOfNewSong)
err = mean_squared_error(trueSignal, predictedSignal)
return err;
classIndices = [1, 3, 4, 5] # index of columns in overview file corresponding to different classes