Tensorflow Hub Error When Saving Model As H5 Or Savedmodel
Solution 1:
It's been a while, but assuming you have migrated to the TF2, this can easily be accomplished with the most recent model version as follows:
import tensorflow as tf
import tensorflow_hub as hub
num_classes=10# For example
m = tf.keras.Sequential([
hub.KerasLayer("https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/5", trainable=True)
tf.keras.layers.Dense(num_classes, activation='softmax')
])
m.build([None, 224, 224, 3]) # Batch input shape.# train as needed
m.save("/some/output/path")
Please update this question if that doesn't work for you. I believe your issue arose from mixing hub.Module
with hub.KerasLayer
. The model version you were using was in TF1 Hub format, so within TF1 it is meant to be used exclusively with hub.Module
, and not mixed with hub.KerasLayer
. Within TF2, hub.KerasLayer
can load TF1 Hub format models directly from their URL for composition in larger models, but they cannot be fine-tuned.
Please refer to this compatibility guide for more information
Solution 2:
You should use tf.keras.models.save_model(model,'NeuralNetworkModel')
You will get saved model in a folder that can be used later in your sequential nework
Post a Comment for "Tensorflow Hub Error When Saving Model As H5 Or Savedmodel"