将数据加载到CNN表单OpenCV

时间:2018-05-09 08:25:01

标签: python machine-learning keras artificial-intelligence conv-neural-network

我将带有OpenCV的视频文件中的帧加载到数组中,并使用sklearn将数据拆分为X_trainX_test

我的X_train.shape(363, 1, 40, 40, 15),目前我正在使用4个班级,而我用来学习这些数据的模型编码如下:

    model = Sequential()
    model.add(Conv3D(32, (3,3,3), activation='relu', input_shape=(1, 40, 40, 15), data_format='channels_first'))
    model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
    model.add(Conv3D(64, (3,3,3), activation='relu'))
    model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
    model.add(Conv3D(128, (3,3,3), activation='relu'))
    model.add(Conv3D(128, (3,3,3), activation='relu'))
    model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))
    model.add(Conv3D(256, (2,2,2), activation='relu'))
    model.add(Conv3D(256, (2,2,2), activation='relu'))
    model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))

    model.add(Flatten())

    model.add(Dense(1024))
    model.add(Dropout(0.5))
    model.add(Dense(1024))
    model.add(Dropout(0.5))
    model.add(Dense(4, activation='softmax'))

我尝试加载模型时收到此错误:

ValueError: Negative dimension size caused by subtracting 2 from 1 for 'conv3d_44/convolution' (op: 'Conv3D') with input shapes: [?,25,1,1,256], [2,2,2,256,256].

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

在StackOverflow上已经多次讨论过:请参阅herehere。根据卷积和池化层参数,在每个Conv3DMaxPooling3D之后对张量进行下采样。以下是模型在断开之前的样子:

Layer (type)                 Output Shape              Param #   
=================================================================
conv3d_1 (Conv3D)            (None, 32, 38, 38, 13)    896       
_________________________________________________________________
max_pooling3d_1 (MaxPooling3 (None, 32, 19, 19, 13)    0         
_________________________________________________________________
conv3d_2 (Conv3D)            (None, 30, 17, 17, 64)    22528     
_________________________________________________________________
max_pooling3d_2 (MaxPooling3 (None, 30, 8, 8, 64)      0         
_________________________________________________________________
conv3d_3 (Conv3D)            (None, 28, 6, 6, 128)     221312    
_________________________________________________________________
conv3d_4 (Conv3D)            (None, 26, 4, 4, 128)     442496    
_________________________________________________________________
max_pooling3d_3 (MaxPooling3 (None, 26, 2, 2, 128)     0         
_________________________________________________________________
conv3d_5 (Conv3D)            (None, 25, 1, 1, 256)     262400    

张量(None, 25, 1, 1, 256)不能进一步下采样,因此 错误。

解决方案是调整Conv3D参数:使用padding='same'(在这种情况下,张卷形状在卷积后保留,并且仅在合并图层后减半)或从{{减少滤镜大小1}}到3

示例:

2