commit 35fb670c9e16622a3d1ecbbfa5c648016f2746bf
parent 58d60b367d7f811a61ee26684d8edf67093aaefa
Author: CarlSouthall <[email protected]>
Date: Thu, 18 May 2017 15:46:25 +0100
Revert "Convert to Python3"
Diffstat:
8 files changed, 22 insertions(+), 297 deletions(-)
diff --git a/ADTLib/__init__.py b/ADTLib/__init__.py
@@ -5,6 +5,6 @@
"""
-
+from __future__ import absolute_import, division, print_function
from . import utils, nn, models
diff --git a/ADTLib/__init__.py.bak b/ADTLib/__init__.py.bak
@@ -1,10 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-@author: CarlSouthall
-
-"""
-
-
-from __future__ import absolute_import, division, print_function
-
-from . import utils, nn, models
diff --git a/ADTLib/models/__init__.py b/ADTLib/models/__init__.py
@@ -6,7 +6,7 @@ ADTBDRNN
"""
-
+from __future__ import absolute_import, division, print_function
import madmom
import tensorflow as tf
@@ -37,24 +37,24 @@ def ADTBDRNN(TrackNames, out_sort='time',ret='yes', out_text='no', savedir='curr
names=list(np.zeros(len(TrackNames)))
Track=list(np.zeros(len(TrackNames)))
- for i in range(len(TrackNames)):
+ for i in xrange(len(TrackNames)):
Track[i]=Wavread(TrackNames[i])
name=TrackNames[i].split('.wav')
names[i]=name[0]
Frames=list(np.zeros(len(Track)))
Train=list(np.zeros(len(Track)))
- for j in range(len(Track)):
+ for j in xrange(len(Track)):
NFrames=int(np.ceil(len(Track[j])/float(HS)))
Frames[j]=np.zeros((NFrames,WL))
- for i in range(NFrames):
+ for i in xrange(NFrames):
Frames[j][i]=np.squeeze(madmom.audio.signal.signal_frame(Track[j],i,WL,HS,origin=-HS))
Spectrogram=madmom.audio.spectrogram.spec(madmom.audio.stft.stft(Frames[j],np.hanning(WL), fft_size=WL))
Train[j]=np.zeros((len(Spectrogram),Time_Steps,len(Spectrogram[0])))
- for i in range(len(Spectrogram)):
- for k in range(Time_Steps):
+ for i in xrange(len(Spectrogram)):
+ for k in xrange(Time_Steps):
if i-k >= 0:
Train[j][i][Time_Steps-k-1]=Spectrogram[i-k,:]
@@ -68,17 +68,17 @@ def ADTBDRNN(TrackNames, out_sort='time',ret='yes', out_text='no', savedir='curr
AF=list(np.zeros(len(Track)))
P=list(np.zeros(len(Track)))
- for j in range(len(Track)):
+ for j in xrange(len(Track)):
AF[j]=list([Kout[j][:,0],Sout[j][:,0],Hout[j][:,0]])
P[j]=list(np.zeros(3))
- for i in range(len(AF[j])):
+ for i in xrange(len(AF[j])):
P[j][i]=MeanPP(AF[j][i],lambd[i])
x=np.sort(P[j][i])
peak=[]
if len(x) > 0:
peak=np.append(peak,x[0])
- for k in range(len(x)-1):
+ for k in xrange(len(x)-1):
if (x[k+1]-peak[len(peak)-1]) >= close_error:
peak=np.append(peak,x[k+1])
@@ -88,7 +88,7 @@ def ADTBDRNN(TrackNames, out_sort='time',ret='yes', out_text='no', savedir='curr
if out_text == 'yes':
write_text(P,names,save_dir=savedir)
- for i in range(len(P)):
+ for i in xrange(len(P)):
Pnew=list(np.zeros(2))
Pnew[0]=np.array(P[i][:,0],dtype=float)
Pnew[1]=np.array(P[i][:,1],dtype=str)
diff --git a/ADTLib/models/__init__.py.bak b/ADTLib/models/__init__.py.bak
@@ -1,102 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-@author: CarlSouthall
-ADTBDRNN
-
-"""
-
-
-from __future__ import absolute_import, division, print_function
-
-import madmom
-import tensorflow as tf
-import numpy as np
-import scipy
-import os
-import inspect
-import ADTLib.nn as ADTnn
-from ADTLib.utils import Wavread, MeanPP, arrange_output, write_text
-
-
-
-def ADTBDRNN(TrackNames, out_sort='time',ret='yes', out_text='no', savedir='current',close_error=0.05,lambd=[9.7,9.9,4.9]):
-
- ''' Bi-directional neural network algorithm outlined in:
-
- Southall, C., R. Stables, J. Hockman, Automatic Drum Transcription Using
- Bi-directional Recurrent Neural Networks,
- Proceedings of the 17th International Society for Music Information
- Retrieval Conference (ISMIR), 2016.
-
- For usage help see github.com/CarlSouthall/ADTLib/usage.md'''
-
- Time_Steps=1
- WL=2048
- HS=512
-
- names=list(np.zeros(len(TrackNames)))
-
- Track=list(np.zeros(len(TrackNames)))
- for i in xrange(len(TrackNames)):
- Track[i]=Wavread(TrackNames[i])
- name=TrackNames[i].split('.wav')
- names[i]=name[0]
-
- Frames=list(np.zeros(len(Track)))
- Train=list(np.zeros(len(Track)))
- for j in xrange(len(Track)):
- NFrames=int(np.ceil(len(Track[j])/float(HS)))
- Frames[j]=np.zeros((NFrames,WL))
- for i in xrange(NFrames):
- Frames[j][i]=np.squeeze(madmom.audio.signal.signal_frame(Track[j],i,WL,HS,origin=-HS))
-
- Spectrogram=madmom.audio.spectrogram.spec(madmom.audio.stft.stft(Frames[j],np.hanning(WL), fft_size=WL))
- Train[j]=np.zeros((len(Spectrogram),Time_Steps,len(Spectrogram[0])))
-
- for i in xrange(len(Spectrogram)):
- for k in xrange(Time_Steps):
- if i-k >= 0:
- Train[j][i][Time_Steps-k-1]=Spectrogram[i-k,:]
-
- Path=os.path.split(inspect.getfile(ADTnn))
- NNPath=Path[0]
-
- Kout=ADTnn.BDRNNRestoreAll(Train,NNPath+'/NNFiles/BDKAll-1000')
- Sout=ADTnn.BDRNNRestoreAll(Train,NNPath+'/NNFiles/BDSAll-1000')
- Hout=ADTnn.BDRNNRestoreAll(Train,NNPath+'/NNFiles/BDHAll-1000')
-
-
- AF=list(np.zeros(len(Track)))
- P=list(np.zeros(len(Track)))
- for j in xrange(len(Track)):
- AF[j]=list([Kout[j][:,0],Sout[j][:,0],Hout[j][:,0]])
- P[j]=list(np.zeros(3))
- for i in xrange(len(AF[j])):
- P[j][i]=MeanPP(AF[j][i],lambd[i])
- x=np.sort(P[j][i])
- peak=[]
- if len(x) > 0:
- peak=np.append(peak,x[0])
-
- for k in xrange(len(x)-1):
- if (x[k+1]-peak[len(peak)-1]) >= close_error:
- peak=np.append(peak,x[k+1])
-
- P[j][i]=peak
-
- P=arrange_output(P,output_sort=out_sort)
-
- if out_text == 'yes':
- write_text(P,names,save_dir=savedir)
- for i in xrange(len(P)):
- Pnew=list(np.zeros(2))
- Pnew[0]=np.array(P[i][:,0],dtype=float)
- Pnew[1]=np.array(P[i][:,1],dtype=str)
- P[i]=Pnew
-
- if len(P)==1:
- P=P[0]
-
- if ret=='yes':
- return P
-
-\ No newline at end of file
diff --git a/ADTLib/nn/__init__.py b/ADTLib/nn/__init__.py
@@ -2,7 +2,7 @@
"""
@author: CarlSouthall
"""
-
+from __future__ import absolute_import, division, print_function
import numpy as np
import tensorflow as tf
from tensorflow.python.ops import rnn, rnn_cell
@@ -11,7 +11,7 @@ def BDRNNRestoreAll(X, RestoreFileName, num_layers=3,Truncated=1,n_hidden=50,n_c
tf.reset_default_graph()
batch_size=0;
- for i in range(len(X)):
+ for i in xrange(len(X)):
if len(X[i]) > batch_size:
batch_size=len(X[i])
@@ -57,7 +57,7 @@ def BDRNNRestoreAll(X, RestoreFileName, num_layers=3,Truncated=1,n_hidden=50,n_c
sess.run(init)
saver.restore(sess,RestoreFileName)
- for i in range (len(Test)):
+ for i in xrange (len(Test)):
test_len = len(Test[i])
if test_len != batch_size:
e=np.zeros((batch_size-test_len,1,len(Test[i][0,0])))
diff --git a/ADTLib/nn/__init__.py.bak b/ADTLib/nn/__init__.py.bak
@@ -1,76 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-@author: CarlSouthall
-"""
-from __future__ import absolute_import, division, print_function
-import numpy as np
-import tensorflow as tf
-from tensorflow.python.ops import rnn, rnn_cell
-
-def BDRNNRestoreAll(X, RestoreFileName, num_layers=3,Truncated=1,n_hidden=50,n_classes=2,cells='tanh'):
-
- tf.reset_default_graph()
- batch_size=0;
- for i in xrange(len(X)):
- if len(X[i]) > batch_size:
- batch_size=len(X[i])
-
- n_input = len(X[0][0][0])
- n_steps = len(X[0][0])
- state_len=num_layers
-
- x = tf.placeholder("float", [None, n_steps, n_input])
- y = tf.placeholder("float", [None, n_classes])
- istate_fw = tf.placeholder("float", [None, (state_len)*n_hidden])
- istate_bw = tf.placeholder("float", [None, (state_len)*n_hidden])
-
- weights = { 'out': tf.Variable(tf.random_normal([n_hidden*2, n_classes]))}
- biases = { 'out': tf.Variable(tf.random_normal([n_classes]))}
-
- def BiRNN(_X, _istate_fw, _istate_bw, _weights, _biases):
-
- _X = tf.transpose(_X, [1, 0, 2])
- _X = tf.reshape(_X, [-1, n_input])
-
- fw_cell_1 = rnn_cell.BasicRNNCell(n_hidden)
- bw_cell_1 = rnn_cell.BasicRNNCell(n_hidden)
- fw_cell=rnn_cell.MultiRNNCell([fw_cell_1]*num_layers)
- bw_cell=rnn_cell.MultiRNNCell([bw_cell_1]*num_layers)
- _X = tf.split(0, n_steps, _X)
- seq=np.int32(np.ones(batch_size)*Truncated)
-
- outputs, statefw,statebw = rnn.bidirectional_rnn(fw_cell, bw_cell, _X,
- initial_state_fw=_istate_fw,
- initial_state_bw=_istate_bw,
- sequence_length=seq)
-
- return tf.matmul(outputs[-1], _weights['out']) + _biases['out']
-
- pred = BiRNN(x, istate_fw, istate_bw, weights, biases)
- out=tf.nn.softmax(pred)
-
- init = tf.initialize_all_variables()
- saver = tf.train.Saver()
- Test=X
- oh=list(np.zeros(len(Test)))
- with tf.Session() as sess:
-
- sess.run(init)
- saver.restore(sess,RestoreFileName)
- for i in xrange (len(Test)):
- test_len = len(Test[i])
- if test_len != batch_size:
- e=np.zeros((batch_size-test_len,1,len(Test[i][0,0])))
- f=np.concatenate((Test[i],e))
- else:
- f=Test[i]
-
- o = sess.run(out, feed_dict={x: f,
- istate_fw: np.zeros((batch_size, (state_len)*n_hidden)),
- istate_bw: np.zeros((batch_size, (state_len)*n_hidden))
- })
- oh[i]=o[:test_len]
-
-
-
- return oh
diff --git a/ADTLib/utils/__init__.py b/ADTLib/utils/__init__.py
@@ -2,7 +2,7 @@
"""
@author: CarlSouthall
"""
-
+from __future__ import absolute_import, division, print_function
import scipy.io.wavfile as wav
import numpy as np
import os
@@ -13,7 +13,7 @@ def MeanPP(Track,Lambda):
m=np.mean(Track)*Lambda
onsets=[]
Track=np.append(Track,0)
- for i in range(len(Track)):
+ for i in xrange(len(Track)):
if Track[i]>Track[i-1] and Track[i]>Track[i+1] and Track[i]> m:
onsets=np.append(onsets,i)
@@ -36,13 +36,13 @@ def arrange_output(Inputs,output_sort='time'):
Out=list(np.zeros(len(Inputs)))
Out1=list(np.zeros(len(Inputs)))
- for i in range(len(Inputs)):
+ for i in xrange(len(Inputs)):
Out[i]=list(np.zeros(len(Inputs[i])))
Out1[i]=list(np.zeros((1,2)))
- for j in range(len(Inputs[i])):
+ for j in xrange(len(Inputs[i])):
Out[i][j]=list(np.zeros((len(Inputs[i][j]))))
- for k in range(len(Inputs[i][j])):
+ for k in xrange(len(Inputs[i][j])):
Out[i][j][k]=list(np.zeros(2))
Out[i][j][k][0]=Inputs[i][j][k]
Out[i][j][k][1]=Names[j]
@@ -58,7 +58,7 @@ def arrange_output(Inputs,output_sort='time'):
Out[i][:,0]=np.array(np.sort(Out1[i]),dtype=str)
indexs=np.argsort(Out1[i])
out_names=list(Out[i][:,1])
- for j in range(len(indexs)):
+ for j in xrange(len(indexs)):
Out[i][j,1]=out_names[indexs[j]]
@@ -70,9 +70,9 @@ def write_text(X,names,suffix='.ADT.txt',save_dir='current'):
current_dir=os.getcwd()
os.chdir(save_dir)
- for i in range(len(names)):
+ for i in xrange(len(names)):
file = open(names[i]+suffix, "w")
- for j in range(len(X[i])):
+ for j in xrange(len(X[i])):
X[i][j][0]=X[i][j][0][0:8]
item=" ".join(X[i][j])
file.write("%s\n" % item)
diff --git a/ADTLib/utils/__init__.py.bak b/ADTLib/utils/__init__.py.bak
@@ -1,85 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-@author: CarlSouthall
-"""
-from __future__ import absolute_import, division, print_function
-import scipy.io.wavfile as wav
-import numpy as np
-import os
-
-
-def MeanPP(Track,Lambda):
-
- m=np.mean(Track)*Lambda
- onsets=[]
- Track=np.append(Track,0)
- for i in xrange(len(Track)):
- if Track[i]>Track[i-1] and Track[i]>Track[i+1] and Track[i]> m:
- onsets=np.append(onsets,i)
-
- if len(onsets) > 0:
- onsets=(onsets*512)/float(44100)
- return onsets
-
-def Wavread(TrackName):
- x=wav.read(TrackName)
- y=x[1]
- if len(y.shape)>1:
- y=np.squeeze(np.sum(y,axis=1))
- y=y/float(np.max(abs(y)))
-
- return y
-
-def arrange_output(Inputs,output_sort='time'):
-
- Names=['BD','SD','HH']
-
- Out=list(np.zeros(len(Inputs)))
- Out1=list(np.zeros(len(Inputs)))
- for i in xrange(len(Inputs)):
-
- Out[i]=list(np.zeros(len(Inputs[i])))
- Out1[i]=list(np.zeros((1,2)))
- for j in xrange(len(Inputs[i])):
- Out[i][j]=list(np.zeros((len(Inputs[i][j]))))
- for k in xrange(len(Inputs[i][j])):
- Out[i][j][k]=list(np.zeros(2))
- Out[i][j][k][0]=Inputs[i][j][k]
- Out[i][j][k][1]=Names[j]
-
-
- if len(Out[i][j])>1:
- Out1[i]=np.concatenate([Out1[i],Out[i][j]],axis=0)
-
- Out[i]=Out1[i][1:]
-
- if output_sort=='time':
- Out1[i]=np.array(Out[i][:,0],dtype=float)
- Out[i][:,0]=np.array(np.sort(Out1[i]),dtype=str)
- indexs=np.argsort(Out1[i])
- out_names=list(Out[i][:,1])
- for j in xrange(len(indexs)):
- Out[i][j,1]=out_names[indexs[j]]
-
-
- return Out
-
-def write_text(X,names,suffix='.ADT.txt',save_dir='current'):
-
- if save_dir != 'current':
- current_dir=os.getcwd()
- os.chdir(save_dir)
-
- for i in xrange(len(names)):
- file = open(names[i]+suffix, "w")
- for j in xrange(len(X[i])):
- X[i][j][0]=X[i][j][0][0:8]
- item=" ".join(X[i][j])
- file.write("%s\n" % item)
-
- if save_dir != 'current':
- os.chdir(current_dir)
-
-
-
-
-\ No newline at end of file