adjust cropper and demo

pull/13419/head
yeyunpeng2020 4 years ago
parent 93f64e38dd
commit 2c8ab3f483

File diff suppressed because it is too large Load Diff

@ -23,7 +23,9 @@ get_version() {
}
get_version
MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_imagenet/mobilenetv2.ms"
MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/linux/mindspore-lite-${VERSION_STR}-inference-linux-x64.tar.gz"
MINDSPORE_FILE_NAME="mindspore-lite-${VERSION_STR}-linux-x64"
MINDSPORE_FILE="${MINDSPORE_FILE_NAME}.tar.gz"
MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/linux/${MINDSPORE_FILE}"
mkdir -p build
mkdir -p lib
@ -31,13 +33,12 @@ mkdir -p model
if [ ! -e ${BASEPATH}/model/mobilenetv2.ms ]; then
wget -c -O ${BASEPATH}/model/mobilenetv2.ms --no-check-certificate ${MODEL_DOWNLOAD_URL}
fi
if [ ! -e ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-linux-x64.tar.gz ]; then
wget -c -O ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-linux-x64.tar.gz --no-check-certificate ${MINDSPORE_LITE_DOWNLOAD_URL}
if [ ! -e ${BASEPATH}/build/${MINDSPORE_FILE} ]; then
wget -c -O ${BASEPATH}/build/${MINDSPORE_FILE} --no-check-certificate ${MINDSPORE_LITE_DOWNLOAD_URL}
fi
tar xzvf ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-linux-x64.tar.gz -C ${BASEPATH}/build/
cp -r ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-linux-x64-avx/lib/libmindspore-lite.a ${BASEPATH}/lib
cp -r ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-linux-x64-avx/include ${BASEPATH}/
tar xzvf ${BASEPATH}/build/${MINDSPORE_FILE} -C ${BASEPATH}/build/
cp -r ${BASEPATH}/build/${MINDSPORE_FILE_NAME}/inference/lib/libmindspore-lite.a ${BASEPATH}/lib
cp -r ${BASEPATH}/build/${MINDSPORE_FILE_NAME}/inference/include ${BASEPATH}/
cd ${BASEPATH}/build
cmake ${BASEPATH}
make
make

@ -0,0 +1,43 @@
#!/bin/bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
BASEPATH=$(cd "$(dirname $0)"; pwd)
get_version() {
VERSION_MAJOR=$(grep "const int ms_version_major =" ${BASEPATH}/../../include/version.h | tr -dc "[0-9]")
VERSION_MINOR=$(grep "const int ms_version_minor =" ${BASEPATH}/../../include/version.h | tr -dc "[0-9]")
VERSION_REVISION=$(grep "const int ms_version_revision =" ${BASEPATH}/../../include/version.h | tr -dc "[0-9]")
VERSION_STR=${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION}
}
get_version
MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_imagenet/mobilenetv2.ms"
MINDSPORE_FILE_NAME="mindspore-lite-${VERSION_STR}-linux-x64"
MINDSPORE_FILE="${MINDSPORE_FILE_NAME}.tar.gz"
MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/linux/${MINDSPORE_FILE}"
mkdir -p build
mkdir -p lib
mkdir -p model
if [ ! -e ${BASEPATH}/model/mobilenetv2.ms ]; then
wget -c -O ${BASEPATH}/model/mobilenetv2.ms --no-check-certificate ${MODEL_DOWNLOAD_URL}
fi
if [ ! -e ${BASEPATH}/build/${MINDSPORE_FILE} ]; then
wget -c -O ${BASEPATH}/build/${MINDSPORE_FILE} --no-check-certificate ${MINDSPORE_LITE_DOWNLOAD_URL}
fi
tar xzvf ${BASEPATH}/build/${MINDSPORE_FILE} -C ${BASEPATH}/build/
cp -r ${BASEPATH}/build/${MINDSPORE_FILE_NAME}/inference/lib/jar/* ${BASEPATH}/lib
cd ${BASEPATH}/
mvn package

@ -26,7 +26,9 @@ get_version() {
}
get_version
MODEL_DOWNLOAD_URL="https://download.mindspore.cn/model_zoo/official/lite/mobilenetv2_imagenet/mobilenetv2.ms"
MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/android/mindspore-lite-${VERSION_STR}-inference-android.tar.gz"
MINDSPORE_FILE_NAME="mindspore-lite-${VERSION_STR}-android-aarch64"
MINDSPORE_FILE="${MINDSPORE_FILE_NAME}.tar.gz"
MINDSPORE_LITE_DOWNLOAD_URL="https://ms-release.obs.cn-north-4.myhuaweicloud.com/${VERSION_STR}/MindSpore/lite/release/android/${MINDSPORE_FILE}"
mkdir -p build
mkdir -p lib
@ -34,14 +36,13 @@ mkdir -p model
if [ ! -e ${BASEPATH}/model/mobilenetv2.ms ]; then
wget -c -O ${BASEPATH}/model/mobilenetv2.ms --no-check-certificate ${MODEL_DOWNLOAD_URL}
fi
if [ ! -e ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-android.tar.gz ]; then
wget -c -O ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-android.tar.gz --no-check-certificate ${MINDSPORE_LITE_DOWNLOAD_URL}
if [ ! -e ${BASEPATH}/build/${MINDSPORE_FILE} ]; then
wget -c -O ${BASEPATH}/build/${MINDSPORE_FILE} --no-check-certificate ${MINDSPORE_LITE_DOWNLOAD_URL}
fi
tar xzvf ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-android.tar.gz -C ${BASEPATH}/build/
cp -r ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-android/lib/aarch64/libmindspore-lite.a ${BASEPATH}/lib
cp -r ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-android/third_party/hiai_ddk/lib/aarch64/*.so ${BASEPATH}/lib
cp -r ${BASEPATH}/build/mindspore-lite-${VERSION_STR}-inference-android/include ${BASEPATH}/
tar xzvf ${BASEPATH}/build/${MINDSPORE_FILE} -C ${BASEPATH}/build/
cp -r ${BASEPATH}/build/${MINDSPORE_FILE_NAME}/inference/lib/libmindspore-lite.a ${BASEPATH}/lib
cp -r ${BASEPATH}/build/${MINDSPORE_FILE_NAME}/inference/third_party/hiai_ddk/lib/*.so ${BASEPATH}/lib
cp -r ${BASEPATH}/build/${MINDSPORE_FILE_NAME}/inference/include ${BASEPATH}/
cd ${BASEPATH}/build
cmake -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" -DANDROID_NATIVE_API_LEVEL="19" \
-DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="arm64-v8a" -DANDROID_STL="c++_shared" ${BASEPATH}

@ -53,6 +53,7 @@ std::string RealPath(const char *path) {
std::string res = resolved_path.get();
return res;
}
char *ReadFile(const char *file, size_t *size) {
if (file == nullptr) {
std::cerr << "file is nullptr." << std::endl;

@ -14,7 +14,6 @@
* limitations under the License.
*/
#include <memory>
#include <string>
#include "tools/cropper/cropper.h"
#include "tools/cropper/cropper_utils.h"
@ -31,7 +30,7 @@ int Cropper::ReadPackage() {
in_file.close();
char buf[BUF_SIZE];
String cmd = "ar -t " + this->flags_->package_file_;
std::string cmd = "ar -t " + this->flags_->package_file_;
MS_LOG(DEBUG) << cmd;
FILE *p_file = popen(cmd.c_str(), "r");
@ -40,8 +39,8 @@ int Cropper::ReadPackage() {
return RET_ERROR;
}
while (fgets(buf, BUF_SIZE, p_file) != nullptr) {
this->all_files_.push_back(String(buf).substr(0, String(buf).length() - 1));
this->discard_files_.push_back(String(buf).substr(0, String(buf).length() - 1));
this->all_files_.push_back(std::string(buf).substr(0, std::string(buf).length() - 1));
this->discard_files_.push_back(std::string(buf).substr(0, std::string(buf).length() - 1));
}
pclose(p_file);
MS_LOG(DEBUG) << "file nums: " << this->all_files_.size();
@ -85,9 +84,9 @@ int Cropper::RunCropper() {
}
return RET_OK;
}
int Cropper::GetModelOps() {
for (const auto &path : this->model_files_) {
auto context = std::make_shared<Context>();
size_t buffer_lens;
char *graph_buf = ReadFile(path.c_str(), &buffer_lens);
if (graph_buf == nullptr) {
@ -124,6 +123,7 @@ int Cropper::GetModelOps() {
}
return RET_OK;
}
int Cropper::GetModelFiles() {
if (!this->flags_->model_file_.empty()) {
auto files = StringSplit(this->flags_->model_file_, std::string(DELIM_COMMA));
@ -132,16 +132,16 @@ int Cropper::GetModelFiles() {
return RET_INPUT_PARAM_INVALID;
}
MS_LOG(DEBUG) << file;
String realPath = RealPath(file.c_str());
if (realPath.empty()) {
std::string real_path = RealPath(file.c_str());
if (real_path.empty()) {
return RET_INPUT_PARAM_INVALID;
}
this->model_files_.push_back(realPath);
this->model_files_.push_back(real_path);
}
}
// get models from folder
if (!this->flags_->model_folder_path_.empty()) {
String cmd = "find " + this->flags_->model_folder_path_ + " -name '*.ms'";
std::string cmd = "find " + this->flags_->model_folder_path_ + " -name '*.ms'";
MS_LOG(DEBUG) << cmd;
char buf[BUF_SIZE];
@ -151,12 +151,12 @@ int Cropper::GetModelFiles() {
return RET_ERROR;
}
while (fgets(buf, BUF_SIZE, p_file) != nullptr) {
String realPath = RealPath(String(buf).substr(0, String(buf).length() - 1).c_str());
if (realPath.empty()) {
std::string real_path = RealPath(std::string(buf).substr(0, std::string(buf).length() - 1).c_str());
if (real_path.empty()) {
pclose(p_file);
return RET_INPUT_PARAM_INVALID;
}
this->model_files_.emplace_back(realPath);
this->model_files_.emplace_back(real_path);
}
pclose(p_file);
}
@ -177,9 +177,9 @@ int Cropper::GetOpMatchFiles() {
std::string buf_str = buf;
auto mapping = StringSplit(buf_str, DELIM_COMMA);
if (!mapping.empty()) {
String primitive = mapping[0];
String type = mapping[1];
String file = mapping[2];
std::string primitive = mapping.at(0);
std::string type = mapping.at(1);
std::string file = mapping.at(2);
if (type == "kNumberTypeFloat32" || type == "kNumberTypeFloat16" || type == "kNumberTypeInt32") {
for (auto op : this->fp32_operators_) {
if (schema::EnumNamePrimitiveType(op) == primitive) {
@ -219,6 +219,7 @@ int Cropper::GetOpMatchFiles() {
}
return RET_OK;
}
int Cropper::GetDiscardFileList() {
// discard_files_=all_files_-archive_files_
for (const auto &archive : this->archive_files_) {
@ -233,13 +234,13 @@ int Cropper::GetDiscardFileList() {
return RET_OK;
}
int Cropper::CutPackage() {
String copy_bak_cmd = "cp " + this->flags_->package_file_ + " " + this->flags_->package_file_ + ".bak";
String ar_cmd = "ar -d " + this->flags_->package_file_ + ".bak ";
std::string copy_bak_cmd = "cp " + this->flags_->package_file_ + " " + this->flags_->package_file_ + ".bak";
std::string ar_cmd = "ar -d " + this->flags_->package_file_ + ".bak ";
for (const auto &file : this->discard_files_) {
ar_cmd.append(file).append(" ");
}
String copy_to_output_cmd = "cp " + this->flags_->package_file_ + ".bak " + this->flags_->output_file_;
String rm_bak_cmd = "rm " + this->flags_->package_file_ + ".bak";
std::string copy_to_output_cmd = "cp " + this->flags_->package_file_ + ".bak " + this->flags_->output_file_;
std::string rm_bak_cmd = "rm " + this->flags_->package_file_ + ".bak";
int status;
status = system(copy_bak_cmd.c_str());
if (status != 0) {

@ -20,17 +20,15 @@
#include <set>
#include <string>
#include <vector>
#include "include/model.h"
#include "tools/common/flag_parser.h"
#include "src/common/file_utils.h"
#include "src/common/utils.h"
#include "schema/model_generated.h"
#include "include/lite_session.h"
#include "tools/cropper/cropper_flags.h"
namespace mindspore::lite::cropper {
class MS_API Cropper {
class Cropper {
public:
explicit Cropper(CropperFlags *flags) : flags_(flags) {}
@ -63,6 +61,6 @@ class MS_API Cropper {
CropperFlags *flags_;
};
int MS_API RunCropper(int argc, const char **argv);
int RunCropper(int argc, const char **argv);
} // namespace mindspore::lite::cropper
#endif // MINDSPORE_LITE_TOOLS_CROPPER_CROPPER_H_

Loading…
Cancel
Save