!12623 Update ascend310_infer of ssd.

From: @c_34
Reviewed-by: @wuxuejian,@guoqi1024
Signed-off-by: @wuxuejian
This commit is contained in:
mindspore-ci-bot 2021-02-26 16:26:36 +08:00 committed by Gitee
commit e1980d2386
4 changed files with 66 additions and 66 deletions

View File

@ -27,6 +27,6 @@
std::vector<std::string> GetAllFiles(std::string_view dirName);
DIR *OpenDir(std::string_view dirName);
std::string RealPath(std::string_view path);
std::shared_ptr<mindspore::api::Tensor> ReadFileToTensor(const std::string &file);
int WriteResult(const std::string& imageFile, const std::vector<mindspore::api::Buffer> &outputs);
mindspore::MSTensor ReadFileToTensor(const std::string &file);
int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs);
#endif

View File

@ -25,23 +25,25 @@
#include "include/api/model.h"
#include "include/api/context.h"
#include "minddata/dataset/include/minddata_eager.h"
#include "../inc/utils.h"
#include "include/api/types.h"
#include "include/api/serialization.h"
#include "minddata/dataset/include/vision.h"
#include "include/minddata/dataset/include/vision.h"
#include "include/minddata/dataset/include/execute.h"
#include "../inc/utils.h"
using mindspore::GlobalContext;
using mindspore::Serialization;
using mindspore::Model;
using mindspore::ModelContext;
using mindspore::Status;
using mindspore::ModelType;
using mindspore::GraphCell;
using mindspore::kSuccess;
using mindspore::MSTensor;
using mindspore::dataset::Execute;
using mindspore::dataset::vision::DvppDecodeResizeCropJpeg;
using mindspore::api::Context;
using mindspore::api::Serialization;
using mindspore::api::Model;
using mindspore::api::kModelOptionInsertOpCfgPath;
using mindspore::api::Status;
using mindspore::api::MindDataEager;
using mindspore::api::Buffer;
using mindspore::api::ModelType;
using mindspore::api::GraphCell;
using mindspore::api::SUCCESS;
using mindspore::dataset::vision::DvppDecodeResizeJpeg;
DEFINE_string(mindir_path, "", "mindir path");
DEFINE_string(dataset_path, ".", "dataset path");
@ -59,16 +61,17 @@ int main(int argc, char **argv) {
return 1;
}
Context::Instance().SetDeviceTarget("Ascend310").SetDeviceID(FLAGS_device_id);
GlobalContext::SetGlobalDeviceTarget(mindspore::kDeviceTypeAscend310);
GlobalContext::SetGlobalDeviceID(FLAGS_device_id);
auto graph = Serialization::LoadModel(FLAGS_mindir_path, ModelType::kMindIR);
Model model((GraphCell(graph)));
std::map<std::string, std::string> build_options;
auto model_context = std::make_shared<mindspore::ModelContext>();
if (!FLAGS_aipp_path.empty()) {
build_options.emplace(kModelOptionInsertOpCfgPath, FLAGS_aipp_path);
ModelContext::SetInsertOpConfigPath(model_context, FLAGS_aipp_path);
}
Status ret = model.Build(build_options);
if (ret != SUCCESS) {
Model model(GraphCell(graph), model_context);
Status ret = model.Build();
if (ret != kSuccess) {
std::cout << "ERROR: Build failed." << std::endl;
return 1;
}
@ -81,22 +84,24 @@ int main(int argc, char **argv) {
std::map<double, double> costTime_map;
size_t size = all_files.size();
MindDataEager SingleOp({DvppDecodeResizeJpeg({640, 640})});
Execute resize_op(DvppDecodeResizeCropJpeg({640, 640}, {640, 640}));
for (size_t i = 0; i < size; ++i) {
struct timeval start = {0};
struct timeval end = {0};
double startTimeMs;
double endTimeMs;
std::vector<Buffer> inputs;
std::vector<Buffer> outputs;
std::vector<MSTensor> inputs;
std::vector<MSTensor> outputs;
std::cout << "Start predict input files:" << all_files[i] << std::endl;
auto imgDvpp = SingleOp(ReadFileToTensor(all_files[i]));
auto imgDvpp = std::make_shared<MSTensor>();
resize_op(ReadFileToTensor(all_files[i]), imgDvpp.get());
inputs.emplace_back(imgDvpp->Data(), imgDvpp->DataSize());
inputs.emplace_back(imgDvpp->Name(), imgDvpp->DataType(), imgDvpp->Shape(),
imgDvpp->Data().get(), imgDvpp->DataSize());
gettimeofday(&start, nullptr);
ret = model.Predict(inputs, &outputs);
gettimeofday(&end, nullptr);
if (ret != SUCCESS) {
if (ret != kSuccess) {
std::cout << "Predict " << all_files[i] << " failed." << std::endl;
return 1;
}

View File

@ -19,10 +19,8 @@
#include <iostream>
#include "../inc/utils.h"
using mindspore::api::Tensor;
using mindspore::api::Buffer;
using mindspore::api::DataType;
using mindspore::MSTensor;
using mindspore::DataType;
std::vector<std::string> GetAllFiles(std::string_view dirName) {
struct dirent *filename;
@ -45,11 +43,11 @@ std::vector<std::string> GetAllFiles(std::string_view dirName) {
return res;
}
int WriteResult(const std::string& imageFile, const std::vector<Buffer> &outputs) {
int WriteResult(const std::string& imageFile, const std::vector<MSTensor> &outputs) {
std::string homePath = "./result_Files";
for (size_t i = 0; i < outputs.size(); ++i) {
size_t outputSize;
const void * netOutput;
std::shared_ptr<const void> netOutput;
netOutput = outputs[i].Data();
outputSize = outputs[i].DataSize();
int pos = imageFile.rfind('/');
@ -57,44 +55,42 @@ int WriteResult(const std::string& imageFile, const std::vector<Buffer> &outputs
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin");
std::string outFileName = homePath + "/" + fileName;
FILE * outputFile = fopen(outFileName.c_str(), "wb");
fwrite(netOutput, outputSize, sizeof(char), outputFile);
fwrite(netOutput.get(), outputSize, sizeof(char), outputFile);
fclose(outputFile);
outputFile = nullptr;
}
return 0;
}
std::shared_ptr<Tensor> ReadFileToTensor(const std::string &file) {
auto buffer = std::make_shared<Tensor>();
if (file.empty()) {
std::cout << "Pointer file is nullptr" << std::endl;
return buffer;
}
std::ifstream ifs(file);
if (!ifs.good()) {
std::cout << "File: " << file << " is not exist" << std::endl;
return buffer;
}
if (!ifs.is_open()) {
std::cout << "File: " << file << "open failed" << std::endl;
return buffer;
}
ifs.seekg(0, std::ios::end);
size_t size = ifs.tellg();
buffer->ResizeData(size);
if (buffer->DataSize() != size) {
std::cout << "Malloc buf failed, file: " << file << std::endl;
ifs.close();
return buffer;
}
ifs.seekg(0, std::ios::beg);
ifs.read(reinterpret_cast<char *>(buffer->MutableData()), size);
ifs.close();
buffer->SetDataType(DataType::kMsUint8);
buffer->SetShape({static_cast<int64_t>(size)});
return buffer;
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;

View File

@ -423,7 +423,6 @@ class SSDWithLossCell(nn.Cell):
self.less = P.Less()
self.tile = P.Tile()
self.reduce_sum = P.ReduceSum()
self.reduce_mean = P.ReduceMean()
self.expand_dims = P.ExpandDims()
self.class_loss = SigmoidFocalClassificationLoss(config.gamma, config.alpha)
self.loc_loss = nn.SmoothL1Loss()
@ -436,7 +435,7 @@ class SSDWithLossCell(nn.Cell):
# Localization Loss
mask_loc = self.tile(self.expand_dims(mask, -1), (1, 1, 4))
smooth_l1 = self.loc_loss(pred_loc, gt_loc) * mask_loc
loss_loc = self.reduce_sum(self.reduce_mean(smooth_l1, -1), -1)
loss_loc = self.reduce_sum(self.reduce_sum(smooth_l1, -1), -1)
# Classification Loss
loss_cls = self.class_loss(pred_label, gt_label)