Ludwig: Multi-label classification for Text throws an error on /predict when "ludwig serve"

Created on 30 Sep 2020  路  4Comments  路  Source: ludwig-ai/ludwig

Describe the bug

When I am trying to serve a trained ludwig model for multi-label classification, on /predict I always get an Internal server error

To Reproduce
Steps to reproduce the behavior:

  1. create a model_definition.yaml:
    input_features: - level: "word" name: "content" type: "text" output_features: - name: tags type: set
  1. create a text_data.csv

    "content","tags"
    "hello world","hello, world, stupid"
    "my name is Bob","name, hello"
    
  2. train the model

    ludwig experiment --data_csv text_data.csv --model_definition_file model_definition.yaml
    
  3. start serve command on the newly created model

    ludwig serve --model_path ./results/experiment_run/model
    
  4. Run /predict request

    curl http://localhost:8000/predict -X POST -F "content=something"
    

Expected behavior
I'd expect to see the 200 OK status with result of prediction

Observed behavior
Request fails with 500 Internal Server Error

Logs of the ludwig:

INFO:     Started server process [753]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
Error: Object of type 'float32' is not JSON serializable
ERROR:ludwig.serve:Error: Object of type 'float32' is not JSON serializable
INFO:     172.17.0.1:57522 - "POST /predict HTTP/1.1" 500 Internal Server Error

Environment (please complete the following information):

  • Docker base image: tensorflow/tensorflow:1.15.2-py3

    • Python version 3.6.9

    • Ludwig version tested on v0.2.2 and v0.2.2.8

Additional context

Error doesn't happen on ludwig predict, it happens only when ludwig serve + /predict request

From debugging I realized that tags_probability field is a list of numpy.float32 numbers, which are not convertable to JSON via JSONResponse - so error is thrown exactly on this line: https://github.com/uber/ludwig/blob/v0.2.2.8/ludwig/serve.py#L76

I assume that the easiest way to fix it would be to convert values into float somewhere here: https://github.com/uber/ludwig/blob/v0.2.2.8/ludwig/data/postprocessing.py#L108 because when output_feature_type is set, result is processed in this else branch

bug looking into it

Most helpful comment

Pushed a commit that solves the issue. Please confirm if you can by using the code on master.

All 4 comments

@catr1ne55 thank you for posting this and for the detailed instruction for reproducing. Ludwig had a JSONEncoder that deals with numpy arrays (utils/data_utils.py::NumpyEncoder). We'll replace the current JSON decoder with that and this should work fine. Will keep you posted.

Pushed a commit that solves the issue. Please confirm if you can by using the code on master.

Some minor comments on you example that may be useful:

  1. you don't need the " in the YAML file
  2. When you have a set column, you cna format it in many ways, by default it is formatted with whitespace, meaning your column value should look like: "hello world stupid"
  3. you don't need the quotes in the CSV unless the specific value contains a comma

@w4nderlust After updating and retraining model everything works as expected.
Thank you very much for quick fix and comments!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

allenkao picture allenkao  路  3Comments

tommylees112 picture tommylees112  路  4Comments

pixxelfactory picture pixxelfactory  路  6Comments

w4nderlust picture w4nderlust  路  4Comments

jenishah picture jenishah  路  5Comments