如何将PyTorch张量转换为图像并与烧瓶一起发送



我在PyTorch中有一个神经网络,它将图像作为张量。我已经将其转换为numpy数组,然后按照这里的解释将该图像发送到html。问题是它总是黑色的。

这是我在PyTorch中的代码:

def getLinearImage():
with torch.no_grad():
test_z = torch.randn(1, 100).to(device)
generated = G(test_z)
generated = generated.cpu()
numpy = generated[0].view(64, 64).numpy()
numpu = (numpy + 1) * 128
return numpy

这是烧瓶中的代码,其中arrgetLinearImage((的返回值

def getImage(arr):
img = Image.fromarray(arr.astype("uint8"))
file_object = io.BytesIO()
img.save(file_object, "PNG")  
file_object.seek(0)
return send_file(file_object, mimetype="image/PNG")

如果我打开一个静态图像并将其发送到getImage((,它可以工作,但不能与生成的图像一起工作。在html中,我称之为:<img src="/getLinearImage" alt="User Image" height="100px" width="100px">

从逻辑上讲,由于静态图像是有效的,错误就在getLinearImage代码中。我建议使用PDB(或您选择的调试器(来运行它,以找出为什么它没有正确生成。

也就是说,我在你的代码中创建了一个变量:

numpu = (numpy + 1) * 128

你似乎没有使用它,因为你随后返回了另一个变量:

return numpy

这会是你的问题吗?

另外:我想,当你创建这个时,你在本地保存了原始图像,以确保首先生成一些东西?

最新更新