2

我遵循TensorFlow的詩人教程,並用我自己的幾個班級取代了股票flower_photos。現在我已將我的labels.txt文件和我的graph.pb保存在本地計算機上。如何將本地培訓的TensorFlow圖形文件部署到Google Cloud Platform?

我有什麼方法可以將此預先訓練的模型部署到Google Cloud Platform?我一直在閱讀文檔,所有我能找到的是關於如何在ML引擎中創建,訓練和部署模型的說明。但我不想花錢在Google的服務器上訓練我的模型,因爲我只需要他們託管我的模型,以便我可以將其稱爲預測。

其他人遇到同樣的問題?

回答

1

部署本地訓練模型是一個受支持的用例;在instructions基本上是一樣的,不管你在那裏受過訓練的吧:

要部署你需要一個模型版本:

一個TensorFlow SavedModel保存在谷歌雲存儲。你可以得到一個 模型:

  • 繼雲ML引擎的訓練步驟在 雲培訓。

  • 在其他地方培訓並導出到SavedModel。

不幸的是,TensorFlow for Poets不顯示如何導出SavedModel(我已經提交了功能要求,以解決)。在此期間,你可以寫一個「轉換器」腳本如下內容(你可以交替做這個在訓練結束,而不是節約出來graph.pb和閱讀它的回):

基於
input_graph = 'graph.pb' 
saved_model_dir = 'my_model' 

with tf.Graph() as graph: 
    # Read in the export graph 
    with tf.gfile.FastGFile(input_graph, 'rb') as f: 
     graph_def = tf.GraphDef() 
     graph_def.ParseFromString(f.read()) 
     tf.import_graph_def(graph_def, name='') 

    # CloudML Engine and early versions of TensorFlow Serving do 
    # not currently support graphs without variables. Add a 
    # prosthetic variable. 
    dummy_var = tf.Variable(0) 

    # Define SavedModel Signature (inputs and outputs) 
    in_image = graph.get_tensor_by_name('DecodeJpeg/contents:0') 
    inputs = {'image_bytes': 
tf.saved_model.utils.build_tensor_info(in_image)} 

    out_classes = graph.get_tensor_by_name('final_result:0') 
    outputs = {'prediction': tf.saved_model.utils.build_tensor_info(out_classes)} 

    signature = tf.saved_model.signature_def_utils.build_signature_def(
     inputs=inputs, 
     outputs=outputs, 
     method_name='tensorflow/serving/predict' 
) 

    # Save out the SavedModel. 
    b = saved_model_builder.SavedModelBuilder(saved_model_dir) 
    b.add_meta_graph_and_variables(sess, 
           [tf.saved_model.tag_constants.SERVING], 
           signature_def_map={'predict_images': signature}) 
    b.save() 

(未經測試的代碼this codelabthis SO post)。

如果你想輸出使用字符串標籤,而不是整數索引,做如下改變:

# Loads label file, strips off carriage return 
    label_lines = [line.rstrip() for line 
       in tf.gfile.GFile("retrained_labels.txt")] 
    out_classes = graph.get_tensor_by_name('final_result:0') 
    out_labels = tf.gather(label_lines, ot_classes) 
    outputs = {'prediction': tf.saved_model.utils.build_tensor_info(out_labels)} 
1

部分答案而已,很不幸,但我已經能夠做到這一點......但也有一些我還沒有解決的持續性問題。我將訓練有素的pb和txt文件移植到我的服務器上,安裝了Tensorflow,並通過HTTP請求調用訓練好的模型。它在第一次運行中完美地工作。然後每隔一段時間都失敗。

tensorflow deployment on openshift, errors with gunicorn and mod_wsgi

驚訝有沒有更多的人在那裏嘗試後這個普遍問題中去。

相關問題