python - 使用张量索引对张量进行切片

标签 python tensorflow

我在 TF 中有一个张量 img 表示图像,其形状为 (n_channels, img_height, img_width)

我还有几个整数张量,h_starth_endw_startw_end

我想提取图像中与 numpy 中的 img[:, :, h_start:h_end, w_start:w_end] 相对应的部分

我怎样才能做到这一点?

最佳答案

您可以使用tf.Tensor.__getitem__与 NumPy 索引非常相似:

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

或者,使用 tf.slice :

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

关于python - 使用张量索引对张量进行切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55359922/

相关文章:

python - tensorflow 中的优化器如何访问在单独函数中创建的变量

python - 用填充堆叠 numpy 数组

python - 删除 Python 标准库中的变量

python - 为什么我的 QFileSystemModel QModelIndex 无法获取子节点信息?

python - 手写数字分类教程有错误/缺失属性

python - 如何在 Tensorflow 中用 2-D 张量索引 3-D 张量?

python - For 循环 vs Numpy 向量化计算时间

具有复杂的用户角色权限结构的 Python App Engine 项目

Python 成员资格运算符 "In"TensorFlow 数据集

python - 如何制作 tensorflow 中值滤波器?