メインコンテンツへスキップ

FuseCapをgoogle colabで試してみた。


    FuseCapとは

    FuseCapとはImage captionのモデルであり、BLIPの派生系として紹介されたものです。


    リンク

    Colab
    github

    準備

    Google Colabを開き、メニューから「ランタイム→ランタイムのタイプを変更」でランタイムを「GPU」に変更します。

    環境構築

    インストール手順です。

    !pip install transformers accelerate bitsandbytes

    推論

    (1)モデルのロード

    import torch
    from transformers import BlipProcessor, BlipForConditionalGeneration
    
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    
    processor = BlipProcessor.from_pretrained("noamrot/FuseCap")
    model = BlipForConditionalGeneration.from_pretrained("noamrot/FuseCap").to(device)

    (2)データの準備

    !wget "https://www.bhg.com/thmb/Ke48CUYRrXfAc_CJP4eXuZsrWkw=/750x0/filters:no_upscale():max_bytes(150000):strip_icc():format(webp)/living-room-gallery-shelves-l-shaped-couch-ELeyNpyyqpZ8hosOG3EG1X-b5a39646574544e8a75f2961332cd89a.jpg" -O /content/room.jpg

    以下の画像を利用します。


    画像

    (3)推論

    def inference(raw_image):
        text = "a picture of "
        inputs = processor(raw_image, text, return_tensors="pt").to(device)
        out = model.generate(**inputs)
        caption = processor.decode(out[0], skip_special_tokens=True)
        return caption
        
    from PIL import Image
    raw_image = Image.open("/content/room.jpg")
    caption = inference(raw_image)
    print(caption)

    output

    a picture of a cozy living room with a gray couch adorned with blue and green pillows, accompanied by a white chair and a brown wooden table the room is adorned with potted green plants and

    最後に

    今回はImageCaptionモデルであるFuseCapを利用してみました。かなり特徴を含んだcaptionを生成できていてすごくいい、BLIP系列から乗り換えるレベルです。これは利用範囲が非常に広そう。

    今後ともLLM, Diffusion model, Image Analysis, 3Dに関連する試した記事を投稿していく予定なのでよろしくお願いします。

    あなたへのおすすめ