Giter VIP home page Giter VIP logo

envlogger's Introduction

EnvironmentLogger

EnvLogger is a standard dm_env.Environment class wrapper that records interactions between a real environment and an agent. These interactions are saved on disk as trajectories and can be retrieved in whole, by individual timesteps or by specific episodes.

drawing

Metadata

To better categorize your logged data, you may want to add some tags in the metadata when you construct the logger wrapper. The metadata is written once per EnvLogger instance.

env = envlogger.EnvLogger(
    env,
    data_directory='/tmp/experiment_logs',
    metadata={
        'environment_type': 'dm_control',
        'agent_type': 'D4PG'
    })

How to use Envlogger

Most of the time, it is just a one-liner wrapper, e.g.

import envlogger
from envlogger.testing import catch_env
import numpy as np

env = catch_env.Catch()
with envlogger.EnvLogger(
    env, data_directory='/tmp/experiment_logs') as env:

  env.reset()
  for step in range(100):
    action = np.random.randint(low=0, high=3)
    timestep = env.step(action)

Full example of random agent in Catch is available here: random_agent_catch.py

Step metadata

Envlogger also allows to record custom metadata per step by defining a function that can be passed to the wrapper. In this example, we want to record the timestamp of when each step was produced:

  def step_fn(unused_timestep, unused_action, unused_env):
    return {'timestamp': time.time()}

  ...

  with envlogger.Envlogger(
    env,
    data_directory='/tmp/experiment_logs',
    step_fn=step_fn) as env:

  ...

Episode metadata

Recording custom episode metadata is also possible by providing a callback. This callback is invoked at every step but only the last value returned that is not None (if any) is stored.

In the following example, we only store the timestamp of the first step of the episode.

  def episode_fn(timestep, unused_action, unused_env):
    if timestemp.first:
      return {'timestamp': time.time()}
    else:
      return None

  ...

  with envlogger.Envlogger(
    env,
    data_directory=FLAGS.trajectories_dir,
    episode_fn=episode_fn) as env:

  ...

TFDS backend

Envlogger supports writing data that is directly compatible with TFDS and RLDS.

For that, you need to indicate the specs of your environment in terms of TFDS Features using an RLDS DatasetConfig like in the example (note that the config can include step_metadata_infoand episode metadata_info).

  dataset_config = tfds.rlds.rlds_base.DatasetConfig(
      name='catch_example',
      observation_info=tfds.features.Tensor(
          shape=(10, 5), dtype=tf.float32,
          encoding=tfds.features.Encoding.ZLIB),
      action_info=tf.int64,
      reward_info=tf.float64,
      discount_info=tf.float64)

And then use the TFDS Backend when instantiating the Envlogger:

envlogger.EnvLogger(
      env,
      backend = tfds_backend_writer.TFDSBackendWriter(
        data_directory=FLAGS.trajectories_dir,
        split_name='train',
        max_episodes_per_file=500,
        ds_config=dataset_config),
  )

A full example is available here random_agent_catch.py

Note: If you are installing Envlogger via pip, remember to install the extra dependencies:

pip install envlogger[tfds]

Reading stored trajectories

Reader can read stored trajectories. Example:

from envlogger import reader

with reader.Reader(
    data_directory='/tmp/experiment_logs') as r:
  for episode in r.episodes:
    for step in episode:
       # step is a step_data.StepData.
       # Use step.timestep.observation, step.timestep.reward, step.action etc...

Reading the dataset with TFDS/RLDS

Datasets generated with Envlogger are compatible with RLDS.

If you used the TFDS backend, you can read your data directly with tfds.builder_from_directory. Check the RLDS website for colabs and tools to manipulate your datasets.

Otherwise, you can transform them into a TFDS compatible dataset (learn how in the RLDS documentation).

Getting Started

EnvLogger currently only supports Linux based OSes and Python 3.

You can install EnvLogger via pip:

pip install envlogger

If you want to use the TFDS backend, you need to install the package with extra dependencies:

pip install envlogger[tfds]
Compiling from source

For this option you will need to install Bazel and GMP (libgmp-dev on Debian-based systems). Please note that Bazel versions >4.0 are not supported. Our recommended version is 3.7.2. Then:

git clone https://github.com/deepmind/envlogger/
cd envlogger
bazel test --test_output=errors envlogger/...
Running inside Docker

We provide a Docker image that can be used to run tests in a more controlled environment. You can run it as follows:

sh docker/build.sh
docker run -it envlogger bash
bazel test --test_output=errors envlogger/...

Benchmarks

Wrapping your environment with EnvLogger adds an approximately 2 millisecond overhead per environment step. See the following difference in distributions in the case of random agent on a 100 steps per second Catch environment (measured in milliseconds).

Percentiles 50th 95th 99th 99.9th mean (± std)
w/o EnvLogger 10.15 10.23 11.51 14.70 10.19 (± 1.06)
w/ EnvLogger 12.65 14.39 15.94 19.43 12.88 (± 0.34)

Acknowledgements

We greatly appreciate all the support from the TF-Agents team in setting up building and testing for EnvLogger.

envlogger's People

Contributors

a-gergely avatar conchylicultor avatar gribozavr avatar kenjitoyama avatar laramiel avatar qrczakmk avatar rchen152 avatar rwgk avatar sabelaraga avatar sgirgin avatar tkoeppe avatar tomvdw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

envlogger's Issues

Python 3.11 Wheel

Would it be possible to get an updated Python 3.11 build of the envlogger wheels?

Fatal Python error: Aborted

Hello, I running into an error when playing with the py_writer.
I modified it to store 100000 trajectories, which it does apparently by creating a 100000 timestamp subfolders...
But it failed to read the data using the following code:

with reader.Reader(data_directory=_TRAJECTORIES_DIR.value) as r:
for i, episode in enumerate(r.episodes):
logging.info(f"-------------- {i} --------------------: {len(episode)}")

Any ideas? Is there a way to gather the stored files into the same subfolders as it seems to be the pb?
Thanx.

WARNING: Logging before InitGoogleLogging() is written to STDERR
F20211207 11:48:44.483004 104240 bundle.cc:24] Check failed: finished_ JoinAll() should be called before releasing the bundle.
*** Check failure stack trace: ***
Fatal Python error: Aborted

Current thread 0x00007fa729473740 (most recent call first):
File "/usr/local/lib/python3.9/dist-packages/envlogger/backends/riegeli_backend_reader.py", line 45 in init
File "/usr/local/lib/python3.9/dist-packages/envlogger/reader.py", line 45 in init
File "/envlogger/envlogger/backends/cross_language_test/py_writer.py", line 92 in main
File "/usr/local/lib/python3.9/dist-packages/absl/app.py", line 258 in _run_main
File "/usr/local/lib/python3.9/dist-packages/absl/app.py", line 312 in run
File "/envlogger/envlogger/backends/cross_language_test/py_writer.py", line 104 in
Aborted (core dumped)

importing acme and envlogger crashes

Hello,

Using a very minimal setup I have an error on import, I guess the name StatusCode is used by both programs in absl...

conda create -n test_envlogger python=3.7 pip
conda activate test_envlogger
pip install tensorflow gym dm-acme[tensorflow] envlogger
python -c "import acme; import envlogger"
Traceback (most recent call last):
  File "temp_test.py", line 2, in <module>
    import envlogger
  File "/home/vmallet/anaconda3/envs/test_3/lib/python3.7/site-packages/envlogger/__init__.py", line 18, in <module>
    from envlogger import environment_logger
  File "/home/vmallet/anaconda3/envs/test_3/lib/python3.7/site-packages/envlogger/environment_logger.py", line 32, in <module>
    from envlogger.backends import riegeli_backend_writer
  File "/home/vmallet/anaconda3/envs/test_3/lib/python3.7/site-packages/envlogger/backends/riegeli_backend_writer.py", line 24, in <module>
    from envlogger.backends.python import riegeli_dataset_writer
ImportError: generic_type: cannot initialize type "StatusCode": an object with that name is already defined

I hope you can help me !

Best,
Vincent

Python 3.10 Wheel

Hi!

Would it be possible to publish Python 3.10 wheels? Other DeepMind projects are already publishing for 3.10 and it would be really nice to have envlogger.

Thanks!

Including logger metadata in the TFDS backend.

Hi,

It looks like the TFDS backend does not support recording metadata from envlogger. It would be very useful for having this feature. I think there is a metadata key tfds DatasetConfig that we might be able to use for that.

I am happy to investigate this and create a PR.

Multiprocessed reader

Hello,
I'm trying to boost the reading using multiprocessing but I get an error a AttributeError: Can't pickle local object 'BackendReader._get_nth_episode..get_nth_step_from_episode'. Probably the object is not pickable for some reason.

Any chance you managed to do it on your side? Or would have other hints to boost the reading?

Thanx.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.