获取运行时错误:configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml 在模型动物园中不可用!运行 Detectron2 进行物体检测时

问题描述 投票:0回答:1

想要通过 Detectron2 Faster_RCNN 模型训练自定义图像数据集。我在 Windows 操作系统中使用 wsl2 ubuntu 终端 和 VScode。在我的 train.py 中,我为 modelzoo.py 启动了一个带有“configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml”的 config_file_path。在 Model Zoo 目录 -> configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml 文件也可用,但发生此错误在此处输入图像描述

#train.py

import numpy as np
from detectron2.utils.logger import setup_logger

setup_logger()

from detectron2.data.datasets import register_coco_instances
from detectron2.engine import DefaultTrainer

import os
import pickle

from utils import *

config_file_path = "configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"
checkpoint_url = "configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"

output_dir = "./output/object_detection"
num_classes = 1

device = "cuda"

train_dataset_name = "LP_train"
train_image_path = "train"
train_json_annot_path = "train.json"

test_dataset_name = "LP_test"
test_image_path = "test"
test_json_annot_path = "test.json"

###############################################
register_coco_instances(name = train_dataset_name, metadata = {},
json_file=train_json_annot_path, image_root=train_image_path)

register_coco_instances(name=test_dataset_name, metadata={},json_file=test_json_annot_path, image_root=test_image_path)

#plot_samples(dataset_name= train_dataset_name, n = 2)

###############################################


def main():
    cfg = get_train_cfg(config_file_path, checkpoint_url, train_dataset_name, test_dataset_name, num_classes,device, output_dir)
    
    #saving cfg
    with open(cfg_save_path, 'wb') as f:
        pickle.dump(cfg, f, protocol= pickle.HIGHEST_PROTOCOL)
        
    os.makedirs(cfg.OUTPUT_DIR, exist_ok= True)
    
    trainer = DefaultTrainer(cfg)
    trainer.resume_or_load(resume= False)
    
    trainer.train()
    
if __name__ == '__main__':
    main( )

#utlis.py

from detectron2.data import DatasetCatalog, MetadataCatalog
from detectron2.utils.visualizer import Visualizer
from detectron2.config import get_cfg
from detectron2 import model_zoo

from detectron2.utils.visualizer import ColorMode

import random
import cv2
import matplotlib.pyplot as plt

def plot_samples(dataset_name, n=1):
    dataset_custom = DatasetCatalog.get(dataset_name)
    dataset_custom_metadata = MetadataCatalog.get(dataset_name)
    
    for s in random.sample(dataset_custom, n):
        img = cv2.imread(s["file_name"])
        v = Visualizer(img[:,:,::-1], metadata=dataset_custom_metadata, scale= 0.5)
        v = v.draw_dataset_dict(s)
        plt.figure(figsize=(15,20))
        plt.imshow(v.get_image())
        plt.show()
        #plt.savefig("matplotlib.png") #save config , don't show
        
def get_train_cfg(self,config_file_path, checkpoint_url, train_dataset_name, num_classes, device, output_dir):
    cfg = get_cfg()
    
    cfg.merge_from_file(model_zoo.get_config_file(config_file_path))
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(checkpoint_url)
    cfg.DATASETS.TRAIN = (train_dataset_name,)
    cfg.DATASETS.TEST = (train_dataset_name,)
    
    cfg.DATALOADER.NUM_WORKERS = 5
    
    cfg.SOLVER.IMS_PER_BATCH = 5
    cfg.SOLVER.BASE_LR = 0.00025
    cfg.SOLVER.MAX_ITER = 1000
    cfg.SOLVER.STEPS = []
    
    cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes
    cfg.MODEL.DEVICE = device
    cfg.OUTPUT_DIR = output_dir
    
    return cfg

我尝试改变路径 - config_file_path =“COCO-检测/faster_rcnn_R_50_FPN_3x.yaml” checkpoint_url = "COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml"

但发生了类似类型的错误在此处输入图像描述,顺便说一句,我是初学者,我期望通过从 Zoo 运行此预训练模型,我将构建用于对象识别的自定义图像数据集!

python data-science pre-trained-model faster-rcnn detectron
1个回答
0
投票

要解决此问题,您可以将 configs 文件夹从 detectorron2/configs 移至 detectorron2/Detectron2/model_zoo/。然后,使用以下行加载配置:cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) .它会起作用

© www.soinside.com 2019 - 2024. All rights reserved.