I see example https://docs.djl.ai/jupyter/load_pytorch_model.html
I made my own torchscript
witch named qooModel.pt
file and synset.txt
I tried to imitate the example. Here is my code.
val translator = ImageClassificationTranslator.builder()
.setPipeline(Pipeline().apply {
add(Resize(128))
add(ToTensor())
})
.optSynsetArtifactName("synset.txt")
.build()
val criteria = Criteria.builder()
.setTypes(Image::class.java, Classifications::class.java)
.optApplication(Application.CV.IMAGE_CLASSIFICATION)
.optModelPath(Paths.get("models/qooModel.pt"))
.optTranslator(translator)
.build()
val imgFactory = ImageFactory.getInstance()
val imgPath = Paths.get(args.getOrElse(0){"./images/qoo_learning.png"})
val image = imgFactory.fromFile(imgPath)
val model = ModelZoo.loadModel(criteria)
val predict = model.newPredictor()
val result = predict.predict(image)
print(result)
model.close()
My code does not makes error, but forwarded value in result
looks strange.
I expect
[
{"class": "qoo", "probability": 0.9597}
]
but
[
{"class": "qoo", "probability": 0.00463}
]
printed.
I exepect 0.9597
because when I running under Python code in Google Colab
import torch
from PIL import Image
import torchvision.transforms as T
tensor_transform = T.Compose([T.PILToTensor()])
image = Image.open("qoo_learning.png").convert("RGB").resize((128, 128))
img_tensor = tensor_transform(image).type(torch.FloatTensor)
img_tensor = torch.unsqueeze(img_tensor, 0)
model(img_tensor)
the result of code show me tensor([[0.9597]], grad_fn=<SigmoidBackward0>)
Questions.
Q1. Why I cannot get correct result (likes 0.9597) in my Kotlin code?
Q2. What should I do for get correct result?
My guess
G1. there some issue between pytorch version between 2.0.1(I write torchscript) and 1.13.1(Kotlin use)
G2. I should use pytorch 2.0.1 in my kotlin code ....unfortunately I tried to use torch 2.0.1 but I couldn't understand document
Explicitly specify pytorch-native-xxx package version to override the version in the BOM.
and I could not find example about that
or
G1. My code wrong. It load model but it does not load pre-trained weights.
G2. I should fix my code
....example https://docs.djl.ai/jupyter/load_pytorch_model.html looks like using pre-trained model
but I think my code is almost same.
My Develop Environment.
I make my torchscript file(qooModel.pt
) in Window 11, pytorch 2.0.1
I run my Kotlin code in macOS, I did not install torch by pip
... and it looks like running pytorch 1.13.1, look under debug log
[main] DEBUG ai.djl.pytorch.jni.LibUtils - Using cache dir: /Users/romeo/.djl.ai/pytorch/1.13.1-cpu-osx-aarch64
[main] DEBUG ai.djl.pytorch.jni.LibUtils - Loading native library: /Users/romeo/.djl.ai/pytorch/1.13.1-cpu-osx-aarch64/libc10.dylib
[main] DEBUG ai.djl.pytorch.jni.LibUtils - Loading native library: /Users/romeo/.djl.ai/pytorch/1.13.1-cpu-osx-aarch64/libtorch_cpu.dylib
[main] DEBUG ai.djl.pytorch.jni.LibUtils - Loading native library: /Users/romeo/.djl.ai/pytorch/1.13.1-cpu-osx-aarch64/libtorch.dylib
[main] DEBUG ai.djl.pytorch.jni.LibUtils - Loading native library: /Users/romeo/.djl.ai/pytorch/1.13.1-cpu-osx-aarch64/0.23.0-libdjl_torch.dylib
sorry about my poor english.