运行YOLOv5多光谱检测项目train.py遇TypeError报错求助
解决YOLOv5多光谱训练中TensorBoard的
np.greater类型错误 问题原因
报错出现在TensorBoard的make_histogram函数中,np.greater(counts, 0, dtype=np.int32)执行时类型不匹配。这通常是因为counts数组的类型(比如uint64)和指定的dtype=np.int32无法通过ufunc直接转换,或是numpy新版本对类型兼容性的处理更严格导致的。
解决方案
方案1:修改TensorBoard的make_histogram函数代码
找到报错路径下的summary.py文件(路径:C:\Users\hzji1127.conda\envs\multispectral-object-detection\lib\site-packages\torch\utils\tensorboard\summary.py),定位到make_histogram函数中的这一行:
cum_counts = np.cumsum(np.greater(counts, 0, dtype=np.int32))
替换为以下两种写法之一:
- 先转换
counts的类型再比较:cum_counts = np.cumsum(np.greater(counts.astype(np.int32), 0)) - 先执行比较再转换类型:
cum_counts = np.cumsum(np.greater(counts, 0).astype(np.int32))
方案2:降级numpy版本
如果不想修改库文件,可以尝试降级numpy到1.21.x或1.22.x版本,这些版本对ufunc的类型兼容性处理更宽松:
pip install numpy==1.21.6
方案3:临时绕过TensorBoard直方图记录
如果暂时不需要类别直方图的TensorBoard记录,可以直接注释掉train.py中报错的那一行(第647行):
# tb_writer.add_histogram('classes', c, 0)
注释后训练可以正常进行,后续需要再恢复即可。
内容的提问来源于stack exchange,提问作者Poca
相关产品推荐
相关产品推荐

