Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting PyTorch Model to Torch Script #11

Closed
x12901 opened this issue Aug 30, 2021 · 4 comments
Closed

Converting PyTorch Model to Torch Script #11

x12901 opened this issue Aug 30, 2021 · 4 comments

Comments

@x12901
Copy link

x12901 commented Aug 30, 2021

I'm trying to convert pytorch to torch script.I want to know whether the detection speed of the converted model has changed. Neither method in the document can be used.
https://pytorch.org/tutorials/advanced/cpp_export.html

model = SPADE(k=42)  # , backbone_name="hypernet")
train_dataset = StreamingDataset()
test_dataset = StreamingDataset()
app_custom_train_images =  "D:\\cwge\\Dataset\\bottle\\train\\good"
# train images
for root, dirs, files in os.walk(app_custom_train_images):
    for file in files:
        train_dataset.add_pil_image(Image.open(os.path.join(root, file)))
model.fit(train_dataset)
PATH = "test.pth"
torch.save(model.state_dict(), PATH)
# model.load_state_dict(torch.load(PATH))
#example = torch.rand(1, 3, 224, 224).cuda()
#traced_script_module = torch.jit.trace(model, example)
script_net = torch.jit.script(model)
script_net.save('model_script.pt')
#traced_script_module.save("traced_resnet_model.pt")

My C + + code reported an error when loading the model.

#include <iostream>
#include "torch/script.h"
#include <vector>
int main()
{torch::jit::script::Module module = torch::jit::load("D:\\model_script.pt");}

Unhandled exception at 0x00007FFB76BA4B89 in ConsoleApplication1.exe: Microsoft C++ exception: c10::NotImplementedError at memory location 0x00000095C3EFDD50.

@rvorias
Copy link
Owner

rvorias commented Aug 30, 2021

Can you find out what is not implemented from the c10::NotImplementedError?

@x12901
Copy link
Author

x12901 commented Aug 30, 2021

Can you find out what is not implemented from the c10::NotImplementedError?
It stops at libtorch\include\ATen\core\dispatch\OperatorEntry.h

  const KernelFunction& lookup(DispatchKey k) const {
    const auto& kernel = dispatchTable_[static_cast<uint8_t>(k)];
    // A valid kernel *always* has a boxed kernel and *may* have an
    // unboxed kernel. However, we typically do unboxed calls in at::
    // APIs, where the kernel 1) will very likely be valid and 2)
    // should have an unboxed kernel. Checking the unboxed kernel
    // first will allow us to avoid touching the boxed kernel at all
    // in the common case.
    if (C10_UNLIKELY(!kernel.isValidUnboxed())) {
      if (!kernel.isValid()) {
        reportError(k);
      }
    }
    return kernel;
  }

@x12901 x12901 closed this as completed Sep 6, 2021
@ZacharyGao
Copy link

Did u figure out this problem? I'd rather to know that, cause I'm facing the same question as you. Thanks so much!

@ZacharyGao
Copy link

@x12901 Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants