【发布时间】:2021-09-14 11:03:21
【问题描述】:
说,我在 PyTorch 中有一批图像。对于每张图片,我还有一个像素位置,比如(x, y)。可以使用img[x, y] 读取一张图像的像素值。我正在尝试读取批次中每个图像的像素值。请看下面的代码sn-p:
import torch
# create tensors to represent random images in torch format
img_1 = torch.rand(1, 200, 300)
img_2 = torch.rand(1, 200, 300)
img_3 = torch.rand(1, 200, 300)
img_4 = torch.rand(1, 200, 300)
# for each image, x-y value are know, so creating a tuple
img1_xy = (0, 10, 70)
img2_xy = (0, 40, 20)
img3_xy = (0, 30, 50)
img4_xy = (0, 80, 60)
# this is what I am doing right now
imgs = [img_1, img_2, img_3, img_4]
imgs_xy = [img1_xy, img2_xy, img3_xy, img4_xy]
x = [img[xy] for img, xy in zip(imgs, imgs_xy)]
x = torch.as_tensor(x)
我的疑虑和问题
- 在每个图像中,像素位置(即
(x, y))是已知的。但是,我必须创建一个包含更多元素的元组,即 0 以确保元组与图像的形状相匹配。有什么优雅的方式吗? - 不使用
tuple,就不能使用张量,然后获取像素值吗? - 所有图像都可以连接成一个批次
img_batch = torch.cat((img_1, img_2, img_3, img_4))。但是元组呢?
【问题讨论】:
标签: python indexing pytorch tensor