ValueError:只支持至少一个签名密钥.同时将模型转换为TFlite图像



我有模型,它能够通过热图样本检测人类时刻,我正试图将模型转换为tflite图像,我得到ValueError:只支持至少一个签名密钥。

File "D:ppl_count.tar (1)ppl_countppl_countHuman_activity.py", line 360, in <module>
tm.run(LRCN_model)
File "D:ppl_count.tar (1)ppl_countppl_countHuman_activity.py", line 136, in run
self.create_tflite_image()
File "D:ppl_count.tar (1)ppl_countppl_countHuman_activity.py", line 126, in create_tflite_image
converter = tf.lite.TFLiteConverter.from_saved_model(self.tf_model_dir)  # path to the SavedModel directory
File "C:UsersL99002507AppDataRoamingPythonPython310site-packagestensorflowlitepythonlite.py", line 1778, in from_saved_model
raise ValueError("Only support at least one signature key.")
ValueError: Only support at least one signature key.

和我的代码:

class TrainModel:
def __init__(self):
# The Tensorflow model and properties file will be stored here
self.tf_model_dir = os.path.join(basedir, 'models', 'ir', 'tensorflow')
os.makedirs(self.tf_model_dir, exist_ok=True)
self.tf_model_file = os.path.join(self.tf_model_dir, 'ir.pb')
self.tf_properties_file = os.path.join(self.tf_model_dir, 'ir.json')
self.tf_tflite_file = os.path.join(self.tf_model_dir, 'ir.tflite')
h5_model_dir = os.path.join(basedir, 'models', 'ir', 'keras')
os.makedirs(h5_model_dir, exist_ok=True)
self.h5_model_file = os.path.join(h5_model_dir, 'ir.h5')
self.h5_weights_file = os.path.join(h5_model_dir, 'ir.json')
# Base directory that contains your training images and dataset files
self.dataset_base_dir = os.path.join(basedir, 'datasets', 'ir')
self.dataset_dir = os.path.join(self.dataset_base_dir, 'dataset')
# Store your thermal camera images here
self.img_dir = os.path.join(self.dataset_base_dir, 'images')
# Size of the input images
self.input_size = (8, 8)
self.classes = CLASSES_LIST
self.predictions = ''
def save_model_tf2(self, model):
from tensorflow.python.framework.convert_to_constants import 
convert_variables_to_constants_v2
# Save model to SavedModel format
os.makedirs(self.tf_model_dir, exist_ok=True)
tf.saved_model.save(model, self.tf_model_dir)
# Convert Keras model to ConcreteFunction
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(
x=tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
# Get frozen ConcreteFunction
frozen_func = convert_variables_to_constants_v2(full_model)
frozen_func.graph.as_graph_def()
layers = [op.name for op in frozen_func.graph.get_operations()]
# print("-" * 50)
# print("Frozen model layers: ")
# for layer in layers:
#     print(layer)
# print("-" * 50)
# print("Frozen model inputs: ")
# print(frozen_func.inputs)
# print("Frozen model outputs: ")
# print(frozen_func.outputs)
# Save frozen graph from frozen ConcreteFunction to hard drive
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
logdir=os.path.dirname(self.tf_model_file),
name=os.path.basename(self.tf_model_file),
as_text=False)
properties = {}
classes = self.classes
if isinstance(self.classes, np.ndarray):
classes = self.classes.tolist()
properties['classes'] = classes
properties['input_size'] = [*self.input_size]
print(f'properties{properties}')
with open(self.tf_properties_file, 'w') as f:
json.dump(properties, f)
def create_tflite_image(self):
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(self.tf_model_dir)  # path to the SavedModel directory
tflite_model = converter.convert()
# Save the model.
with open(self.tf_tflite_file, 'wb') as f:
f.write(tflite_model)
def run(self, model):
self.save_model_tf2(model)
self.create_tflite_image()
def create_LRCN_model():
"""
This function will construct the required LRCN model.
Returns:
model: It is the required constructed LRCN model.
"""
# We will use a Sequential model for model construction.
model = Sequential()
# Define the Model Architecture.
# #######################################################################################################################
model.add(TimeDistributed(Conv2D(16, (3, 3), padding='same', activation='relu'),
input_shape=(SEQUENCE_LENGTH, IMAGE_HEIGHT, IMAGE_WIDTH, 3)))
model.add(TimeDistributed(MaxPooling2D((4, 4))))
model.add(TimeDistributed(Dropout(0.25)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(32))
model.add(Dense(len(CLASSES_LIST), activation='softmax'))
# #######################################################################################################################
# Display the models summary.
model.summary()
# Return the constructed LRCN model.
return model

# Construct the required LRCN model.
LRCN_model = create_LRCN_model()
tm = TrainModel()
tm.run(LRCN_model)
  • 当我尝试打印签名时,发现是空的。请帮我解决签名密钥问题。

您需要将LSTM层转换为它的layer.LSTM.

这是数学当使用线性Fn与LSTM时,你可能需要学习时间,但你也可以应用标量Fn或其他Fns。

LSTM也只工作,但需要小步和数百步,您可以申请它的分类fns。

[示例]:

import os
from os.path import exists
import tensorflow as tf
import tensorflow_io as tfio
import pandas as pd
import matplotlib.pyplot as plt
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
SEQUENCE_LENGTH = 1
IMAGE_HEIGHT = 32
IMAGE_WIDTH = 32
CHANNELS = 4
list_label_actual = [ 'Candidt Kibt', 'Pikaploy' ]
saved_model_path = "F:\models\checkpoint\" + os.path.basename(__file__).split('.')[0] + "\DekDee naja"
saved_model_dir = os.path.dirname(saved_model_path)
print( saved_model_dir )
if not exists(saved_model_dir) : 
os.mkdir(saved_model_dir)
print("Create directory: " + saved_model_dir)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Class
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class MyLSTMLayer( tf.keras.layers.LSTM ):
def __init__(self, units, return_sequences, return_state):
super(MyLSTMLayer, self).__init__( units, return_sequences=return_sequences, return_state=return_state )
self.num_units = units
self.return_sequences = return_sequences
self.return_state = return_state
def build(self, input_shape):
self.w = self.add_weight(
shape=(input_shape[-1], self.units),
initializer="random_normal",
trainable=True,
)
self.b = self.add_weight(
shape=(self.units,), initializer="random_normal", trainable=True
)
def call(self, inputs):

return tf.matmul(inputs, self.w) + self.b
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Dataset
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
variables = pd.read_excel('F:\temp\Python\excel\Book 7.xlsx', index_col=None, header=[0])
list_label = [ ]
list_Image = [ ]
list_file_actual = [ ]
for Index, Image, Label in variables.values:
print( Label )
list_label.append( Label )

image = tf.io.read_file( Image )
image = tfio.experimental.image.decode_tiff(image, index=0)
list_file_actual.append(image)
image = tf.image.resize(image, [32,32], method='nearest')
list_Image.append(image)

list_label = tf.cast( list_label, dtype=tf.int32 )
list_label = tf.constant( list_label, shape=( 33, 1, 1 ) )
list_Image = tf.cast( list_Image, dtype=tf.int32 )
list_Image = tf.constant( list_Image, shape=( 33, 1, 32, 32, 4 ) )
dataset = tf.data.Dataset.from_tensor_slices(( list_Image, list_label ))
list_Image = tf.constant( list_Image, shape=( 33, 32, 32, 4 ) ).numpy()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 32, 32, 4 ), name="input_01"),
tf.keras.layers.Normalization(mean=3., variance=2., name="normal_01"),
tf.keras.layers.Normalization(mean=4., variance=6., name="normal_02"),
tf.keras.layers.Reshape((32 * 32, 4), name="reshape_01"),
MyLSTMLayer(96, False, False),
tf.keras.layers.Flatten(name="flattern_01"),
tf.keras.layers.Dense(192, activation='relu', name="dense_02"),
tf.keras.layers.Dense(2, name="dense_03"),
])

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam(
learning_rate=0.000000001, beta_1=0.9, beta_2=0.997, epsilon=1e-09,
name='Nadam'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""                               
lossfn = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=False,
reduction=tf.keras.losses.Reduction.AUTO,
name='sparse_categorical_crossentropy'
)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'] )
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Callback
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class custom_callback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if( logs['accuracy'] >= 0.97 ):
self.model.stop_training = True

custom_callback = custom_callback()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit( dataset, batch_size=100, epochs=5000, callbacks=[custom_callback] )
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Save
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.save(
saved_model_dir,
overwrite=True,
include_optimizer=True,
save_format=None,
signatures=None,
options=None,
save_traces=True
)
## Converting a SavedModel to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_saved_model( saved_model_dir )
tflite_model = converter.convert()
# Save the model.
with open( saved_model_dir + '\model.tflite', 'wb' ) as f:
f.write(tflite_model)

plt.figure(figsize=(6,6))
plt.title("Actors recognitions")
for i in range(len(list_Image)):
img = tf.keras.preprocessing.image.array_to_img(
tf.constant( list_Image[i], shape=( 32, 32, 4 ) ),
data_format=None,
scale=True
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
plt.subplot(6, 6, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(list_file_actual[i])
plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" +  str(list_label_actual[tf.math.argmax(score)]))

plt.show()
input('...')

相关内容

  • 没有找到相关文章

最新更新