Ludwig: Exporting a savedmodel from a loaded model

Created on 23 Aug 2020  路  4Comments  路  Source: ludwig-ai/ludwig

EDIT: nevermind... it's already marked as a TODO TF2
https://github.com/uber/ludwig/blob/f167981683c067b50be6a3656cbf553efbf192e9/ludwig/models/trainer.py#L1116-L1145

Do you want a separate issue for the actual issue?
In any case, here's the code I tried to use (several variations, but with the same outcome)
from ludwig.api import LudwigModel
ludwig_model = LudwigModel.load('C:\\blabla\\model_run\\model')
ludwig_model.model.save_savedmodel('C:\\blabla\\savedmodel')

Note that running a prediction on said model works fine:
import pandas as pd
prediction = model.predict(data_df=pd.DataFrame({'ImageFile': ['C:\\somewhere\\over\\the\\rainbow.jpg']}))

I even tried running the prediction before trying to save the savedmodel (as per the raiserror instruction, somewhat), but with no avail. I suspect it has something to do on how each model might be defined. Different model definitions will

WARNING:tensorflow:Skipping full serialization of Keras layer , because it is not built.
Traceback (most recent call last):
File "C:/somewhere/scratch.py", line 3, in
model.model.save_savedmodel('C:\somewhere\savedmodel')
File "C:\Program Files\Python36\lib\site-packages\ludwig\models\trainer.py", line 1139, in save_savedmodel
self.model.save(save_path)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\engine\network.py", line 1052, in save
signatures, options)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\save.py", line 138, in save_model
signatures, options)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\saved_model\save.py", line 65, in save
saving_utils.raise_model_input_error(model)
File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\saving\saving_utils.py", line 95, in raise_model_input_error
'model._set_inputs(inputs).'.format(model))
ValueError: Model cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit() or .predict(). To manually set the shapes, call model._set_inputs(inputs).

I tried doing that with the save_for_serving API call, and it breaks down in a different way:
from ludwig.api import LudwigModel
model = LudwigModel.load('C:\\somewhere\\model_run\\model')
model.save_for_serving('C:\\somewhere\\savedmodel')

Traceback (most recent call last):
File "C:/somewhere/scratch.py", line 6, in
model.save_for_serving('C:\somewhere\savedmodel')
File "C:\Program Files\Python36\lib\site-packages\ludwig\api.py", line 313, in save_for_serving
if (self.model is None or self.model._session is None or
AttributeError: 'Trainer' object has no attribute '_session'

which refers to this
https://github.com/uber/ludwig/blob/f167981683c067b50be6a3656cbf553efbf192e9/ludwig/api.py#L313-L315

_Originally posted by @carlogrisetti in https://github.com/uber/ludwig/issues/841#issuecomment-678616694_

bug

Most helpful comment

FYI, it works perfectly now :)

All 4 comments

In the tests we train before saving, we should add also loading before saving. This looks like a job for the save procedure used for building the graph for collecting weights and activations.
@tgaddair @jimthompson5802 adding it to the todos.

@tgaddair as you merged the last fix on saved model, would you mind checking this out? I believe just obtaining the connected model should solve the issue.

Sure, I'll take a look.

FYI, it works perfectly now :)

Was this page helpful?
0 / 5 - 0 ratings