Files
FastDeploy/fastdeploy/vision/generation/contrib/preprocessor.cc
chenjian 87bcb5df21 [Model] add style transfer model (#922)
* add style transfer model

* add examples for generation model

* add unit test

* add speed comparison

* add speed comparison

* add variable for constant

* add preprocessor and postprocessor

* add preprocessor and postprocessor

* fix

* fix according to review

Co-authored-by: DefTruth <31974251+DefTruth@users.noreply.github.com>
2023-01-03 10:47:08 +08:00

64 lines
2.1 KiB
C++

// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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 "fastdeploy/vision/generation/contrib/preprocessor.h"
namespace fastdeploy {
namespace vision {
namespace generation {
bool AnimeGANPreprocessor::Run(std::vector<Mat>& images, std::vector<FDTensor>* outputs) {
// 1. BGR2RGB
// 2. Convert(opencv style) or Normalize
for (size_t i = 0; i < images.size(); ++i) {
auto ret = BGR2RGB::Run(&images[i]);
if (!ret) {
FDERROR << "Failed to processs image:" << i << " in "
<< "BGR2RGB" << "." << std::endl;
return false;
}
ret = Cast::Run(&images[i], "float");
if (!ret) {
FDERROR << "Failed to processs image:" << i << " in "
<< "Cast" << "." << std::endl;
return false;
}
std::vector<float> mean{1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
std::vector<float> std {-1.f, -1.f, -1.f};
ret = Convert::Run(&images[i], mean, std);
if (!ret) {
FDERROR << "Failed to processs image:" << i << " in "
<< "Cast" << "." << std::endl;
return false;
}
}
outputs->resize(1);
// Concat all the preprocessed data to a batch tensor
std::vector<FDTensor> tensors(images.size());
for (size_t i = 0; i < images.size(); ++i) {
images[i].ShareWithTensor(&(tensors[i]));
tensors[i].ExpandDim(0);
}
if (tensors.size() == 1) {
(*outputs)[0] = std::move(tensors[0]);
} else {
function::Concat(tensors, &((*outputs)[0]), 0);
}
return true;
}
} // namespace generation
} // namespace vision
} // namespace fastdeploy