Merge pull request #13532 from panyx0718/infer
Add a test to load a full model and allow to to post-train from C++develop_doc
commit
ebf9171df3
@ -0,0 +1,30 @@
|
||||
function(train_test TARGET_NAME)
|
||||
set(options "")
|
||||
set(oneValueArgs "")
|
||||
set(multiValueArgs ARGS)
|
||||
cmake_parse_arguments(train_test "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
||||
|
||||
set(PYTHON_TESTS_DIR ${PADDLE_BINARY_DIR}/python/paddle/fluid/tests)
|
||||
set(arg_list "")
|
||||
if(train_test_ARGS)
|
||||
foreach(arg ${train_test_ARGS})
|
||||
list(APPEND arg_list "_${arg}")
|
||||
endforeach()
|
||||
else()
|
||||
list(APPEND arg_list "_")
|
||||
endif()
|
||||
foreach(arg ${arg_list})
|
||||
string(REGEX REPLACE "^_$" "" arg "${arg}")
|
||||
cc_test(test_train_${TARGET_NAME}${arg}
|
||||
SRCS test_train_${TARGET_NAME}.cc
|
||||
DEPS paddle_fluid_origin
|
||||
ARGS --dirname=${PYTHON_TESTS_DIR}/book/${TARGET_NAME}${arg}.train.model/)
|
||||
set_tests_properties(test_train_${TARGET_NAME}${arg}
|
||||
PROPERTIES DEPENDS test_${TARGET_NAME})
|
||||
endforeach()
|
||||
endfunction(train_test)
|
||||
|
||||
|
||||
if(WITH_TESTING)
|
||||
train_test(recognize_digits ARGS mlp conv)
|
||||
endif()
|
@ -0,0 +1,89 @@
|
||||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License. */
|
||||
|
||||
#include <time.h>
|
||||
#include <fstream>
|
||||
|
||||
#include "gflags/gflags.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "paddle/fluid/framework/executor.h"
|
||||
#include "paddle/fluid/framework/op_registry.h"
|
||||
#include "paddle/fluid/framework/program_desc.h"
|
||||
#include "paddle/fluid/framework/tensor_util.h"
|
||||
#include "paddle/fluid/inference/io.h"
|
||||
#include "paddle/fluid/platform/device_context.h"
|
||||
#include "paddle/fluid/platform/init.h"
|
||||
#include "paddle/fluid/platform/place.h"
|
||||
|
||||
DEFINE_string(dirname, "", "Directory of the train model.");
|
||||
|
||||
namespace paddle {
|
||||
|
||||
void Train() {
|
||||
CHECK(!FLAGS_dirname.empty());
|
||||
framework::InitDevices(false);
|
||||
const auto cpu_place = platform::CPUPlace();
|
||||
framework::Executor executor(cpu_place);
|
||||
framework::Scope scope;
|
||||
|
||||
auto train_program = inference::Load(
|
||||
&executor, &scope, FLAGS_dirname + "__model_combined__.main_program",
|
||||
FLAGS_dirname + "__params_combined__");
|
||||
|
||||
std::string loss_name = "";
|
||||
for (auto op_desc : train_program->Block(0).AllOps()) {
|
||||
if (op_desc->Type() == "mean") {
|
||||
loss_name = op_desc->Output("Out")[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PADDLE_ENFORCE_NE(loss_name, "", "loss not found");
|
||||
|
||||
// prepare data
|
||||
auto x_var = scope.Var("img");
|
||||
auto x_tensor = x_var->GetMutable<framework::LoDTensor>();
|
||||
x_tensor->Resize({64, 1, 28, 28});
|
||||
|
||||
auto x_data = x_tensor->mutable_data<float>(cpu_place);
|
||||
for (int i = 0; i < 64 * 28 * 28; ++i) {
|
||||
x_data[i] = 1.0;
|
||||
}
|
||||
|
||||
auto y_var = scope.Var("label");
|
||||
auto y_tensor = y_var->GetMutable<framework::LoDTensor>();
|
||||
y_tensor->Resize({64, 1});
|
||||
auto y_data = y_tensor->mutable_data<int64_t>(cpu_place);
|
||||
for (int i = 0; i < 64 * 1; ++i) {
|
||||
y_data[i] = static_cast<int64_t>(1);
|
||||
}
|
||||
|
||||
auto loss_var = scope.Var(loss_name);
|
||||
float first_loss = 0.0;
|
||||
float last_loss = 0.0;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
executor.Run(*train_program.get(), &scope, 0, false, true);
|
||||
if (i == 0) {
|
||||
first_loss = loss_var->Get<framework::LoDTensor>().data<float>()[0];
|
||||
} else if (i == 99) {
|
||||
last_loss = loss_var->Get<framework::LoDTensor>().data<float>()[0];
|
||||
}
|
||||
}
|
||||
EXPECT_LT(last_loss, first_loss);
|
||||
}
|
||||
|
||||
TEST(train, recognize_digits) { Train(); }
|
||||
|
||||
} // namespace paddle
|
Loading…
Reference in new issue