We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
issue training / generating with polyphony_rnn
Hello,

After (apparently successfully) learning an polyphony_rnn model, I cannot generate any sequence.
If I try to generate from the checkpoint, it returns the following:

> INFO:tensorflow:Checkpoint used: ./2017_05_04//run1/train/model.ckpt-3122
INFO:tensorflow:Restoring parameters from ./2017_05_04//run1/train/model.ckpt-3122
2017-05-04 18:18:18.663347: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key rnn/multi_rnn_cell/cell_2/basic_lstm_cell/weights not found in checkpoint
2017-05-04 18:18:18.666763: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key rnn/multi_rnn_cell/cell_2/basic_lstm_cell/biases not found in checkpoint

It is followed by a long traceback call message (that I'll provide if it helps).

Unsurprisingly I also cannot generate a bundle file from my checkpoint, for the same reason.

Any suggestion?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `magenta/models/polyphony_rnn/polyphony_rnn_generate.py`
Content:
```
1 # Copyright 2016 Google Inc. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Generate polyphonic tracks from a trained checkpoint.
15 
16 Uses flags to define operation.
17 """
18 
19 import ast
20 import os
21 import time
22 
23 # internal imports
24 
25 import tensorflow as tf
26 import magenta
27 
28 from magenta.models.polyphony_rnn import polyphony_model
29 from magenta.models.polyphony_rnn import polyphony_sequence_generator
30 
31 from magenta.music import constants
32 from magenta.protobuf import generator_pb2
33 from magenta.protobuf import music_pb2
34 
35 FLAGS = tf.app.flags.FLAGS
36 tf.app.flags.DEFINE_string(
37     'run_dir', None,
38     'Path to the directory where the latest checkpoint will be loaded from.')
39 tf.app.flags.DEFINE_string(
40     'bundle_file', None,
41     'Path to the bundle file. If specified, this will take priority over '
42     'run_dir, unless save_generator_bundle is True, in which case both this '
43     'flag and run_dir are required')
44 tf.app.flags.DEFINE_boolean(
45     'save_generator_bundle', False,
46     'If true, instead of generating a sequence, will save this generator as a '
47     'bundle file in the location specified by the bundle_file flag')
48 tf.app.flags.DEFINE_string(
49     'bundle_description', None,
50     'A short, human-readable text description of the bundle (e.g., training '
51     'data, hyper parameters, etc.).')
52 tf.app.flags.DEFINE_string(
53     'config', 'polyphony', 'Config to use.')
54 tf.app.flags.DEFINE_string(
55     'output_dir', '/tmp/polyphony_rnn/generated',
56     'The directory where MIDI files will be saved to.')
57 tf.app.flags.DEFINE_integer(
58     'num_outputs', 10,
59     'The number of tracks to generate. One MIDI file will be created for '
60     'each.')
61 tf.app.flags.DEFINE_integer(
62     'num_steps', 128,
63     'The total number of steps the generated track should be, priming '
64     'track length + generated steps. Each step is a 16th of a bar.')
65 tf.app.flags.DEFINE_string(
66     'primer_pitches', '',
67     'A string representation of a Python list of pitches that will be used as '
68     'a starting chord with a quarter note duration. For example: '
69     '"[60, 64, 67]"')
70 tf.app.flags.DEFINE_string(
71     'primer_melody', '',
72     'A string representation of a Python list of '
73     'magenta.music.Melody event values. For example: '
74     '"[60, -2, 60, -2, 67, -2, 67, -2]".')
75 tf.app.flags.DEFINE_string(
76     'primer_midi', '',
77     'The path to a MIDI file containing a polyphonic track that will be used '
78     'as a priming track.')
79 tf.app.flags.DEFINE_boolean(
80     'condition_on_primer', False,
81     'If set, the RNN will receive the primer as its input before it begins '
82     'generating a new sequence.')
83 tf.app.flags.DEFINE_boolean(
84     'inject_primer_during_generation', True,
85     'If set, the primer will be injected as a part of the generated sequence. '
86     'This option is useful if you want the model to harmonize an existing '
87     'melody.')
88 tf.app.flags.DEFINE_float(
89     'qpm', None,
90     'The quarters per minute to play generated output at. If a primer MIDI is '
91     'given, the qpm from that will override this flag. If qpm is None, qpm '
92     'will default to 120.')
93 tf.app.flags.DEFINE_float(
94     'temperature', 1.0,
95     'The randomness of the generated tracks. 1.0 uses the unaltered '
96     'softmax probabilities, greater than 1.0 makes tracks more random, less '
97     'than 1.0 makes tracks less random.')
98 tf.app.flags.DEFINE_integer(
99     'beam_size', 1,
100     'The beam size to use for beam search when generating tracks.')
101 tf.app.flags.DEFINE_integer(
102     'branch_factor', 1,
103     'The branch factor to use for beam search when generating tracks.')
104 tf.app.flags.DEFINE_integer(
105     'steps_per_iteration', 1,
106     'The number of steps to take per beam search iteration.')
107 tf.app.flags.DEFINE_string(
108     'log', 'INFO',
109     'The threshold for what messages will be logged DEBUG, INFO, WARN, ERROR, '
110     'or FATAL.')
111 
112 
113 def get_checkpoint():
114   """Get the training dir or checkpoint path to be used by the model."""
115   if FLAGS.run_dir and FLAGS.bundle_file and not FLAGS.save_generator_bundle:
116     raise magenta.music.SequenceGeneratorException(
117         'Cannot specify both bundle_file and run_dir')
118   if FLAGS.run_dir:
119     train_dir = os.path.join(os.path.expanduser(FLAGS.run_dir), 'train')
120     return train_dir
121   else:
122     return None
123 
124 
125 def get_bundle():
126   """Returns a generator_pb2.GeneratorBundle object based read from bundle_file.
127 
128   Returns:
129     Either a generator_pb2.GeneratorBundle or None if the bundle_file flag is
130     not set or the save_generator_bundle flag is set.
131   """
132   if FLAGS.save_generator_bundle:
133     return None
134   if FLAGS.bundle_file is None:
135     return None
136   bundle_file = os.path.expanduser(FLAGS.bundle_file)
137   return magenta.music.read_bundle_file(bundle_file)
138 
139 
140 def run_with_flags(generator):
141   """Generates polyphonic tracks and saves them as MIDI files.
142 
143   Uses the options specified by the flags defined in this module.
144 
145   Args:
146     generator: The PolyphonyRnnSequenceGenerator to use for generation.
147   """
148   if not FLAGS.output_dir:
149     tf.logging.fatal('--output_dir required')
150     return
151   output_dir = os.path.expanduser(FLAGS.output_dir)
152 
153   primer_midi = None
154   if FLAGS.primer_midi:
155     primer_midi = os.path.expanduser(FLAGS.primer_midi)
156 
157   if not tf.gfile.Exists(output_dir):
158     tf.gfile.MakeDirs(output_dir)
159 
160   primer_sequence = None
161   qpm = FLAGS.qpm if FLAGS.qpm else magenta.music.DEFAULT_QUARTERS_PER_MINUTE
162   if FLAGS.primer_pitches:
163     primer_sequence = music_pb2.NoteSequence()
164     primer_sequence.tempos.add().qpm = qpm
165     primer_sequence.ticks_per_quarter = constants.STANDARD_PPQ
166     for pitch in ast.literal_eval(FLAGS.primer_pitches):
167       note = primer_sequence.notes.add()
168       note.start_time = 0
169       note.end_time = 60.0 / qpm
170       note.pitch = pitch
171       note.velocity = 100
172     primer_sequence.total_time = primer_sequence.notes[-1].end_time
173   elif FLAGS.primer_melody:
174     primer_melody = magenta.music.Melody(ast.literal_eval(FLAGS.primer_melody))
175     primer_sequence = primer_melody.to_sequence(qpm=qpm)
176   elif primer_midi:
177     primer_sequence = magenta.music.midi_file_to_sequence_proto(primer_midi)
178     if primer_sequence.tempos and primer_sequence.tempos[0].qpm:
179       qpm = primer_sequence.tempos[0].qpm
180   else:
181     tf.logging.warning(
182         'No priming sequence specified. Defaulting to empty sequence.')
183     primer_sequence = music_pb2.NoteSequence()
184     primer_sequence.tempos.add().qpm = qpm
185     primer_sequence.ticks_per_quarter = constants.STANDARD_PPQ
186 
187   # Derive the total number of seconds to generate.
188   seconds_per_step = 60.0 / qpm / generator.steps_per_quarter
189   generate_end_time = FLAGS.num_steps * seconds_per_step
190 
191   # Specify start/stop time for generation based on starting generation at the
192   # end of the priming sequence and continuing until the sequence is num_steps
193   # long.
194   generator_options = generator_pb2.GeneratorOptions()
195   # Set the start time to begin when the last note ends.
196   generate_section = generator_options.generate_sections.add(
197       start_time=primer_sequence.total_time,
198       end_time=generate_end_time)
199 
200   if generate_section.start_time >= generate_section.end_time:
201     tf.logging.fatal(
202         'Priming sequence is longer than the total number of steps '
203         'requested: Priming sequence length: %s, Total length '
204         'requested: %s',
205         generate_section.start_time, generate_end_time)
206     return
207 
208   generator_options.args['temperature'].float_value = FLAGS.temperature
209   generator_options.args['beam_size'].int_value = FLAGS.beam_size
210   generator_options.args['branch_factor'].int_value = FLAGS.branch_factor
211   generator_options.args[
212       'steps_per_iteration'].int_value = FLAGS.steps_per_iteration
213 
214   generator_options.args['condition_on_primer'].bool_value = (
215       FLAGS.condition_on_primer)
216   generator_options.args['no_inject_primer_during_generation'].bool_value = (
217       not FLAGS.inject_primer_during_generation)
218 
219   tf.logging.debug('primer_sequence: %s', primer_sequence)
220   tf.logging.debug('generator_options: %s', generator_options)
221 
222   # Make the generate request num_outputs times and save the output as midi
223   # files.
224   date_and_time = time.strftime('%Y-%m-%d_%H%M%S')
225   digits = len(str(FLAGS.num_outputs))
226   for i in range(FLAGS.num_outputs):
227     generated_sequence = generator.generate(primer_sequence, generator_options)
228 
229     midi_filename = '%s_%s.mid' % (date_and_time, str(i + 1).zfill(digits))
230     midi_path = os.path.join(output_dir, midi_filename)
231     magenta.music.sequence_proto_to_midi_file(generated_sequence, midi_path)
232 
233   tf.logging.info('Wrote %d MIDI files to %s',
234                   FLAGS.num_outputs, output_dir)
235 
236 
237 def main(unused_argv):
238   """Saves bundle or runs generator based on flags."""
239   tf.logging.set_verbosity(FLAGS.log)
240 
241   config = polyphony_model.default_configs[FLAGS.config]
242 
243   generator = polyphony_sequence_generator.PolyphonyRnnSequenceGenerator(
244       model=polyphony_model.PolyphonyRnnModel(config),
245       details=config.details,
246       steps_per_quarter=config.steps_per_quarter,
247       checkpoint=get_checkpoint(),
248       bundle=get_bundle())
249 
250   if FLAGS.save_generator_bundle:
251     bundle_filename = os.path.expanduser(FLAGS.bundle_file)
252     if FLAGS.bundle_description is None:
253       tf.logging.warning('No bundle description provided.')
254     tf.logging.info('Saving generator bundle to %s', bundle_filename)
255     generator.create_bundle_file(bundle_filename, FLAGS.bundle_description)
256   else:
257     run_with_flags(generator)
258 
259 
260 def console_entry_point():
261   tf.app.run(main)
262 
263 
264 if __name__ == '__main__':
265   console_entry_point()
266
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/magenta/models/polyphony_rnn/polyphony_rnn_generate.py b/magenta/models/polyphony_rnn/polyphony_rnn_generate.py
--- a/magenta/models/polyphony_rnn/polyphony_rnn_generate.py
+++ b/magenta/models/polyphony_rnn/polyphony_rnn_generate.py
@@ -108,6 +108,11 @@
     'log', 'INFO',
     'The threshold for what messages will be logged DEBUG, INFO, WARN, ERROR, '
     'or FATAL.')
+tf.app.flags.DEFINE_string(
+    'hparams', '{}',
+    'String representation of a Python dictionary containing hyperparameter '
+    'to value mapping. This mapping is merged with the default '
+    'hyperparameters.')
 
 
 def get_checkpoint():
@@ -239,6 +244,7 @@
   tf.logging.set_verbosity(FLAGS.log)
 
   config = polyphony_model.default_configs[FLAGS.config]
+  config.hparams.parse(FLAGS.hparams)
 
   generator = polyphony_sequence_generator.PolyphonyRnnSequenceGenerator(
       model=polyphony_model.PolyphonyRnnModel(config),

{"golden_diff": "diff --git a/magenta/models/polyphony_rnn/polyphony_rnn_generate.py b/magenta/models/polyphony_rnn/polyphony_rnn_generate.py\n--- a/magenta/models/polyphony_rnn/polyphony_rnn_generate.py\n+++ b/magenta/models/polyphony_rnn/polyphony_rnn_generate.py\n@@ -108,6 +108,11 @@\n     'log', 'INFO',\n     'The threshold for what messages will be logged DEBUG, INFO, WARN, ERROR, '\n     'or FATAL.')\n+tf.app.flags.DEFINE_string(\n+    'hparams', '{}',\n+    'String representation of a Python dictionary containing hyperparameter '\n+    'to value mapping. This mapping is merged with the default '\n+    'hyperparameters.')\n \n \n def get_checkpoint():\n@@ -239,6 +244,7 @@\n   tf.logging.set_verbosity(FLAGS.log)\n \n   config = polyphony_model.default_configs[FLAGS.config]\n+  config.hparams.parse(FLAGS.hparams)\n \n   generator = polyphony_sequence_generator.PolyphonyRnnSequenceGenerator(\n       model=polyphony_model.PolyphonyRnnModel(config),\n", "issue": "issue training / generating with polyphony_rnn\nHello,\r\n\r\nAfter (apparently successfully) learning an polyphony_rnn model, I cannot generate any sequence.\r\nIf I try to generate from the checkpoint, it returns the following:\r\n\r\n> INFO:tensorflow:Checkpoint used: ./2017_05_04//run1/train/model.ckpt-3122\r\nINFO:tensorflow:Restoring parameters from ./2017_05_04//run1/train/model.ckpt-3122\r\n2017-05-04 18:18:18.663347: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key rnn/multi_rnn_cell/cell_2/basic_lstm_cell/weights not found in checkpoint\r\n2017-05-04 18:18:18.666763: W tensorflow/core/framework/op_kernel.cc:1152] Not found: Key rnn/multi_rnn_cell/cell_2/basic_lstm_cell/biases not found in checkpoint\r\n\r\nIt is followed by a long traceback call message (that I'll provide if it helps).\r\n\r\nUnsurprisingly I also cannot generate a bundle file from my checkpoint, for the same reason.\r\n\r\nAny suggestion?\r\n\n", "before_files": [{"content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Generate polyphonic tracks from a trained checkpoint.\n\nUses flags to define operation.\n\"\"\"\n\nimport ast\nimport os\nimport time\n\n# internal imports\n\nimport tensorflow as tf\nimport magenta\n\nfrom magenta.models.polyphony_rnn import polyphony_model\nfrom magenta.models.polyphony_rnn import polyphony_sequence_generator\n\nfrom magenta.music import constants\nfrom magenta.protobuf import generator_pb2\nfrom magenta.protobuf import music_pb2\n\nFLAGS = tf.app.flags.FLAGS\ntf.app.flags.DEFINE_string(\n    'run_dir', None,\n    'Path to the directory where the latest checkpoint will be loaded from.')\ntf.app.flags.DEFINE_string(\n    'bundle_file', None,\n    'Path to the bundle file. If specified, this will take priority over '\n    'run_dir, unless save_generator_bundle is True, in which case both this '\n    'flag and run_dir are required')\ntf.app.flags.DEFINE_boolean(\n    'save_generator_bundle', False,\n    'If true, instead of generating a sequence, will save this generator as a '\n    'bundle file in the location specified by the bundle_file flag')\ntf.app.flags.DEFINE_string(\n    'bundle_description', None,\n    'A short, human-readable text description of the bundle (e.g., training '\n    'data, hyper parameters, etc.).')\ntf.app.flags.DEFINE_string(\n    'config', 'polyphony', 'Config to use.')\ntf.app.flags.DEFINE_string(\n    'output_dir', '/tmp/polyphony_rnn/generated',\n    'The directory where MIDI files will be saved to.')\ntf.app.flags.DEFINE_integer(\n    'num_outputs', 10,\n    'The number of tracks to generate. One MIDI file will be created for '\n    'each.')\ntf.app.flags.DEFINE_integer(\n    'num_steps', 128,\n    'The total number of steps the generated track should be, priming '\n    'track length + generated steps. Each step is a 16th of a bar.')\ntf.app.flags.DEFINE_string(\n    'primer_pitches', '',\n    'A string representation of a Python list of pitches that will be used as '\n    'a starting chord with a quarter note duration. For example: '\n    '\"[60, 64, 67]\"')\ntf.app.flags.DEFINE_string(\n    'primer_melody', '',\n    'A string representation of a Python list of '\n    'magenta.music.Melody event values. For example: '\n    '\"[60, -2, 60, -2, 67, -2, 67, -2]\".')\ntf.app.flags.DEFINE_string(\n    'primer_midi', '',\n    'The path to a MIDI file containing a polyphonic track that will be used '\n    'as a priming track.')\ntf.app.flags.DEFINE_boolean(\n    'condition_on_primer', False,\n    'If set, the RNN will receive the primer as its input before it begins '\n    'generating a new sequence.')\ntf.app.flags.DEFINE_boolean(\n    'inject_primer_during_generation', True,\n    'If set, the primer will be injected as a part of the generated sequence. '\n    'This option is useful if you want the model to harmonize an existing '\n    'melody.')\ntf.app.flags.DEFINE_float(\n    'qpm', None,\n    'The quarters per minute to play generated output at. If a primer MIDI is '\n    'given, the qpm from that will override this flag. If qpm is None, qpm '\n    'will default to 120.')\ntf.app.flags.DEFINE_float(\n    'temperature', 1.0,\n    'The randomness of the generated tracks. 1.0 uses the unaltered '\n    'softmax probabilities, greater than 1.0 makes tracks more random, less '\n    'than 1.0 makes tracks less random.')\ntf.app.flags.DEFINE_integer(\n    'beam_size', 1,\n    'The beam size to use for beam search when generating tracks.')\ntf.app.flags.DEFINE_integer(\n    'branch_factor', 1,\n    'The branch factor to use for beam search when generating tracks.')\ntf.app.flags.DEFINE_integer(\n    'steps_per_iteration', 1,\n    'The number of steps to take per beam search iteration.')\ntf.app.flags.DEFINE_string(\n    'log', 'INFO',\n    'The threshold for what messages will be logged DEBUG, INFO, WARN, ERROR, '\n    'or FATAL.')\n\n\ndef get_checkpoint():\n  \"\"\"Get the training dir or checkpoint path to be used by the model.\"\"\"\n  if FLAGS.run_dir and FLAGS.bundle_file and not FLAGS.save_generator_bundle:\n    raise magenta.music.SequenceGeneratorException(\n        'Cannot specify both bundle_file and run_dir')\n  if FLAGS.run_dir:\n    train_dir = os.path.join(os.path.expanduser(FLAGS.run_dir), 'train')\n    return train_dir\n  else:\n    return None\n\n\ndef get_bundle():\n  \"\"\"Returns a generator_pb2.GeneratorBundle object based read from bundle_file.\n\n  Returns:\n    Either a generator_pb2.GeneratorBundle or None if the bundle_file flag is\n    not set or the save_generator_bundle flag is set.\n  \"\"\"\n  if FLAGS.save_generator_bundle:\n    return None\n  if FLAGS.bundle_file is None:\n    return None\n  bundle_file = os.path.expanduser(FLAGS.bundle_file)\n  return magenta.music.read_bundle_file(bundle_file)\n\n\ndef run_with_flags(generator):\n  \"\"\"Generates polyphonic tracks and saves them as MIDI files.\n\n  Uses the options specified by the flags defined in this module.\n\n  Args:\n    generator: The PolyphonyRnnSequenceGenerator to use for generation.\n  \"\"\"\n  if not FLAGS.output_dir:\n    tf.logging.fatal('--output_dir required')\n    return\n  output_dir = os.path.expanduser(FLAGS.output_dir)\n\n  primer_midi = None\n  if FLAGS.primer_midi:\n    primer_midi = os.path.expanduser(FLAGS.primer_midi)\n\n  if not tf.gfile.Exists(output_dir):\n    tf.gfile.MakeDirs(output_dir)\n\n  primer_sequence = None\n  qpm = FLAGS.qpm if FLAGS.qpm else magenta.music.DEFAULT_QUARTERS_PER_MINUTE\n  if FLAGS.primer_pitches:\n    primer_sequence = music_pb2.NoteSequence()\n    primer_sequence.tempos.add().qpm = qpm\n    primer_sequence.ticks_per_quarter = constants.STANDARD_PPQ\n    for pitch in ast.literal_eval(FLAGS.primer_pitches):\n      note = primer_sequence.notes.add()\n      note.start_time = 0\n      note.end_time = 60.0 / qpm\n      note.pitch = pitch\n      note.velocity = 100\n    primer_sequence.total_time = primer_sequence.notes[-1].end_time\n  elif FLAGS.primer_melody:\n    primer_melody = magenta.music.Melody(ast.literal_eval(FLAGS.primer_melody))\n    primer_sequence = primer_melody.to_sequence(qpm=qpm)\n  elif primer_midi:\n    primer_sequence = magenta.music.midi_file_to_sequence_proto(primer_midi)\n    if primer_sequence.tempos and primer_sequence.tempos[0].qpm:\n      qpm = primer_sequence.tempos[0].qpm\n  else:\n    tf.logging.warning(\n        'No priming sequence specified. Defaulting to empty sequence.')\n    primer_sequence = music_pb2.NoteSequence()\n    primer_sequence.tempos.add().qpm = qpm\n    primer_sequence.ticks_per_quarter = constants.STANDARD_PPQ\n\n  # Derive the total number of seconds to generate.\n  seconds_per_step = 60.0 / qpm / generator.steps_per_quarter\n  generate_end_time = FLAGS.num_steps * seconds_per_step\n\n  # Specify start/stop time for generation based on starting generation at the\n  # end of the priming sequence and continuing until the sequence is num_steps\n  # long.\n  generator_options = generator_pb2.GeneratorOptions()\n  # Set the start time to begin when the last note ends.\n  generate_section = generator_options.generate_sections.add(\n      start_time=primer_sequence.total_time,\n      end_time=generate_end_time)\n\n  if generate_section.start_time >= generate_section.end_time:\n    tf.logging.fatal(\n        'Priming sequence is longer than the total number of steps '\n        'requested: Priming sequence length: %s, Total length '\n        'requested: %s',\n        generate_section.start_time, generate_end_time)\n    return\n\n  generator_options.args['temperature'].float_value = FLAGS.temperature\n  generator_options.args['beam_size'].int_value = FLAGS.beam_size\n  generator_options.args['branch_factor'].int_value = FLAGS.branch_factor\n  generator_options.args[\n      'steps_per_iteration'].int_value = FLAGS.steps_per_iteration\n\n  generator_options.args['condition_on_primer'].bool_value = (\n      FLAGS.condition_on_primer)\n  generator_options.args['no_inject_primer_during_generation'].bool_value = (\n      not FLAGS.inject_primer_during_generation)\n\n  tf.logging.debug('primer_sequence: %s', primer_sequence)\n  tf.logging.debug('generator_options: %s', generator_options)\n\n  # Make the generate request num_outputs times and save the output as midi\n  # files.\n  date_and_time = time.strftime('%Y-%m-%d_%H%M%S')\n  digits = len(str(FLAGS.num_outputs))\n  for i in range(FLAGS.num_outputs):\n    generated_sequence = generator.generate(primer_sequence, generator_options)\n\n    midi_filename = '%s_%s.mid' % (date_and_time, str(i + 1).zfill(digits))\n    midi_path = os.path.join(output_dir, midi_filename)\n    magenta.music.sequence_proto_to_midi_file(generated_sequence, midi_path)\n\n  tf.logging.info('Wrote %d MIDI files to %s',\n                  FLAGS.num_outputs, output_dir)\n\n\ndef main(unused_argv):\n  \"\"\"Saves bundle or runs generator based on flags.\"\"\"\n  tf.logging.set_verbosity(FLAGS.log)\n\n  config = polyphony_model.default_configs[FLAGS.config]\n\n  generator = polyphony_sequence_generator.PolyphonyRnnSequenceGenerator(\n      model=polyphony_model.PolyphonyRnnModel(config),\n      details=config.details,\n      steps_per_quarter=config.steps_per_quarter,\n      checkpoint=get_checkpoint(),\n      bundle=get_bundle())\n\n  if FLAGS.save_generator_bundle:\n    bundle_filename = os.path.expanduser(FLAGS.bundle_file)\n    if FLAGS.bundle_description is None:\n      tf.logging.warning('No bundle description provided.')\n    tf.logging.info('Saving generator bundle to %s', bundle_filename)\n    generator.create_bundle_file(bundle_filename, FLAGS.bundle_description)\n  else:\n    run_with_flags(generator)\n\n\ndef console_entry_point():\n  tf.app.run(main)\n\n\nif __name__ == '__main__':\n  console_entry_point()\n", "path": "magenta/models/polyphony_rnn/polyphony_rnn_generate.py"}], "after_files": [{"content": "# Copyright 2016 Google Inc. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\"\"\"Generate polyphonic tracks from a trained checkpoint.\n\nUses flags to define operation.\n\"\"\"\n\nimport ast\nimport os\nimport time\n\n# internal imports\n\nimport tensorflow as tf\nimport magenta\n\nfrom magenta.models.polyphony_rnn import polyphony_model\nfrom magenta.models.polyphony_rnn import polyphony_sequence_generator\n\nfrom magenta.music import constants\nfrom magenta.protobuf import generator_pb2\nfrom magenta.protobuf import music_pb2\n\nFLAGS = tf.app.flags.FLAGS\ntf.app.flags.DEFINE_string(\n    'run_dir', None,\n    'Path to the directory where the latest checkpoint will be loaded from.')\ntf.app.flags.DEFINE_string(\n    'bundle_file', None,\n    'Path to the bundle file. If specified, this will take priority over '\n    'run_dir, unless save_generator_bundle is True, in which case both this '\n    'flag and run_dir are required')\ntf.app.flags.DEFINE_boolean(\n    'save_generator_bundle', False,\n    'If true, instead of generating a sequence, will save this generator as a '\n    'bundle file in the location specified by the bundle_file flag')\ntf.app.flags.DEFINE_string(\n    'bundle_description', None,\n    'A short, human-readable text description of the bundle (e.g., training '\n    'data, hyper parameters, etc.).')\ntf.app.flags.DEFINE_string(\n    'config', 'polyphony', 'Config to use.')\ntf.app.flags.DEFINE_string(\n    'output_dir', '/tmp/polyphony_rnn/generated',\n    'The directory where MIDI files will be saved to.')\ntf.app.flags.DEFINE_integer(\n    'num_outputs', 10,\n    'The number of tracks to generate. One MIDI file will be created for '\n    'each.')\ntf.app.flags.DEFINE_integer(\n    'num_steps', 128,\n    'The total number of steps the generated track should be, priming '\n    'track length + generated steps. Each step is a 16th of a bar.')\ntf.app.flags.DEFINE_string(\n    'primer_pitches', '',\n    'A string representation of a Python list of pitches that will be used as '\n    'a starting chord with a quarter note duration. For example: '\n    '\"[60, 64, 67]\"')\ntf.app.flags.DEFINE_string(\n    'primer_melody', '',\n    'A string representation of a Python list of '\n    'magenta.music.Melody event values. For example: '\n    '\"[60, -2, 60, -2, 67, -2, 67, -2]\".')\ntf.app.flags.DEFINE_string(\n    'primer_midi', '',\n    'The path to a MIDI file containing a polyphonic track that will be used '\n    'as a priming track.')\ntf.app.flags.DEFINE_boolean(\n    'condition_on_primer', False,\n    'If set, the RNN will receive the primer as its input before it begins '\n    'generating a new sequence.')\ntf.app.flags.DEFINE_boolean(\n    'inject_primer_during_generation', True,\n    'If set, the primer will be injected as a part of the generated sequence. '\n    'This option is useful if you want the model to harmonize an existing '\n    'melody.')\ntf.app.flags.DEFINE_float(\n    'qpm', None,\n    'The quarters per minute to play generated output at. If a primer MIDI is '\n    'given, the qpm from that will override this flag. If qpm is None, qpm '\n    'will default to 120.')\ntf.app.flags.DEFINE_float(\n    'temperature', 1.0,\n    'The randomness of the generated tracks. 1.0 uses the unaltered '\n    'softmax probabilities, greater than 1.0 makes tracks more random, less '\n    'than 1.0 makes tracks less random.')\ntf.app.flags.DEFINE_integer(\n    'beam_size', 1,\n    'The beam size to use for beam search when generating tracks.')\ntf.app.flags.DEFINE_integer(\n    'branch_factor', 1,\n    'The branch factor to use for beam search when generating tracks.')\ntf.app.flags.DEFINE_integer(\n    'steps_per_iteration', 1,\n    'The number of steps to take per beam search iteration.')\ntf.app.flags.DEFINE_string(\n    'log', 'INFO',\n    'The threshold for what messages will be logged DEBUG, INFO, WARN, ERROR, '\n    'or FATAL.')\ntf.app.flags.DEFINE_string(\n    'hparams', '{}',\n    'String representation of a Python dictionary containing hyperparameter '\n    'to value mapping. This mapping is merged with the default '\n    'hyperparameters.')\n\n\ndef get_checkpoint():\n  \"\"\"Get the training dir or checkpoint path to be used by the model.\"\"\"\n  if FLAGS.run_dir and FLAGS.bundle_file and not FLAGS.save_generator_bundle:\n    raise magenta.music.SequenceGeneratorException(\n        'Cannot specify both bundle_file and run_dir')\n  if FLAGS.run_dir:\n    train_dir = os.path.join(os.path.expanduser(FLAGS.run_dir), 'train')\n    return train_dir\n  else:\n    return None\n\n\ndef get_bundle():\n  \"\"\"Returns a generator_pb2.GeneratorBundle object based read from bundle_file.\n\n  Returns:\n    Either a generator_pb2.GeneratorBundle or None if the bundle_file flag is\n    not set or the save_generator_bundle flag is set.\n  \"\"\"\n  if FLAGS.save_generator_bundle:\n    return None\n  if FLAGS.bundle_file is None:\n    return None\n  bundle_file = os.path.expanduser(FLAGS.bundle_file)\n  return magenta.music.read_bundle_file(bundle_file)\n\n\ndef run_with_flags(generator):\n  \"\"\"Generates polyphonic tracks and saves them as MIDI files.\n\n  Uses the options specified by the flags defined in this module.\n\n  Args:\n    generator: The PolyphonyRnnSequenceGenerator to use for generation.\n  \"\"\"\n  if not FLAGS.output_dir:\n    tf.logging.fatal('--output_dir required')\n    return\n  output_dir = os.path.expanduser(FLAGS.output_dir)\n\n  primer_midi = None\n  if FLAGS.primer_midi:\n    primer_midi = os.path.expanduser(FLAGS.primer_midi)\n\n  if not tf.gfile.Exists(output_dir):\n    tf.gfile.MakeDirs(output_dir)\n\n  primer_sequence = None\n  qpm = FLAGS.qpm if FLAGS.qpm else magenta.music.DEFAULT_QUARTERS_PER_MINUTE\n  if FLAGS.primer_pitches:\n    primer_sequence = music_pb2.NoteSequence()\n    primer_sequence.tempos.add().qpm = qpm\n    primer_sequence.ticks_per_quarter = constants.STANDARD_PPQ\n    for pitch in ast.literal_eval(FLAGS.primer_pitches):\n      note = primer_sequence.notes.add()\n      note.start_time = 0\n      note.end_time = 60.0 / qpm\n      note.pitch = pitch\n      note.velocity = 100\n    primer_sequence.total_time = primer_sequence.notes[-1].end_time\n  elif FLAGS.primer_melody:\n    primer_melody = magenta.music.Melody(ast.literal_eval(FLAGS.primer_melody))\n    primer_sequence = primer_melody.to_sequence(qpm=qpm)\n  elif primer_midi:\n    primer_sequence = magenta.music.midi_file_to_sequence_proto(primer_midi)\n    if primer_sequence.tempos and primer_sequence.tempos[0].qpm:\n      qpm = primer_sequence.tempos[0].qpm\n  else:\n    tf.logging.warning(\n        'No priming sequence specified. Defaulting to empty sequence.')\n    primer_sequence = music_pb2.NoteSequence()\n    primer_sequence.tempos.add().qpm = qpm\n    primer_sequence.ticks_per_quarter = constants.STANDARD_PPQ\n\n  # Derive the total number of seconds to generate.\n  seconds_per_step = 60.0 / qpm / generator.steps_per_quarter\n  generate_end_time = FLAGS.num_steps * seconds_per_step\n\n  # Specify start/stop time for generation based on starting generation at the\n  # end of the priming sequence and continuing until the sequence is num_steps\n  # long.\n  generator_options = generator_pb2.GeneratorOptions()\n  # Set the start time to begin when the last note ends.\n  generate_section = generator_options.generate_sections.add(\n      start_time=primer_sequence.total_time,\n      end_time=generate_end_time)\n\n  if generate_section.start_time >= generate_section.end_time:\n    tf.logging.fatal(\n        'Priming sequence is longer than the total number of steps '\n        'requested: Priming sequence length: %s, Total length '\n        'requested: %s',\n        generate_section.start_time, generate_end_time)\n    return\n\n  generator_options.args['temperature'].float_value = FLAGS.temperature\n  generator_options.args['beam_size'].int_value = FLAGS.beam_size\n  generator_options.args['branch_factor'].int_value = FLAGS.branch_factor\n  generator_options.args[\n      'steps_per_iteration'].int_value = FLAGS.steps_per_iteration\n\n  generator_options.args['condition_on_primer'].bool_value = (\n      FLAGS.condition_on_primer)\n  generator_options.args['no_inject_primer_during_generation'].bool_value = (\n      not FLAGS.inject_primer_during_generation)\n\n  tf.logging.debug('primer_sequence: %s', primer_sequence)\n  tf.logging.debug('generator_options: %s', generator_options)\n\n  # Make the generate request num_outputs times and save the output as midi\n  # files.\n  date_and_time = time.strftime('%Y-%m-%d_%H%M%S')\n  digits = len(str(FLAGS.num_outputs))\n  for i in range(FLAGS.num_outputs):\n    generated_sequence = generator.generate(primer_sequence, generator_options)\n\n    midi_filename = '%s_%s.mid' % (date_and_time, str(i + 1).zfill(digits))\n    midi_path = os.path.join(output_dir, midi_filename)\n    magenta.music.sequence_proto_to_midi_file(generated_sequence, midi_path)\n\n  tf.logging.info('Wrote %d MIDI files to %s',\n                  FLAGS.num_outputs, output_dir)\n\n\ndef main(unused_argv):\n  \"\"\"Saves bundle or runs generator based on flags.\"\"\"\n  tf.logging.set_verbosity(FLAGS.log)\n\n  config = polyphony_model.default_configs[FLAGS.config]\n  config.hparams.parse(FLAGS.hparams)\n\n  generator = polyphony_sequence_generator.PolyphonyRnnSequenceGenerator(\n      model=polyphony_model.PolyphonyRnnModel(config),\n      details=config.details,\n      steps_per_quarter=config.steps_per_quarter,\n      checkpoint=get_checkpoint(),\n      bundle=get_bundle())\n\n  if FLAGS.save_generator_bundle:\n    bundle_filename = os.path.expanduser(FLAGS.bundle_file)\n    if FLAGS.bundle_description is None:\n      tf.logging.warning('No bundle description provided.')\n    tf.logging.info('Saving generator bundle to %s', bundle_filename)\n    generator.create_bundle_file(bundle_filename, FLAGS.bundle_description)\n  else:\n    run_with_flags(generator)\n\n\ndef console_entry_point():\n  tf.app.run(main)\n\n\nif __name__ == '__main__':\n  console_entry_point()\n", "path": "magenta/models/polyphony_rnn/polyphony_rnn_generate.py"}]}