Initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
[{"name":"Build-Configuration","type":"AscendAppBuild","lastBuild":true,"buildProperties":{"HOST_OS":"Euleros2.8","HOST_ARCH":"aarch64","TARGET_TYPE":"SOC"}}]
|
||||
Generated
+9
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/colorization.iml" filepath="$PROJECT_DIR$/.idea/colorization.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+261
@@ -0,0 +1,261 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="02eb725d-4818-4635-8025-40bac6afd3e4" name="Default Changelist" comment="" />
|
||||
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="FileEditorManager">
|
||||
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300" />
|
||||
</component>
|
||||
<component name="FindInProjectRecents">
|
||||
<findStrings>
|
||||
<find>OPENCV_PATH</find>
|
||||
<find>reiszeMatL</find>
|
||||
<find>itemDataSize</find>
|
||||
<find>resultImage</find>
|
||||
<find>om</find>
|
||||
<find>LoadModelFromFileWithMem</find>
|
||||
<find>CreateDesc</find>
|
||||
<find>CreateOutput</find>
|
||||
<find>CreateInput</find>
|
||||
<find>Execute</find>
|
||||
<find>Postprocess</find>
|
||||
<find>InitResource</find>
|
||||
<find>Preprocess</find>
|
||||
<find>Unload</find>
|
||||
<find>LoadModelFromFileWithMem</find>
|
||||
<find>ColorizeProcess</find>
|
||||
<find>DestroyResource</find>
|
||||
</findStrings>
|
||||
<dirStrings>
|
||||
<dir>$PROJECT_DIR$/out</dir>
|
||||
<dir>$PROJECT_DIR$/src</dir>
|
||||
</dirStrings>
|
||||
</component>
|
||||
<component name="IdeDocumentHistory">
|
||||
<option name="CHANGED_PATHS">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/src/main.cpp" />
|
||||
<option value="$PROJECT_DIR$/inc/utils.h" />
|
||||
<option value="$PROJECT_DIR$/src/CMakeLists.txt" />
|
||||
<option value="$PROJECT_DIR$/inc/colorize_process.h" />
|
||||
<option value="$PROJECT_DIR$/src/model_process.cpp" />
|
||||
<option value="$PROJECT_DIR$/src/colorize_process.cpp" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectFrameBounds" extendedState="6">
|
||||
<option name="x" value="923" />
|
||||
<option name="y" value="35" />
|
||||
<option name="width" value="850" />
|
||||
<option name="height" value="865" />
|
||||
</component>
|
||||
<component name="ProjectView">
|
||||
<navigator proportions="" version="1">
|
||||
<foldersAlwaysOnTop value="true" />
|
||||
</navigator>
|
||||
<panes>
|
||||
<pane id="OperatorsViewPane" />
|
||||
<pane id="ProjectPane">
|
||||
<subPane>
|
||||
<expand>
|
||||
<path>
|
||||
<item name="colorization" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="colorization" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="colorization" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="colorization" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="inc" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="colorization" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="colorization" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="model" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="colorization" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="colorization" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="out" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="colorization" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="colorization" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="out" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="outputs" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="colorization" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="colorization" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="out" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="outputs" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="20200722053738" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
<path>
|
||||
<item name="colorization" type="b2602c69:ProjectViewProjectNode" />
|
||||
<item name="colorization" type="462c0819:PsiDirectoryNode" />
|
||||
<item name="src" type="462c0819:PsiDirectoryNode" />
|
||||
</path>
|
||||
</expand>
|
||||
<select />
|
||||
</subPane>
|
||||
</pane>
|
||||
<pane id="PackagesPane" />
|
||||
<pane id="Scope" />
|
||||
</panes>
|
||||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$/../classification" />
|
||||
</component>
|
||||
<component name="RunDashboard">
|
||||
<option name="ruleStates">
|
||||
<list>
|
||||
<RuleState>
|
||||
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
|
||||
</RuleState>
|
||||
<RuleState>
|
||||
<option name="name" value="StatusDashboardGroupingRule" />
|
||||
</RuleState>
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="RunManager">
|
||||
<configuration default="true" type="Ascend App Configuration Type" factoryName="Ascend App" hostip="192.168.1.2" commandArguments="">
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<configuration name="colorization" type="Ascend App Configuration Type" factoryName="Ascend App" hostip="192.168.1.2" commandArguments="../data" ProfilingParams="{"--ai_core_profiling_mode":"sample-based","ProfilingStatus":"off","--ai_core_metrics":"aicorePipeline","hasError":"off","--profiling_options":"task_trace","ai_core_sample_based":"off","--aicore_sampling_interval":"10","ai_core_task_based":"off"}">
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
</component>
|
||||
<component name="SvnConfiguration">
|
||||
<configuration />
|
||||
</component>
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="02eb725d-4818-4635-8025-40bac6afd3e4" name="Default Changelist" comment="" />
|
||||
<created>1594980321659</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1594980321659</updated>
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
<component name="ToolWindowManager">
|
||||
<frame x="67" y="25" width="1851" height="875" extended-state="6" />
|
||||
<layout>
|
||||
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.29704595" />
|
||||
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
|
||||
<window_info id="Favorites" order="2" side_tool="true" />
|
||||
<window_info anchor="bottom" id="Message" order="0" />
|
||||
<window_info anchor="bottom" id="Find" order="1" />
|
||||
<window_info anchor="bottom" id="Run" order="2" weight="0.5020862" />
|
||||
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
|
||||
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
|
||||
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
|
||||
<window_info anchor="bottom" id="TODO" order="6" />
|
||||
<window_info anchor="bottom" id="Tail" order="7" />
|
||||
<window_info anchor="bottom" id="Log" order="8" />
|
||||
<window_info anchor="bottom" id="Output" order="9" weight="0.21557719" />
|
||||
<window_info anchor="bottom" id="Pylint" order="10" />
|
||||
<window_info anchor="bottom" id="Terminal" order="11" />
|
||||
<window_info anchor="bottom" id="Event Log" order="12" side_tool="true" />
|
||||
<window_info anchor="bottom" id="Version Control" order="13" />
|
||||
<window_info anchor="bottom" id="Build" order="14" weight="0.6258693" />
|
||||
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
|
||||
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
|
||||
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
|
||||
</layout>
|
||||
</component>
|
||||
<component name="editorHistoryManager">
|
||||
<entry file="file://$PROJECT_DIR$/model/colorization.om">
|
||||
<provider selected="true" editor-type-id="om_editor_type_id" />
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/out/outputs/20200717034142/out_dog.png" />
|
||||
<entry file="file://$PROJECT_DIR$/out/outputs/20200717042401/out_dog.png" />
|
||||
<entry file="file://$PROJECT_DIR$/out/outputs/20200717035051/out_dog.png" />
|
||||
<entry file="file://$PROJECT_DIR$/src/CMakeLists.txt">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="135">
|
||||
<caret line="46" column="22" lean-forward="true" selection-start-line="46" selection-start-column="22" selection-end-line="46" selection-end-column="22" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/inc/utils.h">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="208">
|
||||
<caret line="30" column="64" selection-start-line="30" selection-start-column="64" selection-end-line="30" selection-end-column="64" />
|
||||
<folding>
|
||||
<element signature="e#660#680#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/data/dog.png">
|
||||
<provider selected="true" editor-type-id="images" />
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/out/outputs/20200722011213/out_dog.png">
|
||||
<provider selected="true" editor-type-id="images" />
|
||||
</entry>
|
||||
<entry file="file://$USER_HOME$/Ascend/ascend-toolkit/20.0.0.B002/arm64-linux_gcc7.3.0/acllib/include/acl/acl_mdl.h">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="154">
|
||||
<caret line="253" column="30" lean-forward="true" selection-start-line="253" selection-start-column="30" selection-end-line="253" selection-end-column="30" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/utils.cpp">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="2200">
|
||||
<caret line="121" column="49" selection-start-line="121" selection-start-column="49" selection-end-line="121" selection-end-column="49" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/out/outputs/20200722030826/out_dog.png">
|
||||
<provider selected="true" editor-type-id="images" />
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/main.cpp">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="137">
|
||||
<caret line="29" column="18" selection-start-line="29" selection-start-column="18" selection-end-line="29" selection-end-column="18" />
|
||||
<folding>
|
||||
<element signature="e#648#668#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/out/outputs/20200722044241/out_dog.png">
|
||||
<provider selected="true" editor-type-id="images" />
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/inc/colorize_process.h">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="264">
|
||||
<caret line="51" column="23" selection-start-line="51" selection-start-column="17" selection-end-line="51" selection-end-column="23" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/model_process.cpp">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="166">
|
||||
<caret line="29" selection-start-line="29" selection-end-line="32" selection-end-column="20" />
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/src/colorize_process.cpp">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="21">
|
||||
<caret line="275" column="27" selection-start-line="275" selection-start-column="27" selection-end-line="275" selection-end-column="27" />
|
||||
<folding>
|
||||
<element signature="e#655#685#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/out/outputs/20200722053738/out_dog.png">
|
||||
<provider selected="true" editor-type-id="images" />
|
||||
</entry>
|
||||
</component>
|
||||
</project>
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "Ascend ACL App",
|
||||
"project_type": "Custom",
|
||||
"project_desc": "",
|
||||
"target": "",
|
||||
"target_id": "",
|
||||
"adk_version": "1.73.T5.0.B050"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
|
||||
|
||||
# CMake lowest version requirement
|
||||
cmake_minimum_required(VERSION 3.5.1)
|
||||
|
||||
# project information
|
||||
project(sample-colorization)
|
||||
|
||||
add_subdirectory("./src")
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 81 KiB |
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
|
||||
* File sample_process.h
|
||||
* Description: handle acl resource
|
||||
*/
|
||||
#pragma once
|
||||
#include "utils.h"
|
||||
#include "acl/acl.h"
|
||||
#include "model_process.h"
|
||||
#include <memory>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* ColorizeProcess
|
||||
*/
|
||||
class ColorizeProcess {
|
||||
public:
|
||||
ColorizeProcess(const char* modelPath, uint32_t modelWidth, uint32_t modelHeight);
|
||||
~ColorizeProcess();
|
||||
|
||||
Result Init();
|
||||
Result Preprocess(const string& imageFile);
|
||||
Result Inference(aclmdlDataset*& inferenceOutput);
|
||||
Result Postprocess(const string& origImageFile,
|
||||
aclmdlDataset* modelOutput);
|
||||
private:
|
||||
Result InitResource();
|
||||
Result InitModel(const char* omModelPath);
|
||||
|
||||
void* GetInferenceOutputItem(uint32_t& itemDataSize,
|
||||
aclmdlDataset* inferenceOutput);
|
||||
void SaveImage(const string& origImageFile, cv::Mat& image);
|
||||
void DestroyResource();
|
||||
|
||||
private:
|
||||
int32_t deviceId_;
|
||||
|
||||
ModelProcess model_;
|
||||
|
||||
const char* modelPath_;
|
||||
uint32_t modelWidth_;
|
||||
uint32_t modelHeight_;
|
||||
uint32_t inputDataSize_;
|
||||
void* inputBuf_;
|
||||
aclrtRunMode runMode_;
|
||||
|
||||
bool isInited_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
|
||||
* File model_process.h
|
||||
* Description: handle model process
|
||||
*/
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include "utils.h"
|
||||
#include "acl/acl.h"
|
||||
|
||||
/**
|
||||
* ModelProcess
|
||||
*/
|
||||
class ModelProcess {
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor
|
||||
*/
|
||||
ModelProcess();
|
||||
|
||||
/**
|
||||
* @brief Destructor
|
||||
*/
|
||||
~ModelProcess();
|
||||
|
||||
/**
|
||||
* @brief load model from file with mem
|
||||
* @param [in] modelPath: model path
|
||||
* @return result
|
||||
*/
|
||||
Result LoadModelFromFileWithMem(const char *modelPath);
|
||||
|
||||
/**
|
||||
* @brief unload model
|
||||
*/
|
||||
void Unload();
|
||||
|
||||
/**
|
||||
* @brief create model desc
|
||||
* @return result
|
||||
*/
|
||||
Result CreateDesc();
|
||||
|
||||
/**
|
||||
* @brief destroy desc
|
||||
*/
|
||||
void DestroyDesc();
|
||||
|
||||
/**
|
||||
* @brief create model input
|
||||
* @param [in] inputDataBuffer: input buffer
|
||||
* @param [in] bufferSize: input buffer size
|
||||
* @return result
|
||||
*/
|
||||
Result CreateInput(void *inputDataBuffer, size_t bufferSize);
|
||||
|
||||
/**
|
||||
* @brief destroy input resource
|
||||
*/
|
||||
void DestroyInput();
|
||||
|
||||
/**
|
||||
* @brief create output buffer
|
||||
* @return result
|
||||
*/
|
||||
Result CreateOutput();
|
||||
|
||||
/**
|
||||
* @brief destroy output resource
|
||||
*/
|
||||
void DestroyOutput();
|
||||
|
||||
/**
|
||||
* @brief model execute
|
||||
* @return result
|
||||
*/
|
||||
Result Execute();
|
||||
|
||||
/**
|
||||
* @brief get model output data
|
||||
* @return output dataset
|
||||
*/
|
||||
aclmdlDataset *GetModelOutputData();
|
||||
|
||||
private:
|
||||
bool loadFlag_; // model load flag
|
||||
uint32_t modelId_;
|
||||
void *modelMemPtr_;
|
||||
size_t modelMemSize_;
|
||||
void *modelWeightPtr_;
|
||||
size_t modelWeightSize_;
|
||||
aclmdlDesc *modelDesc_;
|
||||
aclmdlDataset *input_;
|
||||
aclmdlDataset *output_;
|
||||
};
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
|
||||
* File utils.h
|
||||
* Description: handle file operations
|
||||
*/
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "acl/acl.h"
|
||||
|
||||
#include "opencv2/opencv.hpp"
|
||||
#include "opencv2/imgcodecs/legacy/constants_c.h"
|
||||
#include "opencv2/imgproc/types_c.h"
|
||||
|
||||
#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args)
|
||||
#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args)
|
||||
#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args)
|
||||
#define RGBF32_CHAN_SIZE(width, height) ((width) * (height) * 4)
|
||||
|
||||
template<class Type>
|
||||
std::shared_ptr<Type> MakeSharedNoThrow() {
|
||||
try {
|
||||
return std::make_shared<Type>();
|
||||
}
|
||||
catch (...) {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
#define MAKE_SHARED_NO_THROW(memory, memory_type) \
|
||||
do { \
|
||||
memory = MakeSharedNoThrow<memory_type>(); \
|
||||
}while(0);
|
||||
|
||||
typedef enum Result {
|
||||
SUCCESS = 0,
|
||||
FAILED = 1
|
||||
}Result;
|
||||
|
||||
struct ImageData {
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
int32_t size = 0;
|
||||
void* data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Utils
|
||||
*/
|
||||
class Utils {
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief create device buffer of pic
|
||||
* @param [in] picDesc: pic desc
|
||||
* @param [in] PicBufferSize: aligned pic size
|
||||
* @return device buffer of pic
|
||||
*/
|
||||
static bool IsDirectory(const std::string &path);
|
||||
|
||||
static bool IsPathExist(const std::string &path);
|
||||
|
||||
static void SplitPath(const std::string &path, std::vector<std::string> &path_vec);
|
||||
|
||||
static void GetAllFiles(const std::string &path, std::vector<std::string> &file_vec);
|
||||
|
||||
static void GetPathFiles(const std::string &path, std::vector<std::string> &file_vec);
|
||||
|
||||
static void* CopyDataDeviceToHost(void* deviceData, uint32_t dataSize);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
# Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
|
||||
|
||||
# CMake lowest version requirement
|
||||
cmake_minimum_required(VERSION 3.5.1)
|
||||
|
||||
# project information
|
||||
project(sample-colorization)
|
||||
|
||||
# Compile options
|
||||
add_compile_options(-std=c++11)
|
||||
|
||||
add_definitions(-DENABLE_DVPP_INTERFACE)
|
||||
|
||||
# Specify target generation path
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../../../out")
|
||||
|
||||
set(OPENCV_PATH "/usr/local")
|
||||
|
||||
set(INC_PATH $ENV{DDK_PATH})
|
||||
if (NOT DEFINED ENV{DDK_PATH})
|
||||
set(INC_PATH "/home/c72/Ascend/")
|
||||
message(STATUS "set default INC_PATH: ${INC_PATH}")
|
||||
else ()
|
||||
message(STATUS "env INC_PATH: ${INC_PATH}")
|
||||
endif()
|
||||
|
||||
set(LIB_PATH $ENV{NPU_HOST_LIB})
|
||||
if (NOT DEFINED ENV{NPU_HOST_LIB})
|
||||
set(LIB_PATH "/usr/local/Ascend/acllib/lib64/stub/")
|
||||
message(STATUS "set default LIB_PATH: ${LIB_PATH}")
|
||||
else ()
|
||||
message(STATUS "env LIB_PATH: ${LIB_PATH}")
|
||||
endif()
|
||||
|
||||
# Header path
|
||||
include_directories(
|
||||
../inc/
|
||||
${INC_PATH}/acllib/include/
|
||||
${OPENCV_PATH}/include/opencv4
|
||||
)
|
||||
|
||||
|
||||
# add host lib path
|
||||
link_directories(
|
||||
${LIB_PATH}
|
||||
$ENV{HOME}/ascend_ddk/host/lib/
|
||||
${OPENCV_PATH}/lib
|
||||
${INC_PATH}/atc/lib64
|
||||
)
|
||||
|
||||
add_executable(main
|
||||
utils.cpp
|
||||
colorize_process.cpp
|
||||
model_process.cpp
|
||||
main.cpp)
|
||||
|
||||
target_link_libraries(main
|
||||
ascendcl acl_dvpp stdc++ opencv_highgui opencv_core opencv_imgproc opencv_imgcodecs opencv_calib3d opencv_features2d opencv_videoio)
|
||||
|
||||
install(TARGETS main DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1,292 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
|
||||
* File sample_process.cpp
|
||||
* Description: handle acl resource
|
||||
*/
|
||||
#include "colorize_process.h"
|
||||
#include <iostream>
|
||||
|
||||
#include "acl/acl.h"
|
||||
#include "model_process.h"
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
uint32_t kTopNConfidenceLevels = 5;
|
||||
}
|
||||
|
||||
ColorizeProcess::ColorizeProcess(const char* modelPath,
|
||||
uint32_t modelWidth, uint32_t modelHeight)
|
||||
:deviceId_(0), inputBuf_(nullptr),
|
||||
modelWidth_(modelWidth), modelHeight_(modelHeight), isInited_(false){
|
||||
modelPath_ = modelPath;
|
||||
inputDataSize_ = RGBF32_CHAN_SIZE(modelWidth_, modelHeight_);
|
||||
}
|
||||
|
||||
ColorizeProcess::~ColorizeProcess() {
|
||||
DestroyResource();
|
||||
}
|
||||
|
||||
Result ColorizeProcess::InitResource() {
|
||||
// ACL init
|
||||
const char *aclConfigPath = "../src/acl.json";
|
||||
aclError ret = aclInit(aclConfigPath);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("Acl init failed");
|
||||
return FAILED;
|
||||
}
|
||||
INFO_LOG("Acl init success");
|
||||
|
||||
// open device
|
||||
ret = aclrtSetDevice(deviceId_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("Acl open device %d failed", deviceId_);
|
||||
return FAILED;
|
||||
}
|
||||
INFO_LOG("Open device %d success", deviceId_);
|
||||
|
||||
ret = aclrtGetRunMode(&runMode_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("acl get run mode failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result ColorizeProcess::InitModel(const char* omModelPath) {
|
||||
Result ret = model_.LoadModelFromFileWithMem(omModelPath);
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("execute LoadModelFromFileWithMem failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = model_.CreateDesc();
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("execute CreateDesc failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = model_.CreateOutput();
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("execute CreateOutput failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
aclrtMalloc(&inputBuf_, (size_t)(inputDataSize_), ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (inputBuf_ == nullptr) {
|
||||
ERROR_LOG("Acl malloc image buffer failed.");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = model_.CreateInput(inputBuf_, inputDataSize_);
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("Create mode input dataset failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result ColorizeProcess::Init() {
|
||||
if (isInited_) {
|
||||
INFO_LOG("Classify instance is initied already!");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result ret = InitResource();
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("Init acl resource failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = InitModel(modelPath_);
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("Init model failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
isInited_ = true;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
Result ColorizeProcess::Preprocess(const string& imageFile) {
|
||||
// read image using OPENCV
|
||||
cv::Mat mat = cv::imread(imageFile, CV_LOAD_IMAGE_COLOR);
|
||||
//resize
|
||||
cv::Mat reiszeMat;
|
||||
cv::resize(mat, reiszeMat, cv::Size(224, 224));
|
||||
|
||||
// deal image
|
||||
reiszeMat.convertTo(reiszeMat, CV_32FC3);
|
||||
reiszeMat = 1.0 * reiszeMat / 255;
|
||||
cv::cvtColor(reiszeMat, reiszeMat, CV_BGR2Lab);
|
||||
|
||||
// pull out L channel and subtract 50 for mean-centering
|
||||
std::vector<cv::Mat> channels;
|
||||
cv::split(reiszeMat, channels);
|
||||
cv::Mat reiszeMatL = channels[0] - 50;
|
||||
|
||||
if (mat.empty()) {
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
if (runMode_ == ACL_HOST) {
|
||||
//AI1上运行时,需要将图片数据拷贝到device侧
|
||||
aclError ret = aclrtMemcpy(inputBuf_, inputDataSize_,
|
||||
reiszeMatL.ptr<uint8_t>(), inputDataSize_,
|
||||
ACL_MEMCPY_HOST_TO_DEVICE);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("Copy resized image data to device failed.");
|
||||
return FAILED;
|
||||
}
|
||||
} else {
|
||||
//Atals200DK上运行时,数据拷贝到本地即可.
|
||||
//reiszeMat是局部变量,数据无法传出函数,需要拷贝一份
|
||||
memcpy(inputBuf_, reiszeMatL.ptr<uint8_t>(), inputDataSize_);
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
Result ColorizeProcess::Inference(aclmdlDataset*& inferenceOutput) {
|
||||
Result ret = model_.Execute();
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("Execute model inference failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
inferenceOutput = model_.GetModelOutputData();
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result ColorizeProcess::Postprocess(const string& imageFile, aclmdlDataset* modelOutput)
|
||||
{
|
||||
uint32_t dataSize = 0;
|
||||
void* data = GetInferenceOutputItem(dataSize, modelOutput);
|
||||
if (data == nullptr) return FAILED;
|
||||
|
||||
uint32_t size = static_cast<uint32_t>(dataSize) / sizeof(float);
|
||||
|
||||
// get a channel and b channel result data
|
||||
cv::Mat mat_a(56, 56, CV_32FC1, const_cast<float*>((float*)data));
|
||||
cv::Mat mat_b(56, 56, CV_32FC1, const_cast<float*>((float*)data + size / 2));
|
||||
|
||||
// pull out L channel in original image
|
||||
cv::Mat mat = cv::imread(imageFile, CV_LOAD_IMAGE_COLOR);
|
||||
mat.convertTo(mat, CV_32FC3);
|
||||
mat = 1.0 * mat / 255;
|
||||
cv::cvtColor(mat, mat, CV_BGR2Lab);
|
||||
std::vector<cv::Mat> channels;
|
||||
cv::split(mat, channels);
|
||||
|
||||
// resize to match size of original image L
|
||||
int r = mat.rows;
|
||||
int c = mat.cols;
|
||||
cv::Mat mat_a_up(r, c, CV_32FC1);
|
||||
cv::Mat mat_b_up(r, c, CV_32FC1);
|
||||
cv::resize(mat_a, mat_a_up, cv::Size(c, r));
|
||||
cv::resize(mat_b, mat_b_up, cv::Size(c, r));
|
||||
|
||||
// result Lab image
|
||||
cv::Mat newChannels[3] = { channels[0], mat_a_up, mat_b_up };
|
||||
cv::Mat resultImage;
|
||||
cv::merge(newChannels, 3, resultImage);
|
||||
|
||||
//convert back to rgb
|
||||
cv::cvtColor(resultImage, resultImage, CV_Lab2BGR);
|
||||
resultImage = resultImage * 255;
|
||||
SaveImage(imageFile, resultImage);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void ColorizeProcess::SaveImage(const string& origImageFile, cv::Mat& image) {
|
||||
int pos = origImageFile.find_last_of("/");
|
||||
|
||||
string filename(origImageFile.substr(pos + 1));
|
||||
stringstream sstream;
|
||||
sstream.str("");
|
||||
sstream << "./output/out_" << filename;
|
||||
|
||||
string outputPath = sstream.str();
|
||||
cv::imwrite(outputPath, image);
|
||||
}
|
||||
|
||||
void* ColorizeProcess::GetInferenceOutputItem(uint32_t& itemDataSize,
|
||||
aclmdlDataset* inferenceOutput) {
|
||||
aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(inferenceOutput, 0);
|
||||
if (dataBuffer == nullptr) {
|
||||
ERROR_LOG("Get the dataset buffer from model "
|
||||
"inference output failed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* dataBufferDev = aclGetDataBufferAddr(dataBuffer);
|
||||
if (dataBufferDev == nullptr) {
|
||||
ERROR_LOG("Get the dataset buffer address "
|
||||
"from model inference output failed");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
size_t bufferSize = aclGetDataBufferSize(dataBuffer);
|
||||
if (bufferSize == 0) {
|
||||
ERROR_LOG("The dataset buffer size of "
|
||||
"model inference output is 0 ");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void* data = nullptr;
|
||||
if (runMode_ == ACL_HOST) {
|
||||
data = Utils::CopyDataDeviceToHost(dataBufferDev, bufferSize);
|
||||
if (data == nullptr) {
|
||||
ERROR_LOG("Copy inference output to host failed");
|
||||
return nullptr;
|
||||
}
|
||||
} else {
|
||||
data = dataBufferDev;
|
||||
}
|
||||
|
||||
itemDataSize = bufferSize;
|
||||
return data;
|
||||
}
|
||||
|
||||
void ColorizeProcess::DestroyResource()
|
||||
{
|
||||
model_.Unload();
|
||||
model_.DestroyDesc();
|
||||
model_.DestroyInput();
|
||||
model_.DestroyOutput();
|
||||
aclError ret;
|
||||
|
||||
ret = aclrtResetDevice(deviceId_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("reset device failed");
|
||||
}
|
||||
INFO_LOG("end to reset device is %d", deviceId_);
|
||||
|
||||
ret = aclFinalize();
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("finalize acl failed");
|
||||
}
|
||||
INFO_LOG("end to finalize acl");
|
||||
aclrtFree(inputBuf_);
|
||||
inputBuf_ = nullptr;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
|
||||
* File main.cpp
|
||||
* Description: dvpp sample main func
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include "colorize_process.h"
|
||||
#include "utils.h"
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
uint32_t kModelWidth = 224;
|
||||
uint32_t kModelHeight = 224;
|
||||
const char* kModelPath = "../model/colorization.om";
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
//检查应用程序执行时的输入,程序执行要求输入图片目录参数
|
||||
if((argc < 2) || (argv[1] == nullptr)){
|
||||
ERROR_LOG("Please input: ./main <image_dir>");
|
||||
return FAILED;
|
||||
}
|
||||
//实例化分类推理对象,参数为分类模型路径,模型输入要求的宽和高
|
||||
ColorizeProcess colorize(kModelPath, kModelWidth, kModelHeight);
|
||||
//初始化分类推理的acl资源, 模型和内存
|
||||
Result ret = colorize.Init();
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("Classification Init resource failed");
|
||||
return FAILED;
|
||||
}
|
||||
//获取图片目录下所有的图片文件名
|
||||
string inputImageDir = string(argv[1]);
|
||||
vector<string> fileVec;
|
||||
Utils::GetAllFiles(inputImageDir, fileVec);
|
||||
if (fileVec.empty()) {
|
||||
ERROR_LOG("Failed to deal all empty path=%s.", inputImageDir.c_str());
|
||||
return FAILED;
|
||||
}
|
||||
//逐张图片推理
|
||||
for (string imageFile : fileVec) {
|
||||
//预处理图片:读取图片,讲图片缩放到模型输入要求的尺寸
|
||||
Result ret = colorize.Preprocess(imageFile);
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("Read file %s failed, continue to read next",
|
||||
imageFile.c_str());
|
||||
continue;
|
||||
}
|
||||
//将预处理的图片送入模型推理,并获取推理结果
|
||||
aclmdlDataset* inferenceOutput = nullptr;
|
||||
ret = colorize.Inference(inferenceOutput);
|
||||
if ((ret != SUCCESS) || (inferenceOutput == nullptr)) {
|
||||
ERROR_LOG("Inference model inference output data failed");
|
||||
return FAILED;
|
||||
}
|
||||
//解析推理输出,并将推理得到的物体类别标记到图片上
|
||||
ret = colorize.Postprocess(imageFile, inferenceOutput);
|
||||
if (ret != SUCCESS) {
|
||||
ERROR_LOG("Process model inference output data failed");
|
||||
return FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
INFO_LOG("Execute sample success");
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
|
||||
* File model_process.cpp
|
||||
* Description: handle model process
|
||||
*/
|
||||
#include "model_process.h"
|
||||
#include <iostream>
|
||||
#include "utils.h"
|
||||
using namespace std;
|
||||
|
||||
ModelProcess::ModelProcess():loadFlag_(false), modelId_(0), modelMemPtr_(nullptr), modelMemSize_(0),
|
||||
modelWeightPtr_(nullptr),modelWeightSize_(0), modelDesc_(nullptr), input_(nullptr), output_(nullptr) {
|
||||
|
||||
}
|
||||
|
||||
ModelProcess::~ModelProcess() {
|
||||
Unload();
|
||||
DestroyDesc();
|
||||
DestroyInput();
|
||||
DestroyOutput();
|
||||
}
|
||||
|
||||
Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) {
|
||||
if (loadFlag_) {
|
||||
ERROR_LOG("has already loaded a model");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("query model failed, model file is %s", modelPath);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_,
|
||||
modelMemSize_, modelWeightPtr_, modelWeightSize_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("load model from file failed, model file is %s", modelPath);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
loadFlag_ = true;
|
||||
INFO_LOG("load model %s success", modelPath);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Result ModelProcess::CreateDesc() {
|
||||
modelDesc_ = aclmdlCreateDesc();
|
||||
if (modelDesc_ == nullptr) {
|
||||
ERROR_LOG("create model description failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
aclError ret = aclmdlGetDesc(modelDesc_, modelId_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("get model description failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
INFO_LOG("create model description success");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void ModelProcess::DestroyDesc() {
|
||||
if (modelDesc_ != nullptr) {
|
||||
(void)aclmdlDestroyDesc(modelDesc_);
|
||||
modelDesc_ = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) {
|
||||
input_ = aclmdlCreateDataset();
|
||||
if (input_ == nullptr) {
|
||||
ERROR_LOG("can't create dataset, create input failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize);
|
||||
if (inputData == nullptr) {
|
||||
ERROR_LOG("can't create data buffer, create input failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
aclError ret = aclmdlAddDatasetBuffer(input_, inputData);
|
||||
if (inputData == nullptr) {
|
||||
ERROR_LOG("can't add data buffer, create input failed");
|
||||
aclDestroyDataBuffer(inputData);
|
||||
inputData = nullptr;
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void ModelProcess::DestroyInput() {
|
||||
if (input_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) {
|
||||
aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i);
|
||||
aclDestroyDataBuffer(dataBuffer);
|
||||
}
|
||||
aclmdlDestroyDataset(input_);
|
||||
input_ = nullptr;
|
||||
}
|
||||
|
||||
Result ModelProcess::CreateOutput() {
|
||||
if (modelDesc_ == nullptr) {
|
||||
ERROR_LOG("no model description, create ouput failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
output_ = aclmdlCreateDataset();
|
||||
if (output_ == nullptr) {
|
||||
ERROR_LOG("can't create dataset, create output failed");
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
size_t outputSize = aclmdlGetNumOutputs(modelDesc_);
|
||||
for (size_t i = 0; i < outputSize; ++i) {
|
||||
size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i);
|
||||
|
||||
void *outputBuffer = nullptr;
|
||||
aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("can't create data buffer, create output failed");
|
||||
aclrtFree(outputBuffer);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
ret = aclmdlAddDatasetBuffer(output_, outputData);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("can't add data buffer, create output failed");
|
||||
aclrtFree(outputBuffer);
|
||||
aclDestroyDataBuffer(outputData);
|
||||
return FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
INFO_LOG("create model output success");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void ModelProcess::DestroyOutput() {
|
||||
if (output_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) {
|
||||
aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i);
|
||||
void* data = aclGetDataBufferAddr(dataBuffer);
|
||||
(void)aclrtFree(data);
|
||||
(void)aclDestroyDataBuffer(dataBuffer);
|
||||
}
|
||||
|
||||
(void)aclmdlDestroyDataset(output_);
|
||||
output_ = nullptr;
|
||||
}
|
||||
|
||||
Result ModelProcess::Execute() {
|
||||
aclError ret = aclmdlExecute(modelId_, input_, output_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("execute model failed, modelId is %u", modelId_);
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
INFO_LOG("model execute success");
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void ModelProcess::Unload() {
|
||||
if (!loadFlag_) {
|
||||
return;
|
||||
}
|
||||
|
||||
aclError ret = aclmdlUnload(modelId_);
|
||||
if (ret != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("unload model failed, modelId is %u", modelId_);
|
||||
}
|
||||
|
||||
if (modelDesc_ != nullptr) {
|
||||
(void)aclmdlDestroyDesc(modelDesc_);
|
||||
modelDesc_ = nullptr;
|
||||
}
|
||||
|
||||
if (modelMemPtr_ != nullptr) {
|
||||
aclrtFree(modelMemPtr_);
|
||||
modelMemPtr_ = nullptr;
|
||||
modelMemSize_ = 0;
|
||||
}
|
||||
|
||||
if (modelWeightPtr_ != nullptr) {
|
||||
aclrtFree(modelWeightPtr_);
|
||||
modelWeightPtr_ = nullptr;
|
||||
modelWeightSize_ = 0;
|
||||
}
|
||||
|
||||
loadFlag_ = false;
|
||||
INFO_LOG("unload model success, modelId is %u", modelId_);
|
||||
}
|
||||
|
||||
aclmdlDataset *ModelProcess::GetModelOutputData() {
|
||||
return output_;
|
||||
}
|
||||
|
||||
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Copyright 2020 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.
|
||||
|
||||
* File utils.cpp
|
||||
* Description: handle file operations
|
||||
*/
|
||||
#include "utils.h"
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <unistd.h>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <vector>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include "acl/acl.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace {
|
||||
const std::string kImagePathSeparator = ",";
|
||||
const int kStatSuccess = 0;
|
||||
const std::string kFileSperator = "/";
|
||||
const std::string kPathSeparator = "/";
|
||||
// output image prefix
|
||||
const std::string kOutputFilePrefix = "out_";
|
||||
}
|
||||
|
||||
bool Utils::IsDirectory(const string &path) {
|
||||
// get path stat
|
||||
struct stat buf;
|
||||
if (stat(path.c_str(), &buf) != kStatSuccess) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// check
|
||||
if (S_ISDIR(buf.st_mode)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Utils::IsPathExist(const string &path) {
|
||||
ifstream file(path);
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Utils::SplitPath(const string &path, vector<string> &path_vec) {
|
||||
char *char_path = const_cast<char*>(path.c_str());
|
||||
const char *char_split = kImagePathSeparator.c_str();
|
||||
char *tmp_path = strtok(char_path, char_split);
|
||||
while (tmp_path) {
|
||||
path_vec.emplace_back(tmp_path);
|
||||
tmp_path = strtok(nullptr, char_split);
|
||||
}
|
||||
}
|
||||
|
||||
void Utils::GetAllFiles(const string &path, vector<string> &file_vec) {
|
||||
// split file path
|
||||
vector<string> path_vector;
|
||||
SplitPath(path, path_vector);
|
||||
|
||||
for (string every_path : path_vector) {
|
||||
// check path exist or not
|
||||
if (!IsPathExist(path)) {
|
||||
ERROR_LOG("Failed to deal path=%s. Reason: not exist or can not access.",
|
||||
every_path.c_str());
|
||||
continue;
|
||||
}
|
||||
// get files in path and sub-path
|
||||
GetPathFiles(every_path, file_vec);
|
||||
}
|
||||
}
|
||||
|
||||
void Utils::GetPathFiles(const string &path, vector<string> &file_vec) {
|
||||
struct dirent *dirent_ptr = nullptr;
|
||||
DIR *dir = nullptr;
|
||||
if (IsDirectory(path)) {
|
||||
dir = opendir(path.c_str());
|
||||
while ((dirent_ptr = readdir(dir)) != nullptr) {
|
||||
// skip . and ..
|
||||
if (dirent_ptr->d_name[0] == '.') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// file path
|
||||
string full_path = path + kPathSeparator + dirent_ptr->d_name;
|
||||
// directory need recursion
|
||||
if (IsDirectory(full_path)) {
|
||||
GetPathFiles(full_path, file_vec);
|
||||
} else {
|
||||
// put file
|
||||
file_vec.emplace_back(full_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
file_vec.emplace_back(path);
|
||||
}
|
||||
}
|
||||
|
||||
void* Utils::CopyDataDeviceToHost(void* deviceData, uint32_t dataSize) {
|
||||
void* hostPtr = nullptr;
|
||||
aclError aclRet = aclrtMalloc(&hostPtr, dataSize, ACL_MEM_MALLOC_HUGE_FIRST);
|
||||
if (aclRet != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("malloc host data buffer failed, aclRet is %d", aclRet);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
aclRet = aclrtMemcpy(hostPtr, dataSize, deviceData, dataSize, ACL_MEMCPY_DEVICE_TO_DEVICE);
|
||||
if (aclRet != ACL_ERROR_NONE) {
|
||||
ERROR_LOG("aclrtMemcpy device to host failed, aclRet is %d", aclRet);
|
||||
(void)aclrtFree(hostPtr);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return hostPtr;
|
||||
}
|
||||
Reference in New Issue
Block a user