| 38 | { |
| 39 | template <typename T> |
| 40 | SimpleTensor<T> |
| 41 | pad_layer(const SimpleTensor<T> &src, const PaddingList &paddings, const PixelValue const_value, const PaddingMode mode) |
| 42 | { |
| 43 | const DataType dst_data_type = src.data_type(); |
| 44 | |
| 45 | const TensorShape orig_shape = src.shape(); |
| 46 | |
| 47 | std::vector<PaddingInfo> paddings_extended = paddings; |
| 48 | |
| 49 | for (size_t i = paddings.size(); i < TensorShape::num_max_dimensions; ++i) |
| 50 | { |
| 51 | paddings_extended.emplace_back(PaddingInfo{0, 0}); |
| 52 | } |
| 53 | |
| 54 | const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(orig_shape, paddings); |
| 55 | |
| 56 | SimpleTensor<T> dst(padded_shape, dst_data_type); |
| 57 | |
| 58 | // Reference algorithm: loop over the different dimension of the input. |
| 59 | const uint32_t num_elements = dst.num_elements(); |
| 60 | for (uint32_t idx = 0; idx < num_elements; ++idx) |
| 61 | { |
| 62 | const Coordinates coord = index2coord(padded_shape, idx); |
| 63 | |
| 64 | const size_t i = coord.x(); |
| 65 | const size_t j = coord.y(); |
| 66 | const size_t k = coord.z(); |
| 67 | const size_t l = coord[3]; |
| 68 | const size_t m = coord[4]; |
| 69 | const size_t n = coord[5]; |
| 70 | |
| 71 | const std::array<size_t, TensorShape::num_max_dimensions> dims = {{0, 1, 2, 3, 4, 5}}; |
| 72 | const std::array<size_t, TensorShape::num_max_dimensions> coords = {{i, j, k, l, m, n}}; |
| 73 | auto is_padding_area = [&](size_t i) { |
| 74 | return (coords[i] < paddings_extended[i].first || |
| 75 | coords[i] > orig_shape[i] + paddings_extended[i].first - 1); |
| 76 | }; |
| 77 | |
| 78 | auto orig_coord_reflect = [&](size_t i) |
| 79 | { |
| 80 | if (is_padding_area(i)) |
| 81 | { |
| 82 | if (coords[i] < paddings_extended[i].first) |
| 83 | { |
| 84 | return paddings_extended[i].first - coords[i]; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | return 2 * orig_shape[i] + paddings_extended[i].first - 2 - coords[i]; |
| 89 | } |
| 90 | } |
| 91 | return coords[i] - paddings_extended[i].first; |
| 92 | }; |
| 93 | |
| 94 | auto orig_coord_symm = [&](size_t i) |
| 95 | { |
| 96 | if (is_padding_area(i)) |
| 97 | { |
no test coverage detected