Skip to content

Commit

Permalink
feat: ram
Browse files Browse the repository at this point in the history
  • Loading branch information
LutingWang committed Sep 19, 2024
1 parent 90c9046 commit dfafc14
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .todd_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7da60bb75eb45b3834f33479d4046ec7fab08345
db442d303be0dd469d9caa556f1fa6d6a93cbf89
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ The following scripts extract features with CLIP, which can be very time-consumi
Extract globals and blocks features, which can be used for both coco and lvis

```bash
[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/coco/clip_globals_cuda configs/oake/clip_globals_cuda.py --config-options dataset::COCO [--auto-fix]
[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/coco/clip_blocks_cuda configs/oake/clip_blocks_cuda.py --config-options dataset::COCO [--auto-fix]
[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/coco/clip_objects_cuda configs/oake/clip_objects_cuda.py --config-options dataset::COCO [--auto-fix]
[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/lvis/clip_objects_cuda configs/oake/clip_objects_cuda.py --config-options dataset::LVIS [--auto-fix]

[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/coco/dino_globals_cuda configs/oake/dino_globals_cuda.py --config-options dataset::COCO [--auto-fix]
[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/coco/dino_blocks_cuda configs/oake/dino_blocks_cuda.py --config-options dataset::COCO [--auto-fix]
[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/coco/dino_objects_cuda configs/oake/dino_objects_cuda.py --config-options dataset::COCO [--auto-fix]
[DRY_RUN=True] bash tools/torchrun.sh -m oadp.oake.val oake/lvis/dino_objects_cuda configs/oake/dino_objects_cuda.py --config-options dataset::LVIS [--auto-fix]
bash tools/torchrun.sh -m oadp.oake.val oake/coco/clip_globals_cuda configs/oake/clip_globals_cuda.py --config-options dataset::COCO [--auto-fix]
bash tools/torchrun.sh -m oadp.oake.val oake/coco/clip_blocks_cuda configs/oake/clip_blocks_cuda.py --config-options dataset::COCO [--auto-fix]
bash tools/torchrun.sh -m oadp.oake.val oake/coco/clip_objects_cuda configs/oake/clip_objects_cuda.py --config-options dataset::COCO [--auto-fix]
bash tools/torchrun.sh -m oadp.oake.val oake/lvis/clip_objects_cuda configs/oake/clip_objects_cuda.py --config-options dataset::LVIS [--auto-fix]

bash tools/torchrun.sh -m oadp.oake.val oake/coco/dino_globals_cuda configs/oake/dino_globals_cuda.py --config-options dataset::COCO [--auto-fix]
bash tools/torchrun.sh -m oadp.oake.val oake/coco/dino_blocks_cuda configs/oake/dino_blocks_cuda.py --config-options dataset::COCO [--auto-fix]
bash tools/torchrun.sh -m oadp.oake.val oake/coco/dino_objects_cuda configs/oake/dino_objects_cuda.py --config-options dataset::COCO [--auto-fix]
bash tools/torchrun.sh -m oadp.oake.val oake/lvis/dino_objects_cuda configs/oake/dino_objects_cuda.py --config-options dataset::LVIS [--auto-fix]

bash tools/torchrun.sh -m oadp.oake.val oake/coco/ram_cuda configs/oake/ram_cuda.py --config-options dataset::COCO
```

The number of files generated by OAKE-objects may be less than the number of images in the dataset.
Expand Down
26 changes: 26 additions & 0 deletions configs/oake/ram_cuda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from typing import Any

from todd.configs import PyConfig

_kwargs_: dict[str, Any]
_kwargs_ = dict(_kwargs_)

_kwargs_.setdefault('branch', 'Global')
_kwargs_.setdefault('strategy', 'cuda')

_base_ = [
PyConfig.load('configs/oake/interface.py', **_kwargs_),
]

runner = dict(
model=dict(type='ram_plus', expand_mask_size=None, adaptive=False),
)
custom_imports = [
'oadp.oake.globals_',
]

_export_ = dict(
trainer=runner,
validator=runner,
custom_imports=custom_imports,
)
1 change: 1 addition & 0 deletions oadp/oake/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .clip import *
from .dino import *
from .expanders import *
from .ram import *
36 changes: 36 additions & 0 deletions oadp/oake/models/ram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
__all__ = [
'ram_plus',
]

import todd.tasks.image_classification as ic
import torch
import torchvision.transforms.v2 as tf_v2
from todd.datasets import IMAGENET_MEAN, IMAGENET_STD
from todd.tasks.image_classification.models.ram import Categories
from torch import nn

from ..registries import OAKEModelRegistry


@OAKEModelRegistry.register_()
def ram_plus(
expand_mask_size: int | None,
adaptive: bool,
) -> tuple[nn.Module, tf_v2.Compose]:
assert expand_mask_size is None
assert not adaptive

categories = Categories.load()
model = ic.models.RAMplus(num_categories=len(categories))
model.load_pretrained('pretrained/ram/ram_plus_swin_large_14m.pth')
model.requires_grad_(False)
model.eval()

transforms = tf_v2.Compose([
tf_v2.Resize((384, 384)),
tf_v2.ToImage(),
tf_v2.ToDtype(torch.float32, True),
tf_v2.Normalize(IMAGENET_MEAN, IMAGENET_STD),
])

return model, transforms

0 comments on commit dfafc14

Please sign in to comment.