Detectron2加载PointRend模型时出现pixel_mean/std未使用警告且检测终止
解决Detectron2加载PointRend模型时的
pixel_mean/pixel_std未使用警告问题 问题场景
运行基于Detectron2的短视频移动物体检测代码时,突然出现以下警告且检测终止:
D:\detectron2>python motion_detection_2.py [02/27 12:39:29 d2.checkpoint.detection_checkpoint]: [DetectionCheckpointer] Loading from detectron2://PointRend/InstanceSegmentation/pointrend_rcnn_X_101_32x8d_FPN_3x_coco/28119989/model_final_ba17b9.pkl ... WARNING [02/27 12:39:30 d2.projects.point_rend.mask_head]: Weight format of PointRend models have changed! Applying automatic conversion now ... The checkpoint state_dict contains keys that are not used by the model: pixel_mean pixel_std
确认detection_checkpoint.py第129行处理pixel_mean和pixel_std的代码已执行,但问题仍存在。
解决方案
1. 手动过滤不需要的权重键
加载模型前,手动从checkpoint的state_dict中移除这两个冗余键,再加载至模型:
from detectron2.checkpoint import DetectionCheckpointer from detectron2.config import get_cfg from detectron2.projects import point_rend from detectron2.modeling import build_model # 初始化配置 cfg = get_cfg() point_rend.add_pointrend_config(cfg) # 补充你的模型配置(如数据集、推理参数等) cfg.MODEL.WEIGHTS = "detectron2://PointRend/InstanceSegmentation/pointrend_rcnn_X_101_32x8d_FPN_3x_coco/28119989/model_final_ba17b9.pkl" # 构建模型 model = build_model(cfg) checkpointer = DetectionCheckpointer(model) # 加载checkpoint并移除冗余键 checkpoint = checkpointer._load_file(cfg.MODEL.WEIGHTS) if "model" in checkpoint: for key in ["pixel_mean", "pixel_std"]: checkpoint["model"].pop(key, None) # 加载处理后的权重 checkpointer._load_model(checkpoint)
2. 显式配置预处理参数
在配置中直接指定pixel_mean和pixel_std,让模型从配置获取参数而非checkpoint:
# 在配置初始化后添加以下内容 cfg.MODEL.PIXEL_MEAN = [103.530, 116.280, 123.675] # COCO数据集默认值 cfg.MODEL.PIXEL_STD = [57.375, 57.120, 58.395]
这样模型会优先使用配置中的参数,忽略checkpoint里的对应键。
3. 临时禁用警告(应急方案)
如果上述方法无效,可临时禁用该模块的警告输出:
import logging # 将检测检查点模块的日志级别设为ERROR,屏蔽警告 logging.getLogger("detectron2.checkpoint.detection_checkpoint").setLevel(logging.ERROR)
内容的提问来源于stack exchange,提问作者dkloska
相关产品推荐
相关产品推荐

