Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with the return of flask
#1
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)
Reply
#2
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, ...].
Reply
#3
@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?
Reply
#4
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.
Reply
#5
thank you. This is really helpful
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Return Frame as well as JSON response using same API in Flask Python Ask jenkins43 0 1,826 May-11-2020, 04:58 PM
Last Post: jenkins43
  Simple flask rest api problem cancerboi 4 2,782 Jan-29-2020, 03:10 PM
Last Post: brighteningeyes
  got some problem with flask signup form. darktitan 1 2,213 Aug-30-2019, 06:05 AM
Last Post: aligoren
  Flask return http status code 200 but web browser not recive supernoobs 2 10,478 Dec-29-2018, 09:27 PM
Last Post: Unisoftdev

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020