Skip to content
The Chat Archive
claude-3-5-haiku-20241022· Anthropic LMArena Human Preference 140K

Can I do it this way: from detectron2.data import DatasetCatalog, MetadataCatalog from…

  1. User

    Can I do it this way: from detectron2.data import DatasetCatalog, MetadataCatalog from datasets import load_dataset, DatasetDict import numpy as np

    hf_dataset_path ="OneFormer/datasets/hf_furnace_tubes" def register_huggingface_dataset(dataset_name, hf_dataset_name, split="train"): # Load dataset from HuggingFace # dataset = load_dataset(hf_dataset_name, split=split) dataset = DatasetDict.load_from_disk(hf_dataset_path)

    def get_data_dicts():
        data_dicts = []
        for idx, example in enumerate(dataset):
            # Adapt this to match your dataset structure
            record = {
                "file_name": example["image_path"] if "image_path" in example else example["image"],
                "image_id": idx,
                "height": example["height"] if "height" in example else example["image"].height,
                "width": example["width"] if "width" in example else example["image"].width,
            }
            
            # Add annotations - adjust based on your dataset format
            annotations = []
            for mask, category_id in zip(example["masks"], example["category_ids"]):
                # Convert mask to binary if needed
                binary_mask = mask if isinstance(mask, np.ndarray) else np.array(mask)
                
                # Find contours for segmentation
                import cv2
                contours, _ = cv2.findContours(
                    binary_mask.astype(np.uint8), 
                    cv2.RETR_EXTERNAL, 
                    cv2.CHAIN_APPROX_SIMPLE
                )
                
                # Convert contours to COCO format
                segmentation = []
                for contour in contours:
                    contour = contour.flatten().tolist()
                    if len(contour) > 4:  # Valid polygons have at least 3 points
                        segmentation.append(contour)
                
                if segmentation:
                    annotations.append({
                        "bbox": cv2.boundingRect(binary_mask.astype(np.uint8)),
                        "bbox_mode": 0,  # XYXY_ABS
                        "segmentation": segmentation,
                        "category_id": category_id,
                    })
            
            record["annotations"] = annotations
            data_dicts.append(record)
        
        return data_dicts
    
    # Register dataset
    DatasetCatalog.register(dataset_name, get_data_dicts)
    
    # Register metadata (adjust class names based on your dataset)
    class_names = dataset.features["category_ids"].feature.names if "category_ids" in dataset.features else ["class_0", "class_1", ...]
    MetadataCatalog.get(dataset_name).set(
        thing_classes=class_names,
        evaluator_type="coco"
    )
    

    Register your datasets

    register_huggingface_dataset("my_dataset_train", "your/huggingface_dataset_name", split="train") register_huggingface_dataset("my_dataset_val", "your/huggingface_dataset_name", split="validation")

  2. Assistant

    Your approach is close, but there are a few modifications needed when using DatasetDict.load_from_disk():

    from detectron2.data import DatasetCatalog, MetadataCatalog
    from datasets import DatasetDict
    import numpy as np
    
    hf_dataset_path = "OneFormer/datasets/hf_furnace_tubes"
    def register_huggingface_dataset(dataset_name, hf_dataset_path, split="train"):
        # Load dataset from disk
        dataset = DatasetDict.load_from_disk(hf_dataset_path)
        
        # Select the specific split
        dataset = dataset[split]
    
        def get_data_dicts():
            data_dicts = []
            for idx, example in enumerate(dataset):
                record = {
                    "file_name": example["image_path"] if "image_path" in example else example["image"],
                    "image_id": idx,
                    "height": example["height"] if "height" in example else example["image"].height,
                    "width": example["width"] if "width" in example else example["image"].width,
                }
                
                annotations = []
                for mask, category_id in zip(example["masks"], example["category_ids"]):
                    binary_mask = mask if isinstance(mask, np.ndarray) else np.array(mask)
                    
                    import cv2
                    contours, _ = cv2.findContours(
                        binary_mask.astype(np.uint8), 
                        cv2.RETR_EXTERNAL, 
                        cv2.CHAIN_APPROX_SIMPLE
                    )
                    
                    segmentation = []
                    for contour in contours:
                        contour = contour.flatten().tolist()
                        if len(contour) > 4:  # Valid polygons have at least 3 points
                            segmentation.append(contour)
                    
                    if segmentation:
                        annotations.append({
                            "bbox": cv2.boundingRect(binary_mask.astype(np.uint8)),
                            "bbox_mode": 0,  # XYXY_ABS
                            "segmentation": segmentation,
                            "category_id": category_id,
                        })
                
                record["annotations"] = annotations
                data_dicts.append(record)
            
            return data_dicts
        
        # Register dataset
        DatasetCatalog.register(dataset_name, get_data_dicts)
        
        # Register metadata
        class_names = dataset.features["category_ids"].feature.names
        MetadataCatalog.get(dataset_name).set(
            thing_classes=class_names,
            evaluator_type="coco"
        )
    
    # Register your datasets
    register_huggingface_dataset("my_dataset_train", hf_dataset_path, split="train")
    register_huggingface_dataset("my_dataset_val", hf_dataset_path, split="validation")
    

    Key changes:

    1. Use dataset[split] to select the specific split
    2. Directly use dataset.features["category_ids"].feature.names for class names
    3. Passed hf_dataset_path instead of a HuggingFace dataset name

    A few things to double-check:

    • Ensure your dataset has the expected keys: "image_path", "masks", "category_ids"
    • Verify the structure of your saved dataset matches the code assumptions
    • Make sure you have OpenCV (cv2) installed for contour processing

    Would you like me to elaborate on any part of the code?

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.