How to have a 3D filter for Conv2D in Keras?

大城市里の小女人 提交于 2020-01-25 07:09:08

问题


My input images have 8 channels and my output (label) has 1 channel and my CNN in keras is like below:

def set_model(ks1=5, ks2=5, nf1=64, nf2=1):
    model = Sequential()
    model.add(Conv2D(nf1, padding="same", kernel_size=(ks1, ks1), 
                     activation='relu', input_shape=(62, 62, 8)))
    model.add(Conv2D(nf2, padding="same", kernel_size=(ks2, ks2), 
                                              activation='relu'))
    model.compile(loss=keras.losses.binary_crossentropy,
                  optimizer=keras.optimizers.Adadelta())
    return model

The filter I have here is the same for all 8 channels. What I would like to have is a 3D filter, something like (8, 5, 5) such that every channel has a separate filter because these channels have not the same importance.

Below is the summary of the model implemented above:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 62, 62, 64)        12864     
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 62, 62, 1)         1601      
=================================================================
Total params: 14,465
Trainable params: 14,465
Non-trainable params: 0
_________________________________________________________________

And when I get the shape of weights for the first layer I have the following results:

for layer in model.layers:
    weights = layer.get_weights()

len(weights)
2

a = np.array(weights[0]) 
a.shape
(5, 5, 64, 1)

And I am wondering where is 8 in the shape of weights of the first layer?

来源:https://stackoverflow.com/questions/50992263/how-to-have-a-3d-filter-for-conv2d-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!