本文整理汇总了Python中midi.utils.midiread函数的典型用法代码示例。如果您正苦于以下问题:Python midiread函数的具体用法?Python midiread怎么用?Python midiread使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了midiread函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
def train(self, file_name, weight_save_file, batch_size=1, num_epoch=200):
print('load data ---------------')
file_train=os.path.join(os.path.split(os.path.dirname(__file__))[0],
'data',file_name,'train','*.mid')
dataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in glob.glob(file_train)]
file_test=os.path.join(os.path.split(os.path.dirname(__file__))[0],
'data',file_name,'test','*.mid')
testdataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in glob.glob(file_test)]
print('load done --------------')
try:
for epoch in range(num_epoch):
t0 = time.time()
numpy.random.shuffle(dataset)
costs = []
accuracys = []
for s, sequence in enumerate(dataset):
y = numpy.hstack((sequence,numpy.zeros((sequence.shape[0],1)) ))
x = numpy.roll(y, 1, axis=0)
x[0,:]=0
x[0,self.maxFeatures-1]=1
cost, accuracy= self.rnnModel.train_on_batch(numpy.array([x]), numpy.array([y]), accuracy=True)
costs.append(cost)
accuracys.append(accuracy)
print('epoch: %i/%i\tcost: %.5f\taccu: %.5f\ttime: %.4f s' % (epoch+1, num_epoch, numpy.mean(costs), numpy.mean(accuracys),time.time()-t0))
sys.stdout.flush()
test_accu=self.evaluate(testdataset)
print('test_accu: %.5f' % ( numpy.mean(test_accu)) )
self.rnnModel.save_weights(weight_save_file)
except KeyboardInterrupt:
print('interrupt by user !')
示例2: modeling_n_gram
def modeling_n_gram(n, files):
assert len(files) > 0, 'Training set is empty!' \
' (did you download the data files?)'
for f in files:
print 'parsing', f
each = midiread(f).piano_roll.astype(theano.config.floatX)
numNote = len(each[0])
# each [ time ] [ note ]
for timeSlice in range(n-1, len(each)):
for noteDest in range(numNote):
valueDest = int(each[timeSlice][noteDest])
for noteFrom in range(numNote):
valueFrom = int(each[timeSlice-1][noteFrom])
#print noteDest,valueDest,noteFrom,valueFrom
probability[noteDest][valueDest][noteFrom][valueFrom] \
= probability[noteDest][valueDest][noteFrom][valueFrom] + 1.0
print 'learning_done'
pkl.dump(probability, open("bi-gram-count.dat", "wb"))
print 'bi-gram-count.dat saved'
示例3: load_dataset
def load_dataset(self):
re = ["data/JSB Chorales/train/*.mid"]
re.append("data/JSB Chorales/test/*.mid")
re.append("data/JSB Chorales/valid/*.mid")
files = []
for r in re:
files += glob.glob(r)
assert(len(files) > 0)
print "generating dataset..."
dataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in files]
memorization_dataset = [[]] # memorize the first unit for 100 time-steps with binary noise
n = 0
for seq in dataset:
for i in range(0, len(seq), self.seq_length):
memorization_dataset[0].append(seq[i:i + self.seq_length])
while False: #n < 100000:
sequence = random.choice(dataset)
if len(sequence) < self.seq_length:
print " to short !"
i = random.choice(range(0, len(sequence), self.seq_length))
memorization_dataset[0].append(sequence[i:i + self.seq_length])
n = n + 1
print "number of sequence for training : ", len(memorization_dataset[0])
self.train = [memorization_dataset[0][:-1000]]
self.valid = [memorization_dataset[0][-1000:]]
self.gradient_dataset = SequenceDataset(self.train, batch_size=None, number_batches=1000)
self.cg_dataset = SequenceDataset(self.train, batch_size=None, number_batches=500)
self.valid_dataset = SequenceDataset(self.valid, batch_size=None, number_batches=500)
示例4: train
def train(self, files, batch_size=100, num_epochs=200):
'''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
files converted to piano-rolls.
files : list of strings --- List of MIDI files that will be loaded as piano-rolls for training.
batch_size : integer --- Training sequences will be split into subsequences of at most this size before applying the SGD updates.
num_epochs : integer --- Number of epochs (pass over the training set) performed. The user can safely interrupt training with Ctrl+C at any time.'''
assert len(files) > 0, 'Training set is empty!' \
' (did you download the data files?)'
dataset = [midiread(f, self.r,
self.dt).piano_roll.astype(theano.config.floatX)
for f in files]
try:
for epoch in xrange(num_epochs):
numpy.random.shuffle(dataset)
costs = []
for s, sequence in enumerate(dataset):
for i in xrange(0, len(sequence), batch_size):
cost = self.train_function(sequence[i:i + batch_size])
costs.append(cost)
print 'Epoch %i/%i' % (epoch + 1, num_epochs),
print numpy.mean(costs)
sys.stdout.flush()
except KeyboardInterrupt:
print 'Interrupted by user.'
示例5: load_jsb
def load_jsb(path):
mkdir_p(path)
d = os.path.join(path, 'JSB Chorales')
if not os.path.isdir(d):
download_jsb(path)
train_filenames = os.path.join(path, 'JSB Chorales', 'train', '*.mid')
valid_filenames = os.path.join(path, 'JSB Chorales', 'valid', '*.mid')
test_filenames = os.path.join(path, 'JSB Chorales', 'test', '*.mid')
train_files = glob.glob(train_filenames)
valid_files = glob.glob(valid_filenames)
test_files = glob.glob(test_filenames)
train_datasets = [midiread(f, r=(21, 109), dt=0.3).piano_roll.astype(theano.config.floatX) for f in train_files]
valid_datasets = [midiread(f, r=(21, 109), dt=0.3).piano_roll.astype(theano.config.floatX) for f in valid_files]
test_datasets = [midiread(f, r=(21, 109), dt=0.3).piano_roll.astype(theano.config.floatX) for f in test_files]
return (train_datasets, [None]), (valid_datasets, [None]), (test_datasets, [None])
示例6: load_midi_data
def load_midi_data(data_dir):
import midi.utils as utils
from midi import MidiInFile as mf
from midi import MidiToText as mt
f = open(data_dir, 'rb')
midiIn = mf.MidiInFile(mt.MidiToText(), f)
midiIn.read()
f.close()
midi_data = utils.midiread(data_dir, dt=0.5)
return midi_data.piano_roll
示例7: LoadDataForPreTraining
def LoadDataForPreTraining(r=(21, 109), dt=0.3):
assert len(trainingSet) > 0, 'Training set is empty!' \
' (did you download the data files?)'
sampleLen=[]
dataset=[]
maxLen=0
nSample=0
for f in trainingSet:
currentMidi=midiread(f, (21, 109),0.3).piano_roll
dataset.append(currentMidi)
sampleLen.append(currentMidi.shape[0])
if maxLen< currentMidi.shape[0]:
maxLen=currentMidi.shape[0]
nSample=nSample+1
return (dataset, sampleLen, nSample, maxLen)
示例8: train
def train(self, files, batch_size=100, num_epochs=200):
'''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
files converted to piano-rolls.
files : list of strings
List of MIDI files that will be loaded as piano-rolls for training.
batch_size : integer
Training sequences will be split into subsequences of at most this
size before applying the SGD updates.
num_epochs : integer
Number of epochs (pass over the training set) performed. The user
can safely interrupt training with Ctrl+C at any time.'''
assert len(files) > 0, 'Training set is empty!' \
' (did you download the data files?)'
dataset = [midiread(f, self.r,
self.dt).piano_roll.astype(theano.config.floatX)
for f in files]
print "Start training process of the recurrent network RBM machine with the given dataset..." ,
print "Lenght of the Dataset: ", len(files)
print "Interrupt if necessariy by pressing Ctrl+C",
print "...Might take some time :) ..."
costst = []
try:
for epoch in xrange(num_epochs):
numpy.random.shuffle(dataset)
costs = []
for s, sequence in enumerate(dataset):
for i in xrange(0, len(sequence), batch_size):
cost = self.train_function(sequence[i:i + batch_size])
costs.append(cost)
print 'Epoch %i/%i' % (epoch + 1, num_epochs),
print "Current mean Energy cost:", numpy.mean(costs)
costst.append(numpy.mean(costs))
print "Training %i percent done; interrupt training by pressing Crtl+C." % (float((float(epoch) + 1.0)*100.0 / float(num_epochs)))
sys.stdout.flush()
except KeyboardInterrupt:
print 'Training Interrupted.'
return self, files, costst
示例9: loadDataSet
def loadDataSet(files):
#File e il path della carterlla contente i file (*.mid)
assert len(files) > 0, 'Training set is empty!' \
' (did you download the data files?)'
#mi calcolo quel'el 'esempio di lunghezza massima
maxLen=0
dataset=[]
for f in files:
currentMidi=midiread(f, (21, 109),0.3).piano_roll.astype(theano.config.floatX)
dataset.append(currentMidi)
if maxLen< currentMidi.shape[0]:
maxLen=currentMidi.shape[0]
#porto tutte le tracce a masima lunghezza aggiongendo silenzio
for i, seq in enumerate(dataset):
if seq.shape[0]<maxLen:
dataset[i]=np.concatenate([seq, np.zeros((maxLen-seq.shape[0], 88))])
#print dataset[0].shape
return np.array(dataset, dtype=theano.config.floatX)
示例10: loadDataSetMin
def loadDataSetMin(files):
#File e il path della carterlla contente i file (*.mid)
assert len(files) > 0, 'Training set is empty!' \
' (did you download the data files?)'
#mi calcolo quel'el 'esempio di lunghezza massima
minLen=sys.maxint
dataset=[]
for f in files:
currentMidi=midiread(f, (21, 109),0.3).piano_roll.astype(theano.config.floatX)
dataset.append(currentMidi)
if minLen> currentMidi.shape[0]:
minLen=currentMidi.shape[0]
#porto tutte le tracce a masima lunghezza aggiongendo silenzio
for i, seq in enumerate(dataset):
if seq.shape[0]>minLen:
dataset[i]=seq[0:minLen,:]
#print dataset[0].shape
#print "MINLEN: ", minLen
return np.array(dataset, dtype=theano.config.floatX)
示例11: train
def train(self, files, batch_size=500, num_epochs=300):
assert len(files) > 0, 'Training set is empty!' \
' (did you download the data files?)'
dataset = [midiread(f, self.r,
self.dt).piano_roll.astype(theano.config.floatX)
for f in files]
try:
for epoch in xrange(num_epochs):
numpy.random.shuffle(dataset)
costs = []
for s, sequence in enumerate(dataset):
for i in xrange(0, len(sequence), batch_size):
cost = self.train_function(sequence[i:i + batch_size])
costs.append(cost)
print 'Epoch %i/%i' % (epoch + 1, num_epochs),
print numpy.mean(costs)
sys.stdout.flush()
except KeyboardInterrupt:
print 'Interrupted by user.'
示例12: LoadDataForPreTraining
def LoadDataForPreTraining(r=(21, 109), dt=0.3):
assert len(trainingSet) > 0, 'Training set is empty!' \
' (did you download the data files?)'
sampleLen=[]
dataset=[]
maxLen=0
nSample=0
for f in trainingSet:
currentMidi=midiread(f, (21, 109),0.3).piano_roll
print f,": lenght= ",currentMidi.shape[0]
sampleLen.append(currentMidi.shape[0])
if maxLen< currentMidi.shape[0]:
maxLen=currentMidi.shape[0]
nSample=nSample+1
print "#-----dataset data-----#"
print "number of sample: ", len(trainingSet)
print "max lenght:", maxLen
print "# rows of matrix M:",sum(sampleLen)
print "# columns of matrix M:", maxLen*88
return (dataset, sampleLen, nSample, maxLen)
示例13: main
def main():
#--- import data ---#
sizeOfMiniBatch = 5 #how many tunes per miniBatch
noOfEpoch = 100
noOfEpochPerMB = 2
lengthOfMB = 100
sparseParam = np.float32(0.01) #increases with no. of cells
path = './Piano-midi.de/train-individual/hpps'
#path = './Piano-midi.de/train'
files = os.listdir(path)
assert len(files) > 0, 'Training set is empty!' \
' (did you download the data files?)'
#pitch range is from 21 to 109
dataset = [midiread((path + "/" + f), (21, 109),0.3).piano_roll.astype(theano.config.floatX) for f in files]
#check number of notes for each tune:
print(str([np.array(dataset[n]).shape[0] for n in np.arange(np.array(dataset).shape[0])]))
# set "silent" to zero in 1-hot format
for k in np.arange(np.array(dataset).shape[0]):
for n in np.arange(0,np.array(dataset[k]).shape[0],1):
if np.sum(dataset[k][n], dtype=theano.config.floatX) == 0 :
dataset[k][n][0] = np.float32(1.0)
#--- training with data ---#
myRNN4Music = RNN4Music(h1_length=176, h2_length=176, h3_length=176, io_length=88, R1=np.float32(0.001), R2=np.float32(0.001), R3=np.float32(0.001), Rout=np.float32(0.001))
#myRNN4Music.loadParameters('120_120_120_0_001_xEn_150epoch_hpps')
#myRNN4Music.loadParameters('120_120_120_0_001_sqr_150epoch_hpps')
myRNN4Music.loadParameters('176_176_176_0_001_xEn_L1_0_01_100epoch_hpps')
#myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_1_300epoch_hpps')
#myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
#myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_100epoch_hpps')
#myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
#myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_200epoch_hpps')
#myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
#myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_300epoch_hpps')
#myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
#myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_400epoch_hpps')
#--- plot some genearted tunes ---#
for baseSample in np.array([0, 20, 15, 31]):
exampleLength = 50
myRNN4Music.resetStates()
generatedTune = myRNN4Music.genMusic(np.float32(dataset[baseSample][0:exampleLength]), 300)
midiwrite('176_176_176_0_001_sqr_hpps150_' + str(baseSample) + '.mid', generatedTune[0], (21, 109),0.3)
#generatedTune[0] is the tune, generatedTune[1] is the probability at each iteration
#plot genearted probability
plt.figure(0 + baseSample*100)
plt.imshow(np.array(generatedTune[1][0:50,25:65]), origin = 'lower', extent=[25,65,0,50], aspect=0.5,
interpolation = 'nearest', cmap='gist_stern_r')
plt.title('probability of generated midi note piano-roll')
plt.xlabel('midi note')
plt.ylabel('sample number (time steps)')
plt.colorbar()##
#plot leading example for generation
plt.figure(1 + baseSample*100)
plt.imshow(np.transpose(dataset[baseSample]), origin='lower', aspect='auto',
interpolation='nearest', cmap=pylab.cm.gray_r)
plt.colorbar()
plt.title('original piano-roll')
plt.xlabel('sample number (time steps)')
plt.ylabel('midi note')
#plot generated tune
plt.figure(2 + baseSample*100)
plt.imshow(np.transpose(np.array(generatedTune[0][0:500])), origin='lower', aspect='auto',
interpolation='nearest', cmap=pylab.cm.gray_r)
plt.colorbar()
plt.title('generated piano-roll')
plt.xlabel('sample number (time steps)')
plt.ylabel('midi note')
plt.show()
示例14: range
save_dir = './'
### read MIDI
#data_dir = '../Data/Cirriculum/easy/'
#data_dir = '../biaxial-rnn-music-composition/music/'
files = os.listdir(data_dir)
files = [data_dir + f for f in files if '.mid' in f or '.MID' in f]
print files
dataset = []
for f in files:
try:
dataset.append(midiread(f, MIDI_RANGE, DT).piano_roll)
print "{} loaded".format(f)
except IndexError:
print "Skipping {}".format(f)
pass
print np.shape(dataset)
X = []
y = []
for song in dataset:
for i in range(0, len(song) - TICKS_PER_INPUT, STEP):
X.append(song[i: i + TICKS_PER_INPUT])
y.append(song[i + TICKS_PER_INPUT])
示例15: get_ipython
# In[109]:
model.train(onlyfiles)
# In[117]:
model.generate('testHaydn3.mid')
# In[23]:
get_ipython().magic(u'pylab inline')
f = 'testNotthingham6.mid'
extent = (0, 0.25 * len(piano_roll)) + (21, 109)
piano_roll = midiread(f, (21, 109), 0.25).piano_roll.astype(theano.config.floatX)
pylab.figure()
pylab.imshow(piano_roll.T, origin='lower', aspect='auto',
interpolation='nearest', cmap=pylab.cm.gray_r,
extent=extent)
pylab.xlabel('time (s)')
pylab.ylabel('MIDI note number')
pylab.title('generated piano-roll')
pylab.show()
# In[ ]: