我正在嘗試采用以下預訓練的 keras 模型,但它需要輸入為張量。任何人都可以幫助解決它嗎?
from keras.applications.vgg19 import VGG19
inputs = layers.Input(shape = (32,32,4))
vgg_model = VGG19(weights='imagenet', include_top=False)
vgg_model.trainable = False
x = tensorflow.keras.layers.Flatten(name='flatten')(vgg_model)
x = tensorflow.keras.layers.Dense(512, activation='relu', name='fc1')(x)
x = tensorflow.keras.layers.Dense(512, activation='relu', name='fc2')(x)
x = tensorflow.keras.layers.Dense(1,name='predictions')(x)
new_model = tensorflow.keras.models.Model(inputs=inputs, outputs=x)
new_model.compile(optimizer='adam', loss='mean_squared_error',
metrics=['mae'])
錯誤:
TypeError: Inputs to a layer should be tensors. Got: <keras.engine.functional.Functional object at 0x000001F48267B588>
uj5u.com熱心網友回復:
如果要將VGG19用作基本模型,則必須將其輸出用作自定義模型的輸入:
import tensorflow as tf
from keras.applications.vgg19 import VGG19
vgg_model = VGG19(weights='imagenet', include_top=False, input_shape=(32, 32, 3))
vgg_model.trainable = False
x = vgg_model.output
x = tf.keras.layers.Dense(512, activation='relu', name='fc1')(x)
x = tf.keras.layers.Dense(512, activation='relu', name='fc2')(x)
x = tf.keras.layers.Dense(1, name='predictions')(x)
new_model = tf.keras.Model(inputs=vgg_model.input, outputs=x)
new_model.compile(optimizer='adam', loss='mean_squared_error',
metrics=['mae'])
new_model(tf.random.normal((1, 32, 32, 3)))
請注意,我洗掉了您的Flatten layer,因為 的輸出vgg_model已經具有形狀(batch_size, features).
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/343747.html
上一篇:在mongodb中是否類似于php中的準備陳述句以確保安全?
下一篇:卷積神經網路-1D-特征分類錯誤
