Skip to content

Conversation

@FSund
Copy link

@FSund FSund commented Aug 28, 2024

  • Remove hardcoded .cuda()-call to fix CPU-only inference

@wanghao9610
Copy link
Owner

@FSund, thank you for your PR. While the CPU implementation may not be compatible with OV-DINO, as the model requires significant work to support CPU execution, not just changing the "device" in BertEncoder. If you are able to fully complete the CPU implementation, I will be happy to merge it.

@FSund
Copy link
Author

FSund commented Aug 28, 2024

Inference seems to run fine on CPU here. Here is an example script

import numpy as np
import torch
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import instantiate
from detectron2.config import LazyConfig
from detectron2.data import transforms
from detectron2.utils.visualizer import ColorMode
from PIL import Image

class DefaultPredictor:
    def __init__(
        self,
        model,
        min_size_test=800,
        max_size_test=1333,
        img_format="RGB",
    ):
        self.model = model

        self.aug = transforms.ResizeShortestEdge(
            [min_size_test, min_size_test], max_size_test
        )

        self.input_format = img_format
        assert self.input_format in ["RGB", "BGR"], self.input_format

    def __call__(self, original_image, category_names):
        with torch.no_grad():  # https://github.com/sphinx-doc/sphinx/issues/4258
            # Apply pre-processing to image.
            if self.input_format == "RGB":
                # whether the model expects BGR inputs or RGB
                original_image = original_image[:, :, ::-1]  # RGB to BGR
            height, width = original_image.shape[:2]
            image = self.aug.get_transform(original_image).apply_image(original_image)
            image = torch.as_tensor(image.astype("float32").transpose(2, 0, 1))

            inputs = [
                {
                    "image": image,
                    "height": height,
                    "width": width,
                    "category_names": category_names,
                }
            ]
            predictions = self.model(inputs)[0]

            return predictions


if __name__ == "__main__":
    device = "cpu"  # "cuda" or "cpu"
    config_file = "./OV-DINO/ovdino/projects/ovdino/configs/models/ovdino_swin_tiny224_bert_base.py"
    init_checkpoint = "./weights/ovdino_swint_ogc-coco50.2_lvismv40.1_lvis32.9.pth"

    # Load configuration
    cfg = LazyConfig.load(config_file)
    cfg.model.device = device

    model = instantiate(cfg.model)
    checkpointer = DetectionCheckpointer(model)
    checkpointer.load(init_checkpoint)
    model.eval()

    model = model.to(device)

    instance_mode = ColorMode.IMAGE
    sam_predictor = None

    parallel = False  # TODO: remove this
    predictor = DefaultPredictor(
        model=model,
        min_size_test=800,
        max_size_test=1333,
        img_format="RGB",
    )

    image = Image.open("image.jpg")
    image = np.array(image)

    classes = [
        "person",
        "bicycle",
        "car",
        "motorcycle",
        "airplane",
        "bus",
        "train",
        "truck",
    ]
    predictions = predictor(image, classes)
    print(predictions)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants