问题
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