Skip to main content
 首页 » 编程设计

python之Tensorflow 训练错误

2025年02月15日29mengfanrong

我正在尝试在另一个基于 Pascal VOC 格式的数据集上运行 faster_rcnn。但这就是训练的结果:

在如下所示的警告之后,损失值全部转到nan:

proposal_layer_tf.py:150: RuntimeWarning: invalid value encountered in greater_equal keep = np.where((ws >= min_size) & (hs >= min_size))[0]

这是 proposal_layer_tf.py 的第 146-151 行:

def _filter_boxes(boxes, min_size): 
    """Remove all boxes with any side smaller than min_size.""" 
    ws = boxes[:, 2] - boxes[:, 0] + 1 
    hs = boxes[:, 3] - boxes[:, 1] + 1 
    keep = np.where((ws >= min_size) & (hs >= min_size))[0] 
return keep 

如您所见,总损失值以一种奇怪的方式变化,在警告之后它变成了 nan。我该怎么做才能让它正确?

(GPU:Geforce 940m)

请您参考如下方法:

问题可能是由您的注释引起的。在 Faster-RCNN 实现中,当他们将边界框加载到数据框中时,他们将坐标 x1,y1,x2,y2 减去 1 使其为 0-基于。在我的例子中,我创建了自己的 xml 注释,它们已经是基于 0 的。因此,如果我运行默认的 Faster-RCNN 实现,从 0 中减去 1 会导致下溢错误。所以删除那个减法解决了我的问题。

您可以删除 pascal_voc.py 中的减法或编辑您的注释以使其从 1 开始。如果您选择编辑 pascal_voc.py 文件,请转到此处:

def _load_pascal_annotation(self, index): 
 
    # ... 
    # ... 
    # ... 
 
    # Load object bounding boxes into a data frame. 
    for ix, obj in enumerate(objs): 
        bbox = obj.find('bndbox') 
        # Make pixel indexes 0-based 
        x1 = float(bbox.find('xmin').text) #- 1 <- comment these out 
        y1 = float(bbox.find('ymin').text) #- 1 
        x2 = float(bbox.find('xmax').text) #- 1 
        y2 = float(bbox.find('ymax').text) #- 1 
 
    # ... 
    # ... 
    # ...