r""" The Recognize Anything Model (RAM) inference module. RAM is a strong image tagging model, which can recognize any common category with high accuracy. Described in the paper " Recognize Anything: A Strong Image Tagging Model" https://recognize-anything.github.io/
(self,
med_config=f'{CONFIG_PATH}/configs/med_config.json',
image_size=384,
vit='base',
vit_grad_ckpt=False,
vit_ckpt_layer=0,
prompt='a picture of ',
threshold=0.68,
delete_tag_index=[],
tag_list=f'{CONFIG_PATH}/data/ram_tag_list.txt',
tag_list_chinese=f'{CONFIG_PATH}/data/ram_tag_list_chinese.txt')
| 19 | |
| 20 | class RAM(nn.Module): |
| 21 | def __init__(self, |
| 22 | med_config=f'{CONFIG_PATH}/configs/med_config.json', |
| 23 | image_size=384, |
| 24 | vit='base', |
| 25 | vit_grad_ckpt=False, |
| 26 | vit_ckpt_layer=0, |
| 27 | prompt='a picture of ', |
| 28 | threshold=0.68, |
| 29 | delete_tag_index=[], |
| 30 | tag_list=f'{CONFIG_PATH}/data/ram_tag_list.txt', |
| 31 | tag_list_chinese=f'{CONFIG_PATH}/data/ram_tag_list_chinese.txt'): |
| 32 | r""" The Recognize Anything Model (RAM) inference module. |
| 33 | RAM is a strong image tagging model, which can recognize any common category with high accuracy. |
| 34 | Described in the paper " Recognize Anything: A Strong Image Tagging Model" https://recognize-anything.github.io/ |
| 35 | |
| 36 | Args: |
| 37 | med_config (str): path for the mixture of encoder-decoder model's configuration file |
| 38 | image_size (int): input image size |
| 39 | vit (str): model size of vision transformer |
| 40 | threshold (int): tagging threshold |
| 41 | delete_tag_index (list): delete some tags that may disturb captioning |
| 42 | """ |
| 43 | super().__init__() |
| 44 | |
| 45 | # create image encoder |
| 46 | if vit == 'swin_b': |
| 47 | if image_size == 224: |
| 48 | vision_config_path = f'{CONFIG_PATH}/configs/swin/config_swinB_224.json' |
| 49 | elif image_size == 384: |
| 50 | vision_config_path = f'{CONFIG_PATH}/configs/swin/config_swinB_384.json' |
| 51 | vision_config = read_json(vision_config_path) |
| 52 | assert image_size == vision_config['image_res'] |
| 53 | # assert config['patch_size'] == 32 |
| 54 | vision_width = vision_config['vision_width'] |
| 55 | |
| 56 | self.visual_encoder = SwinTransformer( |
| 57 | img_size=vision_config['image_res'], |
| 58 | patch_size=4, |
| 59 | in_chans=3, |
| 60 | embed_dim=vision_config['embed_dim'], |
| 61 | depths=vision_config['depths'], |
| 62 | num_heads=vision_config['num_heads'], |
| 63 | window_size=vision_config['window_size'], |
| 64 | mlp_ratio=4., |
| 65 | qkv_bias=True, |
| 66 | drop_rate=0.0, |
| 67 | drop_path_rate=0.1, |
| 68 | ape=False, |
| 69 | patch_norm=True, |
| 70 | use_checkpoint=False) |
| 71 | |
| 72 | elif vit == 'swin_l': |
| 73 | if image_size == 224: |
| 74 | vision_config_path = f'{CONFIG_PATH}/configs/swin/config_swinL_224.json' |
| 75 | elif image_size == 384: |
| 76 | vision_config_path = f'{CONFIG_PATH}/configs/swin/config_swinL_384.json' |
| 77 | elif image_size == 444: |
| 78 | vision_config_path = f'{CONFIG_PATH}/configs/swin/config_swinL_444.json' |
nothing calls this directly
no test coverage detected