Ich versuche, ein Modell, das VGG-16 als Basismodell, einen Klassifikator, GlobalAveragePooling2D und Dropout hat, nach einem quantisierungsbewussten Training in Tensorflow Lite zu konvertieren, erhalte jedoch die folgende Fehlermeldung:
Edge TPU Compiler Version 16.0.384591198
FEHLER: Op für integrierten Opcode wurde nicht gefunden 'QUANTISIEREN' Version '3'. Eine ältere Version dieser integrierten Funktion wird möglicherweise unterstützt. Verwenden Sie eine alte TFLite-Binärdatei mit einem neueren Modell?
FEHLER: Registrierung fehlgeschlagen.
Ungültiges Modell: /mnt/e/models/quantized_model .tflite
Modell konnte nicht geparst werden
Ich trainiere gerade mein Modell und speichere es als .h5, lade dann seine Prüfpunkte und erstelle ein neues Modell zur Quantisierung. Wenn ich jedoch versuche, ein quantisierungsbewusstes Training anzuwenden, erhalte ich die oben erwähnte Fehlermeldung. Dies ist der Code, den ich zum Anwenden von QAT verwendet habe:
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)
Die Edge TPU Compiler-Version ist 16.0.384591198.
Die Tensorflow-Version ist 2.14.
Die Tensorflow Lite-Version ist 2.14.0.
Codeausführungsprotokolle lauten wie folgt:
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%
Ich verwende Tensorflow auf WSL2 und führe den Befehl „edgetpu_compiler /mnt/e/models/quantized_model.tflite“ aus, um zu versuchen, das Modell in TFLite zu konvertieren, erhalte jedoch Fehlermeldungen :
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
Warum tritt dieser Fehler bei der Edge-TPU-Kompilierung auf?
Wie kann ich die Kompatibilität zwischen dem TFLite-Modell und dem Edge-TPU-Compiler sicherstellen?
Irgendwelche Erkenntnisse oder Vorschläge sehr geschätzt werden. Danke!
FEHLER: Für den integrierten Opcode „QUANTIZE“ Version „3“ wurde keine Operation gefunden. ⇐ Python
-
- Similar Topics
- Replies
- Views
- Last post