通用图像识别模型:支持开放域类别和高精度标签生成
Recognize Anything Model是一系列开源图像识别模型,包括RAM++、RAM和Tag2Text。这些模型能准确识别常见和开放域类别,支持高精度图像标签生成和全面描述。项目提供预训练模型、推理代码和训练数据集,适用于多种计算机视觉任务。模型性能优于现有先进方案,尤其在零样本识别方面表现突出。
本项目旨在开发一系列开源且强大的基础图像识别模型。
识别万物增强模型 (RAM++) [论文] <br>
RAM++是RAM的下一代版本,能够高精度识别任何类别,包括预定义的常见类别和多样化的开放集类别。
RAM是一个图像标记模型,能够高精度识别任何常见类别。
RAM已被CVPR 2024多模态基础模型研讨会接收。
Tag2Text (ICLR 2024) [论文] [演示]<br>
Tag2Text是一个由标记引导的视觉-语言模型,可以同时支持标记和全面的描述生成。
Tag2Text已被ICLR 2024接收!维也纳见!
RAM++在常见标签类别、非常见标签类别和人-物交互短语方面的表现优于现有的最先进图像基础识别模型。
<p align="center"> <table class="tg"> <tr> <td class="tg-c3ow"><img src="https://yellow-cdn.veclightyear.com/835a84d5/361d5ebc-f0d7-4391-9fe3-4135344a8299.jpg" align="center" width="700" ></td> </tr> </table> <p align="center">零样本图像识别性能比较。</p> </p>我们将Tag2Text和RAM与定位模型(Grounding-DINO和SAM)结合,在Grounded-SAM项目中开发了一个强大的视觉语义分析流程。
RAM++是RAM的下一代版本,能够高精度识别任何类别,包括预定义的常见类别和多样化的开放集类别。
对于常见预定义类别。 RAM++展现了卓越的图像标记能力,具有强大的零样本泛化能力,保持了与RAM相同的能力。
对于多样化的开放集类别。 RAM++在CLIP和RAM的基础上实现了显著的增强。
RAM是一个强大的图像标记模型,能够高精度识别任何常见类别。
RAM在Tag2text框架的基础上显著改进了标记能力。
这些标注文件来自Tag2Text和RAM。Tag2Text从图像-文本对中自动提取图像标签。RAM通过自动数据引擎进一步增强了标签和文本。
数据集 | 大小 | 图像数 | 文本数 | 标签数 |
---|---|---|---|---|
COCO | 168 MB | 113K | 680K | 3.2M |
VG | 55 MB | 100K | 923K | 2.7M |
SBU | 234 MB | 849K | 1.7M | 7.6M |
CC3M | 766 MB | 2.8M | 5.6M | 28.2M |
CC3M-val | 3.5 MB | 12K | 26K | 132K |
CC12M将在下一次更新中发布。
这些标签描述文件来自RAM++,通过调用GPT API生成。您也可以使用generate_tag_des_llm.py自定义任何标签类别。
标签描述 | 标签列表 |
---|---|
RAM标签列表 | 4,585 |
OpenImages不常见 | 200 |
注意:您需要创建"pretrained"文件夹并将这些检查点下载到该文件夹中。
<!-- 插入表格 --> <table> <thead> <tr style="text-align: right;"> <th></th> <th>名称</th> <th>骨干网络</th> <th>数据</th> <th>说明</th> <th>检查点</th> </tr> </thead> <tbody> <tr> <th>1</th> <td>RAM++ (14M)</td> <td>Swin-Large</td> <td>COCO, VG, SBU, CC3M, CC3M-val, CC12M</td> <td>为任何类别提供强大的图像标记能力。</td> <td><a href="https://huggingface.co/xinyu1205/recognize-anything-plus-model/blob/main/ram_plus_swin_large_14m.pth">下载链接</a></td> </tr> <tr> <th>2</th> <td>RAM (14M)</td> <td>Swin-Large</td> <td>COCO, VG, SBU, CC3M, CC3M-val, CC12M</td> <td>为常见类别提供强大的图像标记能力。</td> <td><a href="https://huggingface.co/spaces/xinyu1205/Recognize_Anything-Tag2Text/blob/main/ram_swin_large_14m.pth">下载链接</a></td> </tr> <tr> <th>3</th> <td>Tag2Text (14M)</td> <td>Swin-Base</td> <td>COCO, VG, SBU, CC3M, CC3M-val, CC12M</td> <td>支持全面的图像描述和标记。</td> <td><a href="https://huggingface.co/spaces/xinyu1205/Recognize_Anything-Tag2Text/blob/main/tag2text_swin_14m.pth">下载链接</a></td> </tr> </tbody> </table>conda create -n recognize-anything python=3.8 -y conda activate recognize-anything
recognize-anything
作为包安装:pip install git+https://github.com/xinyu1205/recognize-anything.git
git clone https://github.com/xinyu1205/recognize-anything.git cd recognize-anything pip install -e .
然后可以在其他项目中导入RAM++、RAM和Tag2Text模型:
from ram.models import ram_plus, ram, tag2text
获取图像的英文和中文输出:
python inference_ram_plus.py --image images/demo/demo1.jpg --pretrained pretrained/ram_plus_swin_large_14m.pth
输出将如下所示:
Image Tags: armchair | blanket | lamp | carpet | couch | dog | gray | green | hassock | home | lay | living room | picture frame | pillow | plant | room | wall lamp | sit | wood floor
图像标签: 扶手椅 | 毯子/覆盖层 | 灯 | 地毯 | 沙发 | 狗 | 灰色 | 绿色 | 坐垫/搁脚凳/草丛 | 家/住宅 | 躺 | 客厅 | 相框 | 枕头 | 植物 | 房间 | 壁灯 | 坐/放置/坐落 | 木地板
我们已经在openimages_rare_200_llm_tag_descriptions中发布了OpenImages-Uncommon类别的LLM标签描述。
<pre/> python inference_ram_plus_openset.py --image images/openset_example.jpg \ --pretrained pretrained/ram_plus_swin_large_14m.pth \ --llm_tag_des datasets/openimages_rare_200/openimages_rare_200_llm_tag_descriptions.json </pre>输出将如下所示:
Image Tags: Close-up | Compact car | Go-kart | Horse racing | Sport utility vehicle | Touring car
修改categories,并调用GPT API生成相应的标签描述:
<pre/> python generate_tag_des_llm.py \ --openai_api_key '您的openai api密钥' \ --output_file_path datasets/openimages_rare_200/openimages_rare_200_llm_tag_descriptions.json </pre>我们发布了两个数据集OpenImages-common
(214个常见标签类别)和OpenImages-rare
(200个不常见标签类别)。将OpenImages v6的测试图像复制或符号链接到datasets/openimages_common_214/imgs/
和datasets/openimages_rare_200/imgs
。
要在OpenImages-common
上评估RAM++:
python batch_inference.py \ --model-type ram_plus \ --checkpoint pretrained/ram_plus_swin_large_14m.pth \ --dataset openimages_common_214 \ --output-dir outputs/ram_plus
要评估RAM++在OpenImages-rare
上的开放集能力:
python batch_inference.py \ --model-type ram_plus \ -- pretrained/ram_plus_swin_large_14m.pth \ --open-set \ --dataset openimages_rare_200 \ --output-dir outputs/ram_plus_openset
在 OpenImages-common
上评估 RAM:
python batch_inference.py \ --model-type ram \ -- pretrained/ram_swin_large_14m.pth \ --dataset openimages_common_214 \ --output-dir outputs/ram
在 OpenImages-rare
上评估 RAM 的开放集能力:
python batch_inference.py \ --model-type ram \ -- pretrained/ram_swin_large_14m.pth \ --open-set \ --dataset openimages_rare_200 \ --output-dir outputs/ram_openset
在 OpenImages-common
上评估 Tag2Text:
python batch_inference.py \ --model-type tag2text \ -- pretrained/tag2text_swin_14m.pth \ --dataset openimages_common_214 \ --output-dir outputs/tag2text
更多选项请参考 batch_inference.py
。要获得 RAM 论文表 3 中的 P/R,请为 RAM 传递 --threshold=0.86
,为 Tag2Text 传递 --threshold=0.68
。
要对自定义图像进行批量推理,您可以按照给定的两个数据集设置自己的数据集。
下载 RAM 训练数据集,其中每个 json 文件包含一个列表。列表中的每个项目是一个包含三个键值对的字典:{'image_path': 图像路径, 'caption': 图像文本, 'union_label_id': 用于标记的图像标签,包括解析标签和伪标签}。
在 ram/configs/pretrain.yaml 中,将 'train_file' 设置为 json 文件的路径。
准备 预训练的 Swin-Transformer,并在 ram/configs/swin 中设置 'ckpt'。
下载 RAM++ 冻结标签嵌入文件 "ram_plus_tag_embedding_class_4585_des_51.pth",并将文件设置在 "ram/data/frozen_tag_embedding/ram_plus_tag_embedding_class_4585_des_51.pth"。
使用 8 个 A100 GPU 预训练模型:
python -m torch.distributed.run --nproc_per_node=8 pretrain.py \ --model-type ram_plus \ --config ram/configs/pretrain.yaml \ --output-dir outputs/ram_plus
<details> <summary><font size="4" style="font-weight:bold;"> RAM </font></summary>python -m torch.distributed.run --nproc_per_node=8 finetune.py \ --model-type ram_plus \ --config ram/configs/finetune.yaml \ --checkpoint outputs/ram_plus/checkpoint_04.pth \ --output-dir outputs/ram_plus_ft
下载 RAM 训练数据集,其中每个 json 文件包含一个列表。列表中的每个项目是一个包含四个键值对的字典:{'image_path': 图像路径, 'caption': 图像文本, 'union_label_id': 用于标记的图像标签,包括解析标签和伪标签, 'parse_label_id': 从标题解析的图像标签}。
在 ram/configs/pretrain.yaml 中,将 'train_file' 设置为 json 文件的路径。
准备 预训练的 Swin-Transformer,并在 ram/configs/swin 中设置 'ckpt'