|
| 1 | +/** |
| 2 | + * DeepDetect |
| 3 | + * Copyright (c) 2021 Jolibrain |
| 4 | + * Authors: Emmanuel Benazera <emmanuel.benazera@jolibrain.com> |
| 5 | + * |
| 6 | + * This file is part of deepdetect. |
| 7 | + * |
| 8 | + * deepdetect is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * deepdetect is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with deepdetect. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#include "torchdataaug.h" |
| 23 | + |
| 24 | +namespace dd |
| 25 | +{ |
| 26 | + |
| 27 | + void TorchImgRandAugCV::augment(cv::Mat &src) |
| 28 | + { |
| 29 | + // apply augmentation |
| 30 | + if (_mirror) |
| 31 | + applyMirror(src); |
| 32 | + if (_rotate) |
| 33 | + applyRotate(src); |
| 34 | + |
| 35 | + // should be last, in this order |
| 36 | + if (_cutout > 0.0) |
| 37 | + applyCutout(src); |
| 38 | + if (_crop_size > 0) |
| 39 | + applyCrop(src); |
| 40 | + } |
| 41 | + |
| 42 | + void TorchImgRandAugCV::applyMirror(cv::Mat &src) |
| 43 | + { |
| 44 | +#pragma omp critical |
| 45 | + { |
| 46 | + if (_bernouilli(_rnd_gen)) |
| 47 | + { |
| 48 | + cv::Mat dst; |
| 49 | + cv::flip(src, dst, 1); |
| 50 | + src = dst; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + void TorchImgRandAugCV::applyRotate(cv::Mat &src) |
| 56 | + { |
| 57 | + int rot = 0; |
| 58 | +#pragma omp critical |
| 59 | + { |
| 60 | + rot = _uniform_int_rotate(_rnd_gen); |
| 61 | + } |
| 62 | + if (rot == 0) |
| 63 | + return; |
| 64 | + else if (rot == 1) // 90 |
| 65 | + { |
| 66 | + cv::Mat dst; |
| 67 | + cv::transpose(src, dst); |
| 68 | + cv::flip(dst, src, 1); |
| 69 | + } |
| 70 | + else if (rot == 2) // 180 |
| 71 | + { |
| 72 | + cv::Mat dst; |
| 73 | + cv::flip(src, dst, -1); |
| 74 | + src = dst; |
| 75 | + } |
| 76 | + else if (rot == 3) // 270 |
| 77 | + { |
| 78 | + cv::Mat dst; |
| 79 | + cv::transpose(src, dst); |
| 80 | + cv::flip(dst, src, 0); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + void TorchImgRandAugCV::applyCrop(cv::Mat &src) |
| 85 | + { |
| 86 | + int crop_x = 0; |
| 87 | + int crop_y = 0; |
| 88 | +#pragma omp critical |
| 89 | + { |
| 90 | + crop_x = _uniform_int_crop_x(_rnd_gen); |
| 91 | + crop_y = _uniform_int_crop_y(_rnd_gen); |
| 92 | + } |
| 93 | + cv::Rect crop(crop_x, crop_y, _crop_size, _crop_size); |
| 94 | + cv::Mat dst = src(crop).clone(); |
| 95 | + src = dst; |
| 96 | + } |
| 97 | + |
| 98 | + void TorchImgRandAugCV::applyCutout(cv::Mat &src) |
| 99 | + { |
| 100 | + // Draw random between 0 and 1 |
| 101 | + float r1 = 0.0; |
| 102 | +#pragma omp critical |
| 103 | + { |
| 104 | + r1 = _uniform_real_1(_rnd_gen); |
| 105 | + } |
| 106 | + if (r1 > _cutout) |
| 107 | + return; |
| 108 | + |
| 109 | +#pragma omp critical |
| 110 | + { |
| 111 | + // get shape and area to erase |
| 112 | + float s = _uniform_real_cutout_s(_rnd_gen) * _img_width |
| 113 | + * _img_height; // area |
| 114 | + float r = _uniform_real_cutout_r(_rnd_gen); // aspect ratio |
| 115 | + |
| 116 | + int w = std::min(_img_width, |
| 117 | + static_cast<int>(std::floor(std::sqrt(s / r)))); |
| 118 | + int h = std::min(_img_height, |
| 119 | + static_cast<int>(std::floor(std::sqrt(s * r)))); |
| 120 | + std::uniform_int_distribution<int> distx(0, _img_width - w); |
| 121 | + std::uniform_int_distribution<int> disty(0, _img_height - h); |
| 122 | + int rect_x = distx(_rnd_gen); |
| 123 | + int rect_y = disty(_rnd_gen); |
| 124 | + |
| 125 | + // erase |
| 126 | + cv::Rect rect(rect_x, rect_y, w, h); |
| 127 | + cv::Mat selected_area = src(rect); |
| 128 | + cv::randu(selected_area, cv::Scalar(_cutout_vl, _cutout_vl, _cutout_vl), |
| 129 | + cv::Scalar(_cutout_vh, _cutout_vh, _cutout_vh)); // TODO: bw |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments