Skip to content

Commit beba9e6

Browse files
Switch from TFA resampler op to TFA interpolate_bilinear.
Maintaining the custom resampler op is a maintenance headache that requires aligning the versions of TFA and TFG for the opensource releases (e.g., #650). The interpolate_bilinear function is a pure-TF op that is likely somewhat slower, but has no maintenance problems. Using pure-TF ops also allows using higher-order derivatives and tf.ForwardAccumulator with the transform.sample function. The implemented border behavior of interpolate_bilinear is DUPLICATE, whereas resampler is ZERO, so the sample function changes slightly to pad the image with zeros when BorderType.ZERO is requested. PiperOrigin-RevId: 413971898
1 parent 38771b1 commit beba9e6

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

tensorflow_graphics/image/transformer.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,16 @@ def sample(image: type_alias.TensorLike,
9797
if resampling_type == ResamplingType.NEAREST:
9898
warp = tf.math.round(warp)
9999

100-
if border_type == BorderType.DUPLICATE:
101-
image_size = tf.cast(tf.shape(input=image)[1:3], dtype=warp.dtype)
102-
height, width = tf.unstack(image_size, axis=-1)
103-
warp_x, warp_y = tf.unstack(warp, axis=-1)
104-
warp_x = tf.clip_by_value(warp_x, 0.0, width - 1.0)
105-
warp_y = tf.clip_by_value(warp_y, 0.0, height - 1.0)
106-
warp = tf.stack((warp_x, warp_y), axis=-1)
107-
108-
return tfa_image.resampler(image, warp)
100+
if border_type == BorderType.ZERO:
101+
image = tf.pad(image, ((0, 0), (1, 1), (1, 1), (0, 0)))
102+
warp = warp + 1
103+
104+
warp_shape = tf.shape(warp)
105+
flat_warp = tf.reshape(warp, (warp_shape[0], -1, 2))
106+
flat_sampled = tfa_image.interpolate_bilinear(
107+
image, flat_warp, indexing="xy")
108+
output_shape = tf.concat((warp_shape[:-1], tf.shape(flat_sampled)[-1:]), 0)
109+
return tf.reshape(flat_sampled, output_shape)
109110

110111

111112
def perspective_transform(

0 commit comments

Comments
 (0)