Project import generated by Copybara.
GitOrigin-RevId: 08c2016a4df5aef571b464a4d4491f38c6b2af10
This commit is contained in:
@@ -31,10 +31,10 @@ ResourceProviderFn resource_provider_ = nullptr;
|
||||
|
||||
absl::Status GetResourceContents(const std::string& path, std::string* output,
|
||||
bool read_as_binary) {
|
||||
if (resource_provider_ == nullptr || !resource_provider_(path, output).ok()) {
|
||||
return internal::DefaultGetResourceContents(path, output, read_as_binary);
|
||||
if (resource_provider_) {
|
||||
return resource_provider_(path, output);
|
||||
}
|
||||
return absl::OkStatus();
|
||||
return internal::DefaultGetResourceContents(path, output, read_as_binary);
|
||||
}
|
||||
|
||||
void SetCustomGlobalResourceProvider(ResourceProviderFn fn) {
|
||||
|
||||
@@ -51,7 +51,9 @@ absl::Status DefaultGetResourceContents(const std::string& path,
|
||||
|
||||
// Try the test environment.
|
||||
absl::string_view workspace = "mediapipe";
|
||||
auto test_path = file::JoinPath(std::getenv("TEST_SRCDIR"), workspace, path);
|
||||
const char* test_srcdir = std::getenv("TEST_SRCDIR");
|
||||
auto test_path =
|
||||
file::JoinPath(test_srcdir ? test_srcdir : "", workspace, path);
|
||||
if (file::Exists(test_path).ok()) {
|
||||
return file::GetContents(path, output, file::Defaults());
|
||||
}
|
||||
|
||||
@@ -88,8 +88,9 @@ absl::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
|
||||
// Try the test environment.
|
||||
{
|
||||
absl::string_view workspace = "mediapipe";
|
||||
const char* test_srcdir = std::getenv("TEST_SRCDIR");
|
||||
auto test_path =
|
||||
file::JoinPath(std::getenv("TEST_SRCDIR"), workspace, path);
|
||||
file::JoinPath(test_srcdir ? test_srcdir : "", workspace, path);
|
||||
if ([[NSFileManager defaultManager]
|
||||
fileExistsAtPath:[NSString
|
||||
stringWithUTF8String:test_path.c_str()]]) {
|
||||
|
||||
@@ -81,7 +81,7 @@ cc_library(
|
||||
"@org_tensorflow//tensorflow/lite:framework",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu:api",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/common:model",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/common/testing:tflite_model_reader",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/common:model_builder",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/gl:api2",
|
||||
],
|
||||
"//mediapipe:android": [
|
||||
@@ -93,7 +93,7 @@ cc_library(
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu:api",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/cl:api",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/common:model",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/common/testing:tflite_model_reader",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/common:model_builder",
|
||||
"@org_tensorflow//tensorflow/lite/delegates/gpu/gl:api2",
|
||||
],
|
||||
}) + ["@org_tensorflow//tensorflow/lite/core/api"],
|
||||
|
||||
@@ -17,6 +17,8 @@ licenses(["notice"])
|
||||
|
||||
package(default_visibility = [
|
||||
"//mediapipe:__subpackages__",
|
||||
# For automated benchmarking of Camera models by TFLite team.
|
||||
"//learning/brain/models/app_benchmarks/camera_models:__subpackages__",
|
||||
])
|
||||
|
||||
cc_library(
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "tensorflow/lite/core/api/op_resolver.h"
|
||||
#include "tensorflow/lite/delegates/gpu/api.h"
|
||||
#include "tensorflow/lite/delegates/gpu/common/model.h"
|
||||
#include "tensorflow/lite/delegates/gpu/common/model_builder.h"
|
||||
#include "tensorflow/lite/delegates/gpu/gl/api2.h"
|
||||
#include "tensorflow/lite/model.h"
|
||||
|
||||
@@ -35,7 +36,6 @@
|
||||
#ifdef __ANDROID__
|
||||
#include "tensorflow/lite/delegates/gpu/cl/api.h"
|
||||
#endif
|
||||
#include "tensorflow/lite/delegates/gpu/common/testing/tflite_model_reader.h"
|
||||
|
||||
namespace tflite {
|
||||
namespace gpu {
|
||||
|
||||
@@ -23,11 +23,31 @@ absl::StatusOr<api2::Packet<TfLiteModelPtr>> TfLiteModelLoader::LoadFromPath(
|
||||
const std::string& path) {
|
||||
std::string model_path = path;
|
||||
|
||||
ASSIGN_OR_RETURN(model_path, mediapipe::PathToResourceAsFile(model_path));
|
||||
auto model = tflite::FlatBufferModel::BuildFromFile(model_path.c_str());
|
||||
std::string model_blob;
|
||||
auto status_or_content =
|
||||
mediapipe::GetResourceContents(model_path, &model_blob);
|
||||
// TODO: get rid of manual resolving with PathToResourceAsFile
|
||||
// as soon as it's incorporated into GetResourceContents.
|
||||
if (!status_or_content.ok()) {
|
||||
LOG(WARNING)
|
||||
<< "Trying to resolve path manually as GetResourceContents failed: "
|
||||
<< status_or_content.message();
|
||||
ASSIGN_OR_RETURN(auto resolved_path,
|
||||
mediapipe::PathToResourceAsFile(model_path));
|
||||
MP_RETURN_IF_ERROR(
|
||||
mediapipe::GetResourceContents(resolved_path, &model_blob));
|
||||
}
|
||||
|
||||
auto model = tflite::FlatBufferModel::VerifyAndBuildFromBuffer(
|
||||
model_blob.data(), model_blob.size());
|
||||
RET_CHECK(model) << "Failed to load model from path " << model_path;
|
||||
return api2::MakePacket<TfLiteModelPtr>(
|
||||
model.release(), [](tflite::FlatBufferModel* model) { delete model; });
|
||||
model.release(),
|
||||
[model_blob = std::move(model_blob)](tflite::FlatBufferModel* model) {
|
||||
// It's required that model_blob is deleted only after
|
||||
// model is deleted, hence capturing model_blob.
|
||||
delete model;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace mediapipe
|
||||
|
||||
Reference in New Issue
Block a user