如何将签名密钥添加到 keras 模型
Posted
技术标签:
【中文标题】如何将签名密钥添加到 keras 模型【英文标题】:How to add signature key to a keras model 【发布时间】:2022-01-16 00:35:12 【问题描述】:我正在构建一个 tensorflow keras 模型,该模型必须转换为 tensorflowlite 并在 Kotlin 中运行。该模型在 Anaconda Spyder 中运行良好。但是当我尝试将此模型转换为 tensorflow lite 时,我遇到了错误。
class OneStep(tf.keras.Model):
def __init__(self, model, chars_from_ids, ids_from_chars, temperature=1.0):
super().__init__()
self.temperature = temperature
self.model = model
self.chars_from_ids = chars_from_ids
self.ids_from_chars = ids_from_chars
# Create a mask to prevent "[UNK]" from being generated.
skip_ids = self.ids_from_chars(['[UNK]'])[:, None]
sparse_mask = tf.SparseTensor(
# Put a -inf at each bad index.
values=[-float('inf')]*len(skip_ids),
indices=skip_ids,
# Match the shape to the vocabulary
dense_shape=[len(ids_from_chars.get_vocabulary())])
self.prediction_mask = tf.sparse.to_dense(sparse_mask)
@tf.function
def generate_one_step(self, inputs, states=None):
# Convert strings to token IDs.
input_chars = tf.strings.unicode_split(inputs, 'UTF-8')
input_ids = self.ids_from_chars(input_chars).to_tensor()
# Run the model.
# predicted_logits.shape is [batch, char, next_char_logits]
predicted_logits, states = self.model(inputs=input_ids, states=states,
return_state=True)
# Only use the last prediction.
predicted_logits = predicted_logits[:, -1, :]
predicted_logits = predicted_logits/self.temperature
# Apply the prediction mask: prevent "[UNK]" from being generated.
predicted_logits = predicted_logits + self.prediction_mask
# Sample the output logits to generate token IDs.
predicted_ids = tf.random.categorical(predicted_logits, num_samples=1)
predicted_ids = tf.squeeze(predicted_ids, axis=-1)
# Convert from token ids to characters
predicted_chars = self.chars_from_ids(predicted_ids)
# Return the characters and model state.
return predicted_chars, states
one_step_model = OneStep(model, chars_from_ids, ids_from_chars)
tf.saved_model.save(one_step_model, 'one_step')
我尝试在这段代码摘录中将此模型转换为 tensorflowlite。我试图在 Anaconda Spyder 中转换模型,但它至少需要一个签名密钥。我不确定如何使用签名密钥首先保存模型。
one_step_reloaded = tf.saved_model.load('one_step')
#print(one_step_reloaded.SignatureDefEntry)
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model('one_step') # path to the SavedModel directory
tflite_model = converter.convert()
# Save the model.
with open('Bible.tflite', 'wb') as f:
f.write(tflite_model)
raise ValueError("Only support at least one signature key.")
ValueError: Only support at least one signature key.
您能帮忙在保存之前如何将签名密钥添加到此模型,以便可以将其转换为 tensorflowlite 吗?
【问题讨论】:
【参考方案1】:由于您使用的是 Keras 模型,因此您可以使用 TFLiteConverter.from_keras_model
API 直接从该格式转换为 TensorFlow Lite。举个例子:
# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
# Save the model.
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
【讨论】:
我现在收到一个新错误。如果不是 isinstance(self._keras_model.call, _def_function.Function): AttributeError: '_UserObject' 对象没有属性 'call'以上是关于如何将签名密钥添加到 keras 模型的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Google 签名密钥签署 apk 以上传到其他应用商店
如何修复 Google Api 错误:禁止:将应用程序上传到 Play 商店时,Android App Bundle 使用错误的密钥签名