<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Correct setup and format for calling REST API for image classification in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/correct-setup-and-format-for-calling-rest-api-for-image/m-p/29210#M20956</link>
    <description>&lt;P&gt;@Anthony Gibbons​&amp;nbsp; may be this git should work with your use case - &lt;A href="https://github.com/mlflow/mlflow/issues/1661" target="test_blank"&gt;https://github.com/mlflow/mlflow/issues/1661&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Mar 2022 04:40:04 GMT</pubDate>
    <dc:creator>Atanu</dc:creator>
    <dc:date>2022-03-16T04:40:04Z</dc:date>
    <item>
      <title>Correct setup and format for calling REST API for image classification</title>
      <link>https://community.databricks.com/t5/data-engineering/correct-setup-and-format-for-calling-rest-api-for-image/m-p/29205#M20951</link>
      <description>&lt;P&gt;I trained a basic image classification model on MNIST using Tensorflow, logging the experiment run with MLflow.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Model: "my_sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 reshape (Reshape)         (None, 28, 28, 1)         0         
                                                                 
 conv2d (Conv2D)           (None, 26, 26, 32)        320       
                                                                 
 max_pooling2d (MaxPooling  (None, 13, 13, 32)       0         
 2D)                                                             
                                                                 
 flatten (Flatten)         (None, 5408)              0         
                                                                 
 dense (Dense)            (None, 100)               540900    
                                                                 
 dense_1 (Dense)            (None, 10)                1010      
                                                                 
=================================================================
Total params: 542,230
Trainable params: 542,230
Non-trainable params: 0
_________________________________________________________________&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE&gt;with mlflow.start_run() as run:
  run_id       = run.info.run_id
  
  mlflow.tensorflow.autolog()
&amp;nbsp;
  model.fit(trainX, trainY, 
            validation_data = (testX, testY), 
            epochs  = 2, 
            batch_size  = 64)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I then registered the model and enabled model serving.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When trying to send the JSON text through the browser in the form&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;[{"b64": "AA...AA=="}]&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm getting errors like the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;BAD_REQUEST: Encountered an unexpected error while evaluating the model. Verify that the serialized input Dataframe is compatible with the model for inference.
&amp;nbsp;
Traceback (most recent call last):
  File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/mlflow/pyfunc/scoring_server/__init__.py", line 306, in transformation
    raw_predictions = model.predict(data)
  File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/mlflow/pyfunc/__init__.py", line 605, in predict
    return self._model_impl.predict(data)
  File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/mlflow/keras.py", line 475, in predict
    predicted = _predict(data)
  File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/mlflow/keras.py", line 462, in _predict
    predicted = pd.DataFrame(self.keras_model.predict(data.values))
  File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 1147, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
&amp;nbsp;
    File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/keras/engine/training.py", line 1801, in predict_function  *
        return step_function(self, iterator)
    File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/keras/engine/training.py", line 1790, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/keras/engine/training.py", line 1783, in run_step  **
        outputs = model.predict_step(data)
    File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/keras/engine/training.py", line 1751, in predict_step
        return self(x, training=False)
    File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/databricks/conda/envs/model-10/lib/python3.8/site-packages/keras/layers/core/reshape.py", line 110, in _fix_unknown_dimension
        raise ValueError(msg)
&amp;nbsp;
&amp;nbsp;
ValueError: Exception encountered when calling layer "reshape" (type Reshape).
&amp;nbsp;
  total size of new array must be unchanged, input_shape = [1], output_shape = [28, 28, 1]
&amp;nbsp;
  Call arguments received:
&amp;nbsp;
   • inputs=tf.Tensor(shape=(None, 1), dtype=float32)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="mnist_model_error"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/2117iA7E7D930A2F5D2D1/image-size/large?v=v2&amp;amp;px=999" role="button" title="mnist_model_error" alt="mnist_model_error" /&gt;&lt;/span&gt;This seems to be because I'm passing the image data as an encoded byte string, not a numpy array. According to the &lt;A href="https://www.tensorflow.org/tfx/serving/api_rest#encoding_binary_values" alt="https://www.tensorflow.org/tfx/serving/api_rest#encoding_binary_values" target="_blank"&gt;TensorFlow documentation&lt;/A&gt;, this is how it has to be passed.&lt;/P&gt;&lt;P&gt;If I have an image with shape (28,28,1), called img, I am converting it to the required format like this&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;image_data = base64.b64encode(img)
json = {"b64": image_data.decode()}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My question has two parts:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;How do I adjust my model to handle the &lt;B&gt;b64 encoded string and convert it back to a 28x28 image&lt;/B&gt; first?&lt;/LI&gt;&lt;LI&gt;What is the exact &lt;B&gt;JSON format I need to send the image data&lt;/B&gt; to the REST endpoint?&lt;/LI&gt;&lt;/OL&gt;</description>
      <pubDate>Mon, 07 Feb 2022 16:28:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/correct-setup-and-format-for-calling-rest-api-for-image/m-p/29205#M20951</guid>
      <dc:creator>gibbona1</dc:creator>
      <dc:date>2022-02-07T16:28:46Z</dc:date>
    </item>
    <item>
      <title>Re: Correct setup and format for calling REST API for image classification</title>
      <link>https://community.databricks.com/t5/data-engineering/correct-setup-and-format-for-calling-rest-api-for-image/m-p/29208#M20954</link>
      <description>&lt;P&gt;Hi @Kaniz Fatma​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;Thanks for your answer!&lt;/P&gt;&lt;P&gt;My question is about this backend. You mean putting this line inside the predict() method?&lt;/P&gt;&lt;P&gt;When I'm defining a sequential model in TensorFlow, how do I incorporate what I want it to do to the input from a request?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 12:57:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/correct-setup-and-format-for-calling-rest-api-for-image/m-p/29208#M20954</guid>
      <dc:creator>gibbona1</dc:creator>
      <dc:date>2022-02-18T12:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Correct setup and format for calling REST API for image classification</title>
      <link>https://community.databricks.com/t5/data-engineering/correct-setup-and-format-for-calling-rest-api-for-image/m-p/29210#M20956</link>
      <description>&lt;P&gt;@Anthony Gibbons​&amp;nbsp; may be this git should work with your use case - &lt;A href="https://github.com/mlflow/mlflow/issues/1661" target="test_blank"&gt;https://github.com/mlflow/mlflow/issues/1661&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Mar 2022 04:40:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/correct-setup-and-format-for-calling-rest-api-for-image/m-p/29210#M20956</guid>
      <dc:creator>Atanu</dc:creator>
      <dc:date>2022-03-16T04:40:04Z</dc:date>
    </item>
  </channel>
</rss>

