|
| 1 | +""" |
| 2 | +Copyright 2017-2018 Fizyr (https://fizyr.com) |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +import keras.backend |
| 18 | +from .dynamic import meshgrid |
| 19 | + |
| 20 | +import numpy as np |
| 21 | + |
| 22 | + |
| 23 | +def bbox_transform_inv(boxes, deltas, mean=None, std=None): |
| 24 | + if mean is None: |
| 25 | + mean = [0, 0, 0, 0] |
| 26 | + if std is None: |
| 27 | + std = [0.1, 0.1, 0.2, 0.2] |
| 28 | + |
| 29 | + widths = boxes[:, :, 2] - boxes[:, :, 0] |
| 30 | + heights = boxes[:, :, 3] - boxes[:, :, 1] |
| 31 | + ctr_x = boxes[:, :, 0] + 0.5 * widths |
| 32 | + ctr_y = boxes[:, :, 1] + 0.5 * heights |
| 33 | + |
| 34 | + dx = deltas[:, :, 0] * std[0] + mean[0] |
| 35 | + dy = deltas[:, :, 1] * std[1] + mean[1] |
| 36 | + dw = deltas[:, :, 2] * std[2] + mean[2] |
| 37 | + dh = deltas[:, :, 3] * std[3] + mean[3] |
| 38 | + |
| 39 | + pred_ctr_x = ctr_x + dx * widths |
| 40 | + pred_ctr_y = ctr_y + dy * heights |
| 41 | + pred_w = keras.backend.exp(dw) * widths |
| 42 | + pred_h = keras.backend.exp(dh) * heights |
| 43 | + |
| 44 | + pred_boxes_x1 = pred_ctr_x - 0.5 * pred_w |
| 45 | + pred_boxes_y1 = pred_ctr_y - 0.5 * pred_h |
| 46 | + pred_boxes_x2 = pred_ctr_x + 0.5 * pred_w |
| 47 | + pred_boxes_y2 = pred_ctr_y + 0.5 * pred_h |
| 48 | + |
| 49 | + pred_boxes = keras.backend.stack([pred_boxes_x1, pred_boxes_y1, pred_boxes_x2, pred_boxes_y2], axis=2) |
| 50 | + |
| 51 | + return pred_boxes |
| 52 | + |
| 53 | + |
| 54 | +def shift(shape, stride, anchors): |
| 55 | + """ |
| 56 | + Produce shifted anchors based on shape of the map and stride size |
| 57 | + """ |
| 58 | + shift_x = (keras.backend.arange(0, shape[1], dtype=keras.backend.floatx()) + keras.backend.constant(0.5, dtype=keras.backend.floatx())) * stride |
| 59 | + shift_y = (keras.backend.arange(0, shape[0], dtype=keras.backend.floatx()) + keras.backend.constant(0.5, dtype=keras.backend.floatx())) * stride |
| 60 | + |
| 61 | + shift_x, shift_y = meshgrid(shift_x, shift_y) |
| 62 | + shift_x = keras.backend.reshape(shift_x, [-1]) |
| 63 | + shift_y = keras.backend.reshape(shift_y, [-1]) |
| 64 | + |
| 65 | + shifts = keras.backend.stack([ |
| 66 | + shift_x, |
| 67 | + shift_y, |
| 68 | + shift_x, |
| 69 | + shift_y |
| 70 | + ], axis=0) |
| 71 | + |
| 72 | + shifts = keras.backend.transpose(shifts) |
| 73 | + number_of_anchors = keras.backend.shape(anchors)[0] |
| 74 | + |
| 75 | + k = keras.backend.shape(shifts)[0] # number of base points = feat_h * feat_w |
| 76 | + |
| 77 | + shifted_anchors = keras.backend.reshape(anchors, [1, number_of_anchors, 4]) + keras.backend.cast(keras.backend.reshape(shifts, [k, 1, 4]), keras.backend.floatx()) |
| 78 | + shifted_anchors = keras.backend.reshape(shifted_anchors, [k * number_of_anchors, 4]) |
| 79 | + |
| 80 | + return shifted_anchors |
0 commit comments