2017-07-12 21 views
0

系統信息 OS平臺和分銷(例如,Linux操作系統Ubuntu 16.04):導出和裝載模式

MAC OS塞拉利昂(10.12.5)

TensorFlow從安裝:

使用PIP

TensorFlow版本(使用以下命令): 1.2.1

問題:

我試圖保存和恢復從Python到Python培訓的模型。 我把模型保存在三個.chkpt文件(meta,index和data-000000-of-00001)中,我試圖將它讀入我的會話中,使用add_meta_graph_and_variables保存模型,然後使用loader :loader.load(session,[tf.saved_model.tag_constants.TRAINING], pathToSaveModel)

這是我的代碼:

首先,我從包含「數據」,「指數」和「元」三個文件(元圖和權重「)爲我的會話中使用保護恢復恢復的權重:

with tf.Session(graph=tf.Graph()) as session: 
    ##HERE IS THE CODE OF MY NETWORK (Very long) 

    session.run(tf.global_variables_initializer()) 
    #Load 
    saver = tf.train.Saver() 
    saver.restore(session, "newModel.chkpt") 

    features = loadFeatures(["cat2.jpg"]) 
    res = predictions.eval(
      feed_dict={ 
       x: features, 
       keep_prob: 1.0, }) 
    print('Image {} has a prob {} '.format(image, res)) 

    b = saved_model_builder.SavedModelBuilder(pathToSaveModel) 
    b.add_meta_graph_and_variables(session, [tf.saved_model.tag_constants.TRAINING]) 
    b.save() 

有了這個代碼,我有一個很好的分類,最後含保存add_meta_graph_and_variables模型一個新的文件夾: Folder with the model saved with

現在,我想用保存的模型進行分類,agai ñ,相同的圖像。這一次我用裝載機,而不是恢復:

with tf.Session(graph=tf.Graph()) as session: 
    ##HERE IS THE CODE OF MY NETWORK (Very long) 

    #session.run(tf.global_variables_initializer()) 
    #Load 
    from tensorflow.python.saved_model import loader 
    loader.load(session, [tf.saved_model.tag_constants.TRAINING], pathToSaveModel) 

    features = loadFeatures(["cat2.jpg"]) 
    res = predictions.eval(
      feed_dict={ 
       x: features, 
       keep_prob: 1.0, }) 
    print('Image {} has a prob {} '.format(image, res)) 

在這裏,問題來了:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value b_fcO 
    [[Node: b_fcO/read = Identity[T=DT_FLOAT, _class=["loc:@b_fcO"], _device="/job:localhost/replica:0/task:0/cpu:0"](b_fcO)]] 

如果我試着使用:session.run(tf.global_variables_initializer()),那麼它作品,但分類是無效的,我認爲權重不是從一開始就被導出/導入,而且在測試之後我遇到了很多事情。

我做錯了什麼線索?

在此先感謝。

更新: 這是模型是如何在三個文件中開頭: ckpt files

回答

0

只是你應該檢查的幾件事情是:

  • 什麼是pathToSaveModel?
  • 檢查點文件在哪裏?
  • 用文本編輯器打開檢查點文件:它指向哪個文件夾?
  • 是權重的路徑是否正確?

通過回顧這些問題,我總是能夠找到我犯的錯誤。希望能幫助到你!

+0

嗨rmeertens,我不確定你的意思是什麼chkpt文件。這三個文件在開始處是二進制文件,而不是文本文件,並且它們中沒有包含任何路徑(據我所知)。我已經調試了代碼,內部加載器的路徑正在構建好。 –

+0

據我所知,Tensorflow總是在檢查點文件中保存變量的文件名。 https://www.tensorflow.org/programmers_guide/variables。另外:您可能想再看看這裏:https://www.tensorflow.org/api_docs/python/tf/saved_model/builder/SavedModelBuilder – rmeertens