(self, item)
return ConverterTensors(inputs=seqs, outputs=seqs, controls=control_seqs)
Converts NoteSequence to unique, one-hot tensor sequences.
Converts NoteSequence to unique, one-hot tensor sequences.
Converts NoteSequence to unique one - hot tensor sequences .
def to_tensors(self, item):
    """Converts NoteSequence to unique, one-hot tensor sequences."""
    note_sequence = item
    try:
      if self._steps_per_quarter:
        quantized_sequence = note_seq.quantize_note_sequence(
            note_sequence, self._steps_per_quarter)
        if (note_seq.steps_per_bar_in_quantized_sequence(quantized_sequence) !=
            self._steps_per_bar):
          return ConverterTensors()
      else:
        quantized_sequence = note_seq.quantize_note_sequence_absolute(
            note_sequence, self._steps_per_second)
    except (note_seq.BadTimeSignatureError, note_seq.NonIntegerStepsPerBarError,
            note_seq.NegativeTimeError) as e:
      return ConverterTensors()

    if (self._chord_encoding and not any(
        ta.annotation_type == CHORD_SYMBOL
        for ta in quantized_sequence.text_annotations)) or (
            self._condition_on_key and not quantized_sequence.key_signatures):
      # We are conditioning on chords and/or key but sequence does not have
      # them. Try to infer chords and optionally key.
      try:
        note_seq.infer_chords_for_sequence(
            quantized_sequence, add_key_signatures=self._condition_on_key)
      except note_seq.ChordInferenceError:
        return ConverterTensors()

    event_lists, unused_stats = self._event_extractor_fn(quantized_sequence)
    if self._pad_to_total_time:
      for e in event_lists:
        e.set_length(len(e) + e.start_step, from_left=True)
        e.set_length(quantized_sequence.total_quantized_steps)
    if self._slice_steps:
      sliced_event_lists = []
      for l in event_lists:
        for i in range(self._slice_steps, len(l) + 1, self._steps_per_bar):
          sliced_event_lists.append(l[i - self._slice_steps: i])
    else:
      sliced_event_lists = event_lists

    # We are going to dedupe the event lists. However, when conditioning on
    # chords and/or key, we want to include the same event list multiple times
    # if it appears with different chords or keys.
    all_sliced_lists = [sliced_event_lists]

    if self._chord_encoding:
      # Extract chord lists that correspond to event lists, i.e. for each event
      # we find the chord active at that time step.
      try:
        sliced_chord_lists = chords_lib.event_list_chords(
            quantized_sequence, sliced_event_lists)
      except chords_lib.CoincidentChordsError:
        return ConverterTensors()
      all_sliced_lists.append(sliced_chord_lists)

    if self._condition_on_key:
      # Extract key lists that correspond to event lists, i.e. for each event
      # we find the key active at that time step.
      if self._steps_per_second:
        steps_per_second = self._steps_per_second
      else:
        qpm = quantized_sequence.tempos[0].qpm
        steps_per_second = self._steps_per_quarter * qpm / 60.0
      sliced_key_lists = chords_lib.event_list_keys(
          quantized_sequence, sliced_event_lists, steps_per_second)
      all_sliced_lists.append(sliced_key_lists)

    all_unique_tuples = self._dedupe_and_sample(all_sliced_lists)
    if not all_unique_tuples:
      return ConverterTensors()

    unique_event_tuples = all_unique_tuples[0]
    unique_chord_tuples = all_unique_tuples[1] if self._chord_encoding else []
    unique_key_tuples = all_unique_tuples[-1] if self._condition_on_key else []

    if self._chord_encoding or self._condition_on_key:
      # We need to encode control sequences consisting of chords and/or keys.
      control_seqs = self._chords_and_keys_to_controls(
          unique_chord_tuples, unique_key_tuples)
      if not control_seqs:
        return ConverterTensors()
    else:
      control_seqs = []

    seqs = []
    for t in unique_event_tuples:
      seqs.append(np_onehot(
          [self._legacy_encoder_decoder.encode_event(e) for e in t] +
          ([] if self.end_token is None else [self.end_token]),
          self.output_depth, self.output_dtype))

    return ConverterTensors(inputs=seqs, outputs=seqs, controls=control_seqs)
def to_tensors ( self , item ) : note_sequence = item try : if self . _steps_per_quarter : quantized_sequence = note_seq . quantize_note_sequence ( note_sequence , self . _steps_per_quarter ) if ( note_seq . steps_per_bar_in_quantized_sequence ( quantized_sequence ) != self . _steps_per_bar ) : return ConverterTensors ( ) else : quantized_sequence = note_seq . quantize_note_sequence_absolute ( note_sequence , self . _steps_per_second ) except ( note_seq . BadTimeSignatureError , note_seq . NonIntegerStepsPerBarError , note_seq . NegativeTimeError ) as e : return ConverterTensors ( ) if ( self . _chord_encoding and not any ( ta . annotation_type == CHORD_SYMBOL for ta in quantized_sequence . text_annotations ) ) or ( self . _condition_on_key and not quantized_sequence . key_signatures ) : # We are conditioning on chords and/or key but sequence does not have # them. Try to infer chords and optionally key. try : note_seq . infer_chords_for_sequence ( quantized_sequence , add_key_signatures = self . _condition_on_key ) except note_seq . ChordInferenceError : return ConverterTensors ( ) event_lists , unused_stats = self . _event_extractor_fn ( quantized_sequence ) if self . _pad_to_total_time : for e in event_lists : e . set_length ( len ( e ) + e . start_step , from_left = True ) e . set_length ( quantized_sequence . total_quantized_steps ) if self . _slice_steps : sliced_event_lists = [ ] for l in event_lists : for i in range ( self . _slice_steps , len ( l ) + 1 , self . _steps_per_bar ) : sliced_event_lists . append ( l [ i - self . _slice_steps : i ] ) else : sliced_event_lists = event_lists # We are going to dedupe the event lists. However, when conditioning on # chords and/or key, we want to include the same event list multiple times # if it appears with different chords or keys. all_sliced_lists = [ sliced_event_lists ] if self . _chord_encoding : # Extract chord lists that correspond to event lists, i.e. for each event # we find the chord active at that time step. try : sliced_chord_lists = chords_lib . event_list_chords ( quantized_sequence , sliced_event_lists ) except chords_lib . CoincidentChordsError : return ConverterTensors ( ) all_sliced_lists . append ( sliced_chord_lists ) if self . _condition_on_key : # Extract key lists that correspond to event lists, i.e. for each event # we find the key active at that time step. if self . _steps_per_second : steps_per_second = self . _steps_per_second else : qpm = quantized_sequence . tempos [ 0 ] . qpm steps_per_second = self . _steps_per_quarter * qpm / 60.0 sliced_key_lists = chords_lib . event_list_keys ( quantized_sequence , sliced_event_lists , steps_per_second ) all_sliced_lists . append ( sliced_key_lists ) all_unique_tuples = self . _dedupe_and_sample ( all_sliced_lists ) if not all_unique_tuples : return ConverterTensors ( ) unique_event_tuples = all_unique_tuples [ 0 ] unique_chord_tuples = all_unique_tuples [ 1 ] if self . _chord_encoding else [ ] unique_key_tuples = all_unique_tuples [ - 1 ] if self . _condition_on_key else [ ] if self . _chord_encoding or self . _condition_on_key : # We need to encode control sequences consisting of chords and/or keys. control_seqs = self . _chords_and_keys_to_controls ( unique_chord_tuples , unique_key_tuples ) if not control_seqs : return ConverterTensors ( ) else : control_seqs = [ ] seqs = [ ] for t in unique_event_tuples : seqs . append ( np_onehot ( [ self . _legacy_encoder_decoder . encode_event ( e ) for e in t ] + ( [ ] if self . end_token is None else [ self . end_token ] ) , self . output_depth , self . output_dtype ) ) return ConverterTensors ( inputs = seqs , outputs = seqs , controls = control_seqs )