david8862/keras-YOLOv3-model-set

saving model in tf format causes loading issues

Opened this issue · 1 comments

I am trying to get away from saving the model as .h5 format, and saving it as saved_model format link. When I load the model using load_model call, I come across an error related to custom yolo_loss layer in yolo3. The model type I picked for training is yolo3_xception.

from tensorflow.keras.models import load_model

yolo_model_keras = "/workspaces/yolo/model"
model = load_model(yolo_model_keras)

Here is the error code:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:1045: UserWarning: yolo3.loss is not loaded, but a Lambda layer uses it. It may cause errors.
, UserWarning)
Traceback (most recent call last):
File "main.py", line 4, in
temp_model = load_model(yolo_model_keras)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py", line 187, in load_model
return saved_model_load.load(filepath, compile, options)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py", line 121, in load
path, options=options, loader_cls=KerasObjectLoader)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py", line 633, in load_internal
ckpt_options)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py", line 194, in init
super(KerasObjectLoader, self).init(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py", line 130, in init
self._load_all()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py", line 221, in _load_all
self._finalize_objects()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py", line 530, in _finalize_objects
self._reconstruct_all_models()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py", line 548, in _reconstruct_all_models
self._reconstruct_model(model_id, model, layers)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/saved_model/load.py", line 589, in _reconstruct_model
config, created_layers={layer.name: layer for layer in layers})
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py", line 1214, in reconstruct_from_config
process_node(layer, node_data)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py", line 1162, in process_node
output_tensors = layer(input_tensors, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 926, in call
input_list)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 1117, in _functional_construction_call
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py", line 903, in call
result = self.function(inputs, **kwargs)
File "/workspaces/yolo/bin/yolo3/loss.py", line 294, in yolo3_loss
grid, raw_pred, pred_xy, pred_wh = yolo3_decode(yolo_outputs[i],
NameError: name 'yolo3_decode' is not defined

importing the following lines also doesn't help, the error changes to:

from yolo3.loss import yolo3_loss
from yolo3.postprocess import yolo3_decode

File "/workspaces/yolo/bin/yolo3/loss.py", line 295, in yolo3_loss
anchors[anchor_mask[i]], num_classes, input_shape, scale_x_y=scale_x_y[i], calc_loss=True)
TypeError: list indices must be integers or slices, not list

I believe this has to do with handling of custom layer during model build. Any idea how to save and load using this method?

@mesolmaz not suggest to save the training checkpoint to save_model format. If hoping to use save_model for inference, you can dump out the checkpoint to inference model with TF save_model. see here