Python Forum
problem with the return of flask - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: problem with the return of flask (/thread-27327.html)



problem with the return of flask - loutsi - Jun-03-2020

I have already built an lstm model and now I want to post some values and get back the results.

so when I go to the postman and in http://127.0.0.1:8000/api i post the following raw data {"accx":0.660583, "accy":0.454468, "accz":-0.585022, "gyrx":32.366615, "gyry":27.206556, "gyrz":-23.471800} i get back

ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (6, 1)

any ideas: the code is below

    from flask import Flask, request, jsonify#--jsonify will return the data
    from keras.models import load_model
    import numpy as np
    import pandas as pd
    
    
    
    #creates the web service running on port 127.0.0.1:12345 and answer post requests on adress/api
    app = Flask(__name__)
    
    
    model=load_model('flask_model.h5')
    
    @app.route("/")
    def hello():
        return "machine learning model APIs!"
    
    
    @app.route('/api', methods=["POST"])
    def predict():
        
        # get the json send from the post as data
        data=request.get_json(force=True)
        print (data)
        
        #i expected jason to have the following values
        predict_request=[data["accx"], data["accy"], data["accz"],data["gyrx"], data["gyry"], data["gyrz"]]
        print (predict_request)
        
        predict_request= np.array(predict_request)
        print (predict_request)
    
        pred= model.predict(predict_request)
        print (pred)
        
        output=[pred[0]]
        print (output)
        
        #take a list of dictionaries and convert them to jason
        return jsonify(results=output)
        
    
    if __name__ == '__main__':
         app.run(port=8000, debug=True)



RE: problem with the return of flask - scidam - Jun-03-2020

First of all, you can debug the problem without flask. E.g. set data variable
and run the lstm model. I suspect, the error says that the model expects array of 3 dims (maybe this last dimension is batch size...) Try to reshape your array, e.g. predict_request=np.array(predict_request)[np.newaxis, ...].


RE: problem with the return of flask - loutsi - Jun-03-2020

@scidam thanks you for the answer. let me ask you something. I have the model loaded in the server. The client sends some data. So when the data receive the model mustn't make the proprocess steps in order to bring the received raw data in order that the model can understand? In my case samples/timestamps/features?


RE: problem with the return of flask - scidam - Jun-04-2020

It depends. Some preprocessing steps may be required before passing the data to the model. In you case, model.predict probably expects as input a row of floating point numbers (1D array); So, you need to ensure that predict_request (after conversion to numpy array) has float dtype (e.g. numpy.float64). Hope that helps.


RE: problem with the return of flask - loutsi - Jun-04-2020

thank you. This is really helpful