deepjavalibrary / djl

An Engine-Agnostic Deep Learning Framework in Java
https://djl.ai
Apache License 2.0
4.07k stars 648 forks source link

Native resource has been release already #1064

Closed endlesshh closed 3 years ago

endlesshh commented 3 years ago

Description

I use the first-order-model to predict,they have tow models kp_detector and generator, fisrt I user have save the model named kp_detector.pt , when i load the file and predict,it return the error. (use PyTorch as default_engine,just cup not cudart)

Expected Behavior

renturn the jacobian

Error Message

Exception in thread "main" java.lang.IllegalStateException: Native resource has been release already. at ai.djl.util.NativeResource.getHandle(NativeResource.java:51) at ai.djl.pytorch.jni.JniUtils.getShape(JniUtils.java:1318) at ai.djl.pytorch.engine.PtNDArray.getShape(PtNDArray.java:131) at ai.djl.ndarray.NDList.toString(NDList.java:284) at java.lang.String.valueOf(String.java:2994) at java.io.PrintStream.println(PrintStream.java:821)

How to Reproduce?

Translator<Image, NDList>

  @Override
    public NDList processInput(TranslatorContext ctx, Image input) { 
        NDArray img = input.toNDArray(ctx.getNDManager()); 
        img = NDImageUtils.resize(img, 255, 255); 
        img = img.transpose(2, 0, 1); 
        img = img.toType(DataType.FLOAT32,false);
        return new NDList(img);
    }
    @Override
    public NDList processOutput(TranslatorContext ctx, NDList list) { 
        return list; 
    }

Environment Info

DJL 0.11.0 version

frankfliu commented 3 years ago

This is expected behavior.

The NDList created in Translator will be destroyed right after the .predict(). We usually don't recommend to return NDList from translator, because you need explicitly close the NDList after you finished using it.

You have a few options here:

  1. return float[] or plain java object, you can do all necessary post porcessing n the translator and return java data type
  2. You can call NDList.detach() function before you return it. But you must make sure you close the NDList manually, otherwise, you will cause memory leak.
endlesshh commented 3 years ago

I got it ,Thanks a lot