Keras:权重共享不起作用

Keras: Weight sharing doesn't work

我想使用ConvNet 来分割图像数据。应该向同一个网络提供不同(但非常相似)的数据,然后合并输出。

其中涉及一个技巧:我的数据是 3D,我将其切成 2D 图像并通过 TimeDistributed 将它们传递给 ConvNet。

对图像使用相同的 ConvNet 很重要,应该共享权重。 这是代码:

dim_x, dim_y, dim_z = 40, 40, 40

inputs = Input((1, dim_x, dim_y, dim_z))

# slice the volume along different axes
x_perm=Permute((2,1,3,4))(inputs)
y_perm=Permute((3,1,2,4))(inputs)
z_perm=Permute((4,1,2,3))(inputs)

#apply the segmentation to each layer and for each slice-direction
x_dist=TimeDistributed(convmodel)(x_perm)
y_dist=TimeDistributed(convmodel)(y_perm)
z_dist=TimeDistributed(convmodel)(z_perm)

# now undo the permutation
x_dist=Permute((2,1,3,4))(x_dist)
y_dist=Permute((2,3,1,4))(y_dist)
z_dist=Permute((2,3,4,1))(z_dist)

#now merge the predictions
segmentation=merge([x_dist, y_dist, z_dist], mode="concat")

temp_model=Model(input=inputs, output=segmentation)

temp_model.summary()

convnet 模型有大约 330 万个参数。排列和 TimeDistributed 层没有自己的参数。 所以完整的模型应该和convnet有相同数量的参数。

不是,它的参数多了3倍,大约990万。

显然权重不共享。 但这是 权重共享应该起作用的。

模型是否共享权重并错误地报告参数数量? 我是否必须更改设置才能启用权重共享?

感谢 Keras-Users Google 组,现在回答了这个问题:https://groups.google.com/forum/#!topic/keras-users/P-BMpdyJfXI

诀窍是先创建分段层,然后将其应用于数据。 这是工作代码:

#apply the segmentation to each layer and for each slice-direction
time_dist=TimeDistributed(convmodel)

x_dist=time_dist(x_perm)
y_dist=time_dist(y_perm)
z_dist=time_dist(z_perm)