How to load a numpy file (.npy) in DJL?

83 Views Asked by At

I have traced a model in PyTorch and loaded it in successfully in DJL. Now I want to make sure pre/postprocessing works correctly in my JAVA server. I have written the result of the pre- and postprocessing into numpy files (.npy), e.g. with np.save('preprocessed.npy', some_tensor.detach().numpy())

How can I load the .npy file in JAVA/DJL into an NDArray to test the input/output of my traced model?

A solution using purely DJL would be preferred, but additional helper libraries are also a possibility.

1

There are 1 best solutions below

0
Christoph Henkelmann On

Thanks to the great help in the DJL Slack I found the following solution:

As of now (v0.23.0) it is possible via a workaround:

The method ai.djl.ndarray.NDSerializer.decodeNumpy provides this functionality. As it is package private, you need to either call it via reflection or create a new class like this as a workaround:

// this is important - we create our own class in the same package 
package ai.djl.ndarray;

import java.io.IOException;
import java.io.InputStream;

public class NDSerializerSpy {
    public static NDArray decodeNumpy(final NDManager manager, final InputStream is)
    throws IOException {
        return NDSerializer.decodeNumpy(manager, is);
    }
}

However, in the next release, DJL will officially support this via the NDArray.decode, NDManager.decode and NDList.decode methods. These methods will autodetect the underlying format and allow for a variety of formats.