使用TensorFlow检查点在C 中还原模型

Using Tensorflow checkpoint to restore model in C++

本文关键字:还原 模型 TensorFlow 检查点 使用      更新时间:2023-10-16

我已经训练了我使用python使用TensorFlow实现的网络。最后,我用tf.train.saver((保存了模型。现在,我想使用C 使用此预训练的网络进行预测。

我该怎么做?有没有办法转换检查点,因此我可以将其与Tiny-DNN或Tensorflow C ?

一起使用

欢迎任何想法:)谢谢!

您可能应该以SavedModel格式导出该模型,该格式封装了计算图和保存变量(tf.train.Saver仅保存变量,因此无论如何您都必须保存图形(。

然后,您可以使用LoadSavedModel加载保存的模型。

确切的调用将取决于模型的输入和输出。但是Python代码看起来像这样:

# You'd adjust the arguments here according to your model
signature = tf.saved_model.signature_def_utils.predict_signature_def(                                                                        
  inputs={'image': input_tensor}, outputs={'scores': output_tensor})                                                                         

builder = tf.saved_model.builder.SavedModelBuilder('/tmp/my_saved_model')                                                                    
builder.add_meta_graph_and_variables(                                                                                                        
   sess=sess,                                                                                                                    
   tags=[tf.saved_model.tag_constants.SERVING],                                                                                             
   signature_def_map={                                                                                                       
 tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:                                                                
        signature                                                                                                                        
})                                                                                                                                       
builder.save()

然后在C 中您会做这样的事情:

tensorflow::SavedModelBundle model;
auto status = tensorflow::LoadSavedModel(session_options, run_options, "/tmp/my_saved_model", {tensorflow::kSavedModelTagServe}, &model);
if (!status.ok()) {
   std::cerr << "Failed: " << status;
   return;
}
// At this point you can use model.session

(请注意,使用SavedModel格式也将允许您使用TensorFlow服务使用模型,如果对您的应用程序有意义(

希望会有所帮助。