使用Detectron2预训练模型时触发NotImplementedError问题求助
使用Detectron2预训练模型时触发NotImplementedError
执行以下代码初始化Detectron2的DefaultPredictor时出现NotImplementedError:
安装与导入代码
import torch torch.__version__ import torchvision #torchvision.__version__ !pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.7/index.html
import detectron2 from detectron2.utils.logger import setup_logger setup_logger() import numpy as np import os, json, cv2, random import matplotlib.pyplot as plt %matplotlib inline from detectron2 import model_zoo from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog, DatasetCatalog from detectron2.structures import BoxMode
配置与初始化代码
cfg = get_cfg() cfg.merge_from_file(model_zoo.get_config_file("COCOInstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCOInstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
predictor = DefaultPredictor(cfg)
错误信息
--------------------------------------------------------------------------- NotImplementedError Traceback (most recent call last) <ipython-input-27-699e754fc9df> in <module> ----> 1 predictor = DefaultPredictor(cfg) 4 frames /usr/local/lib/python3.8/dist-packages/iopath/common/file_io.py in _isfile(self, path, **kwargs) 438 bool: true if the path is a file 439 """ --> 440 raise NotImplementedError() 441 442 def _isdir(self, path: str, **kwargs: Any) -> bool: NotImplementedError:
解决方法
升级iopath库:该错误通常由iopath版本不兼容导致,执行以下命令升级:
pip install --upgrade iopath确保版本匹配:检查PyTorch、CUDA与Detectron2版本是否对应。当前使用的是CUDA10.2+PyTorch1.7,需确保安装的Detectron2轮子适配该环境,版本不匹配会引发依赖问题。
手动加载本地权重:若自动下载权重出现问题,可手动下载后本地加载:
- 下载权重:
wget https://dl.fbaipublicfiles.com/detectron2/COCOInstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl - 修改配置中的权重路径:
cfg.MODEL.WEIGHTS = "./model_final_f10217.pkl"
- 下载权重:
重新安装Detectron2:若上述方法无效,彻底卸载后重装对应版本:
pip uninstall detectron2 -y pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.7/index.html --force-reinstall
内容的提问来源于stack exchange,提问作者Keshav Khanal
相关产品推荐
相关产品推荐

