Keras:体重分享不起作用

时间:2016-11-03 10:11:23

标签: python machine-learning keras

我想使用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万。

显然,权重不是共享的。 但这是way重量分配应该起作用。

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

1 个答案:

答案 0 :(得分:0)

感谢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)
相关问题