# Checkpointer and PolicySaver

<br />

##### Copyright 2023 The TF-Agents Authors.

|---|---|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) View on TensorFlow.org](https://www.tensorflow.org/agents/tutorials/10_checkpointer_policysaver_tutorial) | [![](https://www.tensorflow.org/images/colab_logo_32px.png) Run in Google Colab](https://colab.research.google.com/github/tensorflow/agents/blob/master/docs/tutorials/10_checkpointer_policysaver_tutorial.ipynb) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/agents/blob/master/docs/tutorials/10_checkpointer_policysaver_tutorial.ipynb) | [![](https://www.tensorflow.org/images/download_logo_32px.png)Download notebook](https://storage.googleapis.com/tensorflow_docs/agents/docs/tutorials/10_checkpointer_policysaver_tutorial.ipynb) |

## Introduction

[`tf_agents.utils.common.Checkpointer`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/utils/common/Checkpointer) is a utility to save/load the training state, policy state, and replay_buffer state to/from a local storage.

[`tf_agents.policies.policy_saver.PolicySaver`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/policies/PolicySaver) is a tool to save/load only the policy, and is lighter than `Checkpointer`. You can use `PolicySaver` to deploy the model as well without any knowledge of the code that created the policy.

In this tutorial, we will use DQN to train a model, then use `Checkpointer` and `PolicySaver` to show how we can store and load the states and model in an interactive way. Note that we will use TF2.0's new saved_model tooling and format for `PolicySaver`.

## Setup

If you haven't installed the following dependencies, run:

    sudo apt-get update
    sudo apt-get install -y xvfb ffmpeg python-opengl
    pip install pyglet
    pip install 'imageio==2.4.0'
    pip install 'xvfbwrapper==0.2.9'
    pip install tf-agents[reverb]
    pip install tf-keras

    import os
    # Keep using keras-2 (tf-keras) rather than keras-3 (keras).
    os.environ['TF_USE_LEGACY_KERAS'] = '1'

    from __future__ import absolute_import
    from __future__ import division
    from __future__ import print_function

    import base64
    import imageio
    import io
    import matplotlib
    import matplotlib.pyplot as plt
    import os
    import shutil
    import tempfile
    import tensorflow as tf
    import zipfile
    import IPython

    try:
      from google.colab import files
    except ImportError:
      files = None
    from tf_agents.agents.dqn import dqn_agent
    from tf_agents.drivers import dynamic_step_driver
    from tf_agents.environments import suite_gym
    from tf_agents.environments import tf_py_environment
    from tf_agents.eval import metric_utils
    from tf_agents.metrics import tf_metrics
    from tf_agents.networks import q_network
    from tf_agents.policies import policy_saver
    from tf_agents.policies import py_tf_eager_policy
    from tf_agents.policies import random_tf_policy
    from tf_agents.replay_buffers import tf_uniform_replay_buffer
    from tf_agents.trajectories import trajectory
    from tf_agents.utils import common

    tempdir = os.getenv("TEST_TMPDIR", tempfile.gettempdir())

```
2023-12-22 12:17:45.769390: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
2023-12-22 12:17:45.769434: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
2023-12-22 12:17:45.771084: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
```

    # Set up a virtual display for rendering OpenAI gym environments.
    import xvfbwrapper
    xvfbwrapper.Xvfb(1400, 900, 24).start()

## DQN agent

We are going to set up DQN agent, just like in the previous colab. The details are hidden by default as they are not core part of this colab, but you can click on 'SHOW CODE' to see the details.

### Hyperparameters

    env_name = "CartPole-v1"

    collect_steps_per_iteration = 100
    replay_buffer_capacity = 100000

    fc_layer_params = (100,)

    batch_size = 64
    learning_rate = 1e-3
    log_interval = 5

    num_eval_episodes = 10
    eval_interval = 1000

### Environment

    train_py_env = suite_gym.load(env_name)
    eval_py_env = suite_gym.load(env_name)

    train_env = tf_py_environment.TFPyEnvironment(train_py_env)
    eval_env = tf_py_environment.TFPyEnvironment(eval_py_env)

### Agent

<br />

<button type="button" class="button-red button expand-control">Toggle code</button>

<br />

    q_net = q_network.QNetwork(
        train_env.observation_spec(),
        train_env.action_spec(),
        fc_layer_params=fc_layer_params)

    optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate)

    global_step = tf.compat.v1.train.get_or_create_global_step()

    agent = dqn_agent.DqnAgent(
        train_env.time_step_spec(),
        train_env.action_spec(),
        q_network=q_net,
        optimizer=optimizer,
        td_errors_loss_fn=common.element_wise_squared_loss,
        train_step_counter=global_step)
    agent.initialize()

<br />

<br />

### Data Collection

<br />

<button type="button" class="button-red button expand-control">Toggle code</button>

<br />

    replay_buffer = tf_uniform_replay_buffer.TFUniformReplayBuffer(
        data_spec=agent.collect_data_spec,
        batch_size=train_env.batch_size,
        max_length=replay_buffer_capacity)

    collect_driver = dynamic_step_driver.DynamicStepDriver(
        train_env,
        agent.collect_policy,
        observers=[replay_buffer.add_batch],
        num_steps=collect_steps_per_iteration)

    # Initial data collection
    collect_driver.run()

    # Dataset generates trajectories with shape [BxTx...] where
    # T = n_step_update + 1.
    dataset = replay_buffer.as_dataset(
        num_parallel_calls=3, sample_batch_size=batch_size,
        num_steps=2).prefetch(3)

    iterator = iter(dataset)

<br />

<br />

```
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/autograph/impl/api.py:377: ReplayBuffer.get_next (from tf_agents.replay_buffers.replay_buffer) is deprecated and will be removed in a future version.
Instructions for updating:
Use `as_dataset(..., single_deterministic_pass=False) instead.
```

### Train the agent

<br />

<button type="button" class="button-red button expand-control">Toggle code</button>

<br />

    # (Optional) Optimize by wrapping some of the code in a graph using TF function.
    agent.train = common.function(agent.train)

    def train_one_iteration():

      # Collect a few steps using collect_policy and save to the replay buffer.
      collect_driver.run()

      # Sample a batch of data from the buffer and update the agent's network.
      experience, unused_info = next(iterator)
      train_loss = agent.train(experience)

      iteration = agent.train_step_counter.numpy()
      print ('iteration: {0} loss: {1}'.format(iteration, train_loss.loss))

<br />

<br />

### Video Generation

<br />

<button type="button" class="button-red button expand-control">Toggle code</button>

<br />

    def embed_gif(gif_buffer):
      """Embeds a gif file in the notebook."""
      tag = '<img src="data:image/gif;base64,{0}"/>'.format(base64.b64encode(gif_buffer).decode())
      return IPython.display.HTML(tag)

    def run_episodes_and_create_video(policy, eval_tf_env, eval_py_env):
      num_episodes = 3
      frames = []
      for _ in range(num_episodes):
        time_step = eval_tf_env.reset()
        frames.append(eval_py_env.render())
        while not time_step.is_last():
          action_step = policy.action(time_step)
          time_step = eval_tf_env.step(action_step.action)
          frames.append(eval_py_env.render())
      gif_file = io.BytesIO()
      imageio.mimsave(gif_file, frames, format='gif', fps=60)
      IPython.display.display(embed_gif(gif_file.getvalue()))

<br />

<br />

### Generate a video

Check the performance of the policy by generating a video.

    print ('global_step:')
    print (global_step)
    run_episodes_and_create_video(agent.policy, eval_env, eval_py_env)

```
global_step:
<tf.Variable 'global_step:0' shape=() dtype=int64, numpy=0>
```

![gif](https://www.tensorflow.org/static/agents/tutorials/10_checkpointer_policysaver_tutorial_files/output_FpmPLXWbdG70_1.gif)

## Setup Checkpointer and PolicySaver

Now we are ready to use Checkpointer and PolicySaver.

### Checkpointer

    checkpoint_dir = os.path.join(tempdir, 'checkpoint')
    train_checkpointer = common.Checkpointer(
        ckpt_dir=checkpoint_dir,
        max_to_keep=1,
        agent=agent,
        policy=agent.policy,
        replay_buffer=replay_buffer,
        global_step=global_step
    )

### Policy Saver

    policy_dir = os.path.join(tempdir, 'policy')
    tf_policy_saver = policy_saver.PolicySaver(agent.policy)

### Train one iteration

    print('Training one iteration....')
    train_one_iteration()

```
Training one iteration....
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/util/dispatch.py:1260: calling foldr_v2 (from tensorflow.python.ops.functional_ops) with back_prop=False is deprecated and will be removed in a future version.
Instructions for updating:
back_prop=False is deprecated. Consider using tf.stop_gradient instead.
Instead of:
results = tf.foldr(fn, elems, back_prop=False)
Use:
results = tf.nest.map_structure(tf.stop_gradient, tf.foldr(fn, elems))
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/util/dispatch.py:1260: calling foldr_v2 (from tensorflow.python.ops.functional_ops) with back_prop=False is deprecated and will be removed in a future version.
Instructions for updating:
back_prop=False is deprecated. Consider using tf.stop_gradient instead.
Instead of:
results = tf.foldr(fn, elems, back_prop=False)
Use:
results = tf.nest.map_structure(tf.stop_gradient, tf.foldr(fn, elems))
iteration: 1 loss: 0.936349093914032
```

### Save to checkpoint

    train_checkpointer.save(global_step)

### Restore checkpoint

For this to work, the whole set of objects should be recreated the same way as when the checkpoint was created.

    train_checkpointer.initialize_or_restore()
    global_step = tf.compat.v1.train.get_global_step()

Also save policy and export to a location

    tf_policy_saver.save(policy_dir)

```
WARNING:absl:`0/step_type` is not a valid tf.function parameter name. Sanitizing to `arg_0_step_type`.
WARNING:absl:`0/reward` is not a valid tf.function parameter name. Sanitizing to `arg_0_reward`.
WARNING:absl:`0/discount` is not a valid tf.function parameter name. Sanitizing to `arg_0_discount`.
WARNING:absl:`0/observation` is not a valid tf.function parameter name. Sanitizing to `arg_0_observation`.
WARNING:absl:`0/step_type` is not a valid tf.function parameter name. Sanitizing to `arg_0_step_type`.
INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/saved_model/nested_structure_coder.py:458: UserWarning: Encoding a StructuredValue with type tfp.distributions.Deterministic_ACTTypeSpec; loading this StructuredValue will require that this type be imported and registered.
  warnings.warn("Encoding a StructuredValue with type %s; loading this "
INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets
```

The policy can be loaded without having any knowledge of what agent or network was used to create it. This makes deployment of the policy much easier.

Load the saved policy and check how it performs

    saved_policy = tf.saved_model.load(policy_dir)
    run_episodes_and_create_video(saved_policy, eval_env, eval_py_env)

![gif](https://www.tensorflow.org/static/agents/tutorials/10_checkpointer_policysaver_tutorial_files/output_J6T5KLTMh9ZB_0.gif)

## Export and import

The rest of the colab will help you export / import checkpointer and policy directories such that you can continue training at a later point and deploy the model without having to train again.

Now you can go back to 'Train one iteration' and train a few more times such that you can understand the difference later on. Once you start to see slightly better results, continue below.

### Create zip file and upload zip file (double-click to see the code)

<br />

<button type="button" class="button-red button expand-control">Toggle code</button>

<br />

    def create_zip_file(dirname, base_filename):
      return shutil.make_archive(base_filename, 'zip', dirname)

    def upload_and_unzip_file_to(dirname):
      if files is None:
        return
      uploaded = files.upload()
      for fn in uploaded.keys():
        print('User uploaded file "{name}" with length {length} bytes'.format(
            name=fn, length=len(uploaded[fn])))
        shutil.rmtree(dirname)
        zip_files = zipfile.ZipFile(io.BytesIO(uploaded[fn]), 'r')
        zip_files.extractall(dirname)
        zip_files.close()

<br />

<br />

Create a zipped file from the checkpoint directory.

    train_checkpointer.save(global_step)
    checkpoint_zip_filename = create_zip_file(checkpoint_dir, os.path.join(tempdir, 'exported_cp'))

Download the zip file.

    if files is not None:
      files.download(checkpoint_zip_filename) # try again if this fails: https://github.com/googlecolab/colabtools/issues/469

After training for some time (10-15 times), download the checkpoint zip file,
and go to "Runtime \> Restart and run all" to reset the training,
and come back to this cell. Now you can upload the downloaded zip file,
and continue the training.

    upload_and_unzip_file_to(checkpoint_dir)
    train_checkpointer.initialize_or_restore()
    global_step = tf.compat.v1.train.get_global_step()

Once you have uploaded checkpoint directory, go back to 'Train one iteration' to continue training or go back to 'Generate a video' to check the performance of the loaded policy.

Alternatively, you can save the policy (model) and restore it.
Unlike checkpointer, you cannot continue with the training, but you can still deploy the model. Note that the downloaded file is much smaller than that of the checkpointer.

    tf_policy_saver.save(policy_dir)
    policy_zip_filename = create_zip_file(policy_dir, os.path.join(tempdir, 'exported_policy'))

```
INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets
/tmpfs/src/tf_docs_env/lib/python3.9/site-packages/tensorflow/python/saved_model/nested_structure_coder.py:458: UserWarning: Encoding a StructuredValue with type tfp.distributions.Deterministic_ACTTypeSpec; loading this StructuredValue will require that this type be imported and registered.
  warnings.warn("Encoding a StructuredValue with type %s; loading this "
INFO:tensorflow:Assets written to: /tmpfs/tmp/policy/assets
```

    if files is not None:
      files.download(policy_zip_filename) # try again if this fails: https://github.com/googlecolab/colabtools/issues/469

Upload the downloaded policy directory (exported_policy.zip) and check how the saved policy performs.

    upload_and_unzip_file_to(policy_dir)
    saved_policy = tf.saved_model.load(policy_dir)
    run_episodes_and_create_video(saved_policy, eval_env, eval_py_env)

![gif](https://www.tensorflow.org/static/agents/tutorials/10_checkpointer_policysaver_tutorial_files/output_bgWLimRlXy5z_0.gif)

## SavedModelPyTFEagerPolicy

If you don't want to use TF policy, then you can also use the saved_model directly with the Python env through the use of [`py_tf_eager_policy.SavedModelPyTFEagerPolicy`](https://www.tensorflow.org/agents/api_docs/python/tf_agents/policies/SavedModelPyTFEagerPolicy).

Note that this only works when eager mode is enabled.

    eager_py_policy = py_tf_eager_policy.SavedModelPyTFEagerPolicy(
        policy_dir, eval_py_env.time_step_spec(), eval_py_env.action_spec())

    # Note that we're passing eval_py_env not eval_env.
    run_episodes_and_create_video(eager_py_policy, eval_py_env, eval_py_env)

![gif](https://www.tensorflow.org/static/agents/tutorials/10_checkpointer_policysaver_tutorial_files/output_iUC5XuLf1jF7_0.gif)

## Convert policy to TFLite

See [TensorFlow Lite converter](https://www.tensorflow.org/lite/convert) for more details.

    converter = tf.lite.TFLiteConverter.from_saved_model(policy_dir, signature_keys=["action"])
    tflite_policy = converter.convert()
    with open(os.path.join(tempdir, 'policy.tflite'), 'wb') as f:
      f.write(tflite_policy)

```
2023-12-22 12:18:02.611792: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378] Ignored output_format.
2023-12-22 12:18:02.611828: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381] Ignored drop_control_dependency.
Summary on the non-converted ops:
---

 * Accepted dialects: tfl, builtin, func
 * Non-Converted Ops: 11, Total Ops 28, % non-converted = 39.29 %
 * 11 ARITH ops

- arith.constant:   11 occurrences  (i64: 2, f32: 4, i32: 5)



  (i64: 1)
  (i32: 1)
  (i64: 1)
  (i32: 1)
  (f32: 2)
  (i64: 1)
  (i64: 1)
  (i64: 1, f32: 1)
  (i32: 2)
  (i32: 2)
```

### Run inference on TFLite model

See [TensorFlow Lite Inference](https://tensorflow.org/lite/guide/inference) for more details.

    import numpy as np
    interpreter = tf.lite.Interpreter(os.path.join(tempdir, 'policy.tflite'))

    policy_runner = interpreter.get_signature_runner()
    print(policy_runner._inputs)

```
{'0/discount': 1, '0/observation': 2, '0/reward': 3, '0/step_type': 0}
```

    policy_runner(**{
        '0/discount':tf.constant(0.0),
        '0/observation':tf.zeros([1,4]),
        '0/reward':tf.constant(0.0),
        '0/step_type':tf.constant(0)})

```
{'action': array([1])}
```