无法将 tfhub keras 模型转换为 tflite?

时间:2021-01-27 12:35:57

标签: tensorflow keras tf.keras

我试图将这个 keras 模型从 tfhub Model link 转换为 tflite 模型。 但是我无法转换它,因为它给出了错误消息。 所以当我运行我的代码来转换它时,我收到一个错误消息 Can't provision more than one single cluster at a time

我不知道为什么它不起作用。

代码如下:

import tensorflow as tf
import tensorflow_hub as hub

model = "https://tfhub.dev/google/aiy/vision/classifier/birds_V1/1"

IMAGE_SHAPE = (224, 224)
classifier = tf.keras.Sequential([
    hub.KerasLayer(model, input_shape=IMAGE_SHAPE+(3,))
])

//Then I test the model to see if it is working or not.

import numpy as np
import time
import PIL.Image as Image
import matplotlib.pylab as plt

bird = Image.open("Larus_argentatus_ad.jpg").resize(IMAGE_SHAPE)
bird = np.array(bird)/255.0
result = classifier.predict(bird[np.newaxis, ...])
predicted_class = np.argmax(result[0], axis=-1)
//birdl.txt is the label you can get on tfhub link given above.
imagenet_labels = np.array(open("birdl.txt").read().splitlines())
plt.imshow(bird)
plt.axis('off')
predicted_class_name = imagenet_labels[predicted_class]
_ = plt.title("Prediction: " + predicted_class_name.title())

//So it works fine and now I wanted to convert it to tflite.

//Converting this model to a tflite model.

converter = tf.lite.TFLiteConverter.from_keras_model(classifier)
classifier.save_weights("..")
tflite_models = converter.convert()
with open('data/model.tflite','wb') as f:
  f.write(tflite_models)

//But it fails to convert.

0 个答案:

没有答案