训练好自己的数据集后,在运行eval.py时遇到了一些问题,现已解决。问题及解决方法如下:
1.RuntimeError: Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method.
这个问题在这篇文章【调试】错误 RuntimeError: Legacy autograd function with non-static forward method is deprecated._y = net(xx)-CSDN博客里介绍的比较详细,主要是因为Detect类继承自Function类,在pytorch1.3及以后的版本需要规定forward方法为静态方法,所以在pytorch1.3以上的版本执行出错。为了避免重装新的pytorch版本,对代码进行了相应修改。文章中介绍的比较详细,且改正有效。
但是代码并不像文章中那样能够运行成功,而是遇到了下面这个问题⬇
2.UserWarning: An output with one or more elements was resized since it had shape [125], which does not match the required output shape [120].
问题出在box_utils.py中的
torch.index_select(x1, 0, idx, out=xx1)
torch.index_select(y1, 0, idx, out=yy1)
torch.index_select(x2, 0, idx, out=xx2)
torch.index_select(y2, 0, idx, out=yy2)
torch.index_select(input, dim, index, out=None)
函数是PyTorch中的一个函数,用于在指定维度上选取指定索引位置的元素。其中,input
是输入的Tensor,dim
是要选取的维度,index
是要选取的索引,out
是输出的Tensor,可选参数。如果out
没有被指定,那么函数会新建一个Tensor来存储结果。在这个例子中,torch.index_select(x1, 0, idx, out=xx1)
的作用是在x1
的第0维上选取idx
中指定的索引位置的元素,并将结果存储在xx1
中。
将其修改为:
# torch.index_select(x1, 0, idx, out=xx1)
# torch.index_select(y1, 0, idx, out=yy1)
# torch.index_select(x2, 0, idx, out=xx2)
# torch.index_select(y2, 0, idx, out=yy2)
xx1 = torch.index_select(x1, 0, idx)
yy1 = torch.index_select(y1, 0, idx)
xx2 = torch.index_select(x2, 0, idx)
yy2 = torch.index_select(y2, 0, idx)
问题解决。我猜测是因为该方法在新版本的pytorch中已经不再能够调整输出的大小,导致形状与所需输出的形状不匹配。如果不指定out,则会新建一个Tensor来存储结果,这样再赋值给xx1、yy1、xx2、yy2就不会再报错了。
3.FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
调试中发现是文件路径问题,打印以下两个文件路径,神奇的发现一个可以获取到路径一个获取不到
annopath = os.path.join(args.fire_root, 'Annotations', '%s.xml')
imgsetpath = os.path.join(args.fire_root, 'ImageSets', 'Main', '{:s}.txt')
print(imgsetpath.format('test')) #输出test.txt
print(annopath) #输出整个路径
原因可能是{:s}.txt导致路径无法拼接出来,将该行代码改为:
imgsetpath = os.path.join(args.fire_root, 'ImageSets', 'Main') + os.sep + '{:s}.txt'
人为引入一个路径分隔符,此时能够输出完整的路径了
4.AttributeError: module 'numpy' has no attribute 'bool'.
方法一:降低numpy版本
方法二:将np.bool改为np.bool_
difficult = np.array([x['difficult'] for x in R]).astype(np.bool_)
5.RuntimeWarning: invalid value encountered in divide
rec = tp / float(npos)
这是由于在计算时可能出现了分母为0的情况,使用np.seterr()函数
np.seterr(divide="ignore", invalid="ignore")
6.DeprecationWarning: elementwise comparison failed; this will raise an error in the future. if dets == []:
将if dets == []:修改为if len(dets) == 0:
再次感谢评论区友友的热情帮助♥♥♥
7.当只有一个类别(fire)时发现输出文件如下图所示
在fire.py中修改
#FIRE_CLASSES = ('fire')
FIRE_CLASSES = ('fire',)
在eval.py中修改
# from data import FIRE_CLASSES as labelmap
from data import FIRE_CLASSES
labelmap = list(FIRE_CLASSES)
这样输出的就为一类了