我跟隨 deeplizard 對 MobileNet 進行了微調。我試圖做的是從模型的第 5 層到最后一層獲取輸出并將其存盤在這個變數 x 中。模型第 5 層到最后一層的輸出具有global_average_pooling2d_3 (None, 1, 1, 1024). 然后添加一個具有 10 個單元的輸出密集層。但是,在擬合模型時,出現以下錯誤。任何人都可以請給我一些指導。非常感謝。我的代碼如下所示
mobile = tf.keras.applications.mobilenet.MobileNet()
mobile.summary()
x = mobile.layers[-5].output
output =layers.Dense(units=10, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=output)
for layer in model.layers[:-23]:
layer.trainable = False
model.compile(optimizer=Adam(lr=0.0001),
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(x=train_batches,
steps_per_epoch=len(train_batches),
validation_data=valid_batches,
validation_steps=len(valid_batches),
epochs=30,
verbose=2
)
ValueError: Shapes (None, None) and (None, 1, 1, 10) are incompatible
uj5u.com熱心網友回復:
當您按如下方式呼叫基本模型時,它將使用默認引數啟動。其中,include_top設定為True。
mobile = tf.keras.applications.mobilenet.MobileNet()
并且,這帶來了(源)GlobalAvgwith keepdims=True。
if include_top:
x = layers.GlobalAveragePooling2D(keepdims=True)(x)
現在,根據您的錯誤,我假設了您的真實標簽形狀,在這里您可以簡單地執行以下操作
mobile = keras.applications.mobilenet.MobileNet()
x = mobile.layers[-5].output # shape=(None, 1, 1, 1024)
x = layers.Flatten()(x) # < --- Here shape=(None, 1024)
output =layers.Dense(units=10, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=output)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381034.html
上一篇:形狀(335476,50)和(3,50)未對齊:50(dim1)!=3(dim0)
下一篇:層“sequential_3”的輸入0與層不兼容:expectedshape=(None,60),foundshape=(5,174)
