我在U-Net中遇到模块不可调用错误
def unet():
inputs = Input((1,512, 512))
conv1 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(inputs)
conv1 = BatchNormalization(axis = 1)(conv1)
conv1 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(pool1)
conv2 = BatchNormalization(axis = 1)(conv2)
conv2 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(pool2)
conv3 = BatchNormalization(axis = 1)(conv3)
conv3 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(pool3)
conv4 = BatchNormalization(axis = 1)(conv4)
conv4 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(width*16, 3, 3, activation='relu', border_mode='same')(pool4)
conv5 = BatchNormalization(axis = 1)(conv5)
conv5 = Conv2D(width*16, 3, 3, activation='relu', border_mode='same')(conv5)
up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
conv6 = SpatialDropout2D(0.35)(up6)
conv6 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6)
conv6 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6)
up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1)
conv7 = SpatialDropout2D(0.35)(up7)
conv7 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(conv7)
conv7 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(conv7)
up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1)
conv8 = SpatialDropout2D(0.35)(up8)
conv8 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(conv8)
conv8 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(conv8)
up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1], mode='concat', concat_axis=1)
conv9 = SpatialDropout2D(0.35)(up9)
conv9 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(conv9)
conv9 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(conv9)
conv10 = Conv2D(1, 1, 1, activation='sigmoid')(conv9)
model = Model(input=inputs, output=conv10)
model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])
return model
请查看代码,并提供解决方案
下面是错误
<ipython-input-12-dd57276e32d9> in unet()
38 conv5 = Conv2D(width*16, 3, 3, activation='relu', border_mode='same')(conv5)
39
---> 40 up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
41 conv6 = SpatialDropout2D(0.35)(up6)
42 conv6 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6)
TypeError: 'module' object is not
答案 0 :(得分:0)
Keras中的merge层在不久前已经进行了重构,因此您必须在使用案例中使用concatenate
函数,如下所示:
from keras.layers import concatenate
up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=1)
其他合并调用也是如此。