Я успешно конвертировал файл модели в сохраненный, и он работает при выводе. Но когда я конвертировал сохраненную модель в формат tflite и тестировал, у меня возникла такая ошибка:
Код: Выделить всё
Interpreter successfully created.
Error allocating tensors: tensorflow/lite/util.cc BytesRequired number of elements overflowed.
Node number 183 (MAX_POOL_2D) failed to prepare.Failed to apply the default TensorFlow Lite delegate indexed at 0.
Код: Выделить всё
def convert_onnx_to_saved_model():
# Load the ONNX model
model = onnx.load("model.onnx")
tf_rep = prepare(model)
tf_rep.export_graph("model")
Код: Выделить всё
def convert_saved_model_to_tflite():
converter = tf.lite.TFLiteConverter.from_saved_model("model")
# Convert the model
tflite_model = converter.convert()
# Save the model to a file
with open("model.tflite", "wb") as f:
f.write(tflite_model)
Код: Выделить всё
def load_and_test_tflite(model_path):
try:
# Load the TFLite model
interpreter = tf.lite.Interpreter(model_path=model_path)
print("Interpreter successfully created.")
except Exception as e:
print(f"Error loading model: {e}")
return
try:
# Allocate tensors
interpreter.allocate_tensors()
print("Tensors successfully allocated.")
except RuntimeError as e:
print(f"Error allocating tensors: {e}")
return
# Get input and output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print("Input details:", input_details)
print("Output details:", output_details)
# Create dummy input tensor
input_shape = input_details[0]['shape']
input_data = np.random.random_sample(input_shape).astype(np.float32)
try:
# Set the input tensor
interpreter.set_tensor(input_details[0]['index'], input_data)
# Run inference
interpreter.invoke()
print("Inference successfully run.")
except RuntimeError as e:
print(f"Error during inference: {e}")
return
try:
# Get the output tensor
output_data = interpreter.get_tensor(output_details[0]['index'])
print("Output data:", output_data)
except Exception as e:
print(f"Error getting output tensor: {e}")
Код: Выделить всё
Input details: [{'name': 'serving_default_input:0', 'index': 0, 'shape': array([ 1, 3, 256, 192]), 'shape_signature': array([ -1, 3, 256, 192]), 'dtype': , 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
Output details: [{'name': 'PartitionedCall:0', 'index': 398, 'shape': array([ 1, 1, 384]), 'shape_signature': array([ -1, -1, 384]), 'dtype': , 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'PartitionedCall:1', 'index': 399, 'shape': array([ 1, 1, 512]), 'shape_signature': array([ -1, -1, 512]), 'dtype': , 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
Подробнее здесь: https://stackoverflow.com/questions/789 ... nferencing