keras - 在keras中合并层(连接)

标签 keras conv-neural-network

我正在尝试实现这个paper (模型架构如下所示)并且有两个模型 - coarse_modelfine_model,需要在精细模型的第二步连接。但是,当我尝试使用最后一个轴连接时出现错误。 enter image description here


from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense, Merge
from keras.layers.core import Reshape
from keras.layers.merge import Concatenate
from keras import backend as K


# dimensions of our images
#img_width, img_height = 320, 240

img_width, img_height = 304,228


if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

# coarse model
coarse_model = Sequential()
# coarse layer 1
coarse_model.add(Conv2D(96,(11,11),strides=(4,4),input_shape=input_shape,activation='relu'))
coarse_model.add(MaxPooling2D(pool_size=(2, 2)))
# coarse layer 2
coarse_model.add(Conv2D(256,(5,5),activation='relu',padding='same'))
coarse_model.add(MaxPooling2D(pool_size=(2, 2)))
# coarse layer 3
coarse_model.add(Conv2D(384,(3,3),activation='relu',padding='same'))
# coarse layer 4
coarse_model.add(Conv2D(384,(3,3),activation='relu',padding='same'))
# coarse layer 5
coarse_model.add(Conv2D(256,(3,3),activation='relu',padding='same'))
coarse_model.add(Flatten())
# coarse layer 6
coarse_model.add(Dense(4096,activation='relu'))
# coarse layer 7
coarse_model.add(Dense(4070,activation='linear'))

# fine model
fine_model = Sequential()
fine_model.add(Conv2D(63,(9,9),strides=(2,2),input_shape=input_shape,activation='relu'))
fine_model.add(MaxPooling2D(pool_size=(2, 2)))

# reshape coarse model to shape of fine model
shape = fine_model.layers[1].output_shape
shape_subset = (shape[1],shape[2])


coarse_model.add(Reshape(shape_subset))
model = Sequential()
model.add(Merge([coarse_model.layers[10],fine_model.layers[1]],mode='concat',concat_axis=3))

最后一行给出的错误是: *** ValueError:“concat”模式只能合并具有匹配输出形状的图层,除了 concat 轴之外。图层形状:[(无、74、55)、(无、74、55、63)]

最佳答案

为了回答我自己的问题,将形状更改为

shape_subset = (shape[1],shape[2],1)

model.add(Merge([coarse_model.layers[10],fine_model.layers[1]],mode='concat',concat_axis=-1))

使代码工作。

关于keras - 在keras中合并层(连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44787050/

相关文章:

machine-learning - 如何使用 keras 深度学习对图像序列进行分类

keras - restore_best_weights 问题 keras 提前停止

neural-network - 神经网络层的输出在哪些方面有用?

tensorflow - Bias一定需要卷积层吗?

python - SeLU 激活函数 x 参数导致类型错误

python - 您如何使用经过训练的神经网络来识别图像中的多个对象?

python - conv1D 中的形状尺寸

python - Tensorflow - 带有 sample_weight 的自定义损失函数

python - 有些东西耗尽了大部分 GPU 内存,无法让我使用 Tensorflow 训练模型

python - 模块 'tensorflow' 没有属性 'get_default_graph' - 我不需要任何图表