Skip to content Skip to sidebar Skip to footer

Slice A Tensor Using Tensor Indices

I have a tensor img in TF representing an image, whose shape is (n_channels, img_height, img_width). I also have a couple of integer tensors, h_start, h_end, w_start, w_end. I wan

Solution 1:

You can use tf.Tensor.__getitem__ pretty much like with NumPy indexing:

img[:, h_start:h_end, w_start:w_end]

Alternatively, use tf.slice:

sliced_img = tf.slice(img, [0, h_start, w_start], [-1, h_end - h_start, w_end-w_start]

Post a Comment for "Slice A Tensor Using Tensor Indices"