def main(unused_argv): tf.logging.set_verbosity(mg.FLAGS.log) if not mg.FLAGS.bundle_file: tf.logging.fatal("--bundle_file is required") return model_path = os.path.join(tgt.MODEL_DIR, mg.FLAGS.bundle_file + ".mag") hparams_path = os.path.join(tgt.MODEL_DIR, mg.FLAGS.bundle_file + ".hparams") if tf.gfile.Exists(hparams_path): tf.logging.info("Model parameter is read from file: %s", hparams_path) with tf.gfile.Open(hparams_path) as f: config, hparams = f.readline().split("\t") melody_rnn_config_flags.FLAGS.config = config melody_rnn_config_flags.FLAGS.hparams = hparams config = melody_rnn_config_flags.config_from_flags() elif mg.FLAGS.bundle_file in melody_rnn_model.default_configs: tf.logging.info("Model parameter is set by default") config = melody_rnn_model.default_configs[mg.FLAGS.bundle_file] else: tf.logging.info("Model parameter is read from arguments: %s", mg.FLAGS.hparams) config = melody_rnn_config_flags.config_from_flags() generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=config.steps_per_quarter, bundle=magenta.music.read_bundle_file(model_path)) mg.FLAGS.output_dir = tgt.GENERATED_DIR mg.run_with_flags(generator)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not FLAGS.run_dir: tf.logging.fatal('--run_dir required') return if not FLAGS.sequence_example_file: tf.logging.fatal('--sequence_example_file required') return sequence_example_file = os.path.expanduser(FLAGS.sequence_example_file) run_dir = os.path.expanduser(FLAGS.run_dir) config = melody_rnn_config_flags.config_from_flags() mode = 'eval' if FLAGS.eval else 'train' graph = melody_rnn_graph.build_graph( mode, config, sequence_example_file) train_dir = os.path.join(run_dir, 'train') if not os.path.exists(train_dir): tf.gfile.MakeDirs(train_dir) tf.logging.info('Train dir: %s', train_dir) if FLAGS.eval: eval_dir = os.path.join(run_dir, 'eval') if not os.path.exists(eval_dir): tf.gfile.MakeDirs(eval_dir) tf.logging.info('Eval dir: %s', eval_dir) run_eval(graph, train_dir, eval_dir, FLAGS.num_training_steps, FLAGS.summary_frequency) else: run_training(graph, train_dir, FLAGS.num_training_steps, FLAGS.summary_frequency)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) config = melody_rnn_config_flags.config_from_flags() pipeline_instance = get_pipeline(config, FLAGS.eval_ratio) FLAGS.input = os.path.expanduser(FLAGS.input) FLAGS.output_dir = os.path.expanduser(FLAGS.output_dir) pipeline.run_pipeline_serial(pipeline_instance,pipeline.tf_record_iterator(FLAGS.input, pipeline_instance.input_type),FLAGS.output_dir)
def main(unused_argv): """Saves bundle or runs generator based on flags.""" tf.logging.set_verbosity(FLAGS.log) bundle = get_bundle() if bundle: config_id = bundle.generator_details.id config = melody_rnn_model.default_configs[config_id] config.hparams.parse(FLAGS.hparams) else: config = melody_rnn_config_flags.config_from_flags() generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=config.steps_per_quarter, checkpoint=get_checkpoint(), bundle=bundle) if FLAGS.save_generator_bundle: bundle_filename = os.path.expanduser(FLAGS.bundle_file) if FLAGS.bundle_description is None: tf.logging.warning('No bundle description provided.') tf.logging.info('Saving generator bundle to %s', bundle_filename) generator.create_bundle_file(bundle_filename, FLAGS.bundle_description) else: run_with_flags(generator)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not FLAGS.run_dir: tf.logging.fatal('--run_dir required') return if not FLAGS.sequence_example_file: tf.logging.fatal('--sequence_example_file required') return sequence_example_file = os.path.expanduser(FLAGS.sequence_example_file) run_dir = os.path.expanduser(FLAGS.run_dir) config = melody_rnn_config_flags.config_from_flags() mode = 'eval' if FLAGS.eval else 'train' graph = events_rnn_graph.build_graph( mode, config, sequence_example_file) train_dir = os.path.join(run_dir, 'train') if not os.path.exists(train_dir): tf.gfile.MakeDirs(train_dir) tf.logging.info('Train dir: %s', train_dir) if FLAGS.eval: eval_dir = os.path.join(run_dir, 'eval') if not os.path.exists(eval_dir): tf.gfile.MakeDirs(eval_dir) tf.logging.info('Eval dir: %s', eval_dir) events_rnn_train.run_eval(graph, train_dir, eval_dir, FLAGS.num_training_steps, FLAGS.summary_frequency) else: events_rnn_train.run_training(graph, train_dir, FLAGS.num_training_steps, FLAGS.summary_frequency)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) train_dir = os.path.join(tgt.MODEL_DIR, "logdir/train") hparams_path = os.path.join(train_dir, "hparams") with tf.gfile.Open(hparams_path) as f: config, hparams = f.readline().split("\t") melody_rnn_config_flags.FLAGS.config = config melody_rnn_config_flags.FLAGS.hparams = hparams config = melody_rnn_config_flags.config_from_flags() generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=config.steps_per_quarter, checkpoint=train_dir) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") bundle_file = (FLAGS.bundle_file if FLAGS.bundle_file else "magenta_" + timestamp) + ".mag" bundle_path = os.path.join(tgt.MODEL_DIR, bundle_file) if FLAGS.bundle_description is None: tf.logging.warning('No bundle description provided.') tf.logging.info('Saving generator bundle to %s', bundle_path) generator.create_bundle_file(bundle_path, FLAGS.bundle_description) tf.gfile.Copy(hparams_path, os.path.join(tgt.MODEL_DIR, bundle_file.replace(".mag", ".hparams")), overwrite=True)
def main(unused_argv): """Saves bundle or runs generator based on flags.""" tf.logging.set_verbosity(FLAGS.log) bundle = get_bundle() if bundle: config_id = bundle.generator_details.id config = melody_rnn_model.default_configs[config_id] config.hparams.parse(FLAGS.hparams) else: config = melody_rnn_config_flags.config_from_flags() generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=config.steps_per_quarter, checkpoint=get_checkpoint(), bundle=bundle) if FLAGS.save_generator_bundle: bundle_filename = os.path.expanduser(FLAGS.bundle_file) if FLAGS.bundle_description is None: tf.logging.warning('No bundle description provided.') tf.logging.info('Saving generator bundle to %s', bundle_filename) generator.create_bundle_file(bundle_filename, FLAGS.bundle_description) else: run_with_flags(generator)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not FLAGS.run_dir: tf.logging.fatal('--run_dir required') return if not FLAGS.sequence_example_file: tf.logging.fatal('--sequence_example_file required') return sequence_example_file_paths = tf.gfile.Glob(os.path.expanduser(FLAGS.sequence_example_file)) run_dir = os.path.expanduser(FLAGS.run_dir) config = melody_rnn_config_flags.config_from_flags() mode = 'eval' if FLAGS.eval else 'train' graph = events_rnn_graph.build_graph(mode, config, sequence_example_file_paths) train_dir = os.path.join(run_dir, 'train') if not os.path.exists(train_dir): tf.gfile.MakeDirs(train_dir) tf.logging.info('Train dir: %s', train_dir) if FLAGS.eval: eval_dir = os.path.join(run_dir, 'eval') if not os.path.exists(eval_dir): tf.gfile.MakeDirs(eval_dir) tf.logging.info('Eval dir: %s', eval_dir) num_batches = ((FLAGS.num_eval_examples if FLAGS.num_eval_examples else magenta.common.count_records(sequence_example_file_paths)) config.hparams.batch_size) events_rnn_train.run_eval(graph, train_dir, eval_dir, num_batches) else: events_rnn_train.run_training(graph, train_dir, FLAGS.num_training_steps,FLAGS.summary_frequency,checkpoints_to_keep=FLAGS.num_checkpoints)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not FLAGS.run_dir: tf.logging.fatal('--run_dir required') return if not FLAGS.sequence_example_file: tf.logging.fatal('--sequence_example_file required') return sequence_example_file_paths = tf.gfile.Glob( os.path.expanduser(FLAGS.sequence_example_file)) run_dir = os.path.expanduser(FLAGS.run_dir) config = melody_rnn_config_flags.config_from_flags() if FLAGS.learn_initial_state: if not FLAGS.id_file: tf.logging.fatal('--id_file required') return # Count records for embedding id_file = os.path.expanduser(FLAGS.id_file) last_line = subprocess.check_output(['tail', '-1', id_file]) config.num_records = int(last_line.split(',')[0]) + 1 tf.logging.info('Counted %d records', config.num_records) mode = 'eval' if FLAGS.eval else 'train' graph = events_rnn_graph.build_graph(mode, config, sequence_example_file_paths) train_dir = os.path.join(run_dir, 'train') if not os.path.exists(train_dir): tf.gfile.MakeDirs(train_dir) tf.logging.info('Train dir: %s', train_dir) embedding_file = os.path.join(train_dir, 'embedding') if FLAGS.eval: eval_dir = os.path.join(run_dir, 'eval') if not os.path.exists(eval_dir): tf.gfile.MakeDirs(eval_dir) tf.logging.info('Eval dir: %s', eval_dir) events_rnn_train.run_eval(graph, train_dir, eval_dir, FLAGS.num_training_steps, FLAGS.summary_frequency) else: events_rnn_train.run_training(graph, train_dir, FLAGS.num_training_steps, FLAGS.summary_frequency, num_records=config.num_records, embedding_file=embedding_file)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) config = melody_rnn_config_flags.config_from_flags() pipeline_instance = get_pipeline( config, FLAGS.eval_ratio) FLAGS.input = os.path.expanduser(FLAGS.input) FLAGS.output_dir = os.path.expanduser(FLAGS.output_dir) pipeline.run_pipeline_serial( pipeline_instance, pipeline.tf_record_iterator(FLAGS.input, pipeline_instance.input_type), FLAGS.output_dir)
def main(unused_argv): """Saves bundle or runs generator based on flags.""" config = melody_rnn_config_flags.config_from_flags() generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=FLAGS.steps_per_quarter, checkpoint=get_checkpoint(), bundle=get_bundle()) if FLAGS.save_generator_bundle: bundle_filename = os.path.expanduser(FLAGS.bundle_file) tf.logging.info('Saving generator bundle to %s', bundle_filename) generator.create_bundle_file(bundle_filename) else: run_with_flags(generator)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) tf.logging.info('RUN FROM SOURCE') if not FLAGS.run_dir: tf.logging.fatal('--run_dir required') return if not FLAGS.sequence_example_file: tf.logging.fatal('--sequence_example_file required') return sequence_example_file_paths = tf.gfile.Glob( os.path.expanduser(FLAGS.sequence_example_file)) run_dir = os.path.expanduser(FLAGS.run_dir) config = melody_rnn_config_flags.config_from_flags() mode = 'eval' if FLAGS.eval else 'train' graph = None if FLAGS.graph == 'old': tf.logging.info('Using old graph') graph = events_rnn_graph.build_graph(mode, config, sequence_example_file_paths) else: tf.logging.info('Using new graph') graph = banger_rnn_graph.build_graph(mode, config, sequence_example_file_paths) train_dir = os.path.join(run_dir, 'train') if not os.path.exists(train_dir): tf.gfile.MakeDirs(train_dir) tf.logging.info('Train dir: %s', train_dir) if FLAGS.eval: eval_dir = os.path.join(run_dir, 'eval') if not os.path.exists(eval_dir): tf.gfile.MakeDirs(eval_dir) tf.logging.info('Eval dir: %s', eval_dir) events_rnn_train.run_eval(graph, train_dir, eval_dir, FLAGS.num_training_steps, FLAGS.summary_frequency) else: events_rnn_train.run_training(graph, train_dir, FLAGS.num_training_steps, FLAGS.summary_frequency)
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) config = melody_rnn_config_flags.config_from_flags() pipeline_instance, id_pipeline_instance = get_pipeline( config, FLAGS.eval_ratio) FLAGS.input = os.path.expanduser(FLAGS.input) FLAGS.output_dir = os.path.expanduser(FLAGS.output_dir) pipeline.run_pipeline_serial( pipeline_instance, pipeline.tf_record_iterator(FLAGS.input, pipeline_instance.input_type), FLAGS.output_dir) # Write id/file mappings if config.learn_initial_state: file = open(FLAGS.output_dir + '/melody-ids.csv', 'w') for id, filename in id_pipeline_instance.mappings.iteritems(): file.write('%d, %s\n' % (id, filename)) file.close()
def main(unused_argv): tf.logging.set_verbosity(FLAGS.log) if not FLAGS.run_dir: tf.logging.fatal('--run_dir required') return if not FLAGS.sequence_example_file: tf.logging.fatal('--sequence_example_file required') return sequence_example_file_paths = tf.gfile.Glob( os.path.expanduser(FLAGS.sequence_example_file)) run_dir = os.path.expanduser(FLAGS.run_dir) config = melody_rnn_config_flags.config_from_flags() mode = 'eval' if FLAGS.eval else 'train' build_graph_fn = events_rnn_graph.get_build_graph_fn( mode, config, sequence_example_file_paths) train_dir = os.path.join(run_dir, 'train') if not os.path.exists(train_dir): tf.gfile.MakeDirs(train_dir) tf.logging.info('Train dir: %s', train_dir) if FLAGS.eval: eval_dir = os.path.join(run_dir, 'eval') if not os.path.exists(eval_dir): tf.gfile.MakeDirs(eval_dir) tf.logging.info('Eval dir: %s', eval_dir) num_batches = ( (FLAGS.num_eval_examples if FLAGS.num_eval_examples else magenta.common.count_records(sequence_example_file_paths)) // config.hparams.batch_size) events_rnn_train.run_eval(build_graph_fn, train_dir, eval_dir, num_batches) else: events_rnn_train.run_training(build_graph_fn, train_dir, FLAGS.num_training_steps, FLAGS.summary_frequency, checkpoints_to_keep=FLAGS.num_checkpoints)
def main(unused_argv): if not FLAGS.sequence_example_file: tf.logging.fatal('--sequence_example_file required') return if not FLAGS.record_ids: tf.logging.fatal('--record_ids required') return needed_ids = [ int(rid.strip()) for rid in FLAGS.record_ids.split(',') ] sequence_example_file_paths = tf.gfile.Glob( os.path.expanduser(FLAGS.sequence_example_file)) config = melody_rnn_config_flags.config_from_flags() records = find_records(needed_ids, config, sequence_example_file_paths) for id, labels in records.items(): melody = magenta.music.Melody(events=labels, steps_per_quarter=4) config.encoder_decoder.decode_labels(melody, labels) seq = melody.to_sequence(qpm=FLAGS.qpm) magenta.music.sequence_proto_to_midi_file(seq, 'record-%d.mid' % id)
def make_generator(bundle_name): model_path = os.path.join(os.path.dirname(__file__), "../models/" + bundle_name + ".mag") hparams_path = os.path.join(os.path.dirname(__file__), "../models/" + bundle_name + ".hparams") if tf.gfile.Exists(hparams_path): with tf.gfile.Open(hparams_path) as f: config, hparams = f.readline().split("\t") melody_rnn_config_flags.FLAGS.config = config melody_rnn_config_flags.FLAGS.hparams = hparams config = melody_rnn_config_flags.config_from_flags() elif bundle_name in melody_rnn_model.default_configs: config = melody_rnn_model.default_configs[bundle_name] else: raise Exception("can not define the model config.") generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=STEPS_PER_QUARTER, bundle=magenta.music.read_bundle_file(model_path)) return generator
def call_melody_rnn(primer_melody): flist = tf.app.flags.FLAGS._flags() klist = [] for i in flist: klist.append(i) for k in klist: tf.app.flags.FLAGS.__delattr__(k) FLAGS = tf.app.flags.FLAGS tf.app.flags.DEFINE_string( 'run_dir', None, 'Path to the directory where the latest checkpoint will be loaded from.' ) tf.app.flags.DEFINE_string( 'checkpoint_file', None, 'Path to the checkpoint file. run_dir will take priority over this flag.' ) tf.app.flags.DEFINE_string( 'bundle_file', "/Users/yuhaomao/Downloads/lookback_rnn.mag", 'Path to the bundle file. If specified, this will take priority over ' 'run_dir and checkpoint_file, unless save_generator_bundle is True, in ' 'which case both this flag and either run_dir or checkpoint_file are ' 'required') tf.app.flags.DEFINE_boolean( 'save_generator_bundle', False, 'If true, instead of generating a sequence, will save this generator as a ' 'bundle file in the location specified by the bundle_file flag') tf.app.flags.DEFINE_string( 'bundle_description', None, 'A short, human-readable text description of the bundle (e.g., training ' 'data, hyper parameters, etc.).') tf.app.flags.DEFINE_string( 'output_dir', '/tmp/melody_rnn/generated', 'The directory where MIDI files will be saved to.') tf.app.flags.DEFINE_integer( 'num_outputs', 1, 'The number of melodies to generate. One MIDI file will be created for ' 'each.') tf.app.flags.DEFINE_integer( 'num_steps', 16, 'The total number of steps the generated melodies should be, priming ' 'melody length + generated steps. Each step is a 16th of a bar.') tf.app.flags.DEFINE_string( 'primer_melody', primer_melody, 'A string representation of a Python list of ' 'magenta.music.Melody event values. For example: ' '"[60, -2, 60, -2, 67, -2, 67, -2]". If specified, this melody will be ' 'used as the priming melody. If a priming melody is not specified, ' 'melodies will be generated from scratch.') tf.app.flags.DEFINE_string( 'primer_midi', '', 'The path to a MIDI file containing a melody that will be used as a ' 'priming melody. If a primer melody is not specified, melodies will be ' 'generated from scratch.') tf.app.flags.DEFINE_float( 'qpm', 60, 'The quarters per minute to play generated output at. If a primer MIDI is ' 'given, the qpm from that will override this flag. If qpm is None, qpm ' 'will default to 120.') tf.app.flags.DEFINE_float( 'temperature', 1.0, 'The randomness of the generated melodies. 1.0 uses the unaltered softmax ' 'probabilities, greater than 1.0 makes melodies more random, less than 1.0 ' 'makes melodies less random.') tf.app.flags.DEFINE_integer( 'beam_size', 1, 'The beam size to use for beam search when generating melodies.') tf.app.flags.DEFINE_integer( 'branch_factor', 1, 'The branch factor to use for beam search when generating melodies.') tf.app.flags.DEFINE_integer( 'steps_per_iteration', 1, 'The number of melody steps to take per beam search iteration.') tf.app.flags.DEFINE_string( 'log', 'INFO', 'The threshold for what messages will be logged DEBUG, INFO, WARN, ERROR, ' 'or FATAL.') tf.app.flags.DEFINE_string( 'hparams', "", 'Hyperparameter overrides, ' 'represented as a string containing comma-separated ' 'hparam_name=value pairs.') tf.logging.set_verbosity(FLAGS.log) bundle = get_bundle() if bundle: config_id = bundle.generator_details.id config = melody_rnn_model.default_configs[config_id] config.hparams.parse(FLAGS.hparams) else: config = melody_rnn_config_flags.config_from_flags() # save = Saver() # basemodel = BaseModel() # session = Session() # basemodel._build_graph_for_generation() # basemodel.initialize_with_checkpoint(checkpoint_file="/Users/yuhaomao/Downloads/lookback_rnn.mag") # eventsequencernnmodel = EventSequenceRnnModel(config) generator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=config.steps_per_quarter, checkpoint=get_checkpoint(), bundle=bundle) primer_midi = None if FLAGS.primer_midi: primer_midi = os.path.expanduser(FLAGS.primer_midi) primer_sequence = None qpm = FLAGS.qpm if FLAGS.qpm else magenta.music.DEFAULT_QUARTERS_PER_MINUTE if FLAGS.primer_melody: primer_melody = magenta.music.Melody( ast.literal_eval(FLAGS.primer_melody)) primer_sequence = primer_melody.to_sequence(qpm=qpm) elif primer_midi: primer_sequence = magenta.music.midi_file_to_sequence_proto( primer_midi) if primer_sequence.tempos and primer_sequence.tempos[0].qpm: qpm = primer_sequence.tempos[0].qpm else: tf.logging.warning( 'No priming sequence specified. Defaulting to a single middle C.') primer_melody = magenta.music.Melody([60]) primer_sequence = primer_melody.to_sequence(qpm=qpm) seconds_per_step = 60.0 / qpm / generator.steps_per_quarter total_seconds = FLAGS.num_steps * seconds_per_step generator_options = generator_pb2.GeneratorOptions() if primer_sequence: input_sequence = primer_sequence # Set the start time to begin on the next step after the last note ends. if primer_sequence.notes: last_end_time = max(n.end_time for n in primer_sequence.notes) else: last_end_time = 0 generate_section = generator_options.generate_sections.add( start_time=last_end_time + seconds_per_step, end_time=total_seconds) if generate_section.start_time >= generate_section.end_time: tf.logging.fatal( 'Priming sequence is longer than the total number of steps ' 'requested: Priming sequence length: %s, Generation length ' 'requested: %s', generate_section.start_time, total_seconds) return else: input_sequence = music_pb2.NoteSequence() input_sequence.tempos.add().qpm = qpm generate_section = generator_options.generate_sections.add( start_time=0, end_time=total_seconds) generator_options.args['temperature'].float_value = FLAGS.temperature generator_options.args['beam_size'].int_value = FLAGS.beam_size generator_options.args['branch_factor'].int_value = FLAGS.branch_factor generator_options.args[ 'steps_per_iteration'].int_value = FLAGS.steps_per_iteration tf.logging.debug('input_sequence: %s', input_sequence) tf.logging.debug('generator_options: %s', generator_options) # basesequencegenerator = BaseSequenceGenerator( # model=melody_rnn_model.MelodyRnnModel(config), # details=config.details, # checkpoint=get_checkpoint(), # bundle=bundle # ) melodyrnnsequencegenerator = melody_rnn_sequence_generator.MelodyRnnSequenceGenerator( model=melody_rnn_model.MelodyRnnModel(config), details=config.details, steps_per_quarter=config.steps_per_quarter, checkpoint=get_checkpoint(), bundle=bundle) melodyrnnsequencegenerator._generate(input_sequence, generator_options)