24
24
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
25
# SOFTWARE.
26
26
27
+ import sys
27
28
import tensorflow .compat .v1 as tf
28
29
import numpy as np
29
30
31
+ class Color :
32
+ BLACK = '\033 [30m'
33
+ RED = '\033 [31m'
34
+ GREEN = '\033 [32m'
35
+ YELLOW = '\033 [33m'
36
+ BLUE = '\033 [34m'
37
+ MAGENTA = '\033 [35m'
38
+ CYAN = '\033 [36m'
39
+ WHITE = '\033 [37m'
40
+ COLOR_DEFAULT = '\033 [39m'
41
+ BOLD = '\033 [1m'
42
+ UNDERLINE = '\033 [4m'
43
+ INVISIBLE = '\033 [08m'
44
+ REVERCE = '\033 [07m'
45
+ BG_BLACK = '\033 [40m'
46
+ BG_RED = '\033 [41m'
47
+ BG_GREEN = '\033 [42m'
48
+ BG_YELLOW = '\033 [43m'
49
+ BG_BLUE = '\033 [44m'
50
+ BG_MAGENTA = '\033 [45m'
51
+ BG_CYAN = '\033 [46m'
52
+ BG_WHITE = '\033 [47m'
53
+ BG_DEFAULT = '\033 [49m'
54
+ RESET = '\033 [0m'
55
+
30
56
#Affine transform points
31
57
def TransformLandmarks (operator , custom_options , tensors , interpreter , landmarks2d = None , mat = None ):
32
58
if landmarks2d is None :
@@ -46,7 +72,7 @@ def TransformLandmarks(operator, custom_options, tensors, interpreter, landmarks
46
72
return landmarks2d_transformed
47
73
48
74
#Affine transform images using bilinear interpolation
49
- def TransformTensorBilinear (operator , custom_options , tensors , interpreter , features = None , mat = None ):
75
+ def TransformTensorBilinear (operator , custom_options , tensors , interpreter , optimizing_barracuda , features = None , mat = None ):
50
76
if features is None :
51
77
features = tensors [operator ['inputs' ][0 ]] #float32 [b,48,48,32] feature maps
52
78
if mat is None :
@@ -102,11 +128,46 @@ def TransformTensorBilinear(operator, custom_options, tensors, interpreter, feat
102
128
in_coord_floor = tf .concat ([in_coord_floor [:,:,:,1 :2 ], in_coord_floor [:,:,:,0 :1 ]], axis = 3 ) #[b,h,w,YX]
103
129
in_coord_ceil_ = tf .concat ([in_coord_ceil_ [:,:,:,1 :2 ], in_coord_ceil_ [:,:,:,0 :1 ]], axis = 3 ) #[b,h,w,YX]
104
130
131
+ def barracuda_gather_nd (params , indices ):
132
+ if len (indices .shape ) == 4 and indices .shape [0 ] == 1 :
133
+ indices = indices [0 ]
134
+ elif len (indices .shape ) == 3 :
135
+ pass
136
+ else :
137
+ print (f'{ Color .RED } ERROR:{ Color .RESET } gather_nd when optimizing_barracuda is enabled must have 4 dimensions and batch size = 1 or 3 dimensions.' )
138
+ print (f'{ Color .RED } ERROR:{ Color .RESET } params.shape: { params .shape } , indices.shape: { indices .shape } ' )
139
+ sys .exit (- 1 )
140
+ if len (params .shape ) == 4 and params .shape [0 ] == 1 :
141
+ params = params [0 ]
142
+ elif len (params .shape ) == 3 :
143
+ pass
144
+ else :
145
+ print (f'{ Color .RED } ERROR:{ Color .RESET } gather_nd when optimizing_barracuda is enabled must have 4 dimensions and batch size = 1 or 3 dimensions.' )
146
+ print (f'{ Color .RED } ERROR:{ Color .RESET } params.shape: { params .shape } , indices.shape: { indices .shape } ' )
147
+ sys .exit (- 1 )
148
+ idx_shape = indices .shape
149
+ params_shape = params .shape
150
+ idx_dims = idx_shape [- 1 ]
151
+ gather_shape = params_shape [idx_dims :]
152
+ params_flat = tf .reshape (params , tf .concat ([[- 1 ], gather_shape ], axis = 0 ))
153
+ axis_step = tf .math .cumprod (params_shape [:idx_dims ], exclusive = True , reverse = True )
154
+ mul = tf .math .multiply (indices , axis_step )
155
+ indices_flat = tf .reduce_sum (mul , axis = - 1 )
156
+ result_flat = tf .gather (params_flat , indices_flat )
157
+ return tf .expand_dims (tf .reshape (result_flat , tf .concat ([idx_shape [:- 1 ], gather_shape ], axis = 0 )), axis = 0 )
158
+
105
159
# calc final pixel value
106
- value_floor = tf .gather_nd (params = features , indices = in_coord_floor , batch_dims = 1 ) #[b,h,w,32]
107
- value_ceilX = tf .gather_nd (params = features , indices = in_coord_ceilX , batch_dims = 1 ) #[b,h,w,32]
108
- value_ceilY = tf .gather_nd (params = features , indices = in_coord_ceilY , batch_dims = 1 ) #[b,h,w,32]
109
- value_ceil_ = tf .gather_nd (params = features , indices = in_coord_ceil_ , batch_dims = 1 ) #[b,h,w,32]
160
+ if not optimizing_barracuda :
161
+ value_floor = tf .gather_nd (params = features , indices = in_coord_floor , batch_dims = 1 ) #[b,h,w,32]
162
+ value_ceilX = tf .gather_nd (params = features , indices = in_coord_ceilX , batch_dims = 1 ) #[b,h,w,32]
163
+ value_ceilY = tf .gather_nd (params = features , indices = in_coord_ceilY , batch_dims = 1 ) #[b,h,w,32]
164
+ value_ceil_ = tf .gather_nd (params = features , indices = in_coord_ceil_ , batch_dims = 1 ) #[b,h,w,32]
165
+ else :
166
+ value_floor = barracuda_gather_nd (params = features , indices = in_coord_floor ) #[b,h,w,32]
167
+ value_ceilX = barracuda_gather_nd (params = features , indices = in_coord_ceilX ) #[b,h,w,32]
168
+ value_ceilY = barracuda_gather_nd (params = features , indices = in_coord_ceilY ) #[b,h,w,32]
169
+ value_ceil_ = barracuda_gather_nd (params = features , indices = in_coord_ceil_ ) #[b,h,w,32]
170
+
110
171
value_floor_fraction = tf .multiply (value_floor , weight_floor )
111
172
value_ceil__fraction = tf .multiply (value_ceil_ , weight_ceil_ )
112
173
value_ceilX_fraction = tf .multiply (value_ceilX , weight_ceilX )
@@ -132,8 +193,12 @@ def Landmarks2TransformMatrix(operator, custom_options, tensors, interpreter, la
132
193
######################################
133
194
# calc rotation
134
195
######################################
135
- rot90_t = tf .constant ([[ 0.0 , 1.0 ],
136
- [ - 1.0 , 0.0 ]]) #[2,2], already transposed
196
+ rot90_t = tf .constant (
197
+ [
198
+ [ 0.0 , 1.0 ],
199
+ [ - 1.0 , 0.0 ]
200
+ ]
201
+ ) #[2,2], already transposed
137
202
138
203
idx_rot_l = custom_options ['left_rotation_idx' ]
139
204
idx_rot_r = custom_options ['right_rotation_idx' ]
0 commit comments