aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2018-04-10 15:45:37 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-04-10 15:48:06 -0700
commit15b104a047c1ec8ec07045047d46a300ebc6b2e3 (patch)
tree6ff0408ae98cf584087766fb136517338456d19f /tensorflow
parent9eaab27bc41b6865bc945dcbb6b75c2427826ef3 (diff)
Small changes to testing code, plus a new binary to check diff from command line.
PiperOrigin-RevId: 192360373
Diffstat (limited to 'tensorflow')
-rw-r--r--tensorflow/contrib/lite/testing/BUILD13
-rw-r--r--tensorflow/contrib/lite/testing/generate_testspec.cc49
-rw-r--r--tensorflow/contrib/lite/testing/generate_testspec.h2
-rw-r--r--tensorflow/contrib/lite/testing/tf_driver.cc9
-rw-r--r--tensorflow/contrib/lite/testing/tflite_diff_example_test.cc7
-rw-r--r--tensorflow/contrib/lite/testing/tflite_diff_flags.h4
-rw-r--r--tensorflow/contrib/lite/testing/tflite_diff_util.cc10
-rw-r--r--tensorflow/contrib/lite/testing/tflite_driver.cc1
8 files changed, 65 insertions, 30 deletions
diff --git a/tensorflow/contrib/lite/testing/BUILD b/tensorflow/contrib/lite/testing/BUILD
index 9f0ba43252..198984e7e7 100644
--- a/tensorflow/contrib/lite/testing/BUILD
+++ b/tensorflow/contrib/lite/testing/BUILD
@@ -196,7 +196,6 @@ cc_library(
cc_library(
name = "util",
- testonly = 1,
hdrs = ["util.h"],
)
@@ -251,7 +250,6 @@ cc_test(
cc_library(
name = "generate_testspec",
- testonly = 1,
srcs = ["generate_testspec.cc"],
hdrs = ["generate_testspec.h"],
deps = [
@@ -277,7 +275,6 @@ cc_test(
cc_library(
name = "tflite_diff_util",
- testonly = 1,
srcs = ["tflite_diff_util.cc"],
hdrs = ["tflite_diff_util.h"],
deps = [
@@ -295,7 +292,6 @@ cc_library(
cc_library(
name = "tflite_diff_flags",
- testonly = 1,
hdrs = ["tflite_diff_flags.h"],
deps = [
":split",
@@ -338,6 +334,15 @@ tf_cc_test(
],
)
+cc_binary(
+ name = "tflite_diff",
+ srcs = ["tflite_diff_example_test.cc"],
+ deps = [
+ ":tflite_diff_flags",
+ ":tflite_diff_util",
+ ],
+)
+
tf_cc_test(
name = "generated_examples_zip_test",
size = "large",
diff --git a/tensorflow/contrib/lite/testing/generate_testspec.cc b/tensorflow/contrib/lite/testing/generate_testspec.cc
index eb3deafb69..6580845af4 100644
--- a/tensorflow/contrib/lite/testing/generate_testspec.cc
+++ b/tensorflow/contrib/lite/testing/generate_testspec.cc
@@ -22,7 +22,22 @@ limitations under the License.
namespace tflite {
namespace testing {
-void GenerateTestSpecFromTensorflowModel(
+template <typename T>
+void GenerateCsv(const std::vector<int>& shape, float min, float max,
+ string* out) {
+ auto random_float = [](int min, int max) {
+ static unsigned int seed;
+ return min + (max - min) * static_cast<float>(rand_r(&seed)) / RAND_MAX;
+ };
+
+ std::function<T(int)> random_t = [&](int) {
+ return static_cast<T>(random_float(min, max));
+ };
+ std::vector<T> data = GenerateRandomTensor(shape, random_t);
+ *out = Join(data.data(), data.size(), ",");
+}
+
+bool GenerateTestSpecFromTensorflowModel(
std::iostream& stream, const string& tensorflow_model_path,
const string& tflite_model_path, const std::vector<string>& input_layer,
const std::vector<string>& input_layer_type,
@@ -31,12 +46,6 @@ void GenerateTestSpecFromTensorflowModel(
CHECK_EQ(input_layer.size(), input_layer_type.size());
CHECK_EQ(input_layer.size(), input_layer_shape.size());
- // Initialize random functions.
- static unsigned int seed = 0;
- std::function<float(int)> float_rand = [](int idx) {
- return static_cast<float>(rand_r(&seed)) / RAND_MAX - 0.5f;
- };
-
// Generate inputs.
std::vector<string> input_values;
input_values.resize(input_layer.size());
@@ -46,15 +55,25 @@ void GenerateTestSpecFromTensorflowModel(
auto shape = Split<int>(input_layer_shape[i], ",");
switch (type) {
- case tensorflow::DT_FLOAT: {
- const auto& data = GenerateRandomTensor<float>(shape, float_rand);
- input_values[i] = Join(data.data(), data.size(), ",");
+ case tensorflow::DT_FLOAT:
+ GenerateCsv<float>(shape, -0.5, 0.5, &input_values[i]);
+ break;
+ case tensorflow::DT_UINT8:
+ GenerateCsv<uint8_t>(shape, 0, 255, &input_values[i]);
+ break;
+ case tensorflow::DT_INT32:
+ GenerateCsv<int32_t>(shape, -100, 100, &input_values[i]);
+ break;
+ case tensorflow::DT_INT64:
+ GenerateCsv<int64_t>(shape, -100, 100, &input_values[i]);
+ break;
+ case tensorflow::DT_BOOL:
+ GenerateCsv<int>(shape, 0.01, 1.99, &input_values[i]);
break;
- }
default:
-
- fprintf(stderr, "Unsupported type %d when generating testspec\n", type);
- return;
+ fprintf(stderr, "Unsupported type %d (%s) when generating testspec.\n",
+ type, input_layer_type[i].c_str());
+ return false;
}
}
@@ -82,6 +101,8 @@ void GenerateTestSpecFromTensorflowModel(
stream << " output: \"" << runner.ReadOutput(i) << "\"\n";
}
stream << "}\n";
+
+ return true;
}
} // namespace testing
diff --git a/tensorflow/contrib/lite/testing/generate_testspec.h b/tensorflow/contrib/lite/testing/generate_testspec.h
index 3529ee709b..6e31a853c3 100644
--- a/tensorflow/contrib/lite/testing/generate_testspec.h
+++ b/tensorflow/contrib/lite/testing/generate_testspec.h
@@ -34,7 +34,7 @@ namespace testing {
// input_layer_type: datatypes of input tensors. Example: float
// input_layer_shape: shapes of input tensors, separated by comma. example:
// 1,3,4 output_layer: names of output tensors. Example: output
-void GenerateTestSpecFromTensorflowModel(
+bool GenerateTestSpecFromTensorflowModel(
std::iostream& stream, const string& tensorflow_model_path,
const string& tflite_model_path, const std::vector<string>& input_layer,
const std::vector<string>& input_layer_type,
diff --git a/tensorflow/contrib/lite/testing/tf_driver.cc b/tensorflow/contrib/lite/testing/tf_driver.cc
index 2c253bb198..7b295875aa 100644
--- a/tensorflow/contrib/lite/testing/tf_driver.cc
+++ b/tensorflow/contrib/lite/testing/tf_driver.cc
@@ -87,10 +87,9 @@ TfDriver::TfDriver(const std::vector<string>& input_layer,
void TfDriver::LoadModel(const string& bin_file_path) {
if (!IsValid()) return;
- std::cout << std::endl << "Loading model: " << bin_file_path << std::endl;
std::ifstream model(bin_file_path);
if (model.fail()) {
- Invalidate("Failed to find the model");
+ Invalidate("Failed to find the model " + bin_file_path);
return;
}
@@ -121,6 +120,10 @@ void TfDriver::SetInput(int id, const string& csv_values) {
FillTensorWithData<int32_t>(&tensor, csv_values);
break;
}
+ case tensorflow::DT_UINT8: {
+ FillTensorWithData<uint8_t>(&tensor, csv_values);
+ break;
+ }
default:
fprintf(stderr, "Unsupported type %d in SetInput\n", input_types_[id]);
Invalidate("Unsupported tensor data type");
@@ -162,6 +165,8 @@ string TfDriver::ReadOutput(int id) {
return TensorDataToCsvString<float>(output_tensors_[id]);
case tensorflow::DT_INT32:
return TensorDataToCsvString<int32_t>(output_tensors_[id]);
+ case tensorflow::DT_UINT8:
+ return TensorDataToCsvString<uint8_t>(output_tensors_[id]);
default:
fprintf(stderr, "Unsupported type %d in ResetTensor\n", input_types_[id]);
Invalidate("Unsupported tensor data type");
diff --git a/tensorflow/contrib/lite/testing/tflite_diff_example_test.cc b/tensorflow/contrib/lite/testing/tflite_diff_example_test.cc
index 3817e68111..5afa0f800c 100644
--- a/tensorflow/contrib/lite/testing/tflite_diff_example_test.cc
+++ b/tensorflow/contrib/lite/testing/tflite_diff_example_test.cc
@@ -19,10 +19,13 @@ limitations under the License.
int main(int argc, char** argv) {
::tflite::testing::DiffOptions options =
::tflite::testing::ParseTfliteDiffFlags(&argc, argv);
+ if (options.tensorflow_model.empty()) return 1;
+ int failure_count = 0;
for (int i = 0; i < 100; i++) {
if (!tflite::testing::RunDiffTest(options)) {
- return 1;
+ ++failure_count;
}
}
- return 0;
+ fprintf(stderr, "Num errors: %d\n", failure_count);
+ return failure_count != 0 ? 1 : 0;
}
diff --git a/tensorflow/contrib/lite/testing/tflite_diff_flags.h b/tensorflow/contrib/lite/testing/tflite_diff_flags.h
index 5f1129d501..706108ed73 100644
--- a/tensorflow/contrib/lite/testing/tflite_diff_flags.h
+++ b/tensorflow/contrib/lite/testing/tflite_diff_flags.h
@@ -51,9 +51,11 @@ DiffOptions ParseTfliteDiffFlags(int* argc, char** argv) {
"output_1,output_2"),
};
+ bool no_inputs = *argc == 1;
bool success = tensorflow::Flags::Parse(argc, argv, flags);
- if (!success || (*argc == 2 && !strcmp(argv[1], "--helpfull"))) {
+ if (!success || no_inputs || (*argc == 2 && !strcmp(argv[1], "--helpfull"))) {
fprintf(stderr, "%s", tensorflow::Flags::Usage(argv[0], flags).c_str());
+ return {};
}
return {values.tensorflow_model,
diff --git a/tensorflow/contrib/lite/testing/tflite_diff_util.cc b/tensorflow/contrib/lite/testing/tflite_diff_util.cc
index 9ef4e1f66c..f601d3752d 100644
--- a/tensorflow/contrib/lite/testing/tflite_diff_util.cc
+++ b/tensorflow/contrib/lite/testing/tflite_diff_util.cc
@@ -27,13 +27,13 @@ namespace testing {
bool RunDiffTest(const DiffOptions& options) {
std::stringstream tflite_stream;
- GenerateTestSpecFromTensorflowModel(
- tflite_stream, options.tensorflow_model, options.tflite_model,
- options.input_layer, options.input_layer_type, options.input_layer_shape,
- options.output_layer);
+ if (!GenerateTestSpecFromTensorflowModel(
+ tflite_stream, options.tensorflow_model, options.tflite_model,
+ options.input_layer, options.input_layer_type,
+ options.input_layer_shape, options.output_layer))
+ return false;
TfLiteDriver tflite_driver(/*use_nnapi=*/true);
tflite_driver.LoadModel(options.tflite_model);
- std::cout << tflite_stream.str();
return tflite::testing::ParseAndRunTests(&tflite_stream, &tflite_driver);
}
} // namespace testing
diff --git a/tensorflow/contrib/lite/testing/tflite_driver.cc b/tensorflow/contrib/lite/testing/tflite_driver.cc
index c399f4f2b7..3764bab035 100644
--- a/tensorflow/contrib/lite/testing/tflite_driver.cc
+++ b/tensorflow/contrib/lite/testing/tflite_driver.cc
@@ -143,7 +143,6 @@ void TfLiteDriver::AllocateTensors() {
void TfLiteDriver::LoadModel(const string& bin_file_path) {
if (!IsValid()) return;
- std::cout << std::endl << "Loading model: " << bin_file_path << std::endl;
model_ = FlatBufferModel::BuildFromFile(GetFullPath(bin_file_path).c_str());
if (!model_) {