在进行Faster R-CNN或Mask R-CNN模型的参数量和FLOPs计算时,采用以下代码:
def compute_model(model):
# from ptflops import get_model_complexity_info
from mmcv.cnn.utils.flops_counter import get_model_complexity_info
if hasattr(model, 'forward_dummy'):
model.forward = model.forward_dummy
else:
raise NotImplementedError(
'FLOPs counter is currently not currently supported with {}'.
format(model.__class__.__name__))
ops, params = get_model_complexity_info(model, (3, 1280, 800), as_strings=True,
print_per_layer_stat=True, flush=True)
print("The model paramater:", params)
print("The model flops:", ops)
会出现报错:
RuntimeError: roi_width >= 0 && roi_height >= 0 INTERNAL ASSERT FAILED at "/tmp/mmcv/mmcv/ops/csrc/pytorch/cpu/roi_align.cpp":138, please report a bug to PyTorch. ROIs in ROIAlign cannot have non-negative size!
在全网搜索了很久,还是没有找到非常完整且合适的解决方案。最后还是自己分析,自己解决吧!!!
以下是解决过程:
很明显是计算ROI时,出现了负值, 需要重新安装mmcv
首先安装mmengine:
pip install -i mmengine==0.2.0 http://pypi.douban.com/simple --trusted-host pypi.douban.com
其次,下载mmcv源码(这里建议下载1.x版本https://github.com/open-mmlab/mmcv/tree/1.x,暂时不要下载2.0以上版本,版本变化差异很大):
git clone https://github.com/open-mmlab/mmcv.git
修改mmcv/mmcv/ops/csrc/pytorch/cpu/roi_align.cpp,添加以下内容,即修改负值情况:
if (roi_width < 0) roi_width = 0;
if (roi_height < 0) roi_height = 0;
如下图所示:
重新安装mmcv:
cd mmcv;
MMCV_WITH_OPS=1 pip install -e . -v
--》》》安装时间可能会比较久,耐心等待即可~~
安装成功,最后测试发现问题已解决,不再报错!!!!!