Skip to content

Commit fe46128

Browse files
committed
Project import generated by Copybara.
PiperOrigin-RevId: 248030617
1 parent 0dbc0a7 commit fe46128

File tree

93 files changed

+16922
-57
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+16922
-57
lines changed

CONTRIBUTING.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# How to Contribute
2+
3+
Disclaimer: T2R is not an official Google product. External support not
4+
guaranteed, and Robotics @ Google infra needs are prioritized over those of
5+
third-party. The purpose of developing this codebase in the open is to provide
6+
an avenue for open-sourcing our team's code. If you have questions about the
7+
codebase, or would like to submit a pull request, file a GitHub issue first.
8+
9+
# Issues
10+
11+
* Please tag your issue with `bug`, `feature request`, or `question` to help us
12+
effectively respond.
13+
* Please include the versions of TensorFlow and Tensor2Robot you are running.
14+
* Please provide the command line you ran as well as the log output.
15+
16+
# Pull Requests
17+
18+
We'd love to accept your patches and contributions to this project, provided
19+
that they are aligned with our internal uses of this codebase. There are
20+
just a few guidelines you need to follow.
21+
22+
## Contributor License Agreement
23+
24+
Contributions to this project must be accompanied by a Contributor License
25+
Agreement. You (or your employer) retain the copyright to your contribution,
26+
this simply gives us permission to use and redistribute your contributions as
27+
part of the project. Head over to <https://cla.developers.google.com/> to see
28+
your current agreements on file or to sign a new one.
29+
30+
You generally only need to submit a CLA once, so if you've already submitted one
31+
(even if it was for a different project), you probably don't need to do it
32+
again.
33+
34+
## Code reviews
35+
36+
All submissions, including submissions by project members, require review. We
37+
use GitHub pull requests for this purpose. Consult
38+
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
39+
information on using pull requests.

README.md

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ computer vision communities.
1616

1717
## Features
1818

19-
Tensor2Robot is a library for training, evaluation, and inference of large-scale
20-
deep neural networks, tailored specifically for neural networks relating to
21-
robotic perception and control. It is based on the [TensorFlow](tensorflow.org)
22-
deep learning framework.
19+
Tensor2Robot (T2R) is a library for training, evaluation, and inference of
20+
large-scale deep neural networks, tailored specifically for neural networks
21+
relating to robotic perception and control. It is based on the
22+
[TensorFlow](tensorflow.org) deep learning framework.
2323

2424
A common task in robotics research involves adding a new sensor modality or new
2525
label tensor to a neural network graph. This involves 1) changing what data is
@@ -40,67 +40,69 @@ labels of the model. Example preprocessors can be found in
4040

4141
## Design Decisions
4242

43-
- Scalability: This codebase is designed for training large-scale, real-world
44-
robotic perception models with algorithms that do not require a tight
45-
perception-action-learning loop (supervised learning, off-policy reinforcement
46-
learning of large computer vision models). An example setup might involve
47-
multiple GPUs pulling data asynchronously from a replay buffer and training
48-
with an off-policy RL algorithm, while data collection agents periodically
49-
update their checkpoints and push experiences to the same replay buffer.
43+
- Scalability: This codebase is designed for training large-scale, real-world
44+
robotic perception models with algorithms that do not require a tight
45+
perception-action-learning loop (supervised learning, off-policy reinforcement
46+
learning of large computer vision models). An example setup might involve
47+
multiple GPUs pulling data asynchronously from a replay buffer and training
48+
with an off-policy RL algorithm, while data collection agents periodically
49+
update their checkpoints and push experiences to the same replay buffer.
5050
This can also be run on a single workstation for smaller-scale experiments.
5151

52-
- Due to the sizes of models we work with (e.g. grasping from vision) Inference
53-
is assumed to be within 1-10Hz and without real-time guarantees. If you are
54-
doing reinforcement learning with small networks (e.g. two-layer perceptron)
55-
with on-policy RL (e.g. PPO), or require hard real-time guarantees, this is
56-
probably not the right codebase to use. We recommend using
57-
[TF-Agents](https://github.com/tensorflow/agents) or
52+
- T2R is *NOT* a general-purpose reinforcement learning library. Due to the
53+
sizes of models we work with (e.g. grasping from vision) Inference is assumed
54+
to be within 1-10Hz and without real-time guarantees, and training is assumed
55+
to be distributed by default. If you are doing reinforcement learning with small
56+
networks (e.g. two-layer perceptron) with on-policy RL (e.g. PPO), or require
57+
hard real-time guarantees, this is probably not the right codebase to use. We
58+
recommend using
59+
[TF-Agents](https://github.com/tensorflow/agents) or
5860
[Dopamine](https://github.com/google/dopamine) for those use cases.
5961

60-
- Minimize boilerplate: Tensor2Robot Models auto-generate their own data input
61-
pipelines and provide sensible defaults for optimizers, common architectures
62-
(actors, critics), and train/eval scaffolding. Models automatically work with
63-
both GPUs and TPUs (via `TPUEstimator`), parsing bmp/gif/jpeg/png-encoded
62+
- Minimize boilerplate: Tensor2Robot Models auto-generate their own data input
63+
pipelines and provide sensible defaults for optimizers, common architectures
64+
(actors, critics), and train/eval scaffolding. Models automatically work with
65+
both GPUs and TPUs (via `TPUEstimator`), parsing bmp/gif/jpeg/png-encoded
6466
images.
6567

66-
- gin-configurable: [Gin-Config](https://github.com/google/gin-config) is used
68+
- gin-configurable: [Gin-Config](https://github.com/google/gin-config) is used
6769
to configure models, policies, and other experiment hyperparameters.
6870

6971
## Quickstart
7072

73+
Requirements: Python 3.
74+
7175
```
7276
git clone https://github.com/google/tensor2robot
73-
cd tensor2robot
74-
pip install -r requirements.txt
75-
# Collect some data with a random policy.
76-
# TODO(b/128694019) - finish example.
77-
# Train a model.
78-
# TODO(b/128694019) - finish example.
79-
# Evaluate a policy that uses the model.
80-
# TODO(b/128694019) - finish example.
77+
# Optional: Create a virtualenv
78+
python3 -m venv ~/venv
79+
source ~/venv/bin/activate
80+
pip install -r tensor2robot/requirements.txt
81+
python -m tensor2robot.research.pose_env.pose_env_test
82+
python -m tensor2robot.research.pose_env.pose_env_models_test
8183
```
8284

8385

84-
## TFModel
86+
## T2RModel
8587

86-
To use Tensor2Robot, a user defines a `TFModel` object that define their input
87-
requirements by specifications - one for their features (feature_spec) and one
88+
To use Tensor2Robot, a user defines a `T2RModel` object that define their input
89+
requirements by specifications - one for their features (feature_spec) and one
8890
for their labels (label_spec):
8991

90-
These specifications define all required and optional tensors in order to call
91-
the model_fn. An input pipeline parameterized with the model's input pipeline
92+
These specifications define all required and optional tensors in order to call
93+
the model_fn. An input pipeline parameterized with the model's input pipeline
9294
will ensure that all required specifications are fulfilled. **Note**: we always
9395
omit the batch dimension and only specify the shape of a single element.
9496

95-
At training time, the TFModel provides `model_train_fn` or `model_eval_fn` as
96-
the `model_fn` argument `tf.estimator.Estimator` class. Both `model_train_fn`
97-
and `model_eval_fn` are defined with respect to the features, labels, and
98-
outputs of `inference_network_fn`, which presumably implements the shared
97+
At training time, the T2RModel provides `model_train_fn` or `model_eval_fn` as
98+
the `model_fn` argument `tf.estimator.Estimator` class. Both `model_train_fn`
99+
and `model_eval_fn` are defined with respect to the features, labels, and
100+
outputs of `inference_network_fn`, which presumably implements the shared
99101
portions of the train/eval graphs.
100102

101103

102104
```bash
103-
class MyModel(TFModel):
105+
class MyModel(T2RModel):
104106
def get_feature_specification(self, mode):
105107
spec = tensorspec_utils.TensorSpecStruct()
106108
spec['state'] = ExtendedTensorSpec(
@@ -140,28 +142,28 @@ numpy_feed_dict. We ensure that the name is unique within the whole spec, unless
140142
the specs match, otherwise we cannot guarantee the mapping functionality.
141143

142144

143-
### Benefits of Inheriting a TFModel
145+
### Benefits of Inheriting a T2RModel
144146

145147
- Self-contained input specifications for features and labels.
146148
- Auto-generated `tf.data.Dataset` pipelines for
147149
`tf.train.Examples` and `tf.train.SequenceExamples`.
148-
- For policy inference, TFModels can generate placeholders or export
150+
- For policy inference, T2RModels can generate placeholders or export
149151
`SavedModel`s that are hermetic and can be used with `ExportSavedModelPolicy`.
150152
- Automatic construction of `model_fn` for Estimator for training and evaluation
151-
graphs that share a single `inference_network_fn`.
152-
- It is possible to compose multiple models' `inference_network_fn` and
153-
`model_train_fn` together under a single model. This abstraction allows us to
153+
graphs that share a single `inference_network_fn`.
154+
- It is possible to compose multiple models' `inference_network_fn` and
155+
`model_train_fn` together under a single model. This abstraction allows us to
154156
implement generic Meta-Learning models (e.g. MAML) that call their sub-model's
155157
`model_train_fn`.
156158
- Automatic support for distributed training on GPUs and TPUs.
157159

158160

159161
### Policies and Placeholders
160162

161-
For performance reasons, policy inference is done by a vanilla `session.run()`
162-
or a `predict_fn` call on the output of a model, instead of Estimator.predict.
163-
`tensorspec_utils.make_placeholders` automatically creates placeholders from a
164-
spec structure which can be used in combination with a matching hierarchy of
163+
For performance reasons, policy inference is done by a vanilla `session.run()`
164+
or a `predict_fn` call on the output of a model, instead of Estimator.predict.
165+
`tensorspec_utils.make_placeholders` automatically creates placeholders from a
166+
spec structure which can be used in combination with a matching hierarchy of
165167
numpy inputs to create a feed_dict.
166168

167169
```bash
@@ -174,6 +176,12 @@ feed_dict = inference_model.MakeFeedDict(placeholders, numpy_inputs)
174176
# This can be passed to a sess.run function to evaluate the model.
175177
```
176178

179+
If you use `TFExportedSavedModelPolicy`, note that your T2RModel should not query
180+
the static batch shape (`x.shape[0]`) in the graph. This is because placeholder
181+
generation creates inputs with unknown batch shape, causing static shape
182+
retrieval to fail. Instead, use `tf.shape(x)[0]` to access batch shapes
183+
dynamically.
184+
177185
## Working with Tensor Specifications
178186

179187
**Specifications** can be a hierarchical data structure of either
@@ -285,19 +293,19 @@ for key, value in simple_spec.items():
285293
286294
### Sequential Inputs
287295
288-
Tensor2Robot can parse both `tf.train.Example` and `tf.train.SequenceExample`
289-
protos (useful for training recurrent models like LSTMs). To declare a model
296+
Tensor2Robot can parse both `tf.train.Example` and `tf.train.SequenceExample`
297+
protos (useful for training recurrent models like LSTMs). To declare a model
290298
whose data is parsed from SequenceExamples, set `is_sequence=True`.
291299
292300
```bash
293301
spec['state'] = ExtendedTensorSpec(
294302
shape=(8,128), dtype=tf.float32, name='s', is_sequence=True)
295303
```
296304
297-
This will result in a parsed tensor of shape `(b, ?, 8, 128)` where b is the
298-
batch size and the second dimension is the unknown sequence length (only known
299-
at run-time). Note that if `is_sequence=True` for any ExtendedTensorSpec in the
300-
TensorSpecStruct, the proto will be assumed to be a SequenceExample (and
305+
This will result in a parsed tensor of shape `(b, ?, 8, 128)` where b is the
306+
batch size and the second dimension is the unknown sequence length (only known
307+
at run-time). Note that if `is_sequence=True` for any ExtendedTensorSpec in the
308+
TensorSpecStruct, the proto will be assumed to be a SequenceExample (and
301309
non-sequential Tensors will be assumed to reside in example.context).
302310
303311
@@ -391,5 +399,5 @@ hierarchical structure.
391399
392400
## Disclaimer
393401
394-
This is not an official Google product. External support not guaranteed.
395-
Please file a GitHub issue before working on a pull request.
402+
This is not an official Google product. External support not guaranteed. The API
403+
may change subject to Alphabet needs. File a GitHub issue if you have questions.

__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# coding=utf-8
2+
# Copyright 2019 The Tensor2Robot Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+

bin/run_collect_eval.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# coding=utf-8
2+
# Copyright 2019 The Tensor2Robot Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Runs data collection and policy evaluation for RL experiments.
17+
"""
18+
19+
from __future__ import absolute_import
20+
from __future__ import division
21+
from __future__ import print_function
22+
23+
from absl import app
24+
from absl import flags
25+
import gin
26+
from tensor2robot import continuous_collect_eval
27+
# Contains the gin_configs and gin_bindings flag definitions.
28+
FLAGS = flags.FLAGS
29+
30+
try:
31+
flags.DEFINE_list(
32+
'gin_configs', None,
33+
'A comma-separated list of paths to Gin configuration files.')
34+
flags.DEFINE_multi_string(
35+
'gin_bindings', [], 'A newline separated list of Gin parameter bindings.')
36+
except flags.DuplicateFlagError:
37+
pass
38+
39+
flags.DEFINE_string('root_dir', '',
40+
'Root directory of experiment.')
41+
flags.mark_flag_as_required('gin_configs')
42+
43+
44+
def main(unused_argv):
45+
del unused_argv
46+
gin.parse_config_files_and_bindings(FLAGS.gin_configs, FLAGS.gin_bindings)
47+
continuous_collect_eval.collect_eval_loop(root_dir=FLAGS.root_dir)
48+
49+
50+
if __name__ == '__main__':
51+
app.run(main)

bin/run_t2r_trainer.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# coding=utf-8
2+
# Copyright 2019 The Tensor2Robot Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Binary for training TFModels with Estimator API."""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
from absl import app
23+
from absl import flags
24+
import gin
25+
from tensor2robot import train_eval
26+
import tensorflow as tf
27+
28+
29+
flags.DEFINE_multi_string(
30+
'dummy_flag', [], 'Dummy flag required until b/112361734 is resolved.')
31+
FLAGS = flags.FLAGS
32+
33+
34+
def main(unused_argv):
35+
gin.parse_config_files_and_bindings(FLAGS.gin_configs, FLAGS.gin_bindings)
36+
train_eval.train_eval_model()
37+
38+
39+
if __name__ == '__main__':
40+
tf.logging.set_verbosity(tf.logging.INFO)
41+
app.run(main)

0 commit comments

Comments
 (0)