2017-07-17 01:58:54 -07:00
|
|
|
from ray.rllib.models.model import Model
|
2019-07-27 02:08:16 -07:00
|
|
|
from ray.rllib.models.tf.misc import get_activation_fn, flatten
|
2018-12-08 16:28:58 -08:00
|
|
|
from ray.rllib.utils.annotations import override
|
2019-05-10 20:36:18 -07:00
|
|
|
from ray.rllib.utils import try_import_tf
|
|
|
|
|
|
|
|
tf = try_import_tf()
|
2017-07-17 01:58:54 -07:00
|
|
|
|
|
|
|
|
2019-08-23 02:21:11 -04:00
|
|
|
# Deprecated: see as an alternative models/tf/visionnet_v2.py
|
2017-07-17 01:58:54 -07:00
|
|
|
class VisionNetwork(Model):
|
|
|
|
"""Generic vision network."""
|
|
|
|
|
2018-12-08 16:28:58 -08:00
|
|
|
@override(Model)
|
2018-10-20 15:21:22 -07:00
|
|
|
def _build_layers_v2(self, input_dict, num_outputs, options):
|
|
|
|
inputs = input_dict["obs"]
|
2018-06-27 22:51:04 -07:00
|
|
|
filters = options.get("conv_filters")
|
|
|
|
if not filters:
|
2019-01-03 13:48:33 +08:00
|
|
|
filters = _get_filter_config(inputs.shape.as_list()[1:])
|
2018-06-27 22:51:04 -07:00
|
|
|
|
2018-10-16 15:55:11 -07:00
|
|
|
activation = get_activation_fn(options.get("conv_activation"))
|
2018-06-27 22:51:04 -07:00
|
|
|
|
2017-07-17 01:58:54 -07:00
|
|
|
with tf.name_scope("vision_net"):
|
2017-09-02 17:20:56 -07:00
|
|
|
for i, (out_size, kernel, stride) in enumerate(filters[:-1], 1):
|
2019-05-16 22:12:07 -07:00
|
|
|
inputs = tf.layers.conv2d(
|
2018-07-19 15:30:36 -07:00
|
|
|
inputs,
|
|
|
|
out_size,
|
|
|
|
kernel,
|
|
|
|
stride,
|
2019-05-16 22:12:07 -07:00
|
|
|
activation=activation,
|
|
|
|
padding="same",
|
|
|
|
name="conv{}".format(i))
|
2017-09-02 17:20:56 -07:00
|
|
|
out_size, kernel, stride = filters[-1]
|
2019-07-03 15:59:47 -07:00
|
|
|
|
|
|
|
# skip final linear layer
|
|
|
|
if options.get("no_final_linear"):
|
|
|
|
fc_out = tf.layers.conv2d(
|
|
|
|
inputs,
|
|
|
|
num_outputs,
|
|
|
|
kernel,
|
|
|
|
stride,
|
|
|
|
activation=activation,
|
|
|
|
padding="valid",
|
|
|
|
name="fc_out")
|
|
|
|
return flatten(fc_out), flatten(fc_out)
|
|
|
|
|
2019-05-16 22:12:07 -07:00
|
|
|
fc1 = tf.layers.conv2d(
|
2018-07-19 15:30:36 -07:00
|
|
|
inputs,
|
|
|
|
out_size,
|
|
|
|
kernel,
|
|
|
|
stride,
|
2019-05-16 22:12:07 -07:00
|
|
|
activation=activation,
|
|
|
|
padding="valid",
|
|
|
|
name="fc1")
|
|
|
|
fc2 = tf.layers.conv2d(
|
2018-07-19 15:30:36 -07:00
|
|
|
fc1,
|
|
|
|
num_outputs, [1, 1],
|
2019-05-16 22:12:07 -07:00
|
|
|
activation=None,
|
|
|
|
padding="same",
|
|
|
|
name="fc2")
|
2018-06-27 22:51:04 -07:00
|
|
|
return flatten(fc2), flatten(fc1)
|
|
|
|
|
|
|
|
|
2019-01-03 13:48:33 +08:00
|
|
|
def _get_filter_config(shape):
|
|
|
|
shape = list(shape)
|
2018-08-20 15:28:03 -07:00
|
|
|
filters_84x84 = [
|
2018-06-27 22:51:04 -07:00
|
|
|
[16, [8, 8], 4],
|
|
|
|
[32, [4, 4], 2],
|
2018-08-20 15:28:03 -07:00
|
|
|
[256, [11, 11], 1],
|
2018-06-27 22:51:04 -07:00
|
|
|
]
|
|
|
|
filters_42x42 = [
|
|
|
|
[16, [4, 4], 2],
|
|
|
|
[32, [4, 4], 2],
|
2018-08-20 15:28:03 -07:00
|
|
|
[256, [11, 11], 1],
|
2018-06-27 22:51:04 -07:00
|
|
|
]
|
2018-12-03 01:24:36 -08:00
|
|
|
if len(shape) == 3 and shape[:2] == [84, 84]:
|
2018-08-20 15:28:03 -07:00
|
|
|
return filters_84x84
|
2018-12-03 01:24:36 -08:00
|
|
|
elif len(shape) == 3 and shape[:2] == [42, 42]:
|
2018-06-27 22:51:04 -07:00
|
|
|
return filters_42x42
|
|
|
|
else:
|
|
|
|
raise ValueError(
|
2019-01-03 13:48:33 +08:00
|
|
|
"No default configuration for obs shape {}".format(shape) +
|
2018-12-03 01:24:36 -08:00
|
|
|
", you must specify `conv_filters` manually as a model option. "
|
2019-01-03 13:48:33 +08:00
|
|
|
"Default configurations are only available for inputs of shape "
|
|
|
|
"[42, 42, K] and [84, 84, K]. You may alternatively want "
|
2018-12-03 01:24:36 -08:00
|
|
|
"to use a custom model or preprocessor.")
|