pytorch / android-demo-app

PyTorch android examples of usage in applications
1.45k stars 596 forks source link

Pytorch lite version problem with error in android app #275

Open Jagan3534 opened 1 year ago

Jagan3534 commented 1 year ago

for our custom .ptl model we got error in this Ivalue

for this line code we we got this output

Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,TensorImageUtils.TORCHVISION_NORM_MEAN_RGB,TensorImageUtils.TORCHVISION_NORM_STD_RGB);

the output of this line

Tensor([1, 3, 224, 224], dtype=torch.float32)

from next line onwards we are facing error

Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
float[] scores = outputTensor.getDataAsFloatArray();

and

IValue inputs = IValue.from(outputTensor);
IValue[] outputs = module.forward(inputs).toTuple();

i tried in both ways

i got the error like this

java.lang.IllegalStateException: Expected IValue type Tuple, actual type TensorList
at org.pytorch.IValue.preconditionType(IValue.java:332)
at org.pytorch.IValue.toTuple(IValue.java:313)

if anyone solve this error please help us

aravinthk00 commented 1 year ago

Please try that below lines , It's work for me.

final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(resizedBitmap, PrePostProcessor.NO_MEAN_RGB, PrePostProcessor.NO_STD_RGB); IValue[] outputTuple = mModule.forward(IValue.from(inputTensor)).toTuple(); final Tensor outputTensor = outputTuple[0].toTensor(); final float[] outputs = outputTensor.getDataAsFloatArray();

Thanks..