我在我的CNN中尝试添加一些固定的核,请看我下面的代码。
这是我创建核的方式:
# Kernelsdef create_kernel(x): t = pipe( x, lambda x: tf.constant(x, dtype=tf.float32), lambda x: tf.reshape(x, [3, 3, 1, 1])) return tk_edge1 = create_kernel([1, 0, -1, 0, 0, 0, -1, 0, 1])k_edge2 = create_kernel([0, 1, 0, 1, -4, 1, 0, 1, 0])k_edge3 = create_kernel([-1, -1, -1, -1, 8, -1, -1, -1, -1])
我的卷积网络如下:
# Convolution network# Input layerl_input = Input(shape=(28**2, ))# Reshape layerl_reshape = Reshape(target_shape=(28, 28, 1))(l_input)# Convolution layersl_conv1 = Conv2D(filters=20, kernel_size=(3, 3), padding='valid')(l_reshape)l_edge1 = tf.nn.conv2d(l_reshape, k_edge1, strides=[1, 1, 1, 1], padding='VALID')l_edge2 = tf.nn.conv2d(l_reshape, k_edge2, strides=[1, 1, 1, 1], padding='VALID')l_edge3 = tf.nn.conv2d(l_reshape, k_edge3, strides=[1, 1, 1, 1], padding='VALID')l_conv1a = Concatenate(axis=3)([l_conv1, l_edge1, l_edge2, l_edge3]) # <- 错误应该由这一行引起。l_conv2 = Conv2D(filters=20, kernel_size=(3, 3), padding='valid')(l_conv1a)l_pool1 = MaxPooling2D(pool_size=(2, 2), border_mode='valid')(l_conv2)# Flatten layerl_flat = Flatten()(l_pool1)# Fully connected layersl_fc1 = Dense(50, kernel_initializer='he_normal')(l_flat)l_act1 = PReLU()(l_fc1)l_fc3 = Dense(10, kernel_initializer='he_normal')(l_act1)l_output = Activation('softmax')(l_fc1)# Modelcnn_model = Model(l_input, l_output)
然而,我得到了以下错误:
Traceback (most recent call last):File "<stdin>", line 2, in <module>File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 93, in __init__ self._init_graph_network(*args, **kwargs)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network self.inputs, self.outputs)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1353, in _map_graph_network tensor_index=tensor_index)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1340, in build_map node_index, tensor_index)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1340, in build_map node_index, tensor_index)File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1340, in build_map node_index, tensor_index)[Previous line repeated 2 more times]File "C:\Users\Perry Cheng\AppData\Local\conda\conda\envs\ml_py_3_6\lib\site-packages\keras\engine\network.py", line 1312, in build_map node = layer._inbound_nodes[node_index]AttributeError: 'NoneType' object has no attribute '_inbound_nodes'
经过一些测试,我认为错误来自于:
l_conv1a = Concatenate(axis=3)([l_conv1, l_edge1, l_edge2, l_edge3])
有什么方法可以解决这个问题吗?
回答:
Keras层接受Keras张量而不是普通的张量作为输入。因此,如果你想在Keras中使用tf.nn.conv2d
而不是Conv2D
层,你需要将它们包装在Lambda
层中:
l_edge1 = Lambda(lambda x: tf.nn.conv2d(x, k_edge1, strides=[1, 1, 1, 1], padding='VALID'))(l_reshape)l_edge2 = Lambda(lambda x: tf.nn.conv2d(x, k_edge2, strides=[1, 1, 1, 1], padding='VALID'))(l_reshape)l_edge3 = Lambda(lambda x: tf.nn.conv2d(x, k_edge3, strides=[1, 1, 1, 1], padding='VALID'))(l_reshape)