From: @xutianchun Reviewed-by: @HilbertDavid Signed-off-by: @HilbertDavidtags/v1.1.0
| @@ -15,17 +15,17 @@ | |||||
| */ | */ | ||||
| #include "src/dataset.h" | #include "src/dataset.h" | ||||
| #include <assert.h> | |||||
| #include <arpa/inet.h> | #include <arpa/inet.h> | ||||
| #include <map> | #include <map> | ||||
| #include <iostream> | #include <iostream> | ||||
| #include <fstream> | #include <fstream> | ||||
| #include <memory> | #include <memory> | ||||
| #include "src/utils.h" | |||||
| using LabelId = std::map<std::string, int>; | using LabelId = std::map<std::string, int>; | ||||
| char *ReadFile(const std::string &file, size_t *size) { | char *ReadFile(const std::string &file, size_t *size) { | ||||
| assert(size != nullptr); | |||||
| MS_ASSERT(size != nullptr); | |||||
| std::string realPath(file); | std::string realPath(file); | ||||
| std::ifstream ifs(realPath); | std::ifstream ifs(realPath); | ||||
| if (!ifs.good()) { | if (!ifs.good()) { | ||||
| @@ -20,6 +20,7 @@ | |||||
| #include <iostream> | #include <iostream> | ||||
| #include <fstream> | #include <fstream> | ||||
| #include "include/context.h" | #include "include/context.h" | ||||
| #include "src/utils.h" | |||||
| unsigned int NetRunner::seed_ = time(NULL); | unsigned int NetRunner::seed_ = time(NULL); | ||||
| // Definition of callback function after forwarding operator. | // Definition of callback function after forwarding operator. | ||||
| @@ -61,10 +62,10 @@ void NetRunner::InitAndFigureInputs() { | |||||
| context.thread_num_ = 1; | context.thread_num_ = 1; | ||||
| session_ = mindspore::session::TrainSession::CreateSession(ms_file_, &context); | session_ = mindspore::session::TrainSession::CreateSession(ms_file_, &context); | ||||
| assert(nullptr != session_); | |||||
| MS_ASSERT(nullptr != session_); | |||||
| auto inputs = session_->GetInputs(); | auto inputs = session_->GetInputs(); | ||||
| assert(inputs.size() > 1); | |||||
| MS_ASSERT(inputs.size() > 1); | |||||
| data_index_ = 0; | data_index_ = 0; | ||||
| label_index_ = 1; | label_index_ = 1; | ||||
| batch_size_ = inputs[data_index_]->shape()[0]; | batch_size_ = inputs[data_index_]->shape()[0]; | ||||
| @@ -91,8 +92,8 @@ std::vector<int> NetRunner::FillInputData(const std::vector<DataLabelTuple> &dat | |||||
| auto inputs = session_->GetInputs(); | auto inputs = session_->GetInputs(); | ||||
| char *input_data = reinterpret_cast<char *>(inputs.at(data_index_)->MutableData()); | char *input_data = reinterpret_cast<char *>(inputs.at(data_index_)->MutableData()); | ||||
| auto labels = reinterpret_cast<float *>(inputs.at(label_index_)->MutableData()); | auto labels = reinterpret_cast<float *>(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); | std::fill(labels, labels + inputs.at(label_index_)->ElementsNum(), 0.f); | ||||
| for (int i = 0; i < batch_size_; i++) { | for (int i = 0; i < batch_size_; i++) { | ||||
| if (serially) { | if (serially) { | ||||
| @@ -122,7 +123,7 @@ float NetRunner::CalculateAccuracy(int max_tests) const { | |||||
| auto labels = FillInputData(test_set, (max_tests == -1)); | auto labels = FillInputData(test_set, (max_tests == -1)); | ||||
| session_->RunGraph(); | session_->RunGraph(); | ||||
| auto outputsv = SearchOutputsForSize(batch_size_ * num_of_classes_); | auto outputsv = SearchOutputsForSize(batch_size_ * num_of_classes_); | ||||
| assert(outputsv != nullptr); | |||||
| MS_ASSERT(outputsv != nullptr); | |||||
| auto scores = reinterpret_cast<float *>(outputsv->MutableData()); | auto scores = reinterpret_cast<float *>(outputsv->MutableData()); | ||||
| for (int b = 0; b < batch_size_; b++) { | for (int b = 0; b < batch_size_; b++) { | ||||
| int max_idx = 0; | int max_idx = 0; | ||||
| @@ -147,7 +148,7 @@ int NetRunner::InitDB() { | |||||
| num_of_classes_ = ds_.num_of_classes(); | num_of_classes_ = ds_.num_of_classes(); | ||||
| if (ds_.test_data().size() == 0) { | if (ds_.test_data().size() == 0) { | ||||
| std::cout << "No relevant data was found in " << data_dir_ << std::endl; | 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; | return ret; | ||||
| @@ -155,7 +156,7 @@ int NetRunner::InitDB() { | |||||
| float NetRunner::GetLoss() const { | float NetRunner::GetLoss() const { | ||||
| auto outputsv = SearchOutputsForSize(1); // Search for Loss which is a single value tensor | auto outputsv = SearchOutputsForSize(1); // Search for Loss which is a single value tensor | ||||
| assert(outputsv != nullptr); | |||||
| MS_ASSERT(outputsv != nullptr); | |||||
| auto loss = reinterpret_cast<float *>(outputsv->MutableData()); | auto loss = reinterpret_cast<float *>(outputsv->MutableData()); | ||||
| return loss[0]; | return loss[0]; | ||||
| } | } | ||||
| @@ -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 <cassert> | |||||
| #define MS_ASSERT(f) assert(f) | |||||
| #else | |||||
| #define MS_ASSERT(f) ((void)0) | |||||
| #endif | |||||
| #endif // MINDSPORE_LITE_EXAMPLES_TRAIN_LENET_SRC_UTILS_H_ | |||||
| @@ -15,13 +15,13 @@ | |||||
| */ | */ | ||||
| #include "src/dataset.h" | #include "src/dataset.h" | ||||
| #include <assert.h> | |||||
| #include <dirent.h> | #include <dirent.h> | ||||
| #include <arpa/inet.h> | #include <arpa/inet.h> | ||||
| #include <map> | #include <map> | ||||
| #include <iostream> | #include <iostream> | ||||
| #include <fstream> | #include <fstream> | ||||
| #include <memory> | #include <memory> | ||||
| #include "src/utils.h" | |||||
| #pragma pack(push, 1) | #pragma pack(push, 1) | ||||
| @@ -52,7 +52,7 @@ float CH_STD[3] = {0.229, 0.224, 0.225}; | |||||
| using LabelId = std::map<std::string, int>; | using LabelId = std::map<std::string, int>; | ||||
| static char *ReadBitmapFile(const std::string &filename, size_t *size) { | static char *ReadBitmapFile(const std::string &filename, size_t *size) { | ||||
| assert(size != nullptr); | |||||
| MS_ASSERT(size != nullptr); | |||||
| *size = 0; | *size = 0; | ||||
| bmp_header bitmap_header; | bmp_header bitmap_header; | ||||
| std::ifstream ifs(filename); | 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); | ifs.seekg(bitmap_header.offset, std::ios::beg); | ||||
| unsigned char *bmp_image = reinterpret_cast<unsigned char *>(malloc(bitmap_header.image_size_bytes)); | unsigned char *bmp_image = reinterpret_cast<unsigned char *>(malloc(bitmap_header.image_size_bytes)); | ||||
| if (!bmp_image) { | |||||
| if (bmp_image == nullptr) { | |||||
| ifs.close(); | ifs.close(); | ||||
| return nullptr; | 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; | size_t buffer_size = bitmap_header.width * bitmap_header.height * 3; | ||||
| float *hwc_bin_image = new (std::nothrow) float[buffer_size]; | float *hwc_bin_image = new (std::nothrow) float[buffer_size]; | ||||
| if (!hwc_bin_image) { | |||||
| if (hwc_bin_image == nullptr) { | |||||
| free(bmp_image); | free(bmp_image); | ||||
| ifs.close(); | ifs.close(); | ||||
| return nullptr; | 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) { | char *ReadFile(const std::string &file, size_t *size) { | ||||
| assert(size != nullptr); | |||||
| MS_ASSERT(size != nullptr); | |||||
| std::string realPath(file); | std::string realPath(file); | ||||
| std::ifstream ifs(realPath); | std::ifstream ifs(realPath); | ||||
| if (!ifs.good()) { | if (!ifs.good()) { | ||||
| @@ -203,6 +203,7 @@ std::vector<FileTuple> DataSet::ReadDir(const std::string dpath) { | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| closedir(dp); | |||||
| } | } | ||||
| return vec; | return vec; | ||||
| } | } | ||||
| @@ -20,6 +20,7 @@ | |||||
| #include <iostream> | #include <iostream> | ||||
| #include <fstream> | #include <fstream> | ||||
| #include "include/context.h" | #include "include/context.h" | ||||
| #include "src/utils.h" | |||||
| static unsigned int seed = time(NULL); | static unsigned int seed = time(NULL); | ||||
| @@ -68,10 +69,10 @@ void NetRunner::InitAndFigureInputs() { | |||||
| context.thread_num_ = 1; | context.thread_num_ = 1; | ||||
| session_ = mindspore::session::TrainSession::CreateSession(ms_file_, &context); | session_ = mindspore::session::TrainSession::CreateSession(ms_file_, &context); | ||||
| assert(nullptr != session_); | |||||
| MS_ASSERT(nullptr != session_); | |||||
| auto inputs = session_->GetInputs(); | auto inputs = session_->GetInputs(); | ||||
| assert(inputs.size() > 1); | |||||
| MS_ASSERT(inputs.size() > 1); | |||||
| data_index_ = 0; | data_index_ = 0; | ||||
| label_index_ = 1; | label_index_ = 1; | ||||
| batch_size_ = inputs[data_index_]->shape()[0]; | batch_size_ = inputs[data_index_]->shape()[0]; | ||||
| @@ -99,8 +100,8 @@ std::vector<int> NetRunner::FillInputData(const std::vector<DataLabelTuple> &dat | |||||
| auto inputs = session_->GetInputs(); | auto inputs = session_->GetInputs(); | ||||
| char *input_data = reinterpret_cast<char *>(inputs.at(data_index_)->MutableData()); | char *input_data = reinterpret_cast<char *>(inputs.at(data_index_)->MutableData()); | ||||
| auto labels = reinterpret_cast<float *>(inputs.at(label_index_)->MutableData()); | auto labels = reinterpret_cast<float *>(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); | std::fill(labels, labels + inputs.at(label_index_)->ElementsNum(), 0.f); | ||||
| for (int i = 0; i < batch_size_; i++) { | for (int i = 0; i < batch_size_; i++) { | ||||
| if (serially >= 0) { | if (serially >= 0) { | ||||
| @@ -128,7 +129,7 @@ float NetRunner::CalculateAccuracy(const std::vector<DataLabelTuple> &dataset) c | |||||
| auto labels = FillInputData(dataset, i); | auto labels = FillInputData(dataset, i); | ||||
| session_->RunGraph(); | session_->RunGraph(); | ||||
| auto outputsv = SearchOutputsForSize(batch_size_ * num_of_classes_); | auto outputsv = SearchOutputsForSize(batch_size_ * num_of_classes_); | ||||
| assert(outputsv != nullptr); | |||||
| MS_ASSERT(outputsv != nullptr); | |||||
| auto scores = reinterpret_cast<float *>(outputsv->MutableData()); | auto scores = reinterpret_cast<float *>(outputsv->MutableData()); | ||||
| for (int b = 0; b < batch_size_; b++) { | for (int b = 0; b < batch_size_; b++) { | ||||
| int max_idx = 0; | int max_idx = 0; | ||||
| @@ -158,7 +159,7 @@ int NetRunner::InitDB() { | |||||
| if (ds_.test_data().size() == 0) { | if (ds_.test_data().size() == 0) { | ||||
| std::cout << "No relevant data was found in " << data_dir_ << std::endl; | 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; | return ret; | ||||
| @@ -166,7 +167,7 @@ int NetRunner::InitDB() { | |||||
| float NetRunner::GetLoss() const { | float NetRunner::GetLoss() const { | ||||
| auto outputsv = SearchOutputsForSize(1); // Search for Loss which is a single value tensor | auto outputsv = SearchOutputsForSize(1); // Search for Loss which is a single value tensor | ||||
| assert(outputsv != nullptr); | |||||
| MS_ASSERT(outputsv != nullptr); | |||||
| auto loss = reinterpret_cast<float *>(outputsv->MutableData()); | auto loss = reinterpret_cast<float *>(outputsv->MutableData()); | ||||
| return loss[0]; | return loss[0]; | ||||
| } | } | ||||
| @@ -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 <cassert> | |||||
| #define MS_ASSERT(f) assert(f) | |||||
| #else | |||||
| #define MS_ASSERT(f) ((void)0) | |||||
| #endif | |||||
| #endif // MINDSPORE_LITE_EXAMPLES_TRANSFER_LEARNING_SRC_UTILS_H_ | |||||