如何在不训练新模型的情况下合并COCO数据集类别进行评估?
Got it, you don't need to retrain your model at all—we just need to tweak the evaluation pipeline to map the three vehicle categories into one during the evaluation step. Here's how to do it step by step:
Step 1: Define Category Mapping
First, confirm the COCO dataset IDs for the classes you want to merge:
car→ ID 3truck→ ID 8bus→ ID 6
Create a mapping dictionary that maps these original IDs to a single new ID for your vehicles category (we'll use 0 here for simplicity):
category_mapping = { 3: 0, # Map car to vehicles 8: 0, # Map truck to vehicles 6: 0 # Map bus to vehicles }
If you want to keep other COCO classes in your evaluation, just add their original IDs mapped to new contiguous IDs (e.g., 1:1 for person).
Step 2: Customize COCOEvaluator
We'll create a subclass of COCOEvaluator that overrides two key parts: updating the category metadata (so the evaluator knows about your new vehicles class) and remapping prediction category IDs before generating evaluation results.
from detectron2.evaluation import COCOEvaluator from detectron2.data import MetadataCatalog class MergedCategoryCOCOEvaluator(COCOEvaluator): def __init__(self, dataset_name, cfg, distributed, output_dir, category_mapping, merged_category_name="vehicles"): super().__init__(dataset_name, cfg, distributed, output_dir) self.category_mapping = category_mapping # Update metadata to use our merged category self._metadata = MetadataCatalog.get(dataset_name) # Set the list of classes (adjust this if you're keeping other classes) self._metadata.thing_classes = [merged_category_name] # Map the new category ID to its name self._metadata.thing_dataset_id_to_contiguous_id = { idx: cls for idx, cls in enumerate(self._metadata.thing_classes) } def _predictions_to_coco_json(self, predictions, img_id): # Get original COCO-format predictions from the parent class coco_results = super()._predictions_to_coco_json(predictions, img_id) # Remap category IDs and filter out unneeded classes merged_results = [] for res in coco_results: if res["category_id"] in self.category_mapping: res["category_id"] = self.category_mapping[res["category_id"]] merged_results.append(res) return merged_results
Step 3: Use the Custom Evaluator in Your Pipeline
Replace the original COCOEvaluator with our custom one, and run inference as usual:
from detectron2.evaluation import inference_on_dataset from detectron2.data import build_detection_test_loader from detectron2.modeling import build_model from detectron2.checkpoint import DetectionCheckpointer # Load your model (same as before) model = build_model(cfg) DetectionCheckpointer(model).load(weights_path) # Initialize our custom evaluator evaluator = MergedCategoryCOCOEvaluator( "testsetPre_val", cfg, False, output_dir="./output/", category_mapping=category_mapping, merged_category_name="vehicles" ) # Run evaluation val_loader = build_detection_test_loader(cfg, "testsetPre_val") inference_on_dataset(model, val_loader, evaluator)
How This Works
- Your model still outputs predictions using the original COCO 80-class IDs—no changes needed to the model itself.
- The custom evaluator takes those predictions, remaps the relevant category IDs to your new
vehiclesclass, and filters out any predictions from classes you don't care about (adjust the mapping if you want to keep others). - The updated metadata tells the evaluator to compute metrics only for your merged
vehiclescategory, so the final output will show evaluation results just for that class instead of all 80.
内容的提问来源于stack exchange,提问作者appleUserK

