Skip to content

Commit f14a032

Browse files
Add files via upload
1 parent daf7be4 commit f14a032

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
3+
from .dynamic import *
4+
from .common import *
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
3+
_BACKEND = "tensorflow"
4+
5+
if "KERAS_BACKEND" in os.environ:
6+
_backend = os.environ["KERAS_BACKEND"]
7+
8+
backends = {
9+
"tensorflow"
10+
}
11+
12+
assert _backend in backends
13+
14+
_BACKEND = _backend
15+
16+
if _BACKEND == "tensorflow":
17+
from .tensorflow_backend import *
18+
else:
19+
raise ValueError("Unknown backend: " + str(_BACKEND))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 tensorflow
18+
import keras
19+
20+
21+
def resize_images(*args, **kwargs):
22+
return tensorflow.image.resize_images(*args, **kwargs)
23+
24+
25+
def non_max_suppression(*args, **kwargs):
26+
return tensorflow.image.non_max_suppression(*args, **kwargs)
27+
28+
29+
def range(*args, **kwargs):
30+
return tensorflow.range(*args, **kwargs)
31+
32+
33+
def scatter_nd(*args, **kwargs):
34+
return tensorflow.scatter_nd(*args, **kwargs)
35+
36+
37+
def gather_nd(*args, **kwargs):
38+
return tensorflow.gather_nd(*args, **kwargs)
39+
40+
41+
def meshgrid(*args, **kwargs):
42+
return tensorflow.meshgrid(*args, **kwargs)
43+
44+
45+
def where(*args, **kwargs):
46+
return tensorflow.where(*args, **kwargs)

0 commit comments

Comments
 (0)