!20954 fix: sample crop size maybe zero

Merge pull request !20954 from guozhijian/fix_crop_op_is_0
This commit is contained in:
i-robot 2021-07-29 06:51:04 +00:00 committed by Gitee
commit d6ee1b8407
4 changed files with 30 additions and 6 deletions

View File

@ -585,10 +585,12 @@ Status CropAndResize(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tenso
// image too large or too small, 1000 is arbitrary here to prevent opencv from segmentation fault
const uint32_t kCropShapeLimits = 1000;
if (crop_height == 0 || crop_width == 0 || target_height == 0 || target_height > crop_height * kCropShapeLimits ||
target_width == 0 || target_height > crop_width * kCropShapeLimits) {
target_width == 0 || target_width > crop_width * kCropShapeLimits) {
std::string err_msg =
"CropAndResize: the resizing width or height 1) is too big, it's up to "
"1000 times the original image; 2) can not be 0.";
"CropAndResize: the resizing width or height 1) is too big, it's up to " + std::to_string(kCropShapeLimits) +
" times the original image; 2) can not be 0. Detail info is: crop_height: " + std::to_string(crop_height) +
", crop_width: " + std::to_string(crop_width) + ", target_height: " + std::to_string(target_height) +
", target_width: " + std::to_string(target_width);
RETURN_STATUS_UNEXPECTED(err_msg);
}
cv::Rect roi(x, y, crop_width, crop_height);

View File

@ -80,6 +80,15 @@ Status RandomCropAndResizeOp::GetCropBox(int h_in, int w_in, int *x, int *y, int
*crop_width = static_cast<int32_t>(std::round(std::sqrt(h_in * w_in * sample_scale * sample_aspect)));
*crop_height = static_cast<int32_t>(std::round(*crop_width / sample_aspect));
// forbidden crop_width or crop_height is zero
if (*crop_width <= 0) {
*crop_width = 1;
}
if (*crop_height <= 0) {
*crop_height = 1;
}
if (*crop_width <= w_in && *crop_height <= h_in) {
std::uniform_int_distribution<> rd_x(0, w_in - *crop_width);
std::uniform_int_distribution<> rd_y(0, h_in - *crop_height);
@ -102,6 +111,14 @@ Status RandomCropAndResizeOp::GetCropBox(int h_in, int w_in, int *x, int *y, int
}
}
constexpr float crop_ratio = 2.0;
// forbidden crop_width or crop_height is zero
if (*crop_width <= 0) {
*crop_width = 1;
}
if (*crop_height <= 0) {
*crop_height = 1;
}
*x = static_cast<int32_t>(std::round((w_in - *crop_width) / crop_ratio));
*y = static_cast<int32_t>(std::round((h_in - *crop_height) / crop_ratio));
return Status::OK();

View File

@ -64,7 +64,12 @@ float GetMemoryUsage() {
status_count++;
}
(void)memset_s(buf, sizeof(buf), 0, sizeof(buf));
auto ret = memset_s(buf, sizeof(buf), 0, sizeof(buf));
if (ret != 0) {
MS_LOG(WARNING) << "memset_s failed when get memory usage. This might be caused by insufficient memory.";
fclose(fd);
return 0.0;
}
}
fclose(fd);

View File

@ -43,7 +43,7 @@ void Task::operator()() {
// The thread id in Linux may be duplicate
ss << Services::GetUniqueID();
#endif
MS_LOG(DEBUG) << my_name_ << " Thread ID " << ss.str() << " Started.";
MS_LOG(DEBUG) << "Task: " << my_name_ << " Thread ID " << ss.str() << " Started.";
#if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID) && !defined(__APPLE__)
native_handle_ = pthread_self();
@ -64,7 +64,7 @@ void Task::operator()() {
if (rc_.StatusCode() == StatusCode::kMDNetWorkError) {
MS_LOG(WARNING) << rc_;
} else {
MS_LOG(INFO) << "Task: " << my_name_ << " is terminated with err msg: " << rc_;
MS_LOG(ERROR) << "Task: " << my_name_ << " - thread(" << ss.str() << ") is terminated with err msg: " << rc_;
}
ShutdownGroup();
}