解决:YOLOv3-TypeError: buffer is too small for requested array

计划做一个图片检测的模型训练,但是数据集只有图片的分类标签,没有特征的bounding box,准备用预训练的YOLO3模型提取图片的bounding box,然后对提取结果进行筛选,制作完整的图片检测数据集。

keras版本的代码下载地址:https://github.com/qqwweee/keras-yolo3

Yolo官网地址:https://pjreddie.com/darknet/yolo/

第一步:在github上下载YOLO3的代码,代码用的是keras框架,而YOLO官网是使用darknet训练的,模型保存在yolov3.weight,为了在keras框架中使用YOLO3模型,首先需要将模型权重文件转换为.h5格式的文件,github上提供了转换代码convert.py和转换指令:

python convert.py yolov3.cfg yolov3.weights model_data/yolo.h5

问题:执行后报错:TypeError: buffer is too small for requested array

Parsing section convolutional_1
conv2d bn leaky (3, 3, 32, 64)
Parsing section convolutional_2
conv2d bn leaky (1, 1, 64, 32)
Parsing section convolutional_3
conv2d bn leaky (3, 3, 32, 64)
Parsing section shortcut_0
Parsing section convolutional_4
conv2d bn leaky (3, 3, 64, 128)
Parsing section convolutional_5
conv2d bn leaky (1, 1, 128, 64)
Parsing section convolutional_6
conv2d bn leaky (3, 3, 64, 128)
Parsing section shortcut_1
Parsing section convolutional_7
conv2d bn leaky (1, 1, 128, 64)
Parsing section convolutional_8
conv2d bn leaky (3, 3, 64, 128)
Parsing section shortcut_2
Parsing section convolutional_9
conv2d bn leaky (3, 3, 128, 256)
Traceback (most recent call last):
  File "convert.py", line 263, in <module>
    _main(parser.parse_args())
  File "convert.py", line 144, in _main
    buffer=weights_file.read(weights_size * 4))
TypeError: buffer is too small for requested array

解决:在网上找了一些方法,试了之后没有解决,我的文件转换了一部分之后停止了,说明前半部分转换正常,后半本分有问题了,查看了一下我的yolov3.weights文件,发现只有2.3M,而官网的文件有200多M,重新下载了权重文件,再次运行指令就正常了。(这只是我遇到的一种解决方案,如果没解决可以看看github,有很多类似的问题)

外网下载较慢,我这里分享一个百度网盘的链接供大家使用:
https://pan.baidu.com/s/1bMzehqJD2R6u9S0jmi3dsg 密码: e16k

运行效果:

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, None, None, 3 0                                            
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, None, None, 3 864         input_1[0][0]                    
__________________________________________________________________________________________________
batch_normalization_1 (BatchNor (None, None, None, 3 128         conv2d_1[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_1 (LeakyReLU)       (None, None, None, 3 0           batch_normalization_1[0][0]      
__________________________________________________________________________________________________
zero_padding2d_1 (ZeroPadding2D (None, None, None, 3 0           leaky_re_lu_1[0][0]              
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, None, None, 6 18432       zero_padding2d_1[0][0]           
__________________________________________________________________________________________________
batch_normalization_2 (BatchNor (None, None, None, 6 256         conv2d_2[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_2 (LeakyReLU)       (None, None, None, 6 0           batch_normalization_2[0][0]      
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, None, None, 3 2048        leaky_re_lu_2[0][0]              
__________________________________________________________________________________________________
batch_normalization_3 (BatchNor (None, None, None, 3 128         conv2d_3[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_3 (LeakyReLU)       (None, None, None, 3 0           batch_normalization_3[0][0]      
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, None, None, 6 18432       leaky_re_lu_3[0][0]              
__________________________________________________________________________________________________
batch_normalization_4 (BatchNor (None, None, None, 6 256         conv2d_4[0][0]                   
__________________________________________________________________________________________________
leaky_re_lu_4 (LeakyReLU)       (None, None, None, 6 0           batch_normalization_4[0][0]      
__________________________________________________________________________________________________
add_1 (Add)                     (None, None, None, 6 0           leaky_re_lu_2[0][0]              
                                                                 leaky_re_lu_4[0][0]              
__________________________________________________________________________________________________

为了实现等价功能,可以使用下列代码: ```python def edge_detection(image, type): if type == 'roberts': roberts_x = np.array([[-1, 0], [0, 1]]) roberts_y = np.array([[0, -1], [1, 0]]) gradient_x_roberts = conv2d(image, roberts_x) gradient_y_roberts = conv2d(image, roberts_y) edges_roberts = np.abs(gradient_x_roberts) + np.abs(gradient_y_roberts) edges_roberts = np.uint8(edges_roberts) return edges_roberts elif type == 'prewitt': prewitt_x = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) prewitt_y = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]) gradient_x_prewitt = conv2d(image, prewitt_x) gradient_y_prewitt = conv2d(image, prewitt_y) edges_prewitt = np.abs(gradient_x_prewitt) + np.abs(gradient_y_prewitt) edges_prewitt = np.uint8(edges_prewitt) return edges_prewitt elif type == 'sobel': sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) sobel_y = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) gradient_x_sobel = conv2d(image, sobel_x) gradient_y_sobel = conv2d(image, sobel_y) edges_sobel = np.abs(gradient_x_sobel) + np.abs(gradient_y_sobel) edges_sobel = np.uint8(edges_sobel) return edges_sobel elif type == 'canny': edges = cv2.Canny(image, threshold1=50, threshold2=100) return edges else: raise NotImplementedError ``` 主要的改动在于使用了自定义的 `conv2d` 函数替换了原来的 `cv2.filter2D` 函数。由于 `cv2.filter2D` 函数的实现方式与 `conv2d` 函数有所不同,因此替换后需要重新计算梯度,并对梯度进行绝对值处理和类型转换。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值