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='ConvertMidiDirToSequencesTest') 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_note_sequence_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)
def serialize_collated(self, target_dir): """Saves to disk two .tfrecord files, each holding serialized NoteSequence protos from the previously collated list of .xml file paths. Args: target_dir: directory where to save the .tfrecord files """ # Iterate over 'train', 'eval' and 'test' collections for collection in self.collated_index.keys(): inputs_path = os.path.join(target_dir, collection + '_inputs' + '.tfrecord') targets_path = os.path.join(target_dir, collection + '_targets' + '.tfrecord') if not os.path.exists(target_dir): os.makedirs(target_dir) print('INFO: Created {} directory.'.format(target_dir)) else: for path in [inputs_path, targets_path]: if os.path.exists(path): raise FileExistsError( 'File {} already exists. Please remove and try again.' .format(path)) with note_sequence_io.NoteSequenceRecordWriter(inputs_path) as inputs_writer, \ note_sequence_io.NoteSequenceRecordWriter(targets_path) as targets_writer: for i, pair in enumerate(self.collated_index[collection]): input_proto = self._xml_to_seq_proto( pair[0], collection + '_inputs') target_proto = self._xml_to_seq_proto( pair[1], collection + '_targets') inputs_writer.write(input_proto) targets_writer.write(target_proto) print('INFO: Saved {}.'.format(inputs_path)) print('INFO: Saved {}.'.format(targets_path))
def convert_directory(root_dir, output_file, recursive=False): """Converts files to NoteSequences and writes to `output_file`. Input files found in `root_dir` are converted to NoteSequence protos with the basename of `root_dir` as the collection_name, and the relative path to the file from `root_dir` as the filename. If `recursive` is true, recursively converts any subdirectories of the specified directory. Args: root_dir: A string specifying a root directory. output_file: Path to TFRecord file to write results to. recursive: A boolean specifying whether or not recursively convert files contained in subdirectories of the specified directory. """ with note_sequence_io.NoteSequenceRecordWriter(output_file) as writer: convert_files(root_dir, '', writer, recursive)
def testNoteSequenceRecordWriterAndIterator(self): sequences = [] for i in range(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.assertEqual(sequence, sequences[i])
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not FLAGS.input_dir: tf.logging.fatal('--input_dir required') return if not FLAGS.output_file: tf.logging.fatal('--output_file required') return input_dir = os.path.expanduser(FLAGS.input_dir) output_file = os.path.expanduser(FLAGS.output_file) if not os.path.exists(os.path.dirname(output_file)): os.makedirs(os.path.dirname(output_file)) with note_sequence_io.NoteSequenceRecordWriter( output_file) as sequence_writer: sequences_written = convert_directory(input_dir, '', sequence_writer, FLAGS.recursive) tf.logging.info("Wrote %d NoteSequence protos to '%s'", sequences_written, output_file)
def convert_directory(root_dir, output_file, num_threads, recursive=False): """Converts files to NoteSequences and writes to `output_file`. Input files found in `root_dir` are converted to NoteSequence protos with the basename of `root_dir` as the collection_name, and the relative path to the file from `root_dir` as the filename. If `recursive` is true, recursively converts any subdirectories of the specified directory. Args: root_dir: A string specifying a root directory. output_file: Path to TFRecord file to write results to. num_threads: The number of threads to use for conversions. recursive: A boolean specifying whether or not recursively convert files contained in subdirectories of the specified directory. """ with futures.ThreadPoolExecutor(max_workers=num_threads) as pool: future_to_path = queue_conversions(root_dir, '', pool, recursive) with note_sequence_io.NoteSequenceRecordWriter(output_file) as writer: sequences_written = 0 for future in futures.as_completed(future_to_path): path = future_to_path[future] try: sequence = future.result() except Exception as exc: # pylint: disable=broad-except tf.logging.fatal('%r generated an exception: %s', path, exc) if sequence: writer.write(sequence) sequences_written += 1 tf.logging.log_every_n( tf.logging.INFO, "Wrote %d of %d NoteSequence protos to '%s'", 100, sequences_written, len(future_to_path), output_file) tf.logging.info("Wrote %d NoteSequence protos to '%s'", sequences_written, output_file)
def convert_directory(root_dir, output_file, recursive=False): with note_sequence_io.NoteSequenceRecordWriter(output_file) as writer: convert_files(root_dir, '', writer, recursive)
from magenta.models.melody_rnn import melody_rnn_pipeline from magenta.models.melody_rnn import melody_rnn_model from magenta.pipelines import pipeline import tensorflow as tf tf.logging.set_verbosity('INFO') work_dir = os.getcwd() input_dir = os.path.join(work_dir, 'data') output_dir = os.path.join(work_dir, 'output') anthems_file = os.path.join(output_dir, 'anthems.tfrecord') # files_in_dir = tf.gfile.ListDirectory(input_dir) files_in_dir = tf.io.gfile.listdir(input_dir) with note_sequence_io.NoteSequenceRecordWriter(anthems_file) as writer: for file_in_dir in files_in_dir: full_file_path = os.path.join(input_dir, file_in_dir) print(full_file_path) try: sequence = midi_io.midi_to_sequence_proto( tf.io.gfile.GFile(full_file_path, 'rb').read()) except midi_io.MIDIConversionError as e: tf.logging.warning('Could not parse midi file %s. Error was: %s', full_file_path, e) sequence.collection_name = os.path.basename(work_dir) sequence.filename = os.path.join(output_dir, os.path.basename(full_file_path)) sequence.id = note_sequence_io.generate_note_sequence_id(