3

I have a neural network in PyTorch which gives an image as a Tensor. I have converted it to a numpy array and then followed the explanation here to send that image to the html. The problem is that it's always black.

This is my code in the 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

This is the code in the flask where arr is the returned value from getLinearImage()

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")

If I open a static image and I send it to getImage() it works but won't work with the generated one. In the html I call it like: <img src="/getLinearImage" alt="User Image" height="100px" width="100px">

nikolay1499
  • 145
  • 1
  • 10

1 Answers1

2

Logically speaking, since the static image works, the error is somewhere in your getLinearImage code. I would suggest running things through using PDB (or a debugger of your choice) to figure out why it's not generated correctly.

That said, I create a variable in your code:

numpu = (numpy + 1) * 128

which you don't seem to use, since you return the other variable afterwards:

return numpy

Could that be your problem?

Also: I presume that when you created this, you saved the original image locally to ensure something gets generated in the first place?

JustLudo
  • 1,690
  • 12
  • 29
  • Yeah originally I saved the image with the save_image method in torchvision.utils with nomalize to true. I also show it with matplotlib. Both look perfectly fine. ```numpu = (numpy + 1) * 128``` This line is supposed to normalize the value of the image from the range [-1, 1] used for the neural network to [0, 255] used for the real image. It's supposed to be numpy not numpu but it doesn't work either way. – nikolay1499 Jul 20 '20 at 09:34
  • I would try to save the file to a temp directory before sending it out (really right before the return) and open it separately. It sounds to me like the file is either not shown correctly (someone with more Flask knowledge should answer that) or it is not generated correclty. Furthermore I would really recommend using PDB to walk through the code in order to see where the file might drop off and try to narrow down that way. – JustLudo Jul 20 '20 at 10:12
  • After doing some more tests I have figured out that the image is correctly received by the getImage() method but when I apply ``img = Image.fromarray(arr.astype("uint8"))``` it all gets weird. – nikolay1499 Jul 20 '20 at 10:34
  • The astype expects it to be a able to cast to a certain array type, according to this documentation (https://numpy.org/doc/stable/reference/generated/numpy.ndarray.astype.html). Maybe you can check in your method what kind of array you get in this function? Maybe the array is even empty? – JustLudo Jul 20 '20 at 10:48