2525_image_ops_so = tf .load_op_library (
2626 resource_loader .get_path_to_datafile ("_image_ops.so" ))
2727
28- _IMAGE_DTYPES = set ([tf .dtypes .uint8 , tf .dtypes .int32 , tf .dtypes .int64 ,
29- tf .dtypes .float16 , tf .dtypes .float32 , tf .dtypes .float64 ])
28+ _IMAGE_DTYPES = set ([
29+ tf .dtypes .uint8 , tf .dtypes .int32 , tf .dtypes .int64 , tf .dtypes .float16 ,
30+ tf .dtypes .float32 , tf .dtypes .float64
31+ ])
3032
3133ops .RegisterShape ("ImageProjectiveTransform" )(common_shapes .call_cpp_shape_fn )
3234
@@ -66,13 +68,11 @@ def transform(images,
6668 Raises:
6769 TypeError: If `image` is an invalid type.
6870 ValueError: If output shape is not 1-D int32 Tensor.
69-
7071 """
7172 with ops .name_scope (name , "transform" ):
7273 image_or_images = ops .convert_to_tensor (images , name = "images" )
73- transform_or_transforms = ops .convert_to_tensor (transforms ,
74- name = "transforms" ,
75- dtype = tf .dtypes .float32 )
74+ transform_or_transforms = ops .convert_to_tensor (
75+ transforms , name = "transforms" , dtype = tf .dtypes .float32 )
7676 if image_or_images .dtype .base_dtype not in _IMAGE_DTYPES :
7777 raise TypeError ("Invalid dtype %s." % image_or_images .dtype )
7878 elif image_or_images .get_shape ().ndims is None :
@@ -89,9 +89,8 @@ def transform(images,
8989 if output_shape is None :
9090 output_shape = tf .shape (images )[1 :3 ]
9191
92- output_shape = ops .convert_to_tensor (output_shape ,
93- tf .dtypes .int32 ,
94- name = "output_shape" )
92+ output_shape = ops .convert_to_tensor (
93+ output_shape , tf .dtypes .int32 , name = "output_shape" )
9594
9695 if not output_shape .get_shape ().is_compatible_with ([2 ]):
9796 raise ValueError (
@@ -134,15 +133,13 @@ def compose_transforms(*transforms):
134133 A composed transform tensor. When passed to `transform` op,
135134 equivalent to applying each of the given transforms to the image in
136135 order.
137-
138136 """
139137 assert transforms , "transforms cannot be empty"
140138 with ops .name_scope ("compose_transforms" ):
141139 composed = flat_transforms_to_matrices (transforms [0 ])
142140 for tr in transforms [1 :]:
143141 # Multiply batches of matrices.
144- composed = tf .matmul (composed ,
145- flat_transforms_to_matrices (tr ))
142+ composed = tf .matmul (composed , flat_transforms_to_matrices (tr ))
146143 return matrices_to_flat_transforms (composed )
147144
148145
@@ -163,22 +160,18 @@ def flat_transforms_to_matrices(transforms):
163160
164161 Raises:
165162 ValueError: If `transforms` have an invalid shape.
166-
167163 """
168164 with ops .name_scope ("flat_transforms_to_matrices" ):
169165 transforms = ops .convert_to_tensor (transforms , name = "transforms" )
170166 if transforms .shape .ndims not in (1 , 2 ):
171- raise ValueError ("Transforms should be 1D or 2D, got: %s" %
172- transforms )
167+ raise ValueError (
168+ "Transforms should be 1D or 2D, got: %s" % transforms )
173169 # Make the transform(s) 2D in case the input is a single transform.
174- transforms = tf .reshape (transforms ,
175- tf .constant ([- 1 , 8 ]))
170+ transforms = tf .reshape (transforms , tf .constant ([- 1 , 8 ]))
176171 num_transforms = tf .shape (transforms )[0 ]
177172 # Add a column of ones for the implicit last entry in the matrix.
178173 return tf .reshape (
179- tf .concat (
180- [transforms , tf .ones ([num_transforms , 1 ])],
181- axis = 1 ),
174+ tf .concat ([transforms , tf .ones ([num_transforms , 1 ])], axis = 1 ),
182175 tf .constant ([- 1 , 3 , 3 ]))
183176
184177
@@ -200,17 +193,15 @@ def matrices_to_flat_transforms(transform_matrices):
200193
201194 Raises:
202195 ValueError: If `transform_matrices` have an invalid shape.
203-
204196 """
205197 with ops .name_scope ("matrices_to_flat_transforms" ):
206- transform_matrices = ops .convert_to_tensor (transform_matrices ,
207- name = "transform_matrices" )
198+ transform_matrices = ops .convert_to_tensor (
199+ transform_matrices , name = "transform_matrices" )
208200 if transform_matrices .shape .ndims not in (2 , 3 ):
209- raise ValueError ("Matrices should be 2D or 3D, got: %s" %
210- transform_matrices )
201+ raise ValueError (
202+ "Matrices should be 2D or 3D, got: %s" % transform_matrices )
211203 # Flatten each matrix.
212- transforms = tf .reshape (transform_matrices ,
213- tf .constant ([- 1 , 9 ]))
204+ transforms = tf .reshape (transform_matrices , tf .constant ([- 1 , 9 ]))
214205 # Divide each matrix by the last entry (normally 1).
215206 transforms /= transforms [:, 8 :9 ]
216207 return transforms [:, :8 ]
@@ -232,33 +223,35 @@ def angles_to_projective_transforms(angles,
232223 Returns:
233224 A tensor of shape (num_images, 8). Projective transforms which can be given
234225 to `transform` op.
235-
236226 """
237227 with ops .name_scope (name , "angles_to_projective_transforms" ):
238- angle_or_angles = ops .convert_to_tensor (angles ,
239- name = "angles" ,
240- dtype = tf .dtypes .float32 )
228+ angle_or_angles = ops .convert_to_tensor (
229+ angles , name = "angles" , dtype = tf .dtypes .float32 )
241230 if len (angle_or_angles .get_shape ()) == 0 :
242231 angles = angle_or_angles [None ]
243232 elif len (angle_or_angles .get_shape ()) == 1 :
244233 angles = angle_or_angles
245234 else :
246235 raise TypeError ("Angles should have rank 0 or 1." )
247- x_offset = ((image_width - 1 ) -
248- (tf .math .cos (angles ) * (image_width - 1 ) -
249- tf .math .sin (angles ) * (image_height - 1 ))) / 2.0
250- y_offset = ((image_height - 1 ) -
251- (tf .math .sin (angles ) * (image_width - 1 ) +
252- tf .math .cos (angles ) * (image_height - 1 ))) / 2.0
236+ x_offset = (
237+ (image_width - 1 ) -
238+ (tf .math .cos (angles ) * (image_width - 1 ) - tf .math .sin (angles ) *
239+ (image_height - 1 ))) / 2.0
240+ y_offset = (
241+ (image_height - 1 ) -
242+ (tf .math .sin (angles ) * (image_width - 1 ) + tf .math .cos (angles ) *
243+ (image_height - 1 ))) / 2.0
253244 num_angles = tf .shape (angles )[0 ]
254245 return tf .concat (
255- values = [tf .math .cos (angles )[:, None ],
256- - tf .math .sin (angles )[:, None ],
257- x_offset [:, None ],
258- tf .math .sin (angles )[:, None ],
259- tf .math .cos (angles )[:, None ],
260- y_offset [:, None ],
261- tf .zeros ((num_angles , 2 ), tf .dtypes .float32 ),],
246+ values = [
247+ tf .math .cos (angles )[:, None ],
248+ - tf .math .sin (angles )[:, None ],
249+ x_offset [:, None ],
250+ tf .math .sin (angles )[:, None ],
251+ tf .math .cos (angles )[:, None ],
252+ y_offset [:, None ],
253+ tf .zeros ((num_angles , 2 ), tf .dtypes .float32 ),
254+ ],
262255 axis = 1 )
263256
264257
@@ -270,9 +263,8 @@ def _image_projective_transform_grad(op, grad):
270263 interpolation = op .get_attr ("interpolation" )
271264
272265 image_or_images = ops .convert_to_tensor (images , name = "images" )
273- transform_or_transforms = ops .convert_to_tensor (transforms ,
274- name = "transforms" ,
275- dtype = tf .dtypes .float32 )
266+ transform_or_transforms = ops .convert_to_tensor (
267+ transforms , name = "transforms" , dtype = tf .dtypes .float32 )
276268
277269 if image_or_images .dtype .base_dtype not in _IMAGE_DTYPES :
278270 raise TypeError ("Invalid dtype %s." % image_or_images .dtype )
0 commit comments