Deploying Keras Models via Google Cloud ML

前端 未结 4 2276
情书的邮戳
情书的邮戳 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:58

    Here's another answer that may help. Assuming you already have a keras model you should be able to append this to the end of your script and get an ML Engine compatible version of the model (protocol buffer). Note that you need to upload the saved_model.pb file and the sibling directory with variables to ML Engine for it to work. Note also that the .pb file must be named saved_model.pb or saved_model.pbtxt.

    Assuming your model is name model

    from tensorflow import saved_model
    
    model_builder = saved_model.builder.SavedModelBuilder("exported_model")                                                     
    inputs = {                                                                          
        'input': saved_model.utils.build_tensor_info(model.input)                    
    }                                                                                   
    outputs = {                                                                         
        'earnings': saved_model.utils.build_tensor_info(model.output)                
    }                                                                                                                                                
    signature_def = saved_model.signature_def_utils.build_signature_def(             
        inputs=inputs,                                                                  
        outputs=outputs,                                                                
        method_name=saved_model.signature_constants.PREDICT_METHOD_NAME              
    )                                                                            
    model_builder.add_meta_graph_and_variables(                                         
        K.get_session(),                                                                
        tags=[saved_model.tag_constants.SERVING],                                    
        signature_def_map={saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature_def
        })                                                                                                                                                                       
    model_builder.save()   
    

    will export the model to directory /exported_model.

提交回复
热议问题