serve icon indicating copy to clipboard operation
serve copied to clipboard

Saving model without .eval gives wrong outputs

Open FrancescoSaverioZuppichini opened this issue 6 years ago • 7 comments

Dear all, I come across a problem. If I do not call model.eval() before saving a model I will have a wrong output in the server, even if in the BaseHandler calls self.model.eval() at runtime.

I have the following steps to put in production a resnet34 model

  1. Saving the model
from torchvision.models import resnet34
import torch
model = resnet34(pretrained=True)
example_input = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example_input)
traced_script_module.save("./resnet34.pt")
  1. Create the .mar file
torch-model-archiver --model-name resnet34 --version 1.0 --serialized-file resnet34.pt --extra-files ./index_to_name.json --handler image_classifier  --export-path model-store -f
  1. run torchserve (with docker)
docker run --rm -it -p 8080:8080 -p 8081:8081 -v /home/zuppif/Documents/TorchServeTryout/model-store:/home/model-server/model-store pytorch/torchserve:0.1-cpu torchserve --start --model-store model-store --models resnet34=resnet34.mar

If I did not call model.eval() before saving the model using step 1), I get the following output when calling the prediction API with a cat image

 curl -X POST http://localhost:8080/predictions/resnet34 -T ./kitten.jpg
[
  {
    "bucket": 0.006668070796877146
  },
  {
    "hook": 0.0056739915162324905
  },
  {
    "pole": 0.004775158129632473
  },
  {
    "muzzle": 0.004324578680098057
  },
  {
    "plunger": 0.004318243823945522
  }
]

By changing how I save the model to

  1. Saving the model
from torchvision.models import resnet34
import torch
model = resnet34(pretrained=True)
model.eval() # <-- I call .eval() before saving
example_input = torch.rand(1, 3, 224, 224)
traced_script_module = torch.jit.trace(model, example_input)
traced_script_module.save("./resnet34.pt")

I finally get the correct score

[
  {
    "tiger_cat": 0.732546329498291
  },
  {
    "tabby": 0.2413433939218521
  },
  {
    "Egyptian_cat": 0.022546762600541115
  },
  {
    "plastic_bag": 0.0006128044333308935
  },
  {
    "tiger": 0.000588445458561182
  }
]

Thank you.

Best Regards,

Francesco Saverio Zuppichini

@FrancescoSaverioZuppichini : Thanks for pointing this out. This looks like an issue with PyTorch itself for tracing a model and we had simply followed the torchscript documentation for the examples. Also looks like the output got overlooked at our end for traced mode.

Could you put up a PR with the above suggested change in the examples.

harshbafna avatar May 19 '20 10:05 harshbafna

@harshbafna Thank you for your reply. Sure, I will change the tracing example, I am not sure if this problem applies also to the normal and scripting example. However, is it weird? In the end model.eval() is called correctly in the handler, in your opinion, why the model did not switch to evaluation mode?

@harshbafna Thank you for your reply. Sure, I will change the tracing example, I am not sure if this problem applies also to the normal and scripting example. However, is it weird? In the end model.eval() is called correctly in the handler, in your opinion, why the model did not switch to evaluation mode?

You will need to take this question to the PyTorch development team. I tried this outside torchserve as well and the result remains the same for traced model. This works fine without model.eval() for scripted models

harshbafna avatar May 19 '20 11:05 harshbafna

In your opinion, is it better to use the scripting or the scripting API?

In your opinion, is it better to use the scripting or the scripting API?

@FrancescoSaverioZuppichini This is not the correct forum to discuss torch APIs. I would recommend asking this question at https://discuss.pytorch.org/.

codinnvrends avatar May 19 '20 12:05 codinnvrends

@dhaniram-kshirsagar Thank you for your reply. I was just asking for a feedback to @harshbafna based on the discussion. If I would have wanted the general opinion of the community I would have asked there :)

In your opinion, is it better to use the scripting or the scripting API?

I don't think so. the same performance issue happened to me when using torch.jit.script I had to call model.eval() before hand.

OmniaZayed avatar Jul 11 '24 14:07 OmniaZayed