2016-11-03 4 views
2

ConvNet을 사용하여 이미지 데이터를 분류하고 싶습니다. 동일한 네트워크에 다른 (그러나 매우 유사한) 데이터를 입력해야하며 출력을 병합해야합니다.Keras : 무게 공유가 작동하지 않습니다.

내 트릭이 관련되어 있습니다. 데이터가 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입니다.

모델이 가중치를 공유하고 매개 변수 개수를 잘못보고합니까? 체중 공유가 가능하도록 설정을 변경해야합니까? Keras - 사용자 Google 그룹에

답변

0

감사합니다,이 이제 대답한다 : 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) 
관련 문제