def runTest(self, relative_root, recursive):
    """Tests the output for the given parameters."""
    root_dir = os.path.join(self.root_dir, relative_root)
    expected_filenames = self.expected_dir_midi_contents[relative_root]
    if recursive:
      for sub_dir in self.expected_sub_dirs[relative_root]:
        for filename in self.expected_dir_midi_contents[
            os.path.join(relative_root, sub_dir)]:
          expected_filenames.add(os.path.join(sub_dir, filename))

    with tempfile.NamedTemporaryFile(
        prefix='ConvertMidiDirToSequenesTest') as output_file:
      with note_sequence_io.NoteSequenceRecordWriter(
          output_file.name) as writer:
        convert_midi_dir_to_note_sequences.convert_directory(
            root_dir, '', writer, recursive)
      actual_filenames = set()
      for sequence in note_sequence_io.note_sequence_record_iterator(
          output_file.name):
        self.assertEquals(
            note_sequence_io.generate_id(sequence.filename,
                                         os.path.basename(relative_root),
                                         'midi'),
            sequence.id)
        self.assertEquals(os.path.basename(root_dir), sequence.collection_name)
        self.assertNotEquals(0, len(sequence.notes))
        actual_filenames.add(sequence.filename)

    self.assertEquals(expected_filenames, actual_filenames)
Example #2
0
def run_conversion(melody_encoder_decoder,
                   note_sequences_file,
                   train_output,
                   eval_output=None,
                   eval_ratio=0.0):
    """Loop that converts NoteSequence protos to SequenceExample protos.

  Args:
    melody_encoder_decoder: A melodies_lib.MelodyEncoderDecoder object.
    note_sequences_file: String path pointing to TFRecord file of NoteSequence
        protos.
    train_output: String path to TFRecord file that training samples will be
        saved to.
    eval_output: If set, string path to TFRecord file that evaluation samples
        will be saved to. Omit this argument to not produce an eval set.
    eval_ratio: Fraction of input that will be saved to eval set. A random
        partition is chosen, so the actual train/eval ratio will vary.
  """
    reader = note_sequence_io.note_sequence_record_iterator(
        note_sequences_file)
    train_writer = tf.python_io.TFRecordWriter(train_output)
    eval_writer = (tf.python_io.TFRecordWriter(eval_output)
                   if eval_output else None)

    input_count = 0
    train_output_count = 0
    eval_output_count = 0
    tf.logging.info('Extracting melodies...')
    for sequence_data in reader:
        # Only extract melodies from 4/4 time music.
        if (not sequence_data.time_signatures or
                not (sequence_data.time_signatures[0].numerator == 4
                     and sequence_data.time_signatures[0].denominator == 4)):
            continue
        extracted_melodies = melodies_lib.extract_melodies(sequence_data)
        for melody in extracted_melodies:
            sequence_example = melody_encoder_decoder.encode(melody)
            serialized = sequence_example.SerializeToString()
            if eval_writer and random.random() < eval_ratio:
                eval_writer.write(serialized)
                eval_output_count += 1
            else:
                train_writer.write(serialized)
                train_output_count += 1
        input_count += 1
        if input_count % 10 == 0:
            tf.logging.info('Extracted %d melodies from %d sequences.',
                            eval_output_count + train_output_count,
                            input_count)

    tf.logging.info('Done. Extracted %d melodies from %d sequences.',
                    eval_output_count + train_output_count, input_count)
    tf.logging.info('Extracted %d melodies for training.', train_output_count)
    if eval_writer:
        tf.logging.info('Extracted %d melodies for evaluation.',
                        eval_output_count)
def main(unused_argv):
  if not FLAGS.tfrecord_file:
    tf.logging.fatal('--tfrecord_file required')
    return
  if not FLAGS.output_dir:
    tf.logging.fatal('--output_dir required')
    return
  for note_sequence in note_sequence_io.note_sequence_record_iterator(FLAGS.tfrecord_file):
      pm = midi_io.sequence_proto_to_pretty_midi(note_sequence)
      pm.write(os.path.join(FLAGS.output_dir, note_sequence.filename))
Example #4
0
def run_conversion(melody_encoder_decoder, note_sequences_file, train_output,
                   eval_output=None, eval_ratio=0.0):
  """Loop that converts NoteSequence protos to SequenceExample protos.

  Args:
    melody_encoder_decoder: A melodies_lib.MelodyEncoderDecoder object.
    note_sequences_file: String path pointing to TFRecord file of NoteSequence
        protos.
    train_output: String path to TFRecord file that training samples will be
        saved to.
    eval_output: If set, string path to TFRecord file that evaluation samples
        will be saved to. Omit this argument to not produce an eval set.
    eval_ratio: Fraction of input that will be saved to eval set. A random
        partition is chosen, so the actual train/eval ratio will vary.
  """
  reader = note_sequence_io.note_sequence_record_iterator(note_sequences_file)
  train_writer = tf.python_io.TFRecordWriter(train_output)
  eval_writer = (tf.python_io.TFRecordWriter(eval_output)
                 if eval_output else None)

  input_count = 0
  train_output_count = 0
  eval_output_count = 0
  tf.logging.info('Extracting melodies...')
  for sequence_data in reader:
    # Only extract melodies from 4/4 time music.
    if (not sequence_data.time_signatures or
        not (sequence_data.time_signatures[0].numerator == 4 and
             sequence_data.time_signatures[0].denominator == 4)):
      continue
    extracted_melodies = melodies_lib.extract_melodies(sequence_data)
    for melody in extracted_melodies:
      sequence_example = melody_encoder_decoder.encode(melody)
      serialized = sequence_example.SerializeToString()
      if eval_writer and random.random() < eval_ratio:
        eval_writer.write(serialized)
        eval_output_count += 1
      else:
        train_writer.write(serialized)
        train_output_count += 1
    input_count += 1
    if input_count % 10 == 0:
      tf.logging.info('Extracted %d melodies from %d sequences.',
                      eval_output_count + train_output_count,
                      input_count)

  tf.logging.info('Done. Extracted %d melodies from %d sequences.',
                  eval_output_count + train_output_count,
                  input_count)
  tf.logging.info('Extracted %d melodies for training.', train_output_count)
  if eval_writer:
    tf.logging.info('Extracted %d melodies for evaluation.', eval_output_count)
def run_conversion(encoder, sequences_file, train_output, eval_output="", eval_ratio=0.0):
    """Loop that converts NoteSequence protos to SequenceExample protos.

  Args:
    encoder: A function that converts Melody to SequenceExample which is fed
        into a model. The function takes one input of type melodies_lib.Melody
        and outputs a tuple (tf.train.SequenceExample, reconstruction_data)
        where reconstruction_data is any extra data that is needed to
        reconstruct a Melody from the given SequenceExample.
    sequences_file: String path pointing to TFRecord file of NoteSequence
        protos.
    train_output: String path to TFRecord file that training samples will be
        saved to.
    eval_output: If set, string path to TFRecord file that evaluation samples
        will be saved to. Omit this argument to not produce an eval set.
    eval_ratio: Fraction of input that will be saved to eval set. A random
        partition is chosen, so the actual train/eval ratio will vary.
  """

    reader = note_sequence_io.note_sequence_record_iterator(sequences_file)
    train_writer = tf.python_io.TFRecordWriter(train_output)
    eval_writer = tf.python_io.TFRecordWriter(eval_output) if eval_output else None

    input_count = 0
    train_output_count = 0
    eval_output_count = 0
    for sequence_data in reader:
        extracted_melodies = melodies_lib.extract_melodies(sequence_data)
        for melody in extracted_melodies:
            sequence_example, _ = encoder(melody)
            serialized = sequence_example.SerializeToString()
            if eval_writer and random.random() < eval_ratio:
                eval_writer.write(serialized)
                eval_output_count += 1
            else:
                train_writer.write(serialized)
                train_output_count += 1
        input_count += 1
        tf.logging.log_every_n(
            logging.INFO,
            "Extracted %d melodies from %d sequences.",
            500,
            eval_output_count + train_output_count,
            input_count,
        )

    logging.info("Found %d sequences", input_count)
    logging.info("Extracted %d melodies for training.", train_output_count)
    if eval_writer:
        logging.info("Extracted %d melodies for evaluation.", eval_output_count)
def run_conversion(encoder, sequences_file, train_output, eval_output='', eval_ratio=0.0):
  """Loop that converts NoteSequence protos to SequenceExample protos.

  Args:
    encoder: A function that converts Melody to SequenceExample which is fed
        into a model. The function takes one input of type melodies_lib.Melody
        and outputs a tuple (tf.train.SequenceExample, reconstruction_data)
        where reconstruction_data is any extra data that is needed to
        reconstruct a Melody from the given SequenceExample.
    sequences_file: String path pointing to TFRecord file of NoteSequence
        protos.
    train_output: String path to TFRecord file that training samples will be
        saved to.
    eval_output: If set, string path to TFRecord file that evaluation samples
        will be saved to. Omit this argument to not produce an eval set.
    eval_ratio: Fraction of input that will be saved to eval set. A random
        partition is chosen, so the actual train/eval ratio will vary.
  """

  reader = note_sequence_io.note_sequence_record_iterator(sequences_file)
  train_writer = tf.python_io.TFRecordWriter(train_output)
  eval_writer = (tf.python_io.TFRecordWriter(eval_output)
                 if eval_output else None)

  input_count = 0
  train_output_count = 0
  eval_output_count = 0
  for sequence_data in reader:
    extracted_melodies = melodies_lib.extract_melodies(sequence_data)
    for melody in extracted_melodies:
      sequence_example, _ = encoder(melody)
      serialized = sequence_example.SerializeToString()
      if eval_writer and random.random() < eval_ratio:
        eval_writer.write(serialized)
        eval_output_count += 1
      else:
        train_writer.write(serialized)
        train_output_count += 1
    input_count += 1
    tf.logging.log_every_n(logging.INFO, 
                           'Extracted %d melodies from %d sequences.',
                           500,
                           eval_output_count + train_output_count,
                           input_count)

  logging.info('Found %d sequences', input_count)
  logging.info('Extracted %d melodies for training.', train_output_count)
  if eval_writer:
    logging.info('Extracted %d melodies for evaluation.', eval_output_count)
Example #7
0
  def testNoteSequenceRecordWriterAndIterator(self):
    sequences = []
    for i in xrange(4):
      sequence = music_pb2.NoteSequence()
      sequence.id = str(i)
      sequence.notes.add().pitch = i
      sequences.append(sequence)

    with tempfile.NamedTemporaryFile(prefix='NoteSequenceIoTest') as temp_file:
      with note_sequence_io.NoteSequenceRecordWriter(temp_file.name) as writer:
        for sequence in sequences:
          writer.write(sequence)

      for i, sequence in enumerate(
          note_sequence_io.note_sequence_record_iterator(temp_file.name)):
        self.assertEquals(sequence, sequences[i])
  def testNoteSequenceRecordWriterAndIterator(self):
    sequences = []
    for i in xrange(4):
      sequence = music_pb2.NoteSequence()
      sequence.id = str(i)
      sequence.notes.add().pitch = i
      sequences.append(sequence)

    with tempfile.NamedTemporaryFile(prefix='NoteSequenceIoTest') as temp_file:
      with note_sequence_io.NoteSequenceRecordWriter(temp_file.name) as writer:
        for sequence in sequences:
          writer.write(sequence)

      for i, sequence in enumerate(
          note_sequence_io.note_sequence_record_iterator(temp_file.name)):
        self.assertEquals(sequence, sequences[i])