(argv, config_map, data_fn)
Create transcriptions.
Create transcriptions.
Create transcriptions .
def run(argv, config_map, data_fn):
  """Create transcriptions."""
  tf.logging.set_verbosity(FLAGS.log)

  config = config_map[FLAGS.config]
  hparams = config.hparams
  hparams.parse(FLAGS.hparams)
  hparams.batch_size = 1
  hparams.truncated_length_secs = 0

  with tf.Graph().as_default():
    examples = tf.placeholder(tf.string, [None])

    dataset = data_fn(
        examples=examples,
        preprocess_examples=True,
        params=hparams,
        is_training=False,
        shuffle_examples=False,
        skip_n_initial_records=0)

    estimator = train_util.create_estimator(config.model_fn,
                                            os.path.expanduser(FLAGS.model_dir),
                                            hparams)

    iterator = tf.data.make_initializable_iterator(dataset)
    next_record = iterator.get_next()

    with tf.Session() as sess:
      sess.run([
          tf.initializers.global_variables(),
          tf.initializers.local_variables()
      ])

      for filename in argv[1:]:
        tf.logging.info('Starting transcription for %s...', filename)

        # The reason we bounce between two Dataset objects is so we can use
        # the data processing functionality in data.py without having to
        # construct all the Example protos in memory ahead of time or create
        # a temporary tfrecord file.
        tf.logging.info('Processing file...')
        sess.run(iterator.initializer,
                 {examples: [
                     create_example(filename, hparams.sample_rate,
                                    FLAGS.load_audio_with_librosa)]})

        def transcription_data(params):
          del params
          return tf.data.Dataset.from_tensors(sess.run(next_record))
        input_fn = infer_util.labels_to_features_wrapper(transcription_data)

        tf.logging.info('Running inference...')
        checkpoint_path = None
        if FLAGS.checkpoint_path:
          checkpoint_path = os.path.expanduser(FLAGS.checkpoint_path)
        prediction_list = list(
            estimator.predict(
                input_fn,
                checkpoint_path=checkpoint_path,
                yield_single_examples=False))
        assert len(prediction_list) == 1

        sequence_prediction = music_pb2.NoteSequence.FromString(
            prediction_list[0]['sequence_predictions'][0])

        midi_filename = filename + FLAGS.transcribed_file_suffix + '.midi'
        midi_io.sequence_proto_to_midi_file(sequence_prediction, midi_filename)

        tf.logging.info('Transcription written to %s.', midi_filename)
def run ( argv , config_map , data_fn ) : tf . logging . set_verbosity ( FLAGS . log ) config = config_map [ FLAGS . config ] hparams = config . hparams hparams . parse ( FLAGS . hparams ) hparams . batch_size = 1 hparams . truncated_length_secs = 0 with tf . Graph ( ) . as_default ( ) : examples = tf . placeholder ( tf . string , [ None ] ) dataset = data_fn ( examples = examples , preprocess_examples = True , params = hparams , is_training = False , shuffle_examples = False , skip_n_initial_records = 0 ) estimator = train_util . create_estimator ( config . model_fn , os . path . expanduser ( FLAGS . model_dir ) , hparams ) iterator = tf . data . make_initializable_iterator ( dataset ) next_record = iterator . get_next ( ) with tf . Session ( ) as sess : sess . run ( [ tf . initializers . global_variables ( ) , tf . initializers . local_variables ( ) ] ) for filename in argv [ 1 : ] : tf . logging . info ( 'Starting transcription for %s...' , filename ) # The reason we bounce between two Dataset objects is so we can use # the data processing functionality in data.py without having to # construct all the Example protos in memory ahead of time or create # a temporary tfrecord file. tf . logging . info ( 'Processing file...' ) sess . run ( iterator . initializer , { examples : [ create_example ( filename , hparams . sample_rate , FLAGS . load_audio_with_librosa ) ] } ) def transcription_data ( params ) : del params return tf . data . Dataset . from_tensors ( sess . run ( next_record ) ) input_fn = infer_util . labels_to_features_wrapper ( transcription_data ) tf . logging . info ( 'Running inference...' ) checkpoint_path = None if FLAGS . checkpoint_path : checkpoint_path = os . path . expanduser ( FLAGS . checkpoint_path ) prediction_list = list ( estimator . predict ( input_fn , checkpoint_path = checkpoint_path , yield_single_examples = False ) ) assert len ( prediction_list ) == 1 sequence_prediction = music_pb2 . NoteSequence . FromString ( prediction_list [ 0 ] [ 'sequence_predictions' ] [ 0 ] ) midi_filename = filename + FLAGS . transcribed_file_suffix + '.midi' midi_io . sequence_proto_to_midi_file ( sequence_prediction , midi_filename ) tf . logging . info ( 'Transcription written to %s.' , midi_filename )