Skip to content

How to perform batch inference to accelerate the process - coca #781

@Raychanan

Description

@Raychanan

Hi, thanks for your amazing work!

I'm currently following the official tutorial available on the project's homepage through Colab, and it's working out great for me.

I now have a GPU with ample memory. As I have numerous images to process, I'm wondering if there's a way to handle all the images in a folder in several batches for accelerating the processing? I've only used batching with text before and am quite new to doing this with images. If it's possible, could you provide some guidance or resources on this? Thanks!

Here is my existing code:

import open_clip
import torch
from PIL import Image
import os

# Create model and transforms
model, _, transform = open_clip.create_model_and_transforms(
    model_name="coca_ViT-L-14",
    pretrained="mscoco_finetuned_laion2B-s13B-b90k",
    device='cuda',
    cache_dir="/global/scratch/users/USERNAME/huggingface_cache/"
)

folder_path = './'

# List all files in the directory
files = os.listdir(folder_path)

for file in files:
    # Check if the file is an image (you might want to check for specific extensions)
    if file.lower().endswith(('.png', '.jpg', '.jpeg')):
        image_path = os.path.join(folder_path, file)

        # Load and transform the image
        im = Image.open(image_path).convert("RGB")
        im = transform(im).unsqueeze(0)

        # Transfer the image tensor to CUDA
        im = im.to('cuda')

        with torch.no_grad(), torch.cuda.amp.autocast():
            generated = model.generate(im)

        # Print the generated text
        print(f"Text for {file}:")
        print(open_clip.decode(generated[0]).split("<end_of_text>")[0].replace("<start_of_text>", ""))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions