层预期的ndim = 4处出现不兼容的值错误,找到的ndim = 5

时间:2018-09-17 18:02:15

标签: python-3.x tensorflow keras

我正在使用Faster-RCNN实现对象检测  出现错误:

ValueError: Input 0 is incompatible with layer res5a_branch2a: expected ndim=4, found ndim=5

用于以下网络设计

num_rois=4
roi_input = Input(shape=(num_rois, 4))
out_roi_pool = RoiPoolingConv(14, 3)([model2.output, roi_input])

reference    RoiPoolingConv是用户定义的函数,并且是out_roi_pool的输出

<tf.Tensor 'roi_pooling_conv_49/transpose:0' shape=(1, 3, 14, 14, 2048) 
dtype=float32>


pooling_regions = 14 #Size of pooling region
num_rois=4           #number of regions of interest
input_shape = (num_rois,14,14,1024)
nb_filter1, nb_filter2, nb_filter3 = [512,512,2048]
old_layer = TimeDistributed(Convolution2D(nb_filter1, (1, 1), strides=(1,1), 
trainable=False, kernel_initializer='normal'),input_shape=out_roi_pool.shape, name='2b')(out_roi_pool )

已引用question link,但仍无法解决错误。

source TimeDistributed 任何线索都非常感激.. !!

2 个答案:

答案 0 :(得分:1)

您可以在基础模型的末尾添加密集层

即直接提供RoiPoolingConv函数之前的model2.output

 x = Dense(1024, name='avg_pool')(model2.layers[-1].output)
 in_img = model2.input
 new_model = Model(input=in_img, output=[x])
 new_model.summary()
 out_roi_pool = RoiPoolingConv(14, 3)([new_model.output, roi_input])

或者您已经像github project

那样建立了模型来相应地输入形状

答案 1 :(得分:0)

发布答案以解决可能或将陷入尺寸不匹配错误的人

num_rois=4
roi_input = Input(shape=(num_rois, 4))
out_roi_pool = RoiPoolingConv(14, 3)([model2.output, roi_input])

RoiPoolingConv是用户定义的函数,并重新定义out_roi_pool的输出 现在输出将是

<tf.Tensor 'roi_pooling_conv_49/transpose:0' shape=(1, 3, 14, 14,1024) dtype=float32>
pooling_regions = 14 #Size of pooling region
num_rois=4           #number of regions of interest
input_shape = (num_rois,14,14,1024)
nb_filter1, nb_filter2, nb_filter3 = [512,512,1024]
old_layer = TimeDistributed(Convolution2D(nb_filter1, (1, 1), strides=(1,1), 
trainable=False, 
kernel_initializer='normal'),input_shape=out_roi_pool.shape, name='2b') 
(out_roi_pool )

这解决了我的错误