Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom network integration #7

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
467fc56
custom_policy_from_json for custom neural network
cliebig2019 Jul 21, 2022
6222bf1
Delete agents/burger_CUSTOM_UniformEncoder2022_07_21__11_44/burger di…
cliebig2019 Jul 21, 2022
9052482
Delete agents/burger_AGENT_20_UniformEncoder2022_07_21__11_50/burger …
cliebig2019 Jul 21, 2022
80d5391
Delete agents/burger_CUSTOM_UniformEncoder2022_07_21__11_49/burger di…
cliebig2019 Jul 21, 2022
f9e3c32
Delete hyperparameters.json.lock
cliebig2019 Jul 21, 2022
8e2524b
custom_policy_from_json
cliebig2019 Aug 2, 2022
9575c6d
reverting implementation in agent_factory
cliebig2019 Aug 2, 2022
cf3e646
added cnn and tanh to neural network modules
cliebig2019 Aug 2, 2022
8b0e3e6
outsourced from custom_policy_from_json to utils
cliebig2019 Aug 2, 2022
b46ab41
moved functions for better structure
cliebig2019 Aug 25, 2022
877eb72
Delete rosnav/rosnav_space_manager/__pycache__ directory
cliebig2019 Aug 25, 2022
b37c061
Delete rosnav/model/__pycache__ directory
cliebig2019 Aug 25, 2022
6be4ebb
Delete rosnav/utils/__pycache__ directory
cliebig2019 Aug 25, 2022
b749678
added comments
cliebig2019 Aug 25, 2022
c6f0894
reverted to original
cliebig2019 Aug 25, 2022
246c1d2
added newline at end
cliebig2019 Aug 25, 2022
4ee085e
Merge pull request #6 from cliebig2019/custom_network_reworked
ReykCS Aug 25, 2022
fa17ce6
implementing snake case
cliebig2019 Aug 25, 2022
72f78a8
corrected conv/tanh layers
cliebig2019 Aug 25, 2022
0b9bf5c
Change json format to read
cliebig2019 Oct 25, 2022
fbcd3c6
Merge pull request #10 from cliebig2019/custom_network_reworked
ReykCS Dec 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified agents/DRL_LOCAL_PLANNER_rule2(done)/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/DRL_LOCAL_PLANNER_rule3(done)/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/agvota/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/baseline_ppo_human/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/burger/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/cob4/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/jackal/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/jackal_barn/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/new_jackal/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/pretrained_ppo_human/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/pretrained_tb3/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/ridgeback/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/rto/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/rto_tlabs/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/rule_00/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/rule_01/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/rule_02/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/rule_03/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/rule_04/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/tiago/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified agents/youbot/hyperparameters.json.lock
100755 → 100644
Empty file.
Empty file modified rosnav/__init__.py
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion rosnav/model/agent_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def instantiate(cls, name: str, **kwargs) -> Union[Type[BaseAgent], Type[BasePol
"""
assert name in cls.registry, f"Agent '{name}' is not registered!"
agent_class = cls.registry[name]

if issubclass(agent_class, BaseAgent):
return agent_class(**kwargs)
else:
Expand Down
111 changes: 111 additions & 0 deletions rosnav/model/custom_policy_from_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import os
import rospy
from typing import Callable, Dict, List, Optional, Tuple, Type, Union

import gym
import rospkg
import torch as th
import yaml
import json

from torch import nn
from stable_baselines3.common.policies import ActorCriticPolicy


from .agent_factory import AgentFactory
from ..utils.utils import get_observation_space
from .custom_policy_utils.utils import read_json
from .custom_policy_utils.utils import create_body_network


__all__ = ["CUSTOM"]


"""
_RS: Robot state size - placeholder for robot related inputs to the NN
_L: Number of laser beams - placeholder for the laser beam data
"""
_L, _RS = get_observation_space()


class CUSTOM_NETWORK(nn.Module):
"""
Custom Multilayer Perceptron for policy and value function.

:param path: path to json file containing neural network
:param feature_dim: dimension of the features extracted with the features_extractor (e.g. features from a CNN)
:param last_layer_dim_pi: (int) number of units for the last layer of the policy network
:param last_layer_dim_vf: (int) number of units for the last layer of the value network
"""

def __init__(
self,
path: str,
feature_dim: int,
last_layer_dim_pi: int = 32,
last_layer_dim_vf: int = 32,

):
super(CUSTOM_NETWORK, self).__init__()

# Read file
data = read_json(path)

# Create the network based on JSON
self.body_net = create_body_network(data)

print(self.body_net)

# Save output dimensions, used to create the distributions
self.latent_dim_pi = last_layer_dim_pi
self.latent_dim_vf = last_layer_dim_vf

# Policy network
self.policy_net = nn.Sequential()

# Value network
self.value_net = nn.Sequential()

def forward(self, features: th.Tensor) -> Tuple[th.Tensor, th.Tensor]:
"""
:return: (th.Tensor, th.Tensor) latent_policy, latent_value of the specified network.
If all layers are shared, then ``latent_policy == latent_value``
"""
body_x = self.body_net(features)
return self.policy_net(body_x), self.value_net(body_x)


@AgentFactory.register("CUSTOM")
class CUSTOM(ActorCriticPolicy):
"""
Policy using the custom Multilayer Perceptron.
"""

def __init__(
self,
observation_space: gym.spaces.Space,
action_space: gym.spaces.Space,
lr_schedule: Callable[[float], float],
net_arch: Optional[List[Union[int, Dict[str, List[int]]]]] = None,
activation_fn: Type[nn.Module] = nn.ReLU,
*args,
**kwargs,

):
# Getting the path to NN with rosparams
self.path=rospy.get_param("/custom_network_path")

super(CUSTOM, self).__init__(
observation_space,
action_space,
lr_schedule,
net_arch,
activation_fn,
*args,
**kwargs,
)
# Enable orthogonal initialization
self.ortho_init = True

def _build_mlp_extractor(self) -> None:
self.mlp_extractor = CUSTOM_NETWORK(self.path, 64)
File renamed without changes.
28 changes: 28 additions & 0 deletions rosnav/model/custom_policy_utils/modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from torch import nn

def linear(params):
return nn.Linear(params["in_features"], params["out_features"], params.get("bias", True))

def relu(params):
return nn.ReLU(params.get("inplace", False))

def conv1d(params):
return nn.Conv1d(params["in_channels"], params["out_channels"], params["kernel_size"], params.get("stride", 1), params.get("padding", 0), params.get("dilation", 1), params.get("groups", 1), params.get("bias", True), params.get("padding_mode", 'zeros'))

def conv2d(params):
return nn.Conv2d(params["in_channels"], params["out_channels"], params["kernel_size"], params.get("stride", 1), params.get("padding", 0), params.get("dilation", 1), params.get("groups", 1), params.get("bias", True), params.get("padding_mode", 'zeros'))

def conv3d(params):
return nn.Conv3d(params["in_channels"], params["out_channels"], params["kernel_size"], params.get("stride", 1), params.get("padding", 0), params.get("dilation", 1), params.get("groups", 1), params.get("bias", True), params.get("padding_mode", 'zeros'))

def tanh(params):
return nn.Tanh()

modules = {
"relu" : relu,
"tanh" : tanh,
"linear" : linear,
"conv1d" : conv1d,
"conv2d" : conv2d,
"conv3d" : conv3d,
}
24 changes: 24 additions & 0 deletions rosnav/model/custom_policy_utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import json
from torch import nn
from .modules import modules

def read_json(path):
with open(path, 'r') as f:
return json.load(f)


def create_body_network(data):
body_net = nn.Sequential()

# Number of the module to be added to the NN
module_number = 0

# Iterate over each module given in the json in the policy
for module in data["policy"]:

# Add each module to the body network with corresponding number of module
body_net.add_module(f'{module_number}', modules[module["type"].lower()](module))

module_number += 1

return body_net
Empty file modified rosnav/utils/__init__.py
100755 → 100644
Empty file.