Run MNIST training and eval loop in eager mode. Args: flags_obj: An object containing parsed flag values.
(flags_obj)
| 182 | |
| 183 | |
| 184 | def train_and_export(flags_obj): |
| 185 | """Run MNIST training and eval loop in eager mode. |
| 186 | |
| 187 | Args: |
| 188 | flags_obj: An object containing parsed flag values. |
| 189 | """ |
| 190 | # Load the datasets |
| 191 | train_ds, test_ds = mnist_datasets() |
| 192 | train_ds = train_ds.shuffle(60000).batch(flags_obj.batch_size) |
| 193 | test_ds = test_ds.batch(flags_obj.batch_size) |
| 194 | |
| 195 | # Create the model and optimizer |
| 196 | model = create_model() |
| 197 | optimizer = tf.train.MomentumOptimizer( |
| 198 | flags_obj.learning_rate, flags_obj.momentum) |
| 199 | |
| 200 | # See summaries with `tensorboard --logdir=<model_dir>` |
| 201 | train_dir = os.path.join(flags_obj.model_dir, 'summaries', 'train') |
| 202 | test_dir = os.path.join(flags_obj.model_dir, 'summaries', 'eval') |
| 203 | summary_writer = tf.contrib.summary.create_file_writer( |
| 204 | train_dir, flush_millis=10000) |
| 205 | test_summary_writer = tf.contrib.summary.create_file_writer( |
| 206 | test_dir, flush_millis=10000, name='test') |
| 207 | |
| 208 | # Create and restore checkpoint (if one exists on the path) |
| 209 | checkpoint_dir = os.path.join(flags_obj.model_dir, 'checkpoints') |
| 210 | checkpoint_prefix = os.path.join(checkpoint_dir, 'ckpt') |
| 211 | step_counter = tf.train.get_or_create_global_step() |
| 212 | checkpoint = tf.train.Checkpoint( |
| 213 | model=model, optimizer=optimizer, step_counter=step_counter) |
| 214 | # Restore variables on creation if a checkpoint exists. |
| 215 | checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir)) |
| 216 | |
| 217 | # Train and evaluate for a set number of epochs. |
| 218 | for _ in range(flags_obj.train_epochs): |
| 219 | start = time.time() |
| 220 | with summary_writer.as_default(): |
| 221 | train(model, optimizer, train_ds, step_counter, |
| 222 | flags_obj.log_interval, num_steps=1) |
| 223 | end = time.time() |
| 224 | print('\nTrain time for epoch #%d (%d total steps): %f' % |
| 225 | (checkpoint.save_counter.numpy() + 1, |
| 226 | step_counter.numpy(), |
| 227 | end - start)) |
| 228 | with test_summary_writer.as_default(): |
| 229 | test(model, test_ds) |
| 230 | checkpoint.save(checkpoint_prefix) |
| 231 | |
| 232 | # TODO(brianklee): Enable this functionality after @allenl implements this. |
| 233 | # export_path = os.path.join(flags_obj.model_dir, 'export') |
| 234 | # tf.saved_model.save(export_path, model) |
| 235 | |
| 236 | |
| 237 | def import_and_eval(flags_obj): |
no test coverage detected