我正在使用 Python 3.7.7。和张量流 2.3.0。
我想从 U-Net 网络中提取编码器并添加它GlobalAveragePooling2D
。
我使用函数式 API 来定义 U-Net:
inputs = Input(shape=img_shape)
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', data_format="channels_last", name='conv1_1')(inputs)
conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', data_format="channels_last", name='conv1_2')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last", name='pool1')(conv1)
conv2 = Conv2D(96, (3, 3), activation='relu', padding='same', data_format="channels_last", name='conv2_1')(pool1)
conv2 = Conv2D(96, (3, 3), activation='relu', padding='same', data_format="channels_last", name='conv2_2')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last", name='pool2')(conv2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same', data_format="channels_last", name='conv3_1')(pool2)
conv3 = Conv2D(128, (3, 3), activation='relu', padding='same', data_format="channels_last", name='conv3_2')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last", name='pool3')(conv3)
conv4 = Conv2D(256, (3, 3), activation='relu', padding='same', data_format="channels_last", name='conv4_1')(pool3)
conv4 = Conv2D(256, (4, 4), activation='relu', padding='same', data_format="channels_last", name='conv4_2')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last", name='pool4')(conv4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same', data_format="channels_last", name='conv5_1')(pool4)
conv5 = Conv2D(512, (3, 3), activation='relu', padding='same', data_format="channels_last", name='conv5_2')(conv5)
我这样做是因为 U-Net 已经经过预训练,所以我需要从预训练模型中获取编码器。
但我收到以下错误:
AttributeError: 'Model' object has no attribute 'shape'
在行:
encoder_output = GlobalAveragePooling2D()(encoder_input)
我也尝试过,但没有成功:
encoder_output = GlobalAveragePooling2D()(encoder_input.get_layer('conv5_2'))
和:
encoder_output = GlobalAveragePooling2D()(encoder_input.layers[-1].output)
如何将GlobalAveragePooling2D
图层添加到old_model
?
慕桂英546537
相关分类