def validateProvideBatch_TFRecord(self, truncated_length, batch_size, lengths, expected_num_inputs): hparams = copy.deepcopy(constants.DEFAULT_HPARAMS) examples = [] expected_inputs = [] for i, length in enumerate(lengths): wav_samples = np.zeros( (np.int((length / data.hparams_frames_per_second(hparams)) * constants.DEFAULT_SAMPLE_RATE), 1), np.float32) wav_data = audio_io.samples_to_wav_data( wav_samples, constants.DEFAULT_SAMPLE_RATE) num_frames = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) seq = self._SyntheticSequence( num_frames / data.hparams_frames_per_second(hparams), i + constants.MIN_MIDI_PITCH) examples.append(self._FillExample(seq, wav_data, 'ex%d' % i)) expected_inputs += self._ExampleToInputs(examples[-1], truncated_length) self.assertEqual(expected_num_inputs, len(expected_inputs)) with tempfile.NamedTemporaryFile() as temp_rio: with tf.python_io.TFRecordWriter(temp_rio.name) as writer: for ex in examples: writer.write(ex.SerializeToString()) self.validateProvideBatch(temp_rio.name, truncated_length, batch_size, expected_inputs)
def _CreateExamplesAndExpectedInputs(self, truncated_length, lengths, expected_num_inputs): hparams = copy.deepcopy(configs.DEFAULT_HPARAMS) examples = [] expected_inputs = [] for i, length in enumerate(lengths): wav_samples = np.zeros( (np.int((length / data.hparams_frames_per_second(hparams)) * hparams.sample_rate), 1), np.float32) wav_data = audio_io.samples_to_wav_data(wav_samples, hparams.sample_rate) num_frames = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) seq = self._SyntheticSequence( num_frames / data.hparams_frames_per_second(hparams), i + constants.MIN_MIDI_PITCH) examples.append(self._FillExample(seq, wav_data, 'ex%d' % i)) expected_inputs += self._ExampleToInputs( examples[-1], truncated_length) self.assertEqual(expected_num_inputs, len(expected_inputs)) return examples, expected_inputs
def _ExampleToInputs(self, ex, truncated_length=0): hparams = copy.deepcopy(configs.DEFAULT_HPARAMS) filename = ex.features.feature['id'].bytes_list.value[0] sequence = music_pb2.NoteSequence.FromString( ex.features.feature['sequence'].bytes_list.value[0]) wav_data = ex.features.feature['audio'].bytes_list.value[0] spec = data.wav_to_spec(wav_data, hparams=hparams) roll = sequences_lib.sequence_to_pianoroll( sequence, frames_per_second=data.hparams_frames_per_second(hparams), min_pitch=constants.MIN_MIDI_PITCH, max_pitch=constants.MAX_MIDI_PITCH, min_frame_occupancy_for_label=0.0, onset_mode='length_ms', onset_length_ms=32., onset_delay_ms=0.) length = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) return self._DataToInputs(spec, roll.active, roll.weights, length, filename, truncated_length)
def _ExampleToInputs(self, ex, truncated_length=0, crop_training_sequence_to_notes=False): hparams = copy.deepcopy(constants.DEFAULT_HPARAMS) hparams.crop_training_sequence_to_notes = crop_training_sequence_to_notes filename = ex.features.feature['id'].bytes_list.value[0] sequence, crop_beginning_seconds = data.preprocess_sequence( ex.features.feature['sequence'].bytes_list.value[0], hparams) wav_data = ex.features.feature['audio'].bytes_list.value[0] if crop_training_sequence_to_notes: wav_data = audio_io.crop_wav_data(wav_data, hparams.sample_rate, crop_beginning_seconds, sequence.total_time) spec = data.wav_to_spec(wav_data, hparams=hparams) roll = sequences_lib.sequence_to_pianoroll( sequence, frames_per_second=data.hparams_frames_per_second(hparams), min_pitch=constants.MIN_MIDI_PITCH, max_pitch=constants.MAX_MIDI_PITCH, min_frame_occupancy_for_label=0.0, onset_mode='length_ms', onset_length_ms=32., onset_delay_ms=0.) length = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) return self._DataToInputs(spec, roll.active, roll.weights, length, filename, truncated_length)
def _ValidateProvideBatchMemory(self, truncated_length, batch_size, lengths, expected_num_inputs): hparams = copy.deepcopy(constants.DEFAULT_HPARAMS) examples = [] expected_inputs = [] for i, length in enumerate(lengths): wav_samples = np.zeros( (np.int((length / data.hparams_frames_per_second(hparams)) * hparams.sample_rate), 1), np.float32) wav_data = audio_io.samples_to_wav_data(wav_samples, hparams.sample_rate) num_frames = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) seq = self._SyntheticSequence( num_frames / data.hparams_frames_per_second(hparams), i + constants.MIN_MIDI_PITCH) examples.append(self._FillExample(seq, wav_data, 'ex%d' % i)) expected_inputs += self._ExampleToInputs( examples[-1], truncated_length) self.assertEqual(expected_num_inputs, len(expected_inputs)) self._ValidateProvideBatch( [e.SerializeToString() for e in examples], truncated_length, batch_size, expected_inputs)
def _ValidateProvideBatchMemory(self, truncated_length, batch_size, lengths, expected_num_inputs): hparams = copy.deepcopy(constants.DEFAULT_HPARAMS) examples = [] expected_inputs = [] for i, length in enumerate(lengths): wav_samples = np.zeros( (np.int((length / data.hparams_frames_per_second(hparams)) * hparams.sample_rate), 1), np.float32) wav_data = audio_io.samples_to_wav_data(wav_samples, hparams.sample_rate) num_frames = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) seq = self._SyntheticSequence( num_frames / data.hparams_frames_per_second(hparams), i + constants.MIN_MIDI_PITCH) examples.append(self._FillExample(seq, wav_data, 'ex%d' % i)) expected_inputs += self._ExampleToInputs(examples[-1], truncated_length) self.assertEqual(expected_num_inputs, len(expected_inputs)) self._ValidateProvideBatch([e.SerializeToString() for e in examples], truncated_length, batch_size, expected_inputs)
def _ValidateProvideBatchTFRecord(self, truncated_length, batch_size, lengths, expected_num_inputs, crop_sequence_secs=0): hparams = copy.deepcopy(constants.DEFAULT_HPARAMS) examples = [] expected_inputs = [] for i, length in enumerate(lengths): wav_samples = np.zeros( (np.int((length / data.hparams_frames_per_second(hparams)) * hparams.sample_rate), 1), np.float32) wav_data = audio_io.samples_to_wav_data(wav_samples, hparams.sample_rate) num_frames = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) seq = self._SyntheticSequence( num_frames / data.hparams_frames_per_second(hparams) - crop_sequence_secs * 2, # crop from both ends. i + constants.MIN_MIDI_PITCH, start_time=crop_sequence_secs) examples.append(self._FillExample(seq, wav_data, 'ex%d' % i)) expected_inputs += self._ExampleToInputs( examples[-1], truncated_length, crop_training_sequence_to_notes=crop_sequence_secs > 0) self.assertEqual(expected_num_inputs, len(expected_inputs)) with tempfile.NamedTemporaryFile() as temp_tfr: with tf.python_io.TFRecordWriter(temp_tfr.name) as writer: for ex in examples: writer.write(ex.SerializeToString()) self._ValidateProvideBatch( temp_tfr.name, truncated_length, batch_size, expected_inputs, crop_training_sequence_to_notes=crop_sequence_secs > 0)
def validateProvideBatch_TFRecord(self, truncated_length, batch_size, lengths, expected_num_inputs): hparams = copy.deepcopy(constants.DEFAULT_HPARAMS) examples = [] expected_inputs = [] for i, length in enumerate(lengths): wav_samples = np.zeros( (np.int((length / data.hparams_frames_per_second(hparams)) * constants.DEFAULT_SAMPLE_RATE), 1), np.float32) wav_data = audio_io.samples_to_wav_data(wav_samples, constants.DEFAULT_SAMPLE_RATE) num_frames = data.wav_to_num_frames( wav_data, frames_per_second=data.hparams_frames_per_second(hparams)) seq = self._SyntheticSequence( num_frames / data.hparams_frames_per_second(hparams), i + constants.MIN_MIDI_PITCH) examples.append(self._FillExample(seq, wav_data, 'ex%d' % i)) expected_inputs += self._ExampleToInputs( examples[-1], truncated_length) self.assertEqual(expected_num_inputs, len(expected_inputs)) with tempfile.NamedTemporaryFile() as temp_rio: with tf.python_io.TFRecordWriter(temp_rio.name) as writer: for ex in examples: writer.write(ex.SerializeToString()) self.validateProvideBatch( temp_rio.name, truncated_length, batch_size, expected_inputs)