@@ -108,17 +108,14 @@ def resize_image_tensor(
108108 extra_dims = image .shape [:- 3 ]
109109
110110 if image .numel () > 0 :
111- resized_image = _FT .resize (
111+ image = _FT .resize (
112112 image .view (- 1 , num_channels , old_height , old_width ),
113113 size = [new_height , new_width ],
114114 interpolation = interpolation .value ,
115115 antialias = antialias ,
116116 )
117- else :
118- # TODO: the cloning is probably unnecessary. Review this together with the other perf candidates
119- resized_image = image .clone ()
120117
121- return resized_image .view (extra_dims + (num_channels , new_height , new_width ))
118+ return image .view (extra_dims + (num_channels , new_height , new_width ))
122119
123120
124121def resize_image_pil (
@@ -229,6 +226,9 @@ def affine_image_tensor(
229226 fill : Optional [List [float ]] = None ,
230227 center : Optional [List [float ]] = None ,
231228) -> torch .Tensor :
229+ if img .numel () == 0 :
230+ return img
231+
232232 num_channels , height , width = img .shape [- 3 :]
233233 extra_dims = img .shape [:- 3 ]
234234 img = img .view (- 1 , num_channels , height , width )
@@ -452,23 +452,32 @@ def rotate_image_tensor(
452452) -> torch .Tensor :
453453 num_channels , height , width = img .shape [- 3 :]
454454 extra_dims = img .shape [:- 3 ]
455- img = img .view (- 1 , num_channels , height , width )
456455
457456 center_f = [0.0 , 0.0 ]
458457 if center is not None :
459458 if expand :
460459 warnings .warn ("The provided center argument has no effect on the result if expand is True" )
461460 else :
462- _ , height , width = get_dimensions_image_tensor (img )
463461 # Center values should be in pixel coordinates but translated such that (0, 0) corresponds to image center.
464462 center_f = [1.0 * (c - s * 0.5 ) for c , s in zip (center , [width , height ])]
465463
466464 # due to current incoherence of rotation angle direction between affine and rotate implementations
467465 # we need to set -angle.
468466 matrix = _get_inverse_affine_matrix (center_f , - angle , [0.0 , 0.0 ], 1.0 , [0.0 , 0.0 ])
469- output = _FT .rotate (img , matrix , interpolation = interpolation .value , expand = expand , fill = fill )
470- new_height , new_width = output .shape [- 2 :]
471- return output .view (extra_dims + (num_channels , new_height , new_width ))
467+
468+ if img .numel () > 0 :
469+ img = _FT .rotate (
470+ img .view (- 1 , num_channels , height , width ),
471+ matrix ,
472+ interpolation = interpolation .value ,
473+ expand = expand ,
474+ fill = fill ,
475+ )
476+ new_height , new_width = img .shape [- 2 :]
477+ else :
478+ new_width , new_height = _FT ._compute_affine_output_size (matrix , width , height ) if expand else (width , height )
479+
480+ return img .view (extra_dims + (num_channels , new_height , new_width ))
472481
473482
474483def rotate_image_pil (
@@ -557,19 +566,17 @@ def pad_image_tensor(
557566 num_channels , height , width = img .shape [- 3 :]
558567 extra_dims = img .shape [:- 3 ]
559568
560- left , right , top , bottom = _FT ._parse_pad_padding (padding )
561- new_height = height + top + bottom
562- new_width = width + left + right
563-
564569 if img .numel () > 0 :
565- padded_image = _FT .pad (
570+ img = _FT .pad (
566571 img = img .view (- 1 , num_channels , height , width ), padding = padding , fill = fill , padding_mode = padding_mode
567572 )
573+ new_height , new_width = img .shape [- 2 :]
568574 else :
569- # TODO: the cloning is probably unnecessary. Review this together with the other perf candidates
570- padded_image = img .clone ()
575+ left , right , top , bottom = _FT ._parse_pad_padding (padding )
576+ new_height = height + top + bottom
577+ new_width = width + left + right
571578
572- return padded_image .view (extra_dims + (num_channels , new_height , new_width ))
579+ return img .view (extra_dims + (num_channels , new_height , new_width ))
573580
574581
575582# TODO: This should be removed once pytorch pad supports non-scalar padding values
0 commit comments