嗨,我正在使用相关权重进行图像分类任务。我正在使用Tensorflow 1.14.0版,正在使用来自以下source的mobilenetv1_050_224来完成此任务。
myTextInput.setNativeProps({ text: 'XXXX' })
我训练了这个模型,并且能够使用转移学习在我的数据集上获得良好的训练/验证准确性。以下是学习代码部分。
IMAGE_SHAPE = (400, 400)
n_classes = 10
classifier_url = 'https://tfhub.dev/google/imagenet/mobilenet_v1_050_224/classification/3'
base_model = hub.Module(classifier_url, tags=['train'])
base_model.trainable = False
classifier = tf.keras.Sequential([
hub.KerasLayer(base_model, input_shape=IMAGE_SHAPE+(3,)),
keras.layers.Dense(n_classes, activation='softmax')
])
#print (base_model.summary())
print (classifier.summary())
但是,当我尝试保存模型时:
train_datagen = keras.preprocessing.image.ImageDataGenerator(
rescale=1./255)
validation_datagen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow(
x = train_dataset,
y = train_labels,
batch_size=batch_size,
seed=1)
validation_generator = validation_datagen.flow(
x = validation_dataset, # Source directory for the validation images
y = valid_labels,
batch_size=batch_size,
seed=1)
classifier.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01, beta_1=0.9, beta_2=0.999),
loss='categorical_crossentropy',
metrics=['accuracy'])
epochs = 2
steps_per_epoch = train_generator.n // batch_size
validation_steps = validation_generator.n // batch_size
model = classifier.fit_generator(train_generator,
steps_per_epoch = steps_per_epoch,
epochs=epochs,
workers=4,
validation_data=validation_generator,
validation_steps=validation_steps)
我遇到以下错误:
NotImplementedError:只能针对以下内容生成有效的配置
export_path = '/tmp/simple_keras_model.h5' classifier.save(export_path, save_format='h5')
使用字符串hub.KerasLayer(handle, ...)
。有
handle
:
我被卡住了,无法绕开它。在这方面的任何线索都将有所帮助。谢谢。
答案 0 :(得分:0)
您可以通过以下方式保存classifier
:
# save the underlying tensorflow graph
model_file = classifier.to_json()
with open("model.json", "w") as source:
source.write(model_file)
# save model parameter
classifier.save_weights("model_weights.h5")
然后可以通过
加载保存的模型from keras.models import model_from_json
with open("model.json", "r") as f:
classifier = model_from_json(f.read())
classifier.load_weights("model_weights.h5")
答案 1 :(得分:0)
怎么样?
# Export the model to a SavedModel
keras.experimental.export_saved_model(classifier, '/tmp/simple_keras_model.h5')
# Recreate the exact same model
new_model = keras.experimental.load_from_saved_model('/tmp/simple_keras_model.h5')
答案 2 :(得分:0)