region:us
---
{}
---
# Detectron2 Instance Segmentation Model

This repository contains a Detectron2 model for instance segmentation. The model is a GeneralizedRCNN with a build_resnet_fpn_backbone backbone.

## Model Details

- **Architecture**: GeneralizedRCNN
- **Backbone**: build_resnet_fpn_backbone
- **Classes**: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
- **Training Dataset**: coco_2017_train

## Usage with Detectron2

```python
import detectron2
from detectron2.config import get_cfg
from detectron2.modeling import build_model
from detectron2.checkpoint import DetectionCheckpointer
import torch
import json

# Set up configuration
cfg = get_cfg()
with open("config.json", "r") as f:
    cfg_dict = json.load(f)
    cfg.merge_from_dict(cfg_dict)

# Build model
model = build_model(cfg)

# Load weights
checkpointer = DetectionCheckpointer(model)
checkpointer.load("model.pth")

# Set model to evaluation mode
model.eval()

# For inference
from detectron2.engine import DefaultPredictor
predictor = DefaultPredictor(cfg)

# Load an image
import cv2
image = cv2.imread("your_image.jpg")
outputs = predictor(image)
```

## Sample Visualization Code

```python
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import cv2

# Load class metadata
with open("metadata.json", "r") as f:
    metadata_dict = json.load(f)
    
# Create metadata
metadata = MetadataCatalog.get("inference")
metadata.thing_classes = metadata_dict["thing_classes"]

# Visualize predictions
v = Visualizer(image[:, :, ::-1], metadata=metadata, scale=1.2)
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
cv2.imwrite("output.jpg", out.get_image()[:, :, ::-1])
```

## Model Card for sajabdoli/detectron2-instance-segmentation

This model is a Detectron2 implementation of instance segmentation. It can detect and segment objects in images.
