I try use a tflite model training from teachablemachine.withgoogle.com with google_mlkit_object_detection (Flutter), it working but without labels and incorrect.
Firstly, I add metadata and labels to my tflite with code :
from tflite_support import metadata as _metadata
from tflite_support import metadata_schema_py_generated as _metadata_fb
import flatbuffers
import os
def create_metadata(model_path, normalized_model_path):
# Load the model file
with open(model_path, ‘rb’) as f:
model_buffer = f.read()
# Create a model metadata populator
populator = _metadata.MetadataPopulator.with_model_buffer(model_buffer)
# Create NormalizationOptions
normalization_options = _metadata_fb.NormalizationOptionsT()
normalization_options.mean = [127.5] # Example value, adjust as necessary
normalization_options.std = [127.5] # Example value, adjust as necessary
# Create input tensor metadata
input_meta = _metadata_fb.TensorMetadataT()
input_meta.name = "input_image"
input_meta.description = "Input image to be processed."
input_meta.content = _metadata_fb.ContentT()
input_meta.content.contentProperties = _metadata_fb.ImagePropertiesT()
input_meta.content.contentProperties.colorSpace = _metadata_fb.ColorSpaceType.RGB
input_meta.processUnits = [_metadata_fb.ProcessUnitT()]
input_meta.processUnits[0].optionsType = _metadata_fb.ProcessUnitOptions.NormalizationOptions
input_meta.processUnits[0].options = normalization_options
# Create output tensor metadata
output_meta = _metadata_fb.TensorMetadataT()
output_meta.name = "output"
output_meta.description = "Predicted class probabilities."
# Path to label file
label_file_path = "labels.txt"
# Add the label file info to the populator
populator.load_associated_files([label_file_path])
# Create subgraph info
subgraph = _metadata_fb.SubGraphMetadataT()
subgraph.inputTensorMetadata = [input_meta]
subgraph.outputTensorMetadata = [output_meta]
# Create label file info
label_file_meta = _metadata_fb.AssociatedFileT()
label_file_meta.name = os.path.basename(label_file_path)
label_file_meta.description = "Labels for objects that the model can recognize."
label_file_meta.type = _metadata_fb.AssociatedFileType.TENSOR_AXIS_LABELS
subgraph.associatedFiles = [label_file_meta]
# Set metadata
model_metadata = _metadata_fb.ModelMetadataT()
model_metadata.name = "Image Classification Model"
model_metadata.description = "Detects objects in images"
model_metadata.version = "v1"
model_metadata.author = "Your Name"
model_metadata.license = "Apache License 2.0"
model_metadata.subgraphMetadata = [subgraph]
# Serialize metadata and save it to the model file
b = flatbuffers.Builder(0)
b.Finish(
model_metadata.Pack(b),
_metadata.MetadataPopulator.METADATA_FILE_IDENTIFIER)
metadata_buf = b.Output()
populator.load_metadata_buffer(metadata_buf)
populator.populate()
# Save the new model with metadata
with open(normalized_model_path, 'wb') as f:
f.write(populator.get_model_buffer())
Example usage
create_metadata(‘detect_mask_train.tflite’, ‘detect_mask_with_label.tflite’)
finally, I used detect_mask_with_label.tflite to google_mlkit_object_detection.
I want to know where I went wrong, and how I can use my TFLite (detect_mask_with_label.tflite) model with google_mlkit_object_detection.
help me, pls Thanks!