Deploying Keras Models via Google Cloud ML

前端 未结 4 2284
情书的邮戳
情书的邮戳 2020-12-08 23:48

I am looking to use Google Cloud ML to host my Keras models so that I can call the API and make some predictions. I am running into some issues from the Keras side of things

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 23:48

    I don't know much about Keras. I consulted with some experts, and the following should work:

    from keras import backend as k
    
    # Build the model first
    model = ...    
    
    # Declare the inputs and outputs for CloudML
    inputs = dict(zip((layer.name for layer in model.input_layers),
                      (t.name for t in model.inputs)))
    tf.add_to_collection('inputs', json.dumps(inputs))
    
    outputs = dict(zip((layer.name for layer in model.output_layers),
                       (t.name for t in model.outputs)))
    tf.add_to_collection('outputs', json.dumps(outputs))
    
    # Fit/train the model
    model.fit(...)
    
    # Export the model
    saver = tf.train.Saver()
    session = K.get_session()
    saver.save(session, 'export')
    

    Some important points:

    • You have to call tf.add_to_collection after you create the model but before you ever call K.get_session(), fit etc.,
    • You should be sure set the name of input and output layers when you add them to the graph because you'll need to refer to them when you send prediction requests.

提交回复
热议问题