wingedsheep / music-generation-toolbox

Toolbox for generating music
MIT License
31 stars 3 forks source link

Error while generating midi files #22

Open blechturm opened 1 year ago

blechturm commented 1 year ago

Hi,

me again, finally found some time to play around. Still super stoked about all of this :D

I am training a compound word transformer model and generating 5 midis each set of 250 epochs.

Unfortunately I ran into an issue with this approach. It seems as if the function weighted_sampling(probs) sometimes returns NaNs. Unfortunately I am still too noob in Python to debug this myself. I now just use "try" to catch the error if it happens.

I could share the model and the google colab to make this reproducible if this helps?

This is the function I wrapped around your example code to generate midis:

# this is a function that generates a number of midi files from a model
def generate_x_midis_from_compoundword_modelrun(number_of_midifiles, midi_dir, model_to_use):
  os.makedirs(midi_dir, exist_ok=True)
  for i in range(number_of_midifiles):
    print(i)
    # Generate music
    print("Generating music.")
    filename = os.path.join(midi_dir,'example'+str(i)+'.mid')
    print(os.path.join(midi_dir,'example'+str(i)+'.mid'))
    model_output = model_to_use.generate(1000)
    # Restore events from input data
    midi = datamanager.to_midi(model_output)
    midi.save(filename)

This is the error I got:


[/content/music-generation-toolbox/mgt/models/compound_word_transformer/compound_word_transformer_wrapper.py](https://localhost:8080/#) in weighted_sampling(probs)
     21     sorted_probs = np.sort(probs)[::-1]
     22     sorted_index = np.argsort(probs)[::-1]
---> 23     word = np.random.choice(sorted_index, size=1, p=sorted_probs)[0]
     24     return word
     25 

mtrand.pyx in numpy.random.mtrand.RandomState.choice()

ValueError: probabilities contain NaN

Best Max

wingedsheep commented 1 year ago

Hi Max,

Glad to hear you are stoked about this project! Sorry for not replying sooner. I'll take a look at it, and try to add some tests for these kinds of bugs.

Kind regards, Vincent

wingedsheep commented 1 year ago

I ran into a problem where the compound word transformer could map midis with octave >= 9 (where 8 would be the max that could be processed naturally). This happened because of the transposition. Maybe this is the problem you run into. I'll fix it soon.