serve icon indicating copy to clipboard operation
serve copied to clipboard

How do we get the curl output as image?

Open UdonDa opened this issue 6 years ago • 15 comments

I'd like to try to serve the style transfer model.

So, how do we make the handler of style transfer?

Thanks.

UdonDa avatar May 16 '20 14:05 UdonDa

@UdonDa Please check the custom service documentation and example for this.

chauhang avatar May 16 '20 21:05 chauhang

Hi @chauhang I can implement the custom service following your proposed link.

The return value of serve is str, so we need to cast the image type. How do I implement to get the translated image in the client with python effectively?

I think it's a same problem of image segmentation https://github.com/pytorch/serve/tree/master/examples/image_segmenter .

Then curl -X POST http://127.0.0.1:8080/predictions/style_transfer -T ./content.png -o ./return_fake.png, I get the illegal file.

UdonDa avatar May 17 '20 03:05 UdonDa

Actually, We can get the returned image through server: np.array -> byte -> str and cliant: str-> byte -> np.array. But the image is illegal.

For example, the output image in the server is well. fake2

But, after I get the response and cast it and save it, the image is illegal. fake

UdonDa avatar May 17 '20 06:05 UdonDa

@UdonDa Could you please use the segmentation default handler given here OR checkout issue_355 branch for this file and install from the source i.e. [pip install .]. I have modified the post-processing to return the image. You can use the following to get image output.

-Register model curl -v -X POST "http://localhost:8081/models?initial_workers=1&synchronous=true&url=https://torchserve.s3.amazonaws.com/mar_files/fcn_resnet_101.mar"

-Inferencing curl http://localhost:8080/predictions/fcn_resnet_101 -T <inputfilename>.jpg -o output.jpg

Since we were trying to return a string in an incorrect way, the earlier output seems screwed up in an existing handler.

codinnvrends avatar May 18 '20 20:05 codinnvrends

@dhaniram-kshirsagar can you please attach the output from the example images attached above? This will clarify what the output image looks like.

In addition, we should really specify how our default handlers were tested, especially, in cases where we have tested with models from PyTorch pre-trained models it is crucial to mention it.

FWIW, in most cases users who bring their models that require any steps in pre-processing and post-processing that are not a part of our testing should be clearly mentioned.

IIUC, our default handler assumes the for image segmentation model is https://pytorch.org/hub/pytorch_vision_fcn_resnet101/ not sure if this ideal. Can you explain why this (or some other specific model) was selected and why we expect this to apply more generically?

mycpuorg avatar May 27 '20 23:05 mycpuorg

@mycpuorg - It turned out to be a bug as explained above hence fixed it in PR #385 [as explained in my earlier comment above] Also, this new PR #432 addresses the concerns around structure and things to be taken care of while implementing custom or extending default handlers. This was discussed in the initial stages of torchserve and it was decided that we will have default handlers for commonly used vision related models i.e. object detector, image classifier and image segmentation naturally based on torch hub examples.

codinnvrends avatar Jun 29 '20 05:06 codinnvrends

In a branch of issue_355, I sent a post using curl http://localhost:8080/predictions/fcn -T ./examples/image_segmenter/fcn/persons.jpg -o output.jpg, but I got the illegal jpg file, which following by https://github.com/pytorch/serve/tree/master/examples/image_segmenter.

It's an output of `curl http://localhost:8080/predictions/fcn_resnet_101 -T ./examples/image_segmenter/fcn/persons.jpg

$ curl http://localhost:8080/predictions/fcn -T ./examples/image_segmenter/fcn/persons.jpg
[[[11.49452    11.49452    11.49452    ... 10.846573   10.846573
   10.846573  ]
  [11.49452    11.49452    11.49452    ... 10.846573   10.846573
   10.846573  ]
  [11.49452    11.49452    11.49452    ... 10.846573   10.846573
   10.846573  ]
  ...
  [10.028987   10.028987   10.028987   ...  9.980108    9.980108
    9.980108  ]
  [10.028987   10.028987   10.028987   ...  9.980108    9.980108
    9.980108  ]
  [10.028987   10.028987   10.028987   ...  9.980108    9.980108
    9.980108  ]]

 [[-2.5241811  -2.5241811  -2.5241811  ... -1.4157648  -1.4157648
   -1.4157648 ]
  [-2.5241811  -2.5241811  -2.5241811  ... -1.4157648  -1.4157648
   -1.4157648 ]
  [-2.5241811  -2.5241811  -2.5241811  ... -1.4157648  -1.4157648
   -1.4157648 ]
  ...
  [-0.52271074 -0.52271074 -0.52271074 ... -0.9109308  -0.9109308
   -0.9109308 ]
  [-0.52271074 -0.52271074 -0.52271074 ... -0.9109308  -0.9109308
   -0.9109308 ]
  [-0.52271074 -0.52271074 -0.52271074 ... -0.9109308  -0.9109308
   -0.9109308 ]]

 [[-1.1300582  -1.1300582  -1.1300582  ... -0.8853849  -0.8853849
   -0.8853849 ]
  [-1.1300582  -1.1300582  -1.1300582  ... -0.8853849  -0.8853849
   -0.8853849 ]
  [-1.1300582  -1.1300582  -1.1300582  ... -0.8853849  -0.8853849
   -0.8853849 ]
  ...
  [-1.1726369  -1.1726369  -1.1726369  ... -1.6144364  -1.6144364
   -1.6144364 ]
  [-1.1726369  -1.1726369  -1.1726369  ... -1.6144364  -1.6144364
   -1.6144364 ]
  [-1.1726369  -1.1726369  -1.1726369  ... -1.6144364  -1.6144364
   -1.6144364 ]]

 ...

 [[-0.27236336 -0.27236336 -0.27236336 ... -0.79138166 -0.79138166
   -0.79138166]
  [-0.27236336 -0.27236336 -0.27236336 ... -0.79138166 -0.79138166
   -0.79138166]
  [-0.27236336 -0.27236336 -0.27236336 ... -0.79138166 -0.79138166
   -0.79138166]
  ...
  [-0.16645525 -0.16645525 -0.16645525 ...  0.43779224  0.43779224
    0.43779224]
  [-0.16645525 -0.16645525 -0.16645525 ...  0.43779224  0.43779224
    0.43779224]
  [-0.16645525 -0.16645525 -0.16645525 ...  0.43779224  0.43779224
    0.43779224]]

 [[-0.70757556 -0.70757556 -0.70757556 ... -1.0088176  -1.0088176
   -1.0088176 ]
  [-0.70757556 -0.70757556 -0.70757556 ... -1.0088176  -1.0088176
   -1.0088176 ]
  [-0.70757556 -0.70757556 -0.70757556 ... -1.0088176  -1.0088176
   -1.0088176 ]
  ...
  [ 0.44139224  0.44139224  0.44139224 ...  0.28535953  0.28535953
    0.28535953]
  [ 0.44139224  0.44139224  0.44139224 ...  0.28535953  0.28535953
    0.28535953]
  [ 0.44139224  0.44139224  0.44139224 ...  0.28535953  0.28535953
    0.28535953]]

 [[-0.47415316 -0.47415316 -0.47415316 ... -0.43148765 -0.43148765
   -0.43148765]
  [-0.47415316 -0.47415316 -0.47415316 ... -0.43148765 -0.43148765
   -0.43148765]
  [-0.47415316 -0.47415316 -0.47415316 ... -0.43148765 -0.43148765
   -0.43148765]
  ...
  [ 0.04289086  0.04289086  0.04289086 ... -0.42638043 -0.42638043
   -0.42638043]
  [ 0.04289086  0.04289086  0.04289086 ... -0.42638043 -0.42638043
   -0.42638043]
  [ 0.04289086  0.04289086  0.04289086 ... -0.42638043 -0.42638043
   -0.42638043]]]

I wonder this output is wrong. I think in the segmentation task we use an argmax operation to convert an output feature map to class-label mask.

But, it seems the feature tensor...

If you can get the correct output, please let me show the output image.

UdonDa avatar Jun 30 '20 15:06 UdonDa

Not sure how you test it but this is the output I am getting for person.jpg. I am following the same steps. NOTE now curl command demands you to supply -o flag else it fails. e.g.

--without -o/--output flag curl http://localhost:8080/predictions/fcn_resnet_101 -T ./serve/examples/image_segmenter/fcn/persons.jpg Warning: Binary output can mess up your terminal. Use "--output -" to tell Warning: curl to output it to your terminal anyway, or consider "--output Warning: " to save to a file.

curl http://localhost:8080/predictions/fcn_resnet_101 -T ./serve/examples/image_segmenter/fcn/persons.jpg -o out.jpg % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 303k 100 21476 100 282k 25147 330k --:--:-- --:--:-- --:--:-- 354k

out

codinnvrends avatar Jul 02 '20 12:07 codinnvrends

Closing due to inactivity. The bug fixes in the image segmentation default handler are already merged to master as part of #432.

harshbafna avatar Oct 15 '20 11:10 harshbafna

I am using the latest files from Torchserve for the image segmenter when I use the -o flag and try to save the output as a jpg file, it does not seem to be a valid image file. I suspect that it is still writing out the labels. Can you tell me how to write it out as an image file esp. for applications like segmentation/style transfer ?

anustupc avatar Nov 22 '20 23:11 anustupc

@anustupc In the last release, the implementation has changed and now we don't return the image anymore. You will have to write your custom handler for post-processing to return the image instead of labels. You may refer to the old version of image_segmenter.py handler for this post-processing.

codinnvrends avatar Nov 23 '20 04:11 codinnvrends

Hi, in the old version of image_segmenter.py, the result is returned from server to client through np.array -> str -> (...network...) -> str -> np.array, which is very slow (The client may need several seconds to convert str to np.array if the image is high-resolution).

In my case, I want to serve a depth estimation model, which returns a np.array containing depth value for each pixel. How can I speed up the transferring?

CSU-NXY avatar Jul 02 '21 02:07 CSU-NXY

@CSU-NXY Hi, did you find any solution yet?

lizc126 avatar Feb 21 '23 06:02 lizc126

Hello, can anyone please give a clear indication on what's the standard way of sending an image as output with torchserve? Is the correct way the one mentioned in https://github.com/pytorch/serve/commit/7984492081699372dd164bb6bff37d5706475081 ? If so, why was that example cancelled? Is it just a bad practice to return an image? If so, do you have any indication of a better way to accomplish the task? should I wrap the torchserve instance with yet another postprocess layer to turn tensors into images?

sd3ntato avatar Jan 16 '24 15:01 sd3ntato

Hi @sd3ntato You can refer to this example for one way to do this https://github.com/pytorch/serve/blob/master/examples/diffusers/Readme.md

agunapal avatar Jan 18 '24 22:01 agunapal