|
| 1 | +# Copyright 2022 Intel Corp. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import math |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +from numba import prange |
| 9 | +from numba_mlir import njit |
| 10 | + |
| 11 | + |
| 12 | +@njit(parallel=True, inline="always", fastmath=True) |
| 13 | +def bilinear(input, offset_y, offset_x): |
| 14 | + height, width = input.shape |
| 15 | + start_x = int(math.floor(offset_x)) |
| 16 | + start_x_weight = 1 - (offset_x - start_x) |
| 17 | + start_y = int(math.floor(offset_y)) |
| 18 | + start_y_weight = 1 - (offset_y - start_y) |
| 19 | + |
| 20 | + output = 0 |
| 21 | + if ( |
| 22 | + offset_x >= width |
| 23 | + or offset_y >= height |
| 24 | + or offset_x <= -1 |
| 25 | + or offset_y <= -1 |
| 26 | + ): |
| 27 | + return output |
| 28 | + |
| 29 | + if start_x >= 0 and start_y >= 0: |
| 30 | + w = start_x_weight * start_y_weight |
| 31 | + output += w * input[start_y, start_x] |
| 32 | + |
| 33 | + if start_x + 1 < width and start_y >= 0: |
| 34 | + w = (1 - start_x_weight) * start_y_weight |
| 35 | + output += w * input[start_y, start_x + 1] |
| 36 | + |
| 37 | + if start_x >= 0 and start_y + 1 < height: |
| 38 | + w = start_x_weight * (1 - start_y_weight) |
| 39 | + output += w * input[start_y + 1, start_x] |
| 40 | + |
| 41 | + if start_x + 1 < width and start_y + 1 < height: |
| 42 | + w = (1 - start_x_weight) * (1 - start_y_weight) |
| 43 | + output += w * input[start_y + 1, start_x + 1] |
| 44 | + |
| 45 | + return output |
| 46 | + |
| 47 | + |
| 48 | +@njit(parallel=True, fastmath=True) |
| 49 | +def deform( |
| 50 | + input, offset, output, stride, pad, dilation, groups, deformable_groups |
| 51 | +): |
| 52 | + k_height, k_width, _, out_height, out_width = offset.shape |
| 53 | + channels, _, _ = input.shape |
| 54 | + |
| 55 | + k_h_m = (k_height - 1) // 2 |
| 56 | + k_w_m = (k_width - 1) // 2 |
| 57 | + |
| 58 | + for ckhkw in prange(channels * k_height * k_width): |
| 59 | + for h in prange(out_height): |
| 60 | + for w in prange(out_width): |
| 61 | + c = ckhkw // (k_height * k_width) |
| 62 | + khkw = ckhkw % (k_height * k_width) |
| 63 | + kh = khkw // k_width |
| 64 | + kw = khkw % k_width |
| 65 | + |
| 66 | + offset_y = ( |
| 67 | + offset[kh, kw, 1, h, w] |
| 68 | + + h * stride[0] |
| 69 | + + (kh - k_h_m) * dilation[0] |
| 70 | + - (pad[0] - k_h_m) |
| 71 | + ) |
| 72 | + offset_x = ( |
| 73 | + offset[kh, kw, 0, h, w] |
| 74 | + + w * stride[1] |
| 75 | + + (kw - k_w_m) * dilation[1] |
| 76 | + - (pad[1] - k_w_m) |
| 77 | + ) |
| 78 | + |
| 79 | + output[c, kh, kw, h, w] = bilinear(input[c], offset_y, offset_x) |
| 80 | + |
| 81 | + |
| 82 | +@njit(parallel=True, fastmath=True) |
| 83 | +def deformable_convolution_b1( |
| 84 | + input, |
| 85 | + output, |
| 86 | + offset, |
| 87 | + weights, |
| 88 | + bias, |
| 89 | + tmp, |
| 90 | + stride, |
| 91 | + pad, |
| 92 | + dilation, |
| 93 | + groups, |
| 94 | + deformable_groups, |
| 95 | +): |
| 96 | + out_channels, height, width = output.shape |
| 97 | + _, in_channels, k_height, k_width = weights.shape |
| 98 | + |
| 99 | + deform(input, offset, tmp, stride, pad, dilation, groups, deformable_groups) |
| 100 | + |
| 101 | + tmp = tmp.reshape((in_channels * k_height * k_width, height * width)) |
| 102 | + |
| 103 | + _weights = weights.reshape((out_channels, in_channels * k_height * k_width)) |
| 104 | + _output = output.reshape((out_channels, height * width)) |
| 105 | + np.dot(_weights, tmp, _output) |
| 106 | + |
| 107 | + _bias = bias.reshape((out_channels, 1)) |
| 108 | + _output[:] = _output + _bias |
| 109 | + |
| 110 | + |
| 111 | +@njit(parallel=True) |
| 112 | +def deformable_convolution( |
| 113 | + input, |
| 114 | + output, |
| 115 | + offset, |
| 116 | + weights, |
| 117 | + bias, |
| 118 | + tmp, |
| 119 | + stride_y, |
| 120 | + stride_x, |
| 121 | + pad_y, |
| 122 | + pad_x, |
| 123 | + dilation_y, |
| 124 | + dilation_x, |
| 125 | + groups, |
| 126 | + deformable_groups, |
| 127 | +): |
| 128 | + batch, _, _, _ = input.shape |
| 129 | + for b in range(batch): |
| 130 | + deformable_convolution_b1( |
| 131 | + input[b], |
| 132 | + output[b], |
| 133 | + offset, |
| 134 | + weights, |
| 135 | + bias, |
| 136 | + tmp, |
| 137 | + (stride_y, stride_x), |
| 138 | + (pad_y, pad_x), |
| 139 | + (dilation_y, dilation_x), |
| 140 | + groups, |
| 141 | + deformable_groups, |
| 142 | + ) |
0 commit comments