|
| 1 | +# Copyright 2022 Intel Corp. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import math |
| 6 | +import numpy as np |
| 7 | +from numba import njit, prange |
| 8 | + |
| 9 | +@njit(parallel=True, inline='always', fastmath=True) |
| 10 | +def bilinear(input, offset_y, offset_x): |
| 11 | + height, width = input.shape |
| 12 | + start_x = int(math.floor(offset_x)) |
| 13 | + start_x_weight = 1 - (offset_x - start_x) |
| 14 | + start_y = int(math.floor(offset_y)) |
| 15 | + start_y_weight = 1 - (offset_y - start_y) |
| 16 | + |
| 17 | + output = 0 |
| 18 | + if offset_x >= width or offset_y >= height or offset_x <= -1 or offset_y <= -1: |
| 19 | + return output |
| 20 | + |
| 21 | + if start_x >= 0 and start_y >= 0: |
| 22 | + w = start_x_weight*start_y_weight |
| 23 | + output += w*input[start_y, start_x] |
| 24 | + |
| 25 | + if start_x + 1 < width and start_y >= 0: |
| 26 | + w = (1 - start_x_weight)*start_y_weight |
| 27 | + output += w*input[start_y, start_x + 1] |
| 28 | + |
| 29 | + if start_x >=0 and start_y + 1 < height: |
| 30 | + w = start_x_weight*(1 - start_y_weight) |
| 31 | + output += w*input[start_y + 1, start_x] |
| 32 | + |
| 33 | + if start_x + 1 < width and start_y + 1 < height: |
| 34 | + w = (1 - start_x_weight)*(1 - start_y_weight) |
| 35 | + output += w*input[start_y + 1, start_x + 1] |
| 36 | + |
| 37 | + return output |
| 38 | + |
| 39 | +@njit(parallel=True, fastmath=True) |
| 40 | +def deform(input, offset, output, stride, pad, dilation, groups, deformable_groups): |
| 41 | + k_height, k_width, _, out_height, out_width = offset.shape |
| 42 | + channels, _, _ = input.shape |
| 43 | + |
| 44 | + k_h_m = (k_height - 1)//2 |
| 45 | + k_w_m = (k_width - 1)//2 |
| 46 | + |
| 47 | + for ckhkw in prange(channels*k_height*k_width): |
| 48 | + for h in prange(out_height): |
| 49 | + for w in prange(out_width): |
| 50 | + c = ckhkw//(k_height*k_width) |
| 51 | + khkw = ckhkw%(k_height*k_width) |
| 52 | + kh = khkw//k_width |
| 53 | + kw = khkw%k_width |
| 54 | + |
| 55 | + offset_y = offset[kh, kw, 1, h, w] + h*stride[0] + (kh - k_h_m)*dilation[0] - (pad[0] - k_h_m) |
| 56 | + offset_x = offset[kh, kw, 0, h, w] + w*stride[1] + (kw - k_w_m)*dilation[1] - (pad[1] - k_w_m) |
| 57 | + |
| 58 | + output[c, kh, kw, h, w] = bilinear(input[c], offset_y, offset_x) |
| 59 | + |
| 60 | +@njit(parallel=True, fastmath=True) |
| 61 | +def deformable_convolution_b1(input, output, offset, weights, bias, tmp, stride, pad, dilation, groups, deformable_groups): |
| 62 | + out_channels, height, width = output.shape |
| 63 | + _, in_channels, k_height, k_width = weights.shape |
| 64 | + |
| 65 | + deform(input, offset, tmp, stride, pad, dilation, groups, deformable_groups) |
| 66 | + |
| 67 | + tmp = tmp.reshape((in_channels*k_height*k_width, height*width)) |
| 68 | + |
| 69 | + _weights = weights.reshape((out_channels, in_channels*k_height*k_width)) |
| 70 | + _output = output.reshape((out_channels, height*width)) |
| 71 | + np.dot(_weights, tmp, _output) |
| 72 | + |
| 73 | + _bias = bias.reshape((out_channels, 1)) |
| 74 | + _output[:] = _output + _bias |
| 75 | + |
| 76 | +@njit(parallel=True) |
| 77 | +def deformable_convolution(input, output, offset, weights, bias, tmp, stride_y, stride_x, pad_y, pad_x, dilation_y, dilation_x, groups, deformable_groups): |
| 78 | + batch, _, _, _ = input.shape |
| 79 | + for b in range(batch): |
| 80 | + deformable_convolution_b1(input[b], output[b], offset, weights, bias, tmp, (stride_y, stride_x), (pad_y, pad_x), (dilation_y, dilation_x), groups, deformable_groups) |
| 81 | + |
0 commit comments