How to use module.foward(Ivalue).toTensor()

My code:

final Tensor img1 = TensorImageUtils.bitmapToFloat32Tensor(bitmap1,
                    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);
            final Tensor img2 = TensorImageUtils.bitmapToFloat32Tensor(bitmap2,
                    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB);

            IValue img = IValue.listFrom(img1, img2);
            final Tensor outputTensor = module.forward(img).toTensor();

error:

In fact, there are two pictures in my inputs. At first, I used torch.cat to splice on dim = 0 in pytorch. Later, I found that cat operation is not supported in Android. Instead, I changed the model to receive a list [tensor], read list [0] and list [1] respectively in the model, and then splice in the model. I noticed that the input type of forward can be an Ivalue, and the listfrom of Ivalue seems to support the conversion of list [tensor] to Ivalue, so I don’t understand what the problem is.My model input is two pictures . How should I implement this in Android, or how can I implement the torch.cat operation in Android?

from the error message your model expects a single Tensor as input, instead of a list of tensors. Try

module.forward(img1)

In fact, there are two pictures in my inputs. At first, I used torch.cat to splice on dim = 0 in pytorch. Later, I found that cat operation is not supported in Android. Instead, I changed the model to receive a list [tensor], read list [0] and list [1] respectively in the model, and then splice in the model. I noticed that the input type of forward can be an Ivalue, and the listfrom of Ivalue seems to support the conversion of list [tensor] to Ivalue, so I don’t understand what the problem is.My model input is two pictures. How should I implement this in Android, or how can I implement the torch.cat operation in Android

What is the output of your model? Maybe the type error is caused by output. Try

auto output = module.forward(img);
IValue[] outputTuple = output.toTuple(); 
Tensor outputTensor = outputTuple[0].toTensor();
...

Break the forward statement into two lines for easier debugging.

1 Like

I have solved this problem. Thank you.

Do you know some methods similar to torch. Matmul in Android?

I don’t think there’s Java API for that. But you can use C++ frontend: Namespace torch::linalg — PyTorch master documentation

@gohna i have the same problem, my model accept 2 images, could you help me?

This is how to use a model taking a list of images