2

I used the tensorflow2 object detection API. I received a saved_model.pb which is a TensorFlow graph and not a tf.keras model. So it can be loaded with tf.saved_model.load() but not with tf.keras.load_model(). The model is saved via the tf.saved_model.save() in the export_lib_v2.py of the object detection API in line 271.

I tried to build the model from the config file and load the checkpoints, to then save it as a tf.keras model:

import tensorflow as tf
from object_detection.utils import config_util
from object_detection.builders import model_builder
import os
 
def save_in_tfkeras(save_filepath,label_map_path, config_file_path, checkpoint_path):
    configs = config_util.get_configs_from_pipeline_file(config_file_path)
    model_config = configs['model']
    detection_model = model_builder.build(model_config=model_config, is_training=False)
 
    # Restore checkpoint
    ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
    ckpt.restore(checkpoint_path).expect_partial()
    detection_model.built(input_shape=(320,320))

    tf.keras.models.save_model(detection_model, save_filepath)
    print('modelsaved as tf.keras in ' + save_filepath)
    
if __name__ == "__main__":    
    PATH_TO_LABELMAP = './models/face_model/face_label.pbtxt'
    PATH_TO_CONFIG = './models/face_model/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/pipeline.config'
    PATH_TO_CHECKPOINT = './models/face_model/v2_model_50k/ckpt-51'    
    
    save_filepath='./Kmodels/mobileNet_V2'
    if not os.path.exists(save_filepath):
        os.makedirs(save_filepath)
    save_in_tfkeras(save_filepath,PATH_TO_LABELMAP,PATH_TO_CONFIG,PATH_TO_CHECKPOINT)

However, this does not seem to work. There are errors which origin, in my opinion, in mixing the tf and tf.keras model. The last error message:

ValueError: Weights for model ssd_mobile_net_v2fpn_keras_feature_extractor_1 have not yet been created. Weights are created when the Model is first called on inputs or build() is called with an input_shape.

The model was saved with TensorFlow loaded as tensorflow.compat.v2

Question: Is there a way to build the model, load the checkpoint weights and then save as tf.keras model?

Towlie
  • 77
  • 1
  • 6

0 Answers0