Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
4c80ad0
Initial Commit
Ram81 Sep 29, 2023
83f8f38
[Dataset] Integrate IIN generator
Ram81 Sep 30, 2023
1f71dab
[Dataset] Add Instance filtering in IIN generator
Ram81 Oct 1, 2023
f8b75f5
[Dataset] Incorporate instance filtering in LanguageNav
Ram81 Oct 1, 2023
950cc67
[Dataset] BugFix in IIN save
Ram81 Oct 1, 2023
8741742
[Dataset] Relax generator constraints to increase language goals
Ram81 Oct 3, 2023
90f9b82
Adds goat dataset, task spec, multigoal sensor, topdown measure
mukulkhanna Oct 3, 2023
80497a0
[Baseline] Add langauge nav trainer, sensors and caching
Ram81 Oct 5, 2023
d40101a
[Baseline] Add instance image goal caching using VC-1 and trainer
Ram81 Oct 7, 2023
0473e2c
[Scripts] Add training and dataset scripts
Ram81 Oct 12, 2023
b2db48d
[Task] Add GOAT task and distance to goal metric
Ram81 Oct 14, 2023
2ff0ea0
[Task] Add GOAT success and SPL metrics
Ram81 Oct 15, 2023
b82a00e
[Task] Add GOAT SoftSPL
Ram81 Oct 15, 2023
dd7a0c5
[Task] GOAT DTG reward
Ram81 Oct 18, 2023
28fe168
[Baseline] Add GOAT nav skill chaining baseline
Ram81 Oct 19, 2023
d21e1e6
[Baseline] Add multi-task baseline
Ram81 Oct 27, 2023
6920dd4
[Task] Fix issues with multi-task resets
Ram81 Oct 30, 2023
30dc763
[Scripts] Add GOAT eval and train scripts
Ram81 Oct 30, 2023
9e2f9af
[Scripts] Add batch goat eval
Ram81 Nov 3, 2023
20246cc
[Baseline] Add goat lnav and iin clip baselines
Ram81 Nov 10, 2023
cd03809
[Visualize] Add mturk save and visualize scripts
Ram81 Nov 10, 2023
dfbb4c8
[Eval] Add noise probing evaluations
Ram81 Nov 11, 2023
a52adea
[Eval] Add noise probing evaluations
Ram81 Nov 11, 2023
985213a
[Eval] Add GOAT analysis and mturk scripts
Ram81 Nov 15, 2023
f80d2e3
Initialize Croco
gchhablani Oct 13, 2023
4042b91
Fix clone command
gchhablani Oct 13, 2023
38cef11
Update files
gchhablani Oct 14, 2023
f81b11e
Add changes to make Croco work
gchhablani Oct 16, 2023
e21e8c7
Switch to Croco with adapter and base variant
gchhablani Oct 20, 2023
9866d60
Add ImageNav and InstanceImageGoal HFOV sensor
gchhablani Oct 24, 2023
ad29b16
Update clip policy
gchhablani Oct 28, 2023
9d646e0
Add DDPPO IIN and ImageNav configurations
gchhablani Oct 28, 2023
0506328
Add DDPPO IIN configuration
gchhablani Oct 28, 2023
bbab7f1
Fix things from rebase
gchhablani Oct 28, 2023
432b81c
Fix missing imports
gchhablani Oct 28, 2023
8783b54
Add croco training script
gchhablani Oct 28, 2023
59729d9
Add submodule
gchhablani Oct 28, 2023
75700f0
Remove croco cloning from setup
gchhablani Oct 28, 2023
7e982af
Remove comment
gchhablani Oct 28, 2023
800822e
Fix name of policy
gchhablani Oct 29, 2023
bbea911
Add change in policy name
gchhablani Oct 30, 2023
e4c51e2
Add changes for Goat evaluation
gchhablani Nov 1, 2023
2191256
Add Goat Croco eval config
gchhablani Nov 1, 2023
8132e8a
Fix policy and sensors
gchhablani Nov 6, 2023
ce8cc47
Add methods for caching embeddings
gchhablani Nov 11, 2023
4677402
Update files
gchhablani Nov 16, 2023
5fe934e
Fix noise generation
gchhablani Nov 17, 2023
86a9910
Remove comments
gchhablani Nov 25, 2023
d340c1e
Change directory name
gchhablani Jun 10, 2024
4d05dca
Remove extras
gchhablani Jun 10, 2024
743c38c
Remove extras
gchhablani Jun 10, 2024
820b6a6
Remove extras
gchhablani Jun 10, 2024
f308656
Remove extras
gchhablani Jun 10, 2024
b51669c
Update cache_clip_embeddings.py
gchhablani Jun 11, 2024
c3cf6ec
Remove extras
gchhablani Jun 11, 2024
9adbbb8
Remove extras
gchhablani Jun 11, 2024
8a60151
Remove extras
gchhablani Jun 11, 2024
f9ddea7
Add changes for skill-chain to work and other minor fixes
gchhablani Jun 11, 2024
50e96f0
Remove extras
gchhablani Jun 11, 2024
a94a556
Remove extra changes
gchhablani Jun 27, 2024
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "goat_bench/models/encoders/croco"]
path = goat_bench/models/encoders/croco
url = git@github.com:gchhablani/croco-with-adapter.git
18 changes: 18 additions & 0 deletions goat_bench/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class ClipObjectGoalSensorConfig(LabSensorConfig):
)


@dataclass
class GoatInstanceImageGoalSensorConfig(LabSensorConfig):
type: str = "GoatInstanceImageGoalSensor"
add_noise: bool = False


@dataclass
class GoatGoalSensorConfig(LabSensorConfig):
type: str = "GoatGoalSensor"
Expand Down Expand Up @@ -204,6 +210,11 @@ class GOATPolicyConfig(PolicyConfig):

clip_model: str = "RN50"
add_clip_linear_projection: bool = False
add_instance_linear_projection: bool = False
croco_adapter: bool = False
use_croco: bool = False
croco_ckpt: str = "goat_bench/models/encoders/croco/pretrained_models/CroCo_V2_ViTBase_SmallDecoder.pth"
use_hfov: bool = False
depth_ckpt: str = ""
late_fusion: bool = False

Expand Down Expand Up @@ -257,6 +268,13 @@ class OVONHabitatConfig(HabitatConfig):
node=ClipObjectGoalSensorConfig,
)

cs.store(
package=f"habitat.task.lab_sensors.goat_instance_imagegoal_sensor",
group="habitat/task/lab_sensors",
name="goat_instance_imagegoal_sensor",
node=GoatInstanceImageGoalSensorConfig,
)

cs.store(
package=f"habitat.task.lab_sensors.goat_goal_sensor",
group="habitat/task/lab_sensors",
Expand Down
71 changes: 71 additions & 0 deletions goat_bench/models/clip_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from gym import spaces
from gym.spaces import Dict as SpaceDict
from habitat import logger
from habitat.tasks.nav.instance_image_nav_task import InstanceImageGoalSensor, InstanceImageGoalHFOVSensor
from habitat.tasks.nav.nav import EpisodicCompassSensor, EpisodicGPSSensor
from habitat.tasks.nav.object_nav_task import ObjectGoalSensor
from habitat_baselines.common.baseline_registry import baseline_registry
Expand All @@ -27,9 +28,11 @@
ClipImageGoalSensor,
ClipObjectGoalSensor,
GoatGoalSensor,
GoatInstanceImageGoalSensor,
GoatMultiGoalSensor,
LanguageGoalSensor,
)
from goat_bench.models.encoders.croco_binocular_encoder import CrocoBinocularEncoder


@baseline_registry.register_policy(name="GOATPolicy")
Expand All @@ -45,6 +48,11 @@ def __init__(
policy_config: "DictConfig" = None,
aux_loss_config: Optional["DictConfig"] = None,
add_clip_linear_projection: bool = False,
add_instance_linear_projection: bool = False,
croco_adapter: bool = False,
use_croco: bool = False,
croco_ckpt: str = None,
use_hfov: bool = False,
depth_ckpt: str = "",
late_fusion: bool = False,
**kwargs,
Expand All @@ -70,6 +78,11 @@ def __init__(
backbone=backbone,
discrete_actions=discrete_actions,
add_clip_linear_projection=add_clip_linear_projection,
add_instance_linear_projection=add_instance_linear_projection,
croco_adapter=croco_adapter,
use_croco=use_croco,
croco_ckpt=croco_ckpt,
use_hfov=use_hfov,
depth_ckpt=depth_ckpt,
late_fusion=late_fusion,
),
Expand Down Expand Up @@ -173,6 +186,10 @@ def __init__(
add_clip_linear_projection: bool = False,
add_language_linear_projection: bool = False,
add_instance_linear_projection: bool = False,
croco_adapter: bool = False,
use_croco: bool = False,
croco_ckpt: str = None,
use_hfov: bool = False,
late_fusion: bool = False,
):
super().__init__()
Expand All @@ -181,6 +198,10 @@ def __init__(
self.add_clip_linear_projection = add_clip_linear_projection
self.add_language_linear_projection = add_language_linear_projection
self.add_instance_linear_projection = add_instance_linear_projection
self.croco_adapter = croco_adapter
self.use_croco = use_croco
self.croco_ckpt = croco_ckpt
self.use_hfov = use_hfov
self.late_fusion = late_fusion
self._n_prev_action = 32
if discrete_actions:
Expand Down Expand Up @@ -323,6 +344,43 @@ def __init__(
rnn_input_size += instance_goal_size
rnn_input_size_info["instance_goal"] = instance_goal_size

if self.use_croco and (
InstanceImageGoalSensor.cls_uuid in observation_space.spaces or
GoatInstanceImageGoalSensor.cls_uuid in observation_space.spaces
):
self.croco_binocular_encoder = CrocoBinocularEncoder(
observation_space=observation_space,
checkpoint=self.croco_ckpt,
adapter=self.croco_adapter,
hidden_size=64, # NOTE: Total will be 49 * 64 per goal
)
self.croco_binocular_encoder.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
embedding_dim = 3136 # NOTE: Assuming the base decoder variant for now and FC is inside binocular encoder
print(
f"Binocular encoder embedding: {embedding_dim}, "
f"Add Instance linear: {add_instance_linear_projection}"
)
if self.add_instance_linear_projection:
self.instance_embedding = nn.Linear(embedding_dim, 256)
instance_goal_size = 256
else:
instance_goal_size = embedding_dim

rnn_input_size += instance_goal_size
rnn_input_size_info["instance_goal"] = instance_goal_size

if self.use_hfov and InstanceImageGoalHFOVSensor.cls_uuid in observation_space.spaces:
hfov_input_dim = int(
observation_space.spaces[InstanceImageGoalHFOVSensor.cls_uuid].high[0]
) + 1
hfov_embedding_size = 32
self.hfov_embedding = nn.Embedding(
hfov_input_dim, hfov_embedding_size
)

rnn_input_size += hfov_embedding_size
rnn_input_size_info["hfov_embedding"] = hfov_embedding_size

if EpisodicGPSSensor.cls_uuid in observation_space.spaces:
input_gps_dim = observation_space.spaces[
EpisodicGPSSensor.cls_uuid
Expand Down Expand Up @@ -455,6 +513,19 @@ def forward(
instance_goal = self.instance_embedding(instance_goal)
x.append(instance_goal)

if self.use_croco and (
InstanceImageGoalSensor.cls_uuid in observations or
GoatInstanceImageGoalSensor.cls_uuid in observations
):
instance_goal = self.croco_binocular_encoder(observations)
if self.add_instance_linear_projection:
instance_goal = self.instance_embedding(instance_goal)
x.append(instance_goal)

if self.use_hfov and InstanceImageGoalHFOVSensor.cls_uuid in observations:
instance_goal_hfov = observations[InstanceImageGoalHFOVSensor.cls_uuid].long()
x.append(self.hfov_embedding(instance_goal_hfov).squeeze(dim=1))

if (
ClipImageGoalSensor.cls_uuid in observations
and not self.late_fusion
Expand Down
155 changes: 155 additions & 0 deletions goat_bench/models/encoders/croco2_vit_decoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Copyright (C) 2022-present Naver Corporation. All rights reserved.
# Licensed under CC BY-NC-SA 4.0 (non-commercial use only).

import torch
import torch.nn as nn
torch.backends.cuda.matmul.allow_tf32 = True # for GPU >= Ampere and PyTorch >= 1.12
from functools import partial

from .croco.models.blocks import DecoderBlock, PatchEmbed
from .croco.models.pos_embed import get_2d_sincos_pos_embed, RoPE2D

class Croco2ViTDecoder(nn.Module):
"""
Croco2ViTDecoder: A decoder module for the CroCo model.

Args:
img_size (int): Input image size.
patch_size (int): Patch size.
mask_ratio (float): Ratio of masked tokens.
enc_embed_dim (int): Encoder feature dimension.
enc_depth (int): Encoder depth.
enc_num_heads (int): Number of heads in the encoder's transformer block.
dec_embed_dim (int): Decoder feature dimension.
dec_depth (int): Decoder depth.
dec_num_heads (int): Number of heads in the decoder's transformer block.
mlp_ratio (int): Ratio for MLP layers.
norm_layer (callable): Normalization layer constructor.
norm_im2_in_dec (bool): Whether to apply normalization to the 'memory' (second image) in the decoder.
pos_embed (str): Type of positional embedding (either 'cosine' or 'RoPE100').
"""

def __init__(self, img_size=224, patch_size=16, mask_ratio=0.9, enc_embed_dim=768,
enc_depth=12, enc_num_heads=12, dec_embed_dim=512, dec_depth=8,
dec_num_heads=16, mlp_ratio=4, norm_layer=partial(nn.LayerNorm, eps=1e-6),
norm_im2_in_dec=True, pos_embed='cosine',
adapter=False, adapter_bottleneck=64, adapter_scalar='0.1', adapter_style='parallel'):

super(Croco2ViTDecoder, self).__init__()

# Patch embeddings (with initialization done as in MAE)
self.patch_embed = PatchEmbed(img_size, patch_size, 3, enc_embed_dim)

self.pos_embed = pos_embed
if pos_embed == 'cosine':
# Positional embedding of the encoder
dec_pos_embed = get_2d_sincos_pos_embed(dec_embed_dim, int(self.patch_embed.num_patches**.5), n_cls_token=0)
self.register_buffer('dec_pos_embed', torch.from_numpy(dec_pos_embed).float())
# Position embedding in each block
self.rope = None # Nothing for cosine
elif pos_embed.startswith('RoPE'): # e.g., RoPE100
self.dec_pos_embed = None # Nothing to add in the decoder with RoPE
if RoPE2D is None:
raise ImportError("Cannot find cuRoPE2D, please install it following the README instructions")
freq = float(pos_embed[len('RoPE'):])
self.rope = RoPE2D(freq=freq)
else:
raise NotImplementedError('Unknown pos_embed ' + pos_embed)

# Decoder
self._set_decoder(enc_embed_dim, dec_embed_dim, dec_num_heads, dec_depth, mlp_ratio, norm_layer, norm_im2_in_dec, adapter, adapter_bottleneck, adapter_scalar, adapter_style)

# Initialize weights
self.initialize_weights()

def _set_decoder(self, enc_embed_dim, dec_embed_dim, dec_num_heads, dec_depth, mlp_ratio, norm_layer, norm_im2_in_dec, adapter, adapter_bottleneck, adapter_scalar, adapter_style):
self.dec_depth = dec_depth
self.dec_embed_dim = dec_embed_dim
# Transfer from encoder to decoder
self.decoder_embed = nn.Linear(enc_embed_dim, dec_embed_dim, bias=True)
# Transformer for the decoder
self.dec_blocks = nn.ModuleList([
DecoderBlock(dec_embed_dim, dec_num_heads, mlp_ratio=mlp_ratio, qkv_bias=True, norm_layer=norm_layer, norm_mem=norm_im2_in_dec, rope=self.rope,
adapter=adapter, adapter_bottleneck=adapter_bottleneck, adapter_scalar=adapter_scalar, adapter_style=adapter_style)
for i in range(dec_depth)])
# Final norm layer
self.dec_norm = norm_layer(dec_embed_dim)

def initialize_weights(self):
# patch embed
self.patch_embed._init_weights()
# linears and layer norms
# self.apply(self._init_weights)
self.recursive_apply(self)

def recursive_apply(self, m):
for name, m2 in m.named_children():
if "adaptmlp" in name:
return
else:
self.recursive_apply(m2)
self._init_weights(m)

def _init_weights(self, m):
if isinstance(m, nn.Linear):
# we use xavier_uniform following official JAX ViT:
torch.nn.init.xavier_uniform_(m.weight)
if isinstance(m, nn.Linear) and m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.LayerNorm):
nn.init.constant_(m.bias, 0)
nn.init.constant_(m.weight, 1.0)

def _decoder(self, feat1, pos1, feat2, pos2, return_all_blocks=False):
"""
Decoder function for Croco2ViTDecoder.

Args:
feat1: Features from the first image.
pos1: Position information for the first image.
feat2: Features from the second image.
pos2: Position information for the second image.
return_all_blocks: If True, return the features at the end of every block instead of just the features from the last block.

Returns:
Output features from the decoder.
"""
# Encoder to decoder layer
visf1 = self.decoder_embed(feat1)
f2 = self.decoder_embed(feat2)
f1_ = visf1
# Add positional embedding
if self.dec_pos_embed is not None:
f1_ = f1_ + self.dec_pos_embed
f2 = f2 + self.dec_pos_embed
# Apply Transformer blocks
out = f1_
out2 = f2
if return_all_blocks:
_out, out = out, []
for blk in self.dec_blocks:
_out, out2 = blk(_out, out2, pos1, pos2)
out.append(_out)
out[-1] = self.dec_norm(out[-1])
else:
for blk in self.dec_blocks:
out, out2 = blk(out, out2, pos1, pos2)
out = self.dec_norm(out)
return out

def forward(self, feat1, pos1, feat2, pos2):
"""
Forward pass for Croco2ViTDecoder.

Args:
feat1: Encoder features from the first image.
pos1: Position information for the first image.
feat2: Features from the second image.
pos2: Position information for the second image.

Returns:
Decoded features.
"""
# Decoder
decfeat = self._decoder(feat1, pos1, feat2, pos2)
return decfeat
Loading