-
Notifications
You must be signed in to change notification settings - Fork 30
Migrate RT1 to mbodied-agents #6
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
Open
Tilak1114
wants to merge
15
commits into
mbodiai:main
Choose a base branch
from
Tilak1114:rt1-bare-min
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c262487
Add RT1 bare minimum model
nqyy c4a2926
Make rt1 nn modile
nqyy 77b3c3a
Fix device and remove std and mean in tokenizer
nqyy e53e4cd
Run dummy pass
nqyy 6688ddd
Add doctest
nqyy d16ef3c
Make bounds none
nqyy 3c7baef
Add example for rt1
nqyy a48c8a9
Fix broken test
Tilak1114 88c0f5b
Add MotionAgent as the base class for rt1
Tilak1114 8d53b43
Merge branch 'main' of github.com:Tilak1114/mbodied-agents into rt1-b…
Tilak1114 936b7d1
Backmerge main
Tilak1114 3ce70c0
Add MotorAgent readme
Tilak1114 0398b07
Add mbodi headers
Tilak1114 385d1e2
Add Motion instance check in test rt1
Tilak1114 e3ed6f6
Add transfomer reference
Tilak1114 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Copyright 2024 Mbodi AI | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import torch | ||
| from mbodied_agents.agents.motor.rt1.rt1_agent import RT1Agent | ||
|
|
||
|
|
||
| def main() -> None: | ||
| # Define the configuration for the RT1Agent | ||
| rt1_agent_config = { | ||
| "num_layers": 8, | ||
| "layer_size": 128, | ||
| "observation_history_size": 6, | ||
| "future_prediction": 6, | ||
| "token_embedding_dim": 512, | ||
| "causal_attention": True, | ||
| } | ||
|
|
||
| # Instantiate the RT1Agent | ||
| rt1_agent = RT1Agent(rt1_agent_config) | ||
|
|
||
| # Create dummy input data | ||
| image = torch.rand(224, 224, 3) # Assume this is an example image input | ||
| instruction_emb = torch.rand(1, 512) # Assume this is an example instruction embedding | ||
|
|
||
| # Use the act method of RT1Agent to get actions | ||
| actions = rt1_agent.act(image=image, instruction_emb=instruction_emb) | ||
|
|
||
| # Print the actions | ||
| print("Actions received from RT1Agent:") | ||
| print(actions) | ||
|
|
||
| if __name__ == "__main__": | ||
| main() |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Copyright 2024 Mbodi AI | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from typing import List | ||
|
|
||
| from mbodied_agents.types.controls import Motion | ||
|
|
||
|
|
||
| class MotorAgent(ABC): | ||
| """Abstract base class for a Motion Agent. | ||
|
|
||
| Subclasses must implement the `act` method, which generates a list of | ||
| Motion objects based on given parameters. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def act(self, **kwargs) -> List['Motion']: | ||
| """Generate a list of Motion objects based on given parameters. | ||
|
|
||
| Args: | ||
| **kwargs: Arbitrary keyword arguments that will be used to determine the Motion objects. | ||
|
|
||
| Returns: | ||
| List[Motion]: A list of Motion objects based on the provided arguments. | ||
| """ | ||
| pass | ||
Empty file.
Empty file.
55 changes: 55 additions & 0 deletions
55
src/mbodied_agents/agents/motor/rt1/film_efficientnet/efficient_net.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Copyright 2024 Mbodi AI | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from torch import nn | ||
| from torchvision.models import EfficientNet_B5_Weights, efficientnet_b5 | ||
| from torchvision.models.efficientnet import MBConv | ||
|
|
||
| from .film import FilmLayer | ||
|
|
||
|
|
||
| class EfficientNetB5(nn.Module): | ||
| def __init__(self, context_dim: int = 512): | ||
| super().__init__() | ||
| net = efficientnet_b5(weights=EfficientNet_B5_Weights.IMAGENET1K_V1) | ||
| film_layers = [] | ||
|
|
||
| for layer in net.features: | ||
| for sublayer in layer: | ||
| if isinstance(sublayer, MBConv): | ||
| film_layers.append(FilmLayer(sublayer.out_channels, context_dim)) | ||
|
|
||
| # Don't add a film layer to the last layer | ||
| self.film_layers = nn.ModuleList(film_layers[:-1]) | ||
| self.features = net.features | ||
|
|
||
| def forward(self, x, context): | ||
| film_layers = iter(self.film_layers) | ||
| film_layer = next(film_layers, None) | ||
|
|
||
| for layer in self.features: | ||
| for sublayer in layer: | ||
| x = sublayer(x) | ||
| if isinstance(sublayer, MBConv): | ||
| if film_layer is not None: | ||
| x = film_layer(x, context) | ||
| film_layer = next(film_layers, None) | ||
| else: | ||
| return x | ||
| return x | ||
|
|
||
| # import torchinfo | ||
| # from torchinfo import summary | ||
| # model = EfficientNetB3() | ||
| # summary(model, input_size=[(6, 3, 300, 300),(6, 512)]) |
94 changes: 94 additions & 0 deletions
94
src/mbodied_agents/agents/motor/rt1/film_efficientnet/film.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Copyright 2024 Mbodi AI | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from functools import partial | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
| from einops import rearrange | ||
| from torchvision.models.efficientnet import MBConv | ||
|
|
||
|
|
||
| def film_conditioned(cls): | ||
| """Decorator to add FiLM conditioning to a forward method. Adds argument context | ||
| to forward method and applies FiLM conditioning to the output of the forward. | ||
|
|
||
| Args: | ||
| context_key (str, optional): The argument name for the context on which to | ||
| condition the forward pass. Defaults to "context". | ||
| """ | ||
| init_func = cls.__init__ | ||
|
|
||
| def init_with_film_layers(*args, **kwargs): | ||
| context_dim = kwargs.get("context_dim", None) | ||
| if context_dim is None: | ||
| return init_func(*args, **kwargs) | ||
| self = init_func(*args, **kwargs) | ||
| film_layers = [] | ||
| for layer in self.features: | ||
| for sublayer in layer: | ||
| if isinstance(sublayer, MBConv): | ||
| film_layers.append( | ||
| FilmLayer(sublayer.out_channels, context_dim)) | ||
|
|
||
| # Don't add a film layer to the last layer | ||
| self.film_layers = nn.ModuleList(film_layers[:-1]) | ||
| return self | ||
|
|
||
| cls.__init__ = init_with_film_layers | ||
|
|
||
| forward_func = cls.forward | ||
|
|
||
| def forward_with_film_layers(*args, **kwargs): | ||
| if 'conditioning_funcs' in kwargs or kwargs.get('context', None) is None: | ||
| return forward_func(*args, **kwargs) | ||
|
|
||
| self = args[0] | ||
| context = kwargs['context'] | ||
| conditioners = iter([partial(film_layer, context=context) | ||
| for film_layer in self.film_layers]) | ||
| return forward_func(*args, **kwargs, conditioning_funcs=conditioners) | ||
|
|
||
| cls.forward = forward_with_film_layers | ||
| return cls | ||
|
|
||
|
|
||
| class FilmLayer(nn.Module): | ||
| """Layer to conditionally modulate the input tensor with the context tensor.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| num_channels: int, | ||
| context_dim: int = 512, | ||
| ): | ||
| super().__init__() | ||
| self.beta = nn.Linear(context_dim, num_channels, bias=False) | ||
| self.gamma = nn.Linear(context_dim, num_channels, bias=False) | ||
|
|
||
| nn.init.constant_(self.beta.weight, 0) | ||
| nn.init.constant_(self.gamma.weight, 0) | ||
|
|
||
| def forward(self, x: torch.Tensor, context: torch.Tensor): | ||
| context = context.to(x.device) | ||
| beta = self.beta(context) | ||
| gamma = self.gamma(context) | ||
|
|
||
| beta = rearrange(beta, 'b c -> b c 1 1') | ||
| gamma = rearrange(gamma, 'b c -> b c 1 1') | ||
|
|
||
| # Initialize to identity op. | ||
| result = (1 + gamma) * x + beta | ||
|
|
||
| return result |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call it MotionAgent. Also rename the directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially it was MotionAgent. @sebbyjp suggested MotorAgent