(_)
| 263 | |
| 264 | |
| 265 | def main(_): |
| 266 | (device, data_format) = ('/gpu:0', 'channels_first') |
| 267 | if FLAGS.no_gpu or tf.contrib.eager.num_gpus() <= 0: |
| 268 | (device, data_format) = ('/cpu:0', 'channels_last') |
| 269 | print('Using device %s, and data format %s.' % (device, data_format)) |
| 270 | |
| 271 | # Load the datasets |
| 272 | data = input_data.read_data_sets(FLAGS.data_dir) |
| 273 | dataset = ( |
| 274 | tf.data.Dataset.from_tensor_slices(data.train.images).shuffle(60000) |
| 275 | .batch(FLAGS.batch_size)) |
| 276 | |
| 277 | # Create the models and optimizers. |
| 278 | model_objects = { |
| 279 | 'generator': Generator(data_format), |
| 280 | 'discriminator': Discriminator(data_format), |
| 281 | 'generator_optimizer': tf.compat.v1.train.AdamOptimizer(FLAGS.lr), |
| 282 | 'discriminator_optimizer': tf.compat.v1.train.AdamOptimizer(FLAGS.lr), |
| 283 | 'step_counter': tf.train.get_or_create_global_step(), |
| 284 | } |
| 285 | |
| 286 | # Prepare summary writer and checkpoint info |
| 287 | summary_writer = tf.contrib.summary.create_summary_file_writer( |
| 288 | FLAGS.output_dir, flush_millis=1000) |
| 289 | checkpoint_prefix = os.path.join(FLAGS.checkpoint_dir, 'ckpt') |
| 290 | latest_cpkt = tf.train.latest_checkpoint(FLAGS.checkpoint_dir) |
| 291 | if latest_cpkt: |
| 292 | print('Using latest checkpoint at ' + latest_cpkt) |
| 293 | checkpoint = tf.train.Checkpoint(**model_objects) |
| 294 | # Restore variables on creation if a checkpoint exists. |
| 295 | checkpoint.restore(latest_cpkt) |
| 296 | |
| 297 | with tf.device(device): |
| 298 | for _ in range(100): |
| 299 | start = time.time() |
| 300 | with summary_writer.as_default(): |
| 301 | train_one_epoch(dataset=dataset, log_interval=FLAGS.log_interval, |
| 302 | noise_dim=FLAGS.noise, **model_objects) |
| 303 | end = time.time() |
| 304 | checkpoint.save(checkpoint_prefix) |
| 305 | print('\nTrain time for epoch #%d (step %d): %f' % |
| 306 | (checkpoint.save_counter.numpy(), |
| 307 | checkpoint.step_counter.numpy(), |
| 308 | end - start)) |
| 309 | |
| 310 | |
| 311 | if __name__ == '__main__': |
nothing calls this directly
no test coverage detected