第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

向 tensorflow.python.keras.models.Model 添加一個新層

向 tensorflow.python.keras.models.Model 添加一個新層

飲歌長嘯 2023-06-06 16:26:26
我正在使用 Python 3.7.7。和張量流 2.3.0。我想從 U-Net 網(wǎng)絡(luò)中提取編碼器并添加它GlobalAveragePooling2D。我使用函數(shù)式 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 已經(jīng)經(jīng)過預(yù)訓(xùn)練,所以我需要從預(yù)訓(xùn)練模型中獲取編碼器。但我收到以下錯誤: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?
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經(jīng)驗 獲得超10個贊

我找到了一種方法。

from tensorflow.python.keras.models import Model

from tensorflow.python.keras.layers import GlobalAveragePooling2D


encoder_input = Model(inputs=old_model.layers[0].input, outputs=old_model.layers[14].output)


encoder_output = GlobalAveragePooling2D()(encoder_input.layers[-1].output)


encoder = Model(encoder_input.input, encoder_output, name='encoder')

summary新模型 ( ) 的是encoder:

Model: "encoder"

_________________________________________________________________

Layer (type)? ? ? ? ? ? ? ? ?Output Shape? ? ? ? ? ? ? Param #? ?

=================================================================

input_1 (InputLayer)? ? ? ? ?[(None, 200, 200, 1)]? ? ?0? ? ? ? ?

_________________________________________________________________

conv1_1 (Conv2D)? ? ? ? ? ? ?(None, 200, 200, 64)? ? ? 1664? ? ??

_________________________________________________________________

conv1_2 (Conv2D)? ? ? ? ? ? ?(None, 200, 200, 64)? ? ? 102464? ??

_________________________________________________________________

pool1 (MaxPooling2D)? ? ? ? ?(None, 100, 100, 64)? ? ? 0? ? ? ? ?

_________________________________________________________________

conv2_1 (Conv2D)? ? ? ? ? ? ?(None, 100, 100, 96)? ? ? 55392? ? ?

_________________________________________________________________

conv2_2 (Conv2D)? ? ? ? ? ? ?(None, 100, 100, 96)? ? ? 83040? ? ?

_________________________________________________________________

pool2 (MaxPooling2D)? ? ? ? ?(None, 50, 50, 96)? ? ? ? 0? ? ? ? ?

_________________________________________________________________

conv3_1 (Conv2D)? ? ? ? ? ? ?(None, 50, 50, 128)? ? ? ?110720? ??

_________________________________________________________________

conv3_2 (Conv2D)? ? ? ? ? ? ?(None, 50, 50, 128)? ? ? ?147584? ??

_________________________________________________________________

pool3 (MaxPooling2D)? ? ? ? ?(None, 25, 25, 128)? ? ? ?0? ? ? ? ?

_________________________________________________________________

conv4_1 (Conv2D)? ? ? ? ? ? ?(None, 25, 25, 256)? ? ? ?295168? ??

_________________________________________________________________

conv4_2 (Conv2D)? ? ? ? ? ? ?(None, 25, 25, 256)? ? ? ?1048832? ?

_________________________________________________________________

pool4 (MaxPooling2D)? ? ? ? ?(None, 12, 12, 256)? ? ? ?0? ? ? ? ?

_________________________________________________________________

conv5_1 (Conv2D)? ? ? ? ? ? ?(None, 12, 12, 512)? ? ? ?1180160? ?

_________________________________________________________________

conv5_2 (Conv2D)? ? ? ? ? ? ?(None, 12, 12, 512)? ? ? ?2359808? ?

_________________________________________________________________

global_average_pooling2d (Gl (None, 512)? ? ? ? ? ? ? ?0? ? ? ? ?

=================================================================

Total params: 5,384,832

Trainable params: 5,384,832

Non-trainable params: 0

_________________________________________________________________

None

我希望以下輸出形狀是正確的:


_________________________________________________________________

global_average_pooling2d (Gl (None, 512)? ? ? ? ? ? ? ?0? ? ? ? ?

=================================================================

但是在Transfer learning & fine-tuning中有類似的輸出形狀。


查看完整回答
反對 回復(fù) 2023-06-06
  • 1 回答
  • 0 關(guān)注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號