(Python)张量流中保存的权重未在C ++ tf

时间:2018-06-27 02:08:02

标签: python c++ tensorflow keras

我一直跟踪链接文章,最后设法将训练有素的tensorflow模型的图和权重加载到C ++中,而不会向我抛出错误,但是似乎不能正确地加载权重。我有可能错过了一步,最有可能在推断部分。

我在下面提供了一个功能齐全的最小工作示例。为了运行它,您可能(可能)需要Python + 1.4.0

将“训练”模型并保存其权重的Python代码,如here所示:

from __future__ import absolute_import, division, print_function
import os
import tensorflow as tf
import numpy as np
import random
import keras
from keras import backend as K

# Set it manually so C++ interface can use mem growth
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
K.set_session(sess)

model = tf.keras.Sequential([
    tf.keras.layers.Convolution2D(64, (5, 5), input_shape=(7, 19, 19), data_format='channels_first', name='Input'),
    tf.keras.layers.ZeroPadding2D(padding =(4, 4), data_format='channels_first', name='Pad0'),
    tf.keras.layers.BatchNormalization(axis=1, momentum=0.99, name='Norm0'), 
    tf.keras.layers.Dropout(0.25, name='Drop0'),
    tf.keras.layers.Flatten(name='Flatten0'),
    tf.keras.layers.Dense(361, activation='softmax', name='Output'),
])

X = np.ones((25, 7, 19, 19))
Y = np.zeros((25, 361))

optimizer = tf.train.AdamOptimizer(learning_rate=0.0018)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

x = np.zeros((1, 7, 19, 19))
print(model.predict(x))

model.fit(X, Y, 1, 5, 2)
print(model.predict(x)) # Just to verify output is different after training

K.set_learning_phase(0)
sess.run(tf.global_variables_initializer())

saver = tf.train.Saver(tf.global_variables())
saver.save(sess, save_path='./models/myModel')
// Just used to look at graph structure
tf.train.write_graph(sess.graph, '.', './models/graph.pb', as_text=True)

和目前使用tf 1.5.0的C ++代码:

用于加载模型的代码,如图here

#define _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS
#define COMPILER_MSVC
#define NOMINMAX
#include <iomanip>
#include <tensorflow\core\public\session.h>
#include <tensorflow\core\protobuf\meta_graph.pb.h>
#include <tensorflow\core\framework\tensor.h>
#include <tensorflow\cc\ops\standard_ops.h>

namespace tf = tensorflow;
tf::Status status;
tf::Session* session;
tf::SessionOptions options;
tf::MetaGraphDef graphDef;
std::string pathToGraph = "models/myModel.meta";
std::string pathToCheckpoint = "models/myModel";

int main()
{
    options.config.mutable_gpu_options()->set_allow_growth(true);
    options.config.mutable_gpu_options()->set_per_process_gpu_memory_fraction(1.0);

    session = tf::NewSession(options);
    if (!session)
         throw std::runtime_error("Could no create Tensorflow Session!");

    // Read in the protobuf
    status = tf::ReadBinaryProto(tf::Env::Default(), pathToGraph, &graphDef);
    if (!status.ok())
        throw std::runtime_error("Error reading graph: " + status.ToString());

    status = session->Create(graphDef.graph_def());
    if (!status.ok())
        throw std::runtime_error("Error creating graph: " + status.ToString());

    // Read the weights
    tf::Tensor checkpointPathTensor(tf::DT_STRING, tf::TensorShape());
    checkpointPathTensor.scalar<std::string>()() = pathToCheckpoint;

    const auto fileTensorName = graphDef.saver_def().filename_tensor_name();
    const auto restoreOpName = graphDef.saver_def().restore_op_name();

    status = session->Run(
        { { fileTensorName, checkpointPathTensor }, },
        {},
        { restoreOpName },
        nullptr
    );

    if (!status.ok())
        throw std::runtime_error("Error loading checkpoint from " + pathToCheckpoint + ": " + status.ToString());

    ...

接着是代码以对加载/训练的模型进行推断

    float inData[2527] = { 0.f };

    static const std::string inputName = "Input_input";
    static const std::string outputName = "Output/Softmax";

    static const auto shape = tf::TensorShape({ 1, 7, 19, 19});
    tf::Tensor input(tf::DT_FLOAT, shape);
    std::copy_n(inData, 2527, input.flat<float>().data());

    std::vector<tf::Tensor> outputs;
    status = session->Run({ { inputName, input } }, { outputName }, {}, &outputs);

    tf::TTypes<float>::Flat flatOut = outputs[0].flat<float>();

    for (int i = 0; i < 361; ++i)
    {
        if (i % 19 == 0)
            std::cout << '\n';
        std::cout << std::setw(8) << std::fixed << std::setprecision(8) << flatOut(i) << ", ";
    }

    std::cout << '\n';

}

对我来说,当我运行此命令时,一切都可以在Python部分中正常运行,但是在C ++部分中无提示地无法加载经过训练的图形(或执行类似的操作)。运行C ++部分时,模型输出与未经训练的Python模型完全相同的输出。我在这里缺少一些步骤吗?

在模型训练之前对Python输出进行采样:

[[0.00277008 0.00277008 0.00277008 0.00277008 0.00277008 0.00277008 
0.00277008 0.00277008 0.00277008 0.00277008 0.00277008  ...etc ]]

使用Python训练后的示例输出:

.00387822 0.00228055 0.0018196  0.0014322  0.00266262 0.00234695 
0.0026182  0.00322318 0.00252047 0.00353322 0.00342526 ...etc ]]

加载模型并以相同的输入在C ++中运行后的示例输出:

0.00277008, 0.00277008, 0.00277008, 0.00277008, ... etc

理想情况下,我希望看到训练后的C ++输出与Python输出相同!

1 个答案:

答案 0 :(得分:0)

您可以从tf_label_image_example项目中修改main.cc。

但是,当我使用CMake构建tensorflow时,我将tensorflow_BUILD_CC_EXAMPLE = ON设置为ON。

您可以参考此http://www.stefanseibert.com/2017/10/tensorflow-as-dll-into-your-windows-c-project-with-gpu-support-and-cmake-v1-3/

此外,我构建了一个与tf_label_image_example_project类似的Visual Studio Project(VS 2015 x64版本)。

实际上,我使用Visual Studio打开了tf_label_image_example_project,并将许多配置复制到了我的项目中。最重要的是,*。vcxproj和* .vcxproj.filters中的对象库。