ОШИБКА: не найдена операция для встроенного кода операции «QUANTIZE» версии «3».Python

Программы на Python
Ответить
Anonymous
 ОШИБКА: не найдена операция для встроенного кода операции «QUANTIZE» версии «3».

Сообщение Anonymous »

Я пытаюсь преобразовать модель, в которой VGG-16 является базовой моделью, классификатором, GlobalAveragePooling2D и Dropout, в Tensorflow Lite после обучения с учетом квантования, но получаю следующую ошибку:

Компилятор Edge TPU, версия 16.0.384591198
ОШИБКА: не найден вариант для встроенный опкод «QUANTIZE» версии «3». Может поддерживаться более старая версия этой встроенной функции. Вы используете старый двоичный файл TFLite с более новой моделью?
ОШИБКА: регистрация не удалась.
Неверная модель: /mnt/e/models/quantized_model .tflite
Модель не удалось проанализировать

Сейчас я тренирую свою модель и сохраняю ее как .h5, затем загружаю ее контрольные точки и создаю новую модель для квантование. Однако при попытке применить обучение с учетом квантования я получаю упомянутую выше ошибку. Это код, который я использовал для применения QAT:

Код: Выделить всё

import os
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models, regularizers
from tensorflow.keras.applications import VGG16
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow_model_optimization as tfmot
import gc

hyperparams = {
'batch_size': 32,
'epochs': 1,
'learning_rate': 1e-6
}

IMG_HEIGHT = 384
IMG_WIDTH = 288

def clear_memory():
tf.keras.backend.clear_session()
gc.collect()
print("Memory cleared!")

def create_data_generators(base_dir, batch_size, target_size=(IMG_HEIGHT, IMG_WIDTH)):
datagen = ImageDataGenerator(
rescale=1.0 / 255,
validation_split=0.2
)

train_gen = datagen.flow_from_directory(
base_dir,
target_size=target_size,
batch_size=batch_size,
class_mode='categorical',
subset='training',
shuffle=True
)

val_gen = datagen.flow_from_directory(
base_dir,
target_size=target_size,
batch_size=batch_size,
class_mode='categorical',
subset='validation',
shuffle=False
)

return train_gen, val_gen

def build_model():
inputs = tf.keras.Input(shape=(IMG_HEIGHT, IMG_WIDTH, 3), dtype=tf.float32)
base_model = VGG16(include_top=False, weights=None, input_tensor=inputs)

x = base_model.output
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(512, activation='relu', kernel_regularizer=regularizers.l2(1e-4))(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(2, activation='softmax', kernel_regularizer=regularizers.l2(1e-4))(x)

model = tf.keras.Model(inputs=base_model.input, outputs=outputs)
return model

def load_weights_into_model(model, weights_path):
print(f"Loading weights from {weights_path}...")

# Try loading weights directly
try:
model.load_weights(weights_path)
print("Weights successfully loaded.")
except ValueError as e:
print("Error loading weights:", e)
print("Attempting partial weight loading with skip_mismatch=True...")
model.load_weights(weights_path, by_name=True, skip_mismatch=True)
print("Partial weights loaded (mismatched layers skipped).")
return model

def debug_saved_model(weights_path):
print("Inspecting saved model architecture...")
try:
saved_model = tf.keras.models.load_model(weights_path)
saved_model.summary()
except Exception as e:
print("Failed to load saved model:", e)
print("Ensure the architecture in build_model matches this output.")

def quantize_and_evaluate_model(model, train_gen, val_gen, hyperparams):
print("Applying quantization...")
quantize_model = tfmot.quantization.keras.quantize_model

# Create a model that replaces VGG16 layers with quantizable layers
q_aware_model = quantize_model(model)

q_aware_model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=hyperparams['learning_rate']),
loss='categorical_crossentropy',
metrics=['accuracy']
)

print("Training quantized model...")
q_aware_model.fit(
train_gen,
validation_data=val_gen,
epochs=hyperparams['epochs'],
verbose=1
)

print("Evaluating quantized model...")
val_loss, val_accuracy = q_aware_model.evaluate(val_gen, verbose=1)
print(f"Validation Accuracy after quantization: {val_accuracy:.4f}")

print("Converting to TensorFlow Lite format...")
converter = tf.lite.TFLiteConverter.from_keras_model(q_aware_model)
tflite_model = converter.convert()

tflite_model_path = os.path.join(os.path.dirname(weights_path), "quantized_model.tflite")
with open(tflite_model_path, "wb") as f:
f.write(tflite_model)
print(f"TFLite model saved to {tflite_model_path}")

original_size = os.path.getsize(weights_path)
tflite_size = os.path.getsize(tflite_model_path)
print(f"Original model size: {original_size / 1024:.2f} KB")
print(f"TFLite model size:  {tflite_size / 1024:.2f} KB")
print(f"Size reduction: {(100 * (original_size - tflite_size) / original_size):.2f}%")

weights_path = '/mnt/e/models/vgg16_fold_5.h5'
base_dir = '/mnt/e/Dataset'

print("Creating data generators...")
train_gen, val_gen = create_data_generators(base_dir, hyperparams\['batch_size'\])

clear_memory()

print("Debugging saved model...")
debug_saved_model(weights_path)

print("Building the model...")
model = build_model()

print("Loading weights into the model...")
model = load_weights_into_model(model, weights_path)

print("Quantizing and evaluating the model...")
quantize_and_evaluate_model(model, train_gen, val_gen, hyperparams)
Версия компилятора Edge TPU — 16.0.384591198.
Версия Tensorflow — 2.14.
Версия Tensorflow Lite — 2.14.0.
Журналы выполнения кода выглядят следующим образом:

Код: Выделить всё

Creating data generators...
Found 10595 images belonging to 2 classes.
Found 2648 images belonging to 2 classes.
Memory cleared!
Debugging saved model...
Inspecting saved model architecture...
Failed to load saved model: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 384, 288, 3), dtype=tf.float32, name='input_2'), name='input_2', description="created by layer 'input_2'") at layer "block1_conv1". The following previous layers were accessed without issue: \[\]
Ensure the architecture in build_model matches this output.
Building the model...
Loading weights into the model...
Loading weights from /mnt/e/models/vgg16_fold_5.h5...
Weights successfully loaded.
Quantizing and evaluating the model...
Applying quantization...
Training quantized model...
332/332 \[==============================\] - 187s 555ms/step - loss: 0.5684 - accuracy: 0.7594 - val_loss: 0.4845 - val_accuracy: 0.7572
Evaluating quantized model...
83/83 \[==============================\] - 18s 222ms/step - loss: 0.4845 - accuracy: 0.7572
Validation Accuracy after quantization: 0.7572
Converting to TensorFlow Lite format...
INFO:tensorflow:Assets written to: /tmp/tmp7fsapvli/assets
INFO:tensorflow:Assets written to: /tmp/tmp7fsapvli/assets
2024-12-23 23:27:27.980952: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:378\] Ignored output_format.
2024-12-23 23:27:27.980995: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:381\] Ignored drop_control_dependency.
2024-12-23 23:27:27.981622: I tensorflow/cc/saved_model/reader.cc:83\] Reading SavedModel from: /tmp/tmp7fsapvli
2024-12-23 23:27:27.988824: I tensorflow/cc/saved_model/reader.cc:51\] Reading meta graph with tags { serve }
2024-12-23 23:27:27.988849: I tensorflow/cc/saved_model/reader.cc:146\] Reading SavedModel debug info (if present) from: /tmp/tmp7fsapvli
2024-12-23 23:27:28.009524: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:382\] MLIR V1 optimization pass is not enabled
2024-12-23 23:27:28.014241: I tensorflow/cc/saved_model/loader.cc:233\] Restoring SavedModel bundle.
2024-12-23 23:27:28.234972: I tensorflow/cc/saved_model/loader.cc:217\] Running initialization op on SavedModel bundle at path: /tmp/tmp7fsapvli
2024-12-23 23:27:28.290318: I tensorflow/cc/saved_model/loader.cc:316\] SavedModel load for tags { serve }; Status: success: OK. Took 308699 microseconds.
TFLite model saved to /mnt/e/models/quantized_model.tflite
Original model size: 60662.27 KB
TFLite model size: 58585.52 KB
Size reduction: 3.42%
Я запускаю tensorflow на WSL2 и запускаю команду Edgetpu_compiler /mnt/e/models/quantized_model.tflite, чтобы попытаться преобразовать модель в TFLite, однако получаю ошибки :

Код: Выделить всё

edgetpu_compiler /mnt/e/models/quantized_model.tflite
Edge TPU Compiler version 16.0.384591198
ERROR: Didn't find op for builtin opcode 'QUANTIZE' version '3'. An older version of this builtin might be supported. Are you using an old TFLite binary with a newer model?

ERROR: Registration failed.

Invalid model: /mnt/e/models/quantized_model.tflite
Model could not be parsed

Почему я сталкиваюсь с этой ошибкой во время компиляции Edge TPU?
Как я могу обеспечить совместимость между моделью TFLite и компилятором Edge TPU?
Любые идеи или предложения будут полезны быть очень оценены. Спасибо!

Подробнее здесь: https://stackoverflow.com/questions/793 ... -version-3
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»