'Running the same code yields different results on Colab and on local. No randomization or NN training involved

My code yields different results in the local terminal and on Google Colab. However, if I run the same code on Colab but with a terminal command (i.e. !python test.py), it produces the same result as in local. Unlike another post on Stack Overflow, this bit of code doesn't have randomizer or neural network training stuff, just plain old functions.

Short description of project: I'm trying to write a deep learning code for music generation. I changed the notes (from a sax solo) to lists of fixed length, and put notes in the same measure in a bigger list. I ended up having a list (score) of lists (measures, different length) of lists (notes, fixed length). To transform it into a tensor, I have to make all the lists of the same length, so I decided to pad [0, 0, 0, 0] in the end of each shorter list.

In local and on Colab using terminal command, it outputs both hi and hello as well as padding the zeros successfully. But on Colab using the native run button, it only prints hello and outputs the unpadded list.

The code is as follows:

(data_transform.py)

from music21 import *

def mxl2list(mxlfile): #padded

    print('hi')
    parsed = converter.parse(mxlfile)
    myPiece = []

    measures = parsed[1] #measures[0] is an instrument, not a measure
    measures.pop(0)
    measures[-1].pop(-1)


    for m in measures: #for each measure
        myMeasure = []
        
        #Each note/rest has the following values, in order:
        #Pitch (integer, midi pitch constants, get with n.pitch.midi, 128 for rest)
        #Offset (float)
        #Duration (float, 0.0 for grace notes, get or set with n.quarterLength)
        #Tie (0 for no tie, 1 for tie start, 2 for tie stop, 3 for tie continue)

        for note in m:
            myNote = [0,0,0,0]

            try:
                if note.isRest or note.isNote:
                    pass
            except AttributeError:
                continue

            if note.isRest:
                myNote[0] = 128
            else:
                myNote[0] = note.pitch.midi
            
            myNote[1] = note.offset

            myNote[2] = note.quarterLength

            try:
                isTie = note.tie.type
                if isTie == "start":
                    myNote[3] = 1
                elif isTie == "stop":
                    myNote[3] = 2
                elif isTie == "continue":
                    myNote[3] = 3
            except AttributeError:
                myNote[3] = 0
            
            myMeasure.append(myNote)
        
        myPiece.append(myMeasure)

    #pad shorter length lists with zeros
    zeroList = [0, 0, 0, 0]
    listLen = []
    for measures in myPiece:
        listLen.append(len(measures))
    maxLen = max(listLen)
    for measures in myPiece:
        lenDiff = maxLen - len(measures)
        for i in range(lenDiff):
            measures.append(zeroList)

    return myPiece

(test.py)

from data_transform import *

print('hello')
scoreList = mxl2list('musicxml/gs2.mxl')
print(scoreList)


Solution 1:[1]

By default, Google Colab has a significantly older version of music21 installed. You can upgrade it to the latest version with:

!pip install --upgrade music21

A basic setup for music21 in Colab that will also generate music notation in the notebook can be found at:

https://colab.research.google.com/gist/mscuthbert/431dee45c01598a0c11bc27823bd1c5b/music21_setup.ipynb

Unlike Jupyter Notebook though, MIDI playing in the notebook does not yet work. (edit: there is now a workaround in the link above for playing midi)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1