{"id":19279838,"url":"https://github.com/showlab/afformer","last_synced_at":"2025-06-15T22:11:48.393Z","repository":{"id":148380556,"uuid":"619273798","full_name":"showlab/afformer","owner":"showlab","description":"Affordance Grounding from Demonstration Video to Target Image (CVPR 2023)","archived":false,"fork":false,"pushed_at":"2024-07-26T16:23:25.000Z","size":23615,"stargazers_count":42,"open_issues_count":6,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-01T17:24:48.333Z","etag":null,"topics":["deep-learning","pytorch"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/showlab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-26T19:05:59.000Z","updated_at":"2025-03-30T17:20:42.000Z","dependencies_parsed_at":"2023-05-19T23:00:32.529Z","dependency_job_id":null,"html_url":"https://github.com/showlab/afformer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/showlab%2Fafformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/showlab%2Fafformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/showlab%2Fafformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/showlab%2Fafformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/showlab","download_url":"https://codeload.github.com/showlab/afformer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250158021,"owners_count":21384334,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","pytorch"],"created_at":"2024-11-09T21:16:12.191Z","updated_at":"2025-04-22T00:33:04.444Z","avatar_url":"https://github.com/showlab.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/affordance-grounding-from-demonstration-video-1/video-to-image-affordance-grounding-on-opra)](https://paperswithcode.com/sota/video-to-image-affordance-grounding-on-opra?p=affordance-grounding-from-demonstration-video-1)\n[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/affordance-grounding-from-demonstration-video-1/video-to-image-affordance-grounding-on-opra-1)](https://paperswithcode.com/sota/video-to-image-affordance-grounding-on-opra-1?p=affordance-grounding-from-demonstration-video-1)\n[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/affordance-grounding-from-demonstration-video-1/video-to-image-affordance-grounding-on-epic)](https://paperswithcode.com/sota/video-to-image-affordance-grounding-on-epic?p=affordance-grounding-from-demonstration-video-1)\n\n# Affordance Grounding from Demonstration Video to Target Image\n\nThis repository is the official implementation of [Affordance Grounding from Demonstration Video to Target Image](https://arxiv.org/abs/2303.14644):\n\n![](readme.png)\n\n![](demo/output1.gif) \n```\n@inproceedings{afformer,\n  author  = {Joya Chen and Difei Gao and Kevin Qinghong Lin and Mike Zheng Shou},\n  title   = {Affordance Grounding from Demonstration Video to Target Image},\n  booktitle = {CVPR},\n  year    = {2023},\n}\n```\n\n## Install\n\n### 1. PyTorch \n\nWe now support PyTorch 2.0. Other version should be okay.\n\n```\nconda install -y pytorch torchvision pytorch-cuda=11.8 -c pytorch -c nvidia\n```\n\n*NOTE: If you want to use PyTorch 2.0, you should install CUDA \u003e= 11.7. See https://pytorch.org/.*\n\n### 2. PyTorch Lightning\n\nWe use PyTorch Lightning 2.0 as the training and inference engines.\n\n```\npip install lightning jsonargparse[signatures] --upgrade\n```\n\n### 3. xFormers\n\nWe use memory-efficient attention in [xformers](https://github.com/facebookresearch/xformers). Currently PyTorch 2.0 does not support memory-efficient attention relative positional encoding (see [pytorch/issues/96099](https://github.com/pytorch/pytorch/issues/96099)). We will update this repo when PyTorch supports this.\n\n```\npip install triton --upgrade\npip install --pre xformers\n```\n\n### 4. Timm, Detectron2, Others\n\nWe borrow some implementations from [timm](https://github.com/huggingface/pytorch-image-models) and [detectron2](https://github.com/facebookresearch/detectron2).\n\n```\npip install timm opencv-python av imageio --upgrade\npython -m pip install 'git+https://github.com/facebookresearch/detectron2.git'\n```\n\n## Dataset\n\n* Downloading OPRA dataset from https://github.com/kuanfang/opra. Due to the copyright issue, you may need to download the original video from YouTube.\n\n* We have uploaded our organized annotation json files to [datasets/opra/annotations](datasets/opra/annotations). Now your datasets file tree should be:\n```\ndatasets\n└── opra\n    ├── annotations\n    │   ├── test.json\n    │   ├── train.json\n    ├── clips\n    │   ├── aocom\n    │   ├── appliances\n    │   ├── bestkitchenreview\n    │   ├── cooking\n    │   ├── eguru\n    │   └── seattle\n    └── images\n        ├── aocom\n        ├── appliances\n        ├── bestkitchenreview\n        ├── cooking\n        ├── eguru\n        └── seattle\n```\n\n* *We are working on organizing EPIC-Hotspot and AssistQ Buttons. They will be released as soon as possible.*\n\n## Afformer Model\n\n*Hint: We recommend you to read [LightningCLI](https://lightning.ai/docs/pytorch/stable/cli/lightning_cli.html) if you firstly use it. That helps you better use these commands.*\n\n### 1. ResNet-50-FPN encoder\n\n* You dont need to manually download pre-trained encoder weight. ```torchvision``` will automatically download it. See [torchvision.models.detection.fasterrcnn_resnet50_fpn_v2](https://pytorch.org/vision/main/models/generated/torchvision.models.detection.fasterrcnn_resnet50_fpn_v2.html) for details.\n\n* Training Afformer with ResNet-50-FPN encoder with\n```shell\npython main.py fit --config configs/opra/r50fpn.yaml --trainer.devices 8 --data.batch_size_per_gpu 2\n```\n\n* The training log is saved in `outputs/` by default. You can launch a tensorboard to monitor this folder:\n```shell\ntensorboard --logdir outputs/ --port 2333\n# Then you can see real-time losses, metrics at http://localhost:2333/ \n```\n\n* The evaluation would be done each 1k iterations during training. You can also evaluate with the `validate` command. For example, \n```shell\npython main.py validate --config configs/opra/r50fpn.yaml --trainer.devices 8 --data.batch_size_per_gpu 2 --ckpt outputs/opra/r50fpn/lightning_logs/version_0/checkpoints/xxxx.ckpt\n```\n\n### 2. ViTDet-B encoder\n\n* Downloading [ViTDet-B-COCO weights](https://dl.fbaipublicfiles.com/detectron2/ViTDet/COCO/mask_rcnn_vitdet_b/f325346929/model_final_61ccd1.pkl) and then put it to [weights/](weights/) folder: ```weights/mask_rcnn_vitdet_b_coco.pkl```.\n\n* Training Afformer with ViTDet-B encoder with\n```shell\npython main.py fit --config configs/opra/vitdet.yaml --trainer.devices 8 --data.batch_size_per_gpu 2\n```\n\n* The training log is saved in `outputs/` by default. You can launch a tensorboard to monitor this folder:\n```shell\ntensorboard --logdir outputs/ --port 2333\n# Then you can see real-time losses, metrics at http://localhost:2333/ \n```\n\n* The evaluation would be done each 1k iterations during training. You can also evaluate with the `validate` command. For example, \n```shell\npython main.py validate --config configs/opra/vitdet_b.yaml --trainer.devices 8 --data.batch_size_per_gpu 2 --ckpt outputs/opra/vitdet_b/lightning_logs/version_0/checkpoints/xxxx.ckpt\n```\n\n### 3. Visualization\n\n```shell\npython demo.py --config configs/opra/vitdet_b.yaml --weight weights/afformer_vitdet_b_v1.ckpt --video demo/video.mp4 --image demo/image.jpg --output demo/output.gif\n```\n\n![](demo/output2.gif) \n\n* Hint: we carefully fine-tuned a very strong ViTDet model, which is better than paper reported. [Download it](https://drive.google.com/uc?export=download\u0026id=1bvgBwkRJhA2hxInrPPHlwlGH3tNfDciX). \n\n## MaskAHand Pre-training\n\n*NOTE: A detailed tutorial will be done as soon as possible.*\n\n### 1. Hand Interaction Detection\n\n* Downloading our trained hand interaction detector weights in this [url](https://drive.google.com/file/d/1Q6SLFnn-ztZDjDoFalOhVLsL0nEZWc-w/view?usp=sharing). Then put it to [weights/](weights/) folder: ```weights/hircnn_r50fpnv2_849.pth```.\n\n* The video demo by this hand interaction detector:\n\n![](demo/hircnn.gif) \n\n* *Hint: we trained this simple and accurate hand interaction detector using 100DOH + some Ego datasets. It achieves 84.9 hand+interaction detection AP on 100DOH test set. For MaskAHand pre-training, this weight is enough. We will release its full source code at [chenjoya/hircnn](https://github.com/chenjoya/hircnn) as soon as possible.*\n\n### 2. Hand Interaction Clip Mining\n\n* Make sure your data preparation follows [Dataset](#dataset) part.\n\n* Running [affominer/miner.py](afformer/miner.py). The generated data will be saved at `affominer/outputs`.\n\n### 3. Target Image Synthesis and Transformation\n\nThis would be done during training. You can set the hyper-parameters in [configs/opra/maskahand/pretrain.yaml](configs/opra/maskahand/pretrain.yaml):\n\n```yaml\nmask_ratio: 1.0\nnum_masks: 2\ndistortion_scale: 0.5\nnum_frames: 32\nclip_interval: 16\ncontact_threshold: 0.99\n```\n\n### 4. MaskAHand Pre-training\n\n```shell\npython main.py fit --config configs/opra/maskahand/pretrain.yaml\n```\n\n### 5. Fine-tuning or Zero-shot Evaluation\n\n* Fine-tuning the MaskAHand pre-trained weight by\n\n```shell\npython main.py fit --config configs/opra/maskahand/finetune.yaml \n```\n\n* Zero-shot evaluate the MaskAHand pre-trained weight by\n\n```shell\npython main.py validate --config configs/opra/maskahand/pretrain.yaml\n```\n\n### 6. Visualization\n\nYou can refer to [demo.py](demo.py) to visualize your model results.\n\n## Contact\n\nThis repository is developed by [Joya Chen](https://chenjoya.github.io/).  Questions and discussions are welcome via joyachen@u.nus.edu.\n\n## Acknowledgement\n\nThanks to all co-authors of the paper, [Difei Gao](https://scholar.google.com/citations?user=No9OsocAAAAJ\u0026hl=en), [Kevin Qinghong Lin](https://qinghonglin.github.io/), and [Mike Shou](https://sites.google.com/view/showlab) (my supervisor). Also appreciate the assistance from Dongxing Mao and [Jiawei Liu](https://jia-wei-liu.github.io/). \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshowlab%2Fafformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshowlab%2Fafformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshowlab%2Fafformer/lists"}