Я пытаюсь построить модель Keras Sequential, используя экстрактор функций из TensorFlow Hub, но сталкиваюсь с этой ошибкой:
ValueError: Only instances of `keras.Layer` can be added to a Sequential model.
Received:
Мой код:-
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers
resnet_url = "https://tfhub.dev/google/imagenet/resne ... e_vector/4"
efficientnet_url = "https://tfhub.dev/google/imagenet/effic ... e_vector/2"
def create_model(model_url, num_classes=10):
"""Takes a TensorFlow Hub URL and creates a Keras Sequential model with it."""
feature_extractor_layer = hub.KerasLayer(
model_url,
trainable=False, # freeze the underlying patterns
name='feature_extraction_layer',
input_shape=IMAGE_SHAPE+(3,)
)
model = tf.keras.Sequential([
feature_extractor_layer, # use the feature extraction layer as the base
layers.Dense(num_classes, activation='softmax', name='output_layer')
])
return model
# Create model
resnet_model = create_model(resnet_url, num_classes=train_data_10_percent.num_classes)
# Compile
resnet_model.compile(
loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(),
metrics=['accuracy']
)
Отслеживание ошибок: -
ValueError: Only instances of `keras.Layer` can be added to a Sequential model.
Received:
(of type )
Что я пробовал
Проверил, что установлен tensorflow-hub.
Проверил, что resnet_url является допустимым вектором функций TF Hub.
Пытался использовать модель функционального API вместо последовательного, все равно возникают проблемы.
Пытался понизить версию TensorFlow и TF Hub, но в Google Colab доступны только более новые версии:
!pip install tensorflow==2.14 tensorflow-hub==0.15
# ERROR: No matching distribution found for tensorflow==2.14
Версии TensorFlow и TF Hub в Colab:
import tensorflow as tf
import tensorflow_hub as hub
print(tf.__version__)
print(hub.__version__)
2.19.0
0.16.1
Подробнее здесь: https://stackoverflow.com/questions/797 ... -model-whe