diff --git a/research/cv/ArbitraryStyleTransfer/README.md b/research/cv/ArbitraryStyleTransfer/README.md index 301d81534816489bb908c081daebd11b3205e43f..8acadf07e56061bd0457ceabd53b123a27ecba55 100644 --- a/research/cv/ArbitraryStyleTransfer/README.md +++ b/research/cv/ArbitraryStyleTransfer/README.md @@ -9,6 +9,7 @@ - [Script and Sample Code](#script-and-sample-code) - [Script Parameters](#script-parameters) - [Training Process](#training-process) + - [Ascend310 Inference Process](#ascend310-inference-process) - [Model Description](#model-description) - [Performance](#performance) - [Training Performance](#training-performance) @@ -104,7 +105,9 @@ style transfer 鈹斺攢 inceptionv3py # inception-v3 network define 鈹溾攢 output #result 鈹溾攢 test.py # generate style transfer images -鈹斺攢 train.py # train script +鈹溾攢 train.py # train +鈹溾攢 export_for_310.py # export model for 310 infer +鈹斺攢 ascend310_infer # 310 main ``` ## [Script Parameters](#contents) @@ -138,6 +141,27 @@ bash ./scripts/run_eval.sh [PLATFORM] [DEVICE_ID] [CONTENT_PATH] [STYLE_PATH] [I Evaluation result will be stored in the output. Under this, you can find style transfer pictures. +## [Ascend310 Inference Process](#contents) + +### Export MINDIR file + +```bash +python export_for_310.py --ckpt_file [/path/to/ckpt_file] --inception_ckpt [/path/to/inception_ckpt] +``` + +### Ascend310 Inference + +- Run `run_infer_310.sh` for Ascend310 inference. + +```bash +# infer +bash run_infer_310.sh [MINDIR_PATH] [CONTENT_PATH] [STYLE_PATH] [DEVICE_ID] +# example +bash run_infer_310.sh ./style_transfer_model.mindir ./content_test/ ./style_test/ 0 +``` + +Stylized pictures will be stored in the postprocess_Result path. + # [Model Description](#contents) ## [Performance](#contents) @@ -182,4 +206,4 @@ Evaluation result will be stored in the output. Under this, you can find style t # [ModelZoo Homepage](#contents) -Please check the official [homepage](https://gitee.com/mindspore/models). \ No newline at end of file +Please check the official [homepage](https://gitee.com/mindspore/models). diff --git a/research/cv/ArbitraryStyleTransfer/README_CN.md b/research/cv/ArbitraryStyleTransfer/README_CN.md index 1d9eeb3cf3a8f8f7b65bf7a9452c7e0447e3c3c1..df1b565dd11e43a30773c2043d946bdf956484e3 100644 --- a/research/cv/ArbitraryStyleTransfer/README_CN.md +++ b/research/cv/ArbitraryStyleTransfer/README_CN.md @@ -9,6 +9,7 @@ - [鑴氭湰鍜屾牱渚嬩唬鐮乚(#鑴氭湰鍜屾牱渚嬩唬涔�) - [鑴氭湰鍙傛暟](#鑴氭湰鍙傛暟) - [璁粌杩囩▼](#璁粌杩囩▼) + - [Ascend310鎺ㄧ悊杩囩▼](#ascend310鎺ㄧ悊杩囩▼) - [妯″瀷鎻忚堪](#妯″瀷鎻忚堪) - [鏁堟灉](#鏁堟灉) - [璁粌鏁堟灉](#璁粌鏁堟灉) @@ -71,7 +72,7 @@ Style Transfer Networks鐨勮缁冭繃绋嬮渶瑕侀鍏堣缁冪殑VGG16鍜孖nception-v3 # [鑴氭湰鎻忚堪](#鍐呭) -## [[鑴氭湰鍜屾牱渚嬩唬鐮乚(#鍐呭) +## [鑴氭湰鍜屾牱渚嬩唬鐮乚(#鍐呭) ```shell style transfer @@ -102,7 +103,9 @@ style transfer 鈹斺攢 inceptionv3py # inception-v3 缃戠粶鐨勫畾涔� 鈹溾攢 output # 杈撳嚭缁撴灉鐨勬枃浠跺す 鈹溾攢 test.py # 妯″瀷娴嬭瘯浠g爜 -鈹斺攢 train.py # 妯″瀷璁粌浠g爜 +鈹溾攢 train.py # 妯″瀷璁粌浠g爜 +鈹溾攢 export_for_310.py # 涓�310鎺ㄧ悊瀵煎嚭妯″瀷 +鈹斺攢 ascend310_infer # 310鎺ㄧ悊绋嬪簭 ``` ## [鑴氭湰鍙傛暟](#鍐呭) @@ -136,6 +139,27 @@ bash ./scripts/run_eval.sh [PLATFORM] [DEVICE_ID] [CONTENT_PATH] [STYLE_PATH] [I 璇勪及缁撴灉灏嗗瓨鍌ㄥ湪杈撳嚭涓€傚湪閭i噷锛屼綘鍙互鎵惧埌椋庢牸杞崲鍥剧墖銆� +## [Ascend310鎺ㄧ悊杩囩▼](#鍐呭) + +### 瀵煎嚭 MINDIR 鏂囦欢 + +```bash +python export_for_310.py --ckpt_file [/path/to/ckpt_file] --inception_ckpt [/path/to/inception_ckpt] +``` + +### Ascend310鎺ㄧ悊 + +- 杩愯 `run_infer_310.sh` 浠ヨ繘琛�310鎺ㄧ悊. + +```bash +# 310鎺ㄧ悊 +bash run_infer_310.sh [MINDIR_PATH] [CONTENT_PATH] [STYLE_PATH] [DEVICE_ID] +# 绀轰緥 +bash run_infer_310.sh ./style_transfer_model.mindir ./content_test/ ./style_test/ 0 +``` + +椋庢牸鍖栧浘鐗囧皢瀛樺偍鍦� postprocess_Result 鏂囦欢澶逛腑. + # [妯″瀷鎻忚堪](#鍐呭) ## [鏁堟灉](#鍐呭) @@ -182,4 +206,4 @@ bash ./scripts/run_eval.sh [PLATFORM] [DEVICE_ID] [CONTENT_PATH] [STYLE_PATH] [I # [ModelZoo 涓婚〉](#鍐呭) -璇风偣鍑昏繘鍏ュ畼鏂筟涓婚〉](https://gitee.com/mindspore/models). \ No newline at end of file +璇风偣鍑昏繘鍏ュ畼鏂筟涓婚〉](https://gitee.com/mindspore/models). diff --git a/research/cv/ArbitraryStyleTransfer/ascend310_infer/CMakeLists.txt b/research/cv/ArbitraryStyleTransfer/ascend310_infer/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..f936989f21405ae4ba068d1284d62f4e1b8613a5 --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/ascend310_infer/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.14.1) +project(Ascend310Infer) +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") +set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) +option(MINDSPORE_PATH "mindspore install path" "") +include_directories(${MINDSPORE_PATH}) +include_directories(${MINDSPORE_PATH}/include) +include_directories(${PROJECT_SRC_ROOT}) +find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) +file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) + +add_executable(main src/main.cc src/utils.cc) +target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags) +find_package(gflags REQUIRED) \ No newline at end of file diff --git a/research/cv/ArbitraryStyleTransfer/ascend310_infer/build.sh b/research/cv/ArbitraryStyleTransfer/ascend310_infer/build.sh new file mode 100644 index 0000000000000000000000000000000000000000..713d7f657ddfa5f75b069351c55f8447f77c72d0 --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/ascend310_infer/build.sh @@ -0,0 +1,29 @@ +#!/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. +# ============================================================================ +if [ -d out ]; then + rm -rf out +fi + +mkdir out +cd out || exit + +if [ -f "Makefile" ]; then + make clean +fi + +cmake .. \ + -DMINDSPORE_PATH="`pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make diff --git a/research/cv/ArbitraryStyleTransfer/ascend310_infer/inc/utils.h b/research/cv/ArbitraryStyleTransfer/ascend310_infer/inc/utils.h new file mode 100644 index 0000000000000000000000000000000000000000..13eb133d68b8dd4229b5d3f4f93aef5d9ba43c3e --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/ascend310_infer/inc/utils.h @@ -0,0 +1,33 @@ +/** + * Copyright 2022 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. + */ + +#ifndef MINDSPORE_INFERENCE_UTILS_H_ +#define MINDSPORE_INFERENCE_UTILS_H_ + +#include <sys/stat.h> +#include <dirent.h> +#include <vector> +#include <string> +#include <memory> +#include "include/api/types.h" + +std::vector<std::string> GetAllFiles(std::string_view dirName); +DIR *OpenDir(std::string_view dirName); +std::string RealPath(std::string_view path); +mindspore::MSTensor ReadFileToTensor(const std::string &file); +int WriteResult(const std::string &contentFile, const std::string &styleFile, + const std::vector<mindspore::MSTensor> &outputs); +#endif diff --git a/research/cv/ArbitraryStyleTransfer/ascend310_infer/src/main.cc b/research/cv/ArbitraryStyleTransfer/ascend310_infer/src/main.cc new file mode 100644 index 0000000000000000000000000000000000000000..ca0f2e7f9360f34d930c46f13537dae019416bed --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/ascend310_infer/src/main.cc @@ -0,0 +1,148 @@ +/** + * Copyright 2022 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. + */ +#include <dirent.h> +#include <gflags/gflags.h> +#include <sys/time.h> +#include <algorithm> +#include <fstream> +#include <iosfwd> +#include <iostream> +#include <sstream> +#include <string> +#include <vector> + +#include "inc/utils.h" +#include "include/api/context.h" +#include "include/api/model.h" +#include "include/api/serialization.h" +#include "include/api/types.h" +#include "include/dataset/execute.h" +#include "include/dataset/vision.h" + +using mindspore::Context; +using mindspore::GraphCell; +using mindspore::kSuccess; +using mindspore::Model; +using mindspore::ModelType; +using mindspore::MSTensor; +using mindspore::Serialization; +using mindspore::Status; +using mindspore::dataset::Execute; + +DEFINE_string(mindir_path, "", "mindir path"); +DEFINE_string(input_path, ".", "input path"); +DEFINE_int32(device_id, 0, "device id"); + +int main(int argc, char **argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (RealPath(FLAGS_mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + + auto context = std::make_shared<Context>(); + auto ascend310 = std::make_shared<mindspore::Ascend310DeviceInfo>(); + ascend310->SetDeviceID(FLAGS_device_id); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); + + Model model; + Status ret = model.Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + std::vector<MSTensor> model_inputs = model.GetInputs(); + if (model_inputs.empty()) { + std::cout << "Invalid model, inputs is empty." << std::endl; + return 1; + } + std::map<double, double> costTime_map; + // get content file path + auto content_files = GetAllFiles(FLAGS_input_path + "/content/"); + if (content_files.empty()) { + std::cout << "ERROR: content data empty." << std::endl; + return 1; + } + size_t content_size = content_files.size(); + // get style file path + auto style_files = GetAllFiles(FLAGS_input_path + "/style/"); + + if (style_files.empty()) { + std::cout << "ERROR: style data empty." << std::endl; + return 1; + } + size_t style_size = style_files.size(); + + for (size_t i = 0; i < content_size; ++i) { + for (size_t j = 0; j < style_size; ++j) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs; + double endTimeMs; + std::vector<MSTensor> inputs; + std::vector<MSTensor> outputs; + std::cout << "Start predict content files:" << content_files[i] + << " with style file:" << style_files[j] << std::endl; + + auto input0 = ReadFileToTensor(content_files[i]); + auto input1 = ReadFileToTensor(style_files[j]); + + inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), + model_inputs[0].Shape(), input0.Data().get(), + input0.DataSize()); + + inputs.emplace_back(model_inputs[1].Name(), model_inputs[1].DataType(), + model_inputs[1].Shape(), input1.Data().get(), + input1.DataSize()); + + gettimeofday(&start, nullptr); + ret = model.Predict(inputs, &outputs); + gettimeofday(&end, nullptr); + if (ret != kSuccess) { + std::cout << "Predict " << style_files[j] << " failed." << std::endl; + return 1; + } + startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; + endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; + costTime_map.insert(std::pair<double, double>(startTimeMs, endTimeMs)); + std::cout << content_files[i] + "_" + style_files[j] << std::endl; + WriteResult(content_files[i], style_files[j], outputs); + } + } + double average = 0.0; + int inferCount = 0; + + for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { + double diff = iter->second - iter->first; + average += diff; + inferCount++; + } + average = average / inferCount; + std::stringstream timeCost; + timeCost << "NN inference cost average time: " << average + << " ms of infer_count " << inferCount << std::endl; + std::cout << "NN inference cost average time: " << average + << "ms of infer_count " << inferCount << std::endl; + std::string fileName = + "./time_Result" + std::string("/test_perform_static.txt"); + std::ofstream fileStream(fileName.c_str(), std::ios::trunc); + fileStream << timeCost.str(); + fileStream.close(); + costTime_map.clear(); + return 0; +} diff --git a/research/cv/ArbitraryStyleTransfer/ascend310_infer/src/utils.cc b/research/cv/ArbitraryStyleTransfer/ascend310_infer/src/utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..14b996fff2a6e28d29daac026412bda1f59e12fb --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/ascend310_infer/src/utils.cc @@ -0,0 +1,137 @@ +/** + * Copyright 2022 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. + */ + +#include "inc/utils.h" +#include <algorithm> +#include <fstream> +#include <iostream> + +using mindspore::DataType; +using mindspore::MSTensor; + +std::vector<std::string> GetAllFiles(std::string_view dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector<std::string> res; + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || dName == ".." || filename->d_type != DT_REG) { + continue; + } + res.emplace_back(std::string(dirName) + "/" + filename->d_name); + } + std::sort(res.begin(), res.end()); + for (auto &f : res) { + std::cout << "image file: " << f << std::endl; + } + return res; +} + +int WriteResult(const std::string &contentFile, const std::string &styleFile, + const std::vector<MSTensor> &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr<const void> netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + int pos = contentFile.rfind('/'); + std::string contentName(contentFile, pos + 1); + contentName.replace(contentName.find('.'), + contentName.size() - contentName.find('.'), ""); + + pos = styleFile.rfind('/'); + std::string styleName(styleFile, pos + 1); + styleName.replace(styleName.find('.'), + styleName.size() - styleName.find('.'), ""); + + std::string outFileName = homePath + "/" + contentName + styleName + ".bin"; + FILE *outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + fclose(outputFile); + outputFile = nullptr; + } + return 0; +} + +mindspore::MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return mindspore::MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return mindspore::MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return mindspore::MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, + {static_cast<int64_t>(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast<char *>(buffer.MutableData()), size); + ifs.close(); + + return buffer; +} + +DIR *OpenDir(std::string_view dirName) { + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + std::string realPath = RealPath(dirName); + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; +} + +std::string RealPath(std::string_view path) { + char realPathMem[PATH_MAX] = {0}; + char *realPathRet = nullptr; + realPathRet = realpath(path.data(), realPathMem); + + if (realPathRet == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string realPath(realPathMem); + std::cout << path << " realpath is: " << realPath << std::endl; + return realPath; +} diff --git a/research/cv/ArbitraryStyleTransfer/export_for_310.py b/research/cv/ArbitraryStyleTransfer/export_for_310.py new file mode 100644 index 0000000000000000000000000000000000000000..4ab994a643717a0f1c239bb55c2081417943353a --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/export_for_310.py @@ -0,0 +1,78 @@ +# Copyright 2022 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 for evaling""" +import argparse +import numpy as np + +import mindspore +from mindspore import Tensor, Parameter +import mindspore.nn as nn +import mindspore.ops as ops +from mindspore.common import set_seed +from mindspore.train.serialization import load_checkpoint, load_param_into_net +from mindspore.train.serialization import export +from src.inceptionv3 import inceptionv3 +from src.model import get_model + +set_seed(1) +parser = argparse.ArgumentParser(description="style transfer train") +# data loader +parser.add_argument("--inception_ckpt", type=str, default='./pretrained_model/inceptionv3.ckpt') +parser.add_argument('--file_format', type=str, choices=['AIR', 'ONNX', 'MINDIR'], default='MINDIR', \ + help='file format') +parser.add_argument("--ckpt_path", type=str, default='./ckpt/style_transfer_model_0100.ckpt') +parser.add_argument('--platform', type=str, choices=['Ascend', 'GPU'], default='Ascend', help='Ascend or GPU') +parser.add_argument("--device_id", type=int, default=0, help="device id, default: 0.") +parser.add_argument("--image_size", type=int, default=256, help='image size, default: image_size.') +parser.add_argument('--file_name', type=str, default='style_transfer', help='output file name prefix.') +parser.add_argument("--style_dim", type=int, default=100, + help="Style vector dimension. default: 100") +parser.add_argument('--init_type', type=str, default='normal', choices=("normal", "xavier"), \ + help='network initialization, default is normal.') +parser.add_argument('--init_gain', type=float, default=0.02, \ + help='scaling factor for normal, xavier and orthogonal, default is 0.02.') + +if __name__ == '__main__': + args = parser.parse_args() + image_size = args.image_size + transfer_net = get_model(args) + params = load_checkpoint(args.ckpt_path) + load_param_into_net(transfer_net, params) + + + class stylization(nn.Cell): + def __init__(self): + super(stylization, self).__init__() + self.inception = inceptionv3(args.inception_ckpt) + self.transfer_net = transfer_net + self.scale = Parameter(Tensor(np.array([2, 2, 2]).reshape([1, 3, 1, 1]), mindspore.float32)) + self.offset = Parameter(Tensor(np.array([1, 1, 1]).reshape([1, 3, 1, 1]), mindspore.float32)) + self.op_concat = ops.Concat(axis=0) + + def construct(self, content, style_shifted): + s_in_feat = self.inception(style_shifted) + c_in_feat = self.inception(content[1]) + interporated_stylied_img = self.transfer_net.construct_interpolation_310(content[0], s_in_feat, c_in_feat) + return self.op_concat([style_shifted, interporated_stylied_img, content[0]]) + + + model = stylization() + input1_shape = [2, 1, 3, args.image_size, args.image_size] + input2_shape = [1, 3, args.image_size, args.image_size] + input_array_1 = Tensor(np.random.uniform(-1.0, 1.0, size=input1_shape).astype(np.float32)) + input_array_2 = Tensor(np.random.uniform(-1.0, 1.0, size=input2_shape).astype(np.float32)) + G_file = f"{args.file_name}_model" + export(model, input_array_1, input_array_2, file_name=G_file, file_format=args.file_format) diff --git a/research/cv/ArbitraryStyleTransfer/postprocess.py b/research/cv/ArbitraryStyleTransfer/postprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..bffae964b9b71bc2c0a9a8830adc056180c82fb6 --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/postprocess.py @@ -0,0 +1,77 @@ +# Copyright 2022 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. +# ============================================================================ +"""postprocess""" +import argparse +import os + +import imageio +import numpy as np + +parser = argparse.ArgumentParser() +parser.add_argument("--bin_path", type=str, help='bin_path, path to binary files generated by 310 model, default: None') +parser.add_argument("--image_size", type=int, default=256, help='image size, default: image_size.') +parser.add_argument("--output_dir", type=str, default='output_dir', + help='output_path, path to store output, default: None') +parser.add_argument("--output_dir_interpolation", type=str, default='output_dir', + help='output_path, path to store output, default: None') +args = parser.parse_args() + + +def convert(img): + img = np.swapaxes(img, 0, 1) + img = np.swapaxes(img, 1, 2) + return img + + +def post_process(img): + img = convert(img) + img = (img + 1.0) / 2.0 + img = np.clip(img, 0, 1.0) + return img + + +if __name__ == "__main__": + bin_path = args.bin_path + image_size = args.image_size + content_list = os.listdir(args.bin_path) + + for i in range(0, len(content_list)): + pic_path = os.path.join(args.bin_path, content_list[i]) + b = np.fromfile(pic_path, dtype=np.float32, count=8 * 3 * image_size * image_size) + b = np.reshape(b, (8, 3, image_size, image_size)) + # get output images + output = () + style_shifted = convert(b[0]) + std = np.array([0.229, 0.224, 0.225]).reshape([1, 1, 3]) + mean = np.array([0.485, 0.456, 0.406]).reshape([1, 1, 3]) + style = style_shifted * std + mean + style = np.clip(style, 0, 1.0) + output = (output + (style,)) + for j in range(1, 7): + output = ((post_process(b[j]),) + output) + print(b[7].max()) + print(b[7].min()) + original = post_process(b[7]) + output = ((original,) + output) + # save images + content_name = content_list[i].replace(".bin", "") + image_path_interpolation = os.path.join(args.output_dir_interpolation, content_name) + ".png" + output_interpolation = np.concatenate(output, axis=1) + imageio.imsave(image_path_interpolation, output_interpolation) + + image_path = os.path.join(args.output_dir, content_name) + ".png" + output_single = np.concatenate((output[0], output[7], output[6]), axis=1) + imageio.imsave(image_path, output_single) + print("%d / %d , %s \n" % (i + 1, len(content_list), content_name)) diff --git a/research/cv/ArbitraryStyleTransfer/preprocess.py b/research/cv/ArbitraryStyleTransfer/preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..8781832993bdde27942dcd9bcb7a251be9612307 --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/preprocess.py @@ -0,0 +1,77 @@ +# Copyright 2022 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. +# ============================================================================ +"""preprocess""" +import argparse +import os + +import numpy as np +from PIL import Image + +parser = argparse.ArgumentParser('preprocess') +parser.add_argument("--content_path", type=str, help='content_path, default: None') +parser.add_argument("--style_path", type=str, help='style_path, default: None') +parser.add_argument("--image_size", type=int, default=256, help='image size, default: image_size.') +parser.add_argument('--output_path', type=str, default="./preprocess_Result", help='eval data dir') +args = parser.parse_args() + +if __name__ == "__main__": + + if not os.path.exists(args.output_path): + os.makedirs(args.output_path) + image_dir_style = os.path.join(args.output_path, "style") + if not os.path.exists(image_dir_style): + os.makedirs(image_dir_style) + image_dir_content = os.path.join(args.output_path, "content") + if not os.path.exists(image_dir_content): + os.makedirs(image_dir_content) + content_list = os.listdir(args.content_path) + for j in range(0, len(content_list)): + pic_path = os.path.join(args.content_path, content_list[j]) + # content_pic = crop_and_resize(pic_path, size=256) + img_c = Image.open(pic_path).convert("RGB") + img_c = np.array(img_c.resize((256, 256))) + img_c = (img_c / 127.5) - 1.0 + img_c = img_c.transpose(2, 0, 1).astype(np.float32) + img_c = np.reshape(img_c, (1, 3, 256, 256)) + file_name = content_list[j].replace(".jpg", "") + ".bin" + image_path_content = os.path.join(args.output_path, "content", file_name) + content_pic_shifted = img_c.copy() + content_pic_shifted += 1 + content_pic_shifted /= 2 + content_pic_shifted -= np.array([0.485, 0.456, 0.406]).reshape([1, 3, 1, 1]) + content_pic_shifted /= np.array([0.229, 0.224, 0.225]).reshape([1, 3, 1, 1]) + img_c = np.reshape(img_c, (1, 1, 3, 256, 256)) + content_pic_shifted = np.reshape(content_pic_shifted, (1, 1, 3, 256, 256)) + output = np.concatenate((img_c, content_pic_shifted), axis=0) + output.tofile(image_path_content) + + style_list = os.listdir(args.style_path) + + for j in range(0, len(style_list)): + pic_path = os.path.join(args.style_path, style_list[j]) + img_c = Image.open(pic_path).convert("RGB") + img_c = np.array(img_c.resize((256, 256))) + img_c = (img_c / 127.5) - 1.0 + img_c = img_c.transpose(2, 0, 1).astype(np.float32) + img_c = np.reshape(img_c, (1, 3, 256, 256)) + style_pic = img_c + style_pic += 1 + style_pic /= 2 + style_pic -= np.array([0.485, 0.456, 0.406]).reshape([1, 3, 1, 1]) + style_pic /= np.array([0.229, 0.224, 0.225]).reshape([1, 3, 1, 1]) + file_name = style_list[j].replace(".jpg", "") + ".bin" + image_path_style = os.path.join(args.output_path, "style", file_name) + style_pic.tofile(image_path_style) + print("Export bin files finished!") diff --git a/research/cv/ArbitraryStyleTransfer/scripts/run_infer_310.sh b/research/cv/ArbitraryStyleTransfer/scripts/run_infer_310.sh new file mode 100644 index 0000000000000000000000000000000000000000..f1d25395c97706e4a950e4500416f96a4bdc551f --- /dev/null +++ b/research/cv/ArbitraryStyleTransfer/scripts/run_infer_310.sh @@ -0,0 +1,118 @@ +#!/bin/bash +# Copyright 2022 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. +# ============================================================================ +if [[ $# -lt 3 || $# -gt 4 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [CONTENT_PATH] [STYLE_PATH] [DEVICE_ID] + DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" +exit 1 +fi +get_real_path(){ + + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} +model=$(get_real_path $1) +content_path=$(get_real_path $2) +style_path=$(get_real_path $3) +device_id=0 +if [ $# == 4 ]; then + device_id=$4 +fi +echo "mindir name: "$model +echo "content path: "$content_path +echo "style path: "$style_path +echo "device id: "$device_id + +export ASCEND_HOME=/usr/local/Ascend/ +if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then + export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe + export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp +else + export PATH=$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH + export PYTHONPATH=$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/opp +fi + +function preprocess_data() +{ + if [ -d preprocess_Result ]; then + rm -rf ./preprocess_Result + fi + mkdir preprocess_Result + python3.7 ../preprocess.py --content_path $content_path --style_path $style_path --output_path='./preprocess_Result/' +} +function compile_app() +{ + cd ../ascend310_infer/ || exit + bash build.sh &> build.log +} + +function infer() +{ + cd - || exit + if [ -d result_Files ]; then + rm -rf ./result_Files + fi + if [ -d time_Result ]; then + rm -rf ./time_Result + fi + mkdir result_Files + mkdir time_Result + + ../ascend310_infer/out/main --mindir_path=$model --input_path=./preprocess_Result --device_id=$device_id &> infer.log +} + +function post_process() +{ + if [ -d postprocess_Result ]; then + rm -rf ./postprocess_Result + fi + mkdir postprocess_Result + if [ -d postprocess_Result_interpolation ]; then + rm -rf ./postprocess_Result_interpolation + fi + mkdir postprocess_Result_interpolation + python3.7 ../postprocess.py --bin_path='./result_Files' --output_dir='./postprocess_Result/' --output_dir_interpolation='./postprocess_Result_interpolation/' &> postprocess.log +} + +preprocess_data +if [ $? -ne 0 ]; then + echo "preprocess dataset failed" + exit 1 +fi + +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi + +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi + +post_process +if [ $? -ne 0 ]; then + echo " execute post_process failed" + exit 1 +fi \ No newline at end of file diff --git a/research/cv/ArbitraryStyleTransfer/src/model.py b/research/cv/ArbitraryStyleTransfer/src/model.py index 1d184cb2011eacd4a6a695bbcc666c0ada0a8a5e..3876e6fba159465240e38bf4780c89b3971d77b5 100644 --- a/research/cv/ArbitraryStyleTransfer/src/model.py +++ b/research/cv/ArbitraryStyleTransfer/src/model.py @@ -47,6 +47,7 @@ class style_transfer_model(nn.Cell): super(style_transfer_model, self).__init__() self.style_prediction_network = networks.style_prediction_network(args, style_dim=style_dim) self.style_transfer_network = networks.style_transfer_network(args, style_dim=style_dim) + self.concat_ops = ops.Concat(axis=0) def construct(self, content_img, style_feat): """construct""" @@ -66,6 +67,10 @@ class style_transfer_model(nn.Cell): stylied_img_6 = self.style_transfer_network(content_img, style_vector_1 * 0.0 + style_vector_2 * 1.0) out = [stylied_img_1, stylied_img_2, stylied_img_3, stylied_img_4, stylied_img_5, stylied_img_6] return out + def construct_interpolation_310(self, content_img, style_feat_1, style_feat_2): + """construct_interpolation for 310""" + out = self.construct_interpolation(content_img, style_feat_1, style_feat_2) + return self.concat_ops(out) class TrainOnestepStyleTransfer(nn.Cell): diff --git a/research/cv/ArbitraryStyleTransfer/src/networks.py b/research/cv/ArbitraryStyleTransfer/src/networks.py index a849a9e4a1ce7d8007cfdd23a961ee8bb490deee..f893d1e2513fabe70c3e76a97b4ac1871a484dfd 100644 --- a/research/cv/ArbitraryStyleTransfer/src/networks.py +++ b/research/cv/ArbitraryStyleTransfer/src/networks.py @@ -15,10 +15,12 @@ """arbitrary style transfer network.""" +import numpy as np +import mindspore import mindspore.nn as nn from mindspore import dtype as mstype from mindspore import Parameter -from mindspore import ops +from mindspore import ops, Tensor from mindspore.common import initializer as init @@ -37,18 +39,24 @@ class InstanceNorm2d(nn.Cell): beta_init='zeros'): super().__init__() self.num_features = num_features - self.eps = eps self.moving_mean = Parameter(init.initializer('zeros', num_features), name="mean", requires_grad=False) self.moving_variance = Parameter(init.initializer('ones', num_features), name="variance", requires_grad=False) self.gamma = Parameter(init.initializer(gamma_init, num_features), name="gamma", requires_grad=affine) self.beta = Parameter(init.initializer(beta_init, num_features), name="beta", requires_grad=affine) - + self.sqrt = ops.Sqrt() + self.eps = Tensor(np.array([eps]), mindspore.float32) + self.cast = ops.Cast() def construct(self, x): """calculate InstanceNorm output""" mean = ops.ReduceMean(keep_dims=True)(x, (2, 3)) - var = ops.ReduceMean(keep_dims=True)(((x - mean) ** 2), (2, 3)) - std = (var + self.eps) ** 0.5 - x = (x - mean) / std * self.gamma.reshape(1, -1, 1, 1) + self.beta.reshape(1, -1, 1, 1) + mean = self.cast(mean, mindspore.float32) + tmp = x - mean + tmp = tmp * tmp + var = ops.ReduceMean(keep_dims=True)(tmp, (2, 3)) + std = self.sqrt(var+ self.eps) + gamma_t = self.cast(self.gamma, mindspore.float32) + beta_t = self.cast(self.beta, mindspore.float32) + x = (x - mean) / std * gamma_t.reshape(1, -1, 1, 1) + beta_t.reshape(1, -1, 1, 1) return x @@ -273,6 +281,7 @@ class UpsampleConvInReluWithStyle(nn.Cell): self.instancenorm = InstanceNorm2d(channels_out) self.fc_beta = nn.Dense(style_dim, channels_out) self.fc_gamma = nn.Dense(style_dim, channels_out) + self.cast = ops.Cast() if activation: self.activation = activation() else: @@ -290,7 +299,8 @@ class UpsampleConvInReluWithStyle(nn.Cell): gamma = self.fc_gamma(style) gamma = expand_dims(gamma, 2) gamma = expand_dims(gamma, 3) - + gamma = self.cast(gamma, mindspore.float32) + beta = self.cast(beta, mindspore.float16) if self.upsample: x = self.upsample_layer(x, scale_factor=self.upsample) x = self.pad(x)