diff --git a/mindspore/lite/examples/train_lenet/src/dataset.cc b/mindspore/lite/examples/train_lenet/src/dataset.cc index 54cb9be7ce..bb0a801901 100644 --- a/mindspore/lite/examples/train_lenet/src/dataset.cc +++ b/mindspore/lite/examples/train_lenet/src/dataset.cc @@ -15,17 +15,17 @@ */ #include "src/dataset.h" -#include #include #include #include #include #include +#include "src/utils.h" using LabelId = std::map; char *ReadFile(const std::string &file, size_t *size) { - assert(size != nullptr); + MS_ASSERT(size != nullptr); std::string realPath(file); std::ifstream ifs(realPath); if (!ifs.good()) { diff --git a/mindspore/lite/examples/train_lenet/src/net_runner.cc b/mindspore/lite/examples/train_lenet/src/net_runner.cc index f3c64d5eb3..09beb5dfb1 100644 --- a/mindspore/lite/examples/train_lenet/src/net_runner.cc +++ b/mindspore/lite/examples/train_lenet/src/net_runner.cc @@ -20,6 +20,7 @@ #include #include #include "include/context.h" +#include "src/utils.h" unsigned int NetRunner::seed_ = time(NULL); // Definition of callback function after forwarding operator. @@ -61,10 +62,10 @@ void NetRunner::InitAndFigureInputs() { context.thread_num_ = 1; session_ = mindspore::session::TrainSession::CreateSession(ms_file_, &context); - assert(nullptr != session_); + MS_ASSERT(nullptr != session_); auto inputs = session_->GetInputs(); - assert(inputs.size() > 1); + MS_ASSERT(inputs.size() > 1); data_index_ = 0; label_index_ = 1; batch_size_ = inputs[data_index_]->shape()[0]; @@ -91,8 +92,8 @@ std::vector NetRunner::FillInputData(const std::vector &dat auto inputs = session_->GetInputs(); char *input_data = reinterpret_cast(inputs.at(data_index_)->MutableData()); auto labels = reinterpret_cast(inputs.at(label_index_)->MutableData()); - assert(total_size > 0); - assert(input_data != nullptr); + MS_ASSERT(total_size > 0); + MS_ASSERT(input_data != nullptr); std::fill(labels, labels + inputs.at(label_index_)->ElementsNum(), 0.f); for (int i = 0; i < batch_size_; i++) { if (serially) { @@ -122,7 +123,7 @@ float NetRunner::CalculateAccuracy(int max_tests) const { auto labels = FillInputData(test_set, (max_tests == -1)); session_->RunGraph(); auto outputsv = SearchOutputsForSize(batch_size_ * num_of_classes_); - assert(outputsv != nullptr); + MS_ASSERT(outputsv != nullptr); auto scores = reinterpret_cast(outputsv->MutableData()); for (int b = 0; b < batch_size_; b++) { int max_idx = 0; @@ -147,7 +148,7 @@ int NetRunner::InitDB() { num_of_classes_ = ds_.num_of_classes(); if (ds_.test_data().size() == 0) { std::cout << "No relevant data was found in " << data_dir_ << std::endl; - assert(ds_.test_data().size() != 0); + MS_ASSERT(ds_.test_data().size() != 0); } return ret; @@ -155,7 +156,7 @@ int NetRunner::InitDB() { float NetRunner::GetLoss() const { auto outputsv = SearchOutputsForSize(1); // Search for Loss which is a single value tensor - assert(outputsv != nullptr); + MS_ASSERT(outputsv != nullptr); auto loss = reinterpret_cast(outputsv->MutableData()); return loss[0]; } diff --git a/mindspore/lite/examples/train_lenet/src/utils.h b/mindspore/lite/examples/train_lenet/src/utils.h new file mode 100644 index 0000000000..de3c7e7279 --- /dev/null +++ b/mindspore/lite/examples/train_lenet/src/utils.h @@ -0,0 +1,27 @@ +/** + * 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. + */ + +#ifndef MINDSPORE_LITE_EXAMPLES_TRAIN_LENET_SRC_UTILS_H_ +#define MINDSPORE_LITE_EXAMPLES_TRAIN_LENET_SRC_UTILS_H_ + +#ifdef DEBUG +#include +#define MS_ASSERT(f) assert(f) +#else +#define MS_ASSERT(f) ((void)0) +#endif + +#endif // MINDSPORE_LITE_EXAMPLES_TRAIN_LENET_SRC_UTILS_H_ diff --git a/mindspore/lite/examples/transfer_learning/src/dataset.cc b/mindspore/lite/examples/transfer_learning/src/dataset.cc index fdc21277e2..1a52da7f93 100644 --- a/mindspore/lite/examples/transfer_learning/src/dataset.cc +++ b/mindspore/lite/examples/transfer_learning/src/dataset.cc @@ -15,13 +15,13 @@ */ #include "src/dataset.h" -#include #include #include #include #include #include #include +#include "src/utils.h" #pragma pack(push, 1) @@ -52,7 +52,7 @@ float CH_STD[3] = {0.229, 0.224, 0.225}; using LabelId = std::map; static char *ReadBitmapFile(const std::string &filename, size_t *size) { - assert(size != nullptr); + MS_ASSERT(size != nullptr); *size = 0; bmp_header bitmap_header; std::ifstream ifs(filename); @@ -71,7 +71,7 @@ static char *ReadBitmapFile(const std::string &filename, size_t *size) { ifs.seekg(bitmap_header.offset, std::ios::beg); unsigned char *bmp_image = reinterpret_cast(malloc(bitmap_header.image_size_bytes)); - if (!bmp_image) { + if (bmp_image == nullptr) { ifs.close(); return nullptr; } @@ -80,7 +80,7 @@ static char *ReadBitmapFile(const std::string &filename, size_t *size) { size_t buffer_size = bitmap_header.width * bitmap_header.height * 3; float *hwc_bin_image = new (std::nothrow) float[buffer_size]; - if (!hwc_bin_image) { + if (hwc_bin_image == nullptr) { free(bmp_image); ifs.close(); return nullptr; @@ -114,7 +114,7 @@ static char *ReadBitmapFile(const std::string &filename, size_t *size) { } char *ReadFile(const std::string &file, size_t *size) { - assert(size != nullptr); + MS_ASSERT(size != nullptr); std::string realPath(file); std::ifstream ifs(realPath); if (!ifs.good()) { @@ -203,6 +203,7 @@ std::vector DataSet::ReadDir(const std::string dpath) { } } } + closedir(dp); } return vec; } diff --git a/mindspore/lite/examples/transfer_learning/src/net_runner.cc b/mindspore/lite/examples/transfer_learning/src/net_runner.cc index a96d42ced8..eceb86308a 100644 --- a/mindspore/lite/examples/transfer_learning/src/net_runner.cc +++ b/mindspore/lite/examples/transfer_learning/src/net_runner.cc @@ -20,6 +20,7 @@ #include #include #include "include/context.h" +#include "src/utils.h" static unsigned int seed = time(NULL); @@ -68,10 +69,10 @@ void NetRunner::InitAndFigureInputs() { context.thread_num_ = 1; session_ = mindspore::session::TrainSession::CreateSession(ms_file_, &context); - assert(nullptr != session_); + MS_ASSERT(nullptr != session_); auto inputs = session_->GetInputs(); - assert(inputs.size() > 1); + MS_ASSERT(inputs.size() > 1); data_index_ = 0; label_index_ = 1; batch_size_ = inputs[data_index_]->shape()[0]; @@ -99,8 +100,8 @@ std::vector NetRunner::FillInputData(const std::vector &dat auto inputs = session_->GetInputs(); char *input_data = reinterpret_cast(inputs.at(data_index_)->MutableData()); auto labels = reinterpret_cast(inputs.at(label_index_)->MutableData()); - assert(total_size > 0); - assert(input_data != nullptr); + MS_ASSERT(total_size > 0); + MS_ASSERT(input_data != nullptr); std::fill(labels, labels + inputs.at(label_index_)->ElementsNum(), 0.f); for (int i = 0; i < batch_size_; i++) { if (serially >= 0) { @@ -128,7 +129,7 @@ float NetRunner::CalculateAccuracy(const std::vector &dataset) c auto labels = FillInputData(dataset, i); session_->RunGraph(); auto outputsv = SearchOutputsForSize(batch_size_ * num_of_classes_); - assert(outputsv != nullptr); + MS_ASSERT(outputsv != nullptr); auto scores = reinterpret_cast(outputsv->MutableData()); for (int b = 0; b < batch_size_; b++) { int max_idx = 0; @@ -158,7 +159,7 @@ int NetRunner::InitDB() { if (ds_.test_data().size() == 0) { std::cout << "No relevant data was found in " << data_dir_ << std::endl; - assert(ds_.test_data().size() != 0); + MS_ASSERT(ds_.test_data().size() != 0); } return ret; @@ -166,7 +167,7 @@ int NetRunner::InitDB() { float NetRunner::GetLoss() const { auto outputsv = SearchOutputsForSize(1); // Search for Loss which is a single value tensor - assert(outputsv != nullptr); + MS_ASSERT(outputsv != nullptr); auto loss = reinterpret_cast(outputsv->MutableData()); return loss[0]; } diff --git a/mindspore/lite/examples/transfer_learning/src/utils.h b/mindspore/lite/examples/transfer_learning/src/utils.h new file mode 100644 index 0000000000..5e0535acd7 --- /dev/null +++ b/mindspore/lite/examples/transfer_learning/src/utils.h @@ -0,0 +1,27 @@ +/** + * 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. + */ + +#ifndef MINDSPORE_LITE_EXAMPLES_TRANSFER_LEARNING_SRC_UTILS_H_ +#define MINDSPORE_LITE_EXAMPLES_TRANSFER_LEARNING_SRC_UTILS_H_ + +#ifdef DEBUG +#include +#define MS_ASSERT(f) assert(f) +#else +#define MS_ASSERT(f) ((void)0) +#endif + +#endif // MINDSPORE_LITE_EXAMPLES_TRANSFER_LEARNING_SRC_UTILS_H_