Slice a tensor using tensor indices

我只是一个虾纸丫 提交于 2021-01-28 11:52:27

问题


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 want to extract the part of the image that corresponds to what in numpy would be img[:, :, h_start:h_end, w_start:w_end].

How can I do that?


回答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]


来源:https://stackoverflow.com/questions/55359922/slice-a-tensor-using-tensor-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!