当前位置: 代码迷 >> 综合 >> 在Faster R-CNN VOC2012的基础上训练widerface错误解决(keras)
  详细解决方案

在Faster R-CNN VOC2012的基础上训练widerface错误解决(keras)

热度:72   发布时间:2023-11-14 14:27:04.0

1. Exception: ‘a’ cannot be empty unless no samples are taken

ValueError                                Traceback (most recent call last)
<ipython-input-40-9d45fdd55c03> in <module>59                     selected_neg_samples = np.random.choice(
---> 60                         neg_samples, num_rois - len(selected_pos_samples), replace=False).tolist()61                 except ValueError:mtrand.pyx in numpy.random.mtrand.RandomState.choice()ValueError: 'a' cannot be empty unless no samples are takenDuring handling of the above exception, another exception occurred:ValueError                                Traceback (most recent call last)
<ipython-input-40-9d45fdd55c03> in <module>61                 except ValueError:62                     selected_neg_samples = np.random.choice(
---> 63                         neg_samples, num_rois - len(selected_pos_samples), replace=True).tolist()64 65                 sel_samples = selected_pos_samples + selected_neg_samplesmtrand.pyx in numpy.random.mtrand.RandomState.choice()ValueError: 'a' cannot be empty unless no samples are taken

ValueError: 'a' cannot be empty unless no samples are taken这句话的意思是np.random.choice这个函数的第一个参数不能为空,说明neg_samples的值是一个空值。那么输出neg_samples,pos_samples的值。其中neg_samples代表的是负样本,pos_samples代表的是正样本。
print(neg_samples,pos_samples)
结果是

(array([], dtype=int64),) (array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),)

正样本的数量是10个,负样本的数量没有。由于刚刚开始训练,应该是负样本多于正样本。
训练widerface只有两个标签:背景类和脸,所以有很大可能是这两个类class_mapping搞反了。
手动直接将class_mapping改为class_mapping = {'bg': 1, 'face': 0}
虽然情况好转,但是还是偶尔会有负样本为空的情况,可以用捕获异常来跳过这一轮循环。

2.AxisError: axis 1 is out of bounds for array of dimension 1

     24         R = roi_helper.rpn_to_roi(P_rpn[0], P_rpn[1], K.image_dim_ordering(), use_regr=True, overlap_thresh=0.7, max_boxes=300)25         # note: calc_iou converts from (x1,y1,x2,y2) to (x,y,w,h) format
---> 26         X2, Y1, Y2, IouS = roi_helper.calc_iou(R, img_data, class_mapping)27 28         if X2 is None:roi_helper1.py in calc_iou(R, img_data, class_mapping)131     X = np.array(x_roi)132     Y1 = np.array(y_class_num)
--> 133     Y2 = np.concatenate([np.array(y_class_regr_label),np.array(y_class_regr_coords)],axis=1)134 135     return np.expand_dims(X, axis=0), np.expand_dims(Y1, axis=0), np.expand_dims(Y2, axis=0), IoUs<__array_function__ internals> in concatenate(*args, **kwargs)AxisError: axis 1 is out of bounds for array of dimension 1

这是由于在一定情况下y_class_regr_label,y_class_regr_coords是一维数组(比如shape为(10,)),而concatenate合并函数的参数axis=1,超出了维度,所以将一维数组改为二维(比如shape为(10,1))。在roi_helpers.py文件中的calc_iou函数的最后面改为:

	if len(x_roi) == 0:return None, None, None, NoneX = np.array(x_roi)Y1 = np.array(y_class_num)k = np.array(y_class_regr_label)v = np.array(y_class_regr_coords)try:k1 = np.array(y_class_regr_label)# 当数组为一维时,下面代码报错,进入exceptk_shape0 = k1.shape[1]except:k_shape0 = k.shape[0]v_shape0 = v.shape[0]k = k.reshape((k_shape0,1))v = v.reshape((v_shape0,1))Y2 = np.concatenate([k,v],axis=1)return np.expand_dims(X, axis=0), np.expand_dims(Y1, axis=0), np.expand_dims(Y2, axis=0), IoUs
  相关解决方案