2017-07-17 01:58:54 -07:00
|
|
|
from ray.rllib.models.model import Model
|
2020-04-15 13:25:16 +02:00
|
|
|
from ray.rllib.models.tf.misc import flatten
|
2018-12-08 16:28:58 -08:00
|
|
|
from ray.rllib.utils.annotations import override
|
2020-04-29 12:12:59 +02:00
|
|
|
from ray.rllib.utils.deprecation import deprecation_warning
|
2020-04-15 13:25:16 +02:00
|
|
|
from ray.rllib.utils.framework import get_activation_fn, try_import_tf
|
2019-05-10 20:36:18 -07:00
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
tf1, tf, tfv = try_import_tf()
|
2017-07-17 01:58:54 -07:00
|
|
|
|
|
|
|
|
2020-05-18 17:26:40 +02:00
|
|
|
# Deprecated: see as an alternative models/tf.visionnet.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):
|
2020-04-29 12:12:59 +02:00
|
|
|
# Hard deprecate this class. All Models should use the ModelV2
|
|
|
|
# API from here on.
|
|
|
|
deprecation_warning(
|
|
|
|
"Model->VisionNetwork", "ModelV2->VisionNetwork", error=False)
|
2018-10-20 15:21:22 -07:00
|
|
|
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
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
with tf1.name_scope("vision_net"):
|
2017-09-02 17:20:56 -07:00
|
|
|
for i, (out_size, kernel, stride) in enumerate(filters[:-1], 1):
|
2020-06-30 10:13:20 +02:00
|
|
|
inputs = tf1.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"):
|
2020-06-30 10:13:20 +02:00
|
|
|
fc_out = tf1.layers.conv2d(
|
2019-07-03 15:59:47 -07:00
|
|
|
inputs,
|
|
|
|
num_outputs,
|
|
|
|
kernel,
|
|
|
|
stride,
|
|
|
|
activation=activation,
|
|
|
|
padding="valid",
|
|
|
|
name="fc_out")
|
|
|
|
return flatten(fc_out), flatten(fc_out)
|
|
|
|
|
2020-06-30 10:13:20 +02:00
|
|
|
fc1 = tf1.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")
|
2020-06-30 10:13:20 +02:00
|
|
|
fc2 = tf1.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.")
|