1919
2020import tensorflow as tf
2121from tensorflow .python .framework import common_shapes
22- from tensorflow .python .framework import constant_op
23- from tensorflow .python .framework import dtypes
24- from tensorflow .python .framework import load_library
2522from tensorflow .python .framework import ops
26- from tensorflow .python .ops import array_ops
27- from tensorflow .python .ops import linalg_ops
28- from tensorflow .python .ops import math_ops
2923from tensorflow .python .platform import resource_loader
3024
31- _image_ops_so = load_library .load_op_library (
25+ _image_ops_so = tf .load_op_library (
3226 resource_loader .get_path_to_datafile ("_image_ops.so" ))
3327
34- _IMAGE_DTYPES = set ([dtypes .uint8 , dtypes .int32 , dtypes . int64 , dtypes .float16 ,
35- dtypes .float32 , dtypes .float64 ])
28+ _IMAGE_DTYPES = set ([tf . dtypes .uint8 , tf . dtypes .int32 , tf . dtypes .int64 ,
29+ tf . dtypes .float16 , tf . dtypes . float32 , tf . dtypes .float64 ])
3630
3731ops .RegisterShape ("ImageProjectiveTransform" )(common_shapes .call_cpp_shape_fn )
3832
@@ -78,7 +72,7 @@ def transform(images,
7872 image_or_images = ops .convert_to_tensor (images , name = "images" )
7973 transform_or_transforms = ops .convert_to_tensor (transforms ,
8074 name = "transforms" ,
81- dtype = dtypes .float32 )
75+ dtype = tf . dtypes .float32 )
8276 if image_or_images .dtype .base_dtype not in _IMAGE_DTYPES :
8377 raise TypeError ("Invalid dtype %s." % image_or_images .dtype )
8478 elif image_or_images .get_shape ().ndims is None :
@@ -93,10 +87,10 @@ def transform(images,
9387 raise TypeError ("Images should have rank between 2 and 4." )
9488
9589 if output_shape is None :
96- output_shape = array_ops .shape (images )[1 :3 ]
90+ output_shape = tf .shape (images )[1 :3 ]
9791
9892 output_shape = ops .convert_to_tensor (output_shape ,
99- dtypes .int32 ,
93+ tf . dtypes .int32 ,
10094 name = "output_shape" )
10195
10296 if not output_shape .get_shape ().is_compatible_with ([2 ]):
@@ -147,8 +141,8 @@ def compose_transforms(*transforms):
147141 composed = flat_transforms_to_matrices (transforms [0 ])
148142 for tr in transforms [1 :]:
149143 # Multiply batches of matrices.
150- composed = math_ops .matmul (composed ,
151- flat_transforms_to_matrices (tr ))
144+ composed = tf .matmul (composed ,
145+ flat_transforms_to_matrices (tr ))
152146 return matrices_to_flat_transforms (composed )
153147
154148
@@ -177,15 +171,15 @@ def flat_transforms_to_matrices(transforms):
177171 raise ValueError ("Transforms should be 1D or 2D, got: %s" %
178172 transforms )
179173 # Make the transform(s) 2D in case the input is a single transform.
180- transforms = array_ops .reshape (transforms ,
181- constant_op .constant ([- 1 , 8 ]))
182- num_transforms = array_ops .shape (transforms )[0 ]
174+ transforms = tf .reshape (transforms ,
175+ tf .constant ([- 1 , 8 ]))
176+ num_transforms = tf .shape (transforms )[0 ]
183177 # Add a column of ones for the implicit last entry in the matrix.
184- return array_ops .reshape (
185- array_ops .concat (
186- [transforms , array_ops .ones ([num_transforms , 1 ])],
178+ return tf .reshape (
179+ tf .concat (
180+ [transforms , tf .ones ([num_transforms , 1 ])],
187181 axis = 1 ),
188- constant_op .constant ([- 1 , 3 , 3 ]))
182+ tf .constant ([- 1 , 3 , 3 ]))
189183
190184
191185def matrices_to_flat_transforms (transform_matrices ):
@@ -215,8 +209,8 @@ def matrices_to_flat_transforms(transform_matrices):
215209 raise ValueError ("Matrices should be 2D or 3D, got: %s" %
216210 transform_matrices )
217211 # Flatten each matrix.
218- transforms = array_ops .reshape (transform_matrices ,
219- constant_op .constant ([- 1 , 9 ]))
212+ transforms = tf .reshape (transform_matrices ,
213+ tf .constant ([- 1 , 9 ]))
220214 # Divide each matrix by the last entry (normally 1).
221215 transforms /= transforms [:, 8 :9 ]
222216 return transforms [:, :8 ]
@@ -243,28 +237,28 @@ def angles_to_projective_transforms(angles,
243237 with ops .name_scope (name , "angles_to_projective_transforms" ):
244238 angle_or_angles = ops .convert_to_tensor (angles ,
245239 name = "angles" ,
246- dtype = dtypes .float32 )
240+ dtype = tf . dtypes .float32 )
247241 if len (angle_or_angles .get_shape ()) == 0 :
248242 angles = angle_or_angles [None ]
249243 elif len (angle_or_angles .get_shape ()) == 1 :
250244 angles = angle_or_angles
251245 else :
252246 raise TypeError ("Angles should have rank 0 or 1." )
253247 x_offset = ((image_width - 1 ) -
254- (math_ops .cos (angles ) * (image_width - 1 ) -
255- math_ops .sin (angles ) * (image_height - 1 ))) / 2.0
248+ (tf . math .cos (angles ) * (image_width - 1 ) -
249+ tf . math .sin (angles ) * (image_height - 1 ))) / 2.0
256250 y_offset = ((image_height - 1 ) -
257- (math_ops .sin (angles ) * (image_width - 1 ) +
258- math_ops .cos (angles ) * (image_height - 1 ))) / 2.0
259- num_angles = array_ops .shape (angles )[0 ]
260- return array_ops .concat (
261- values = [math_ops .cos (angles )[:, None ],
262- - math_ops .sin (angles )[:, None ],
251+ (tf . math .sin (angles ) * (image_width - 1 ) +
252+ tf . math .cos (angles ) * (image_height - 1 ))) / 2.0
253+ num_angles = tf .shape (angles )[0 ]
254+ return tf .concat (
255+ values = [tf . math .cos (angles )[:, None ],
256+ - tf . math .sin (angles )[:, None ],
263257 x_offset [:, None ],
264- math_ops .sin (angles )[:, None ],
265- math_ops .cos (angles )[:, None ],
258+ tf . math .sin (angles )[:, None ],
259+ tf . math .cos (angles )[:, None ],
266260 y_offset [:, None ],
267- array_ops .zeros ((num_angles , 2 ), dtypes .float32 ),],
261+ tf .zeros ((num_angles , 2 ), tf . dtypes .float32 ),],
268262 axis = 1 )
269263
270264
@@ -278,7 +272,7 @@ def _image_projective_transform_grad(op, grad):
278272 image_or_images = ops .convert_to_tensor (images , name = "images" )
279273 transform_or_transforms = ops .convert_to_tensor (transforms ,
280274 name = "transforms" ,
281- dtype = dtypes .float32 )
275+ dtype = tf . dtypes .float32 )
282276
283277 if image_or_images .dtype .base_dtype not in _IMAGE_DTYPES :
284278 raise TypeError ("Invalid dtype %s." % image_or_images .dtype )
@@ -291,11 +285,11 @@ def _image_projective_transform_grad(op, grad):
291285
292286 # Invert transformations
293287 transforms = flat_transforms_to_matrices (transforms = transforms )
294- inverse = linalg_ops . matrix_inverse (transforms )
288+ inverse = tf . linalg . inv (transforms )
295289 transforms = matrices_to_flat_transforms (inverse )
296290 output = _image_ops_so .image_projective_transform (
297291 images = grad ,
298292 transforms = transforms ,
299- output_shape = array_ops .shape (image_or_images )[1 :3 ],
293+ output_shape = tf .shape (image_or_images )[1 :3 ],
300294 interpolation = interpolation )
301295 return [output , None , None ]
0 commit comments