Expected Tuple but got String

I had converted PyTorch model to torch script which uses image and meta-data. When I tried to deploy on android, get the above Error. I am expecting someone who can suggest the cause of the error and the possible solution

This is the code I had used

DetectButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            int Moisture = Integer.parseInt(txt.getText().toString());
            String odor = ked.getSelectedItem().toString();
            String insects = ked1.getSelectedItem().toString();


            int[] meta = new int[5];


            if (insects == "Yes") {
                meta [0] = 1;
            } else {
                meta [1] = 1;
            }

            meta [2] = Moisture;

            if (odor == "Yes") {
                meta [3] = 1;
            } else {
                meta [4] = 1;
            }

            Bitmap bitmap = null;
            Module module = null;

            //Getting the image from the image view
            ImageView imageView = (ImageView) findViewById(R.id.image);

            try {
                //Read the image as Bitmap
                bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

                //Here we reshape the image into 400*400
                bitmap = Bitmap.createScaledBitmap(bitmap, 224, 224, true);

                //Loading the model file.
                module = Module.load(fetchModelFile(NewActivity.this, "sub.pt"));
            } catch (IOException e) {
                finish();
            }

            //Input Tensor
            Tensor input2 = Tensor.fromBlob(meta, new long[]{1, meta.length});
            Tensor input1 = TensorImageUtils.bitmapToFloat32Tensor(
                    bitmap,
                    TensorImageUtils.TORCHVISION_NORM_MEAN_RGB,
                    TensorImageUtils.TORCHVISION_NORM_STD_RGB
            );



            //Calling the forward of the model to run our input
            assert module != null;
            final Tensor output = module.forward(IValue.from(input1), IValue.from(input2)).toTensor();


            final float[] score_arr = output.getDataAsFloatArray();

            // Fetch the index of the value with maximum score
            float max_score = -Float.MAX_VALUE;
            int ms_ix = -1;
            for (int i = 0; i < score_arr.length; i++) {
                if (score_arr[i] > max_score) {
                    max_score = score_arr[i];
                    ms_ix = i;
                }
            }

            //Fetching the name from the list based on the index
            String detected_class = MODELCLASSES.MODEL_CLASSES[ms_ix];

            //Writing the detected class in to the text view of the layout
            TextView textView = findViewById(R.id.result_text);
            textView.setText(detected_class);


        }
    });

}

The error messege:

2023-05-08 01:13:08.000 2549-2549/com.example.skinn A/libc: /buildbot/src/android/ndk-release-r21/external/libcxx/…/…/external/libcxxabi/src/abort_message.cpp:72: abort_message: assertion “terminating with uncaught exception of type c10::Error: isTuple() INTERNAL ASSERT FAILED at “…/…/…/…/src/main/cpp/libtorch_include/armeabi-v7a/ATen/core/ivalue_inl.h”:931, please report a bug to PyTorch. Expected Tuple but got String
Exception raised from toTuple at …/…/…/…/src/main/cpp/libtorch_include/armeabi-v7a/ATen/core/ivalue_inl.h:931 (most recent call first):
(no backtrace available)” failed
2023-05-08 01:13:08.001 2549-2549/com.example.skinn A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 2549 (m.example.skinn), pid 2549 (m.example.skinn)

Thank you All those who read the error tried to figureout the error. I already fixed the problem by installing the same dependency.

Hello, I have somehow similar error. I try to make inference on RedisAI using exported torchscript yolov5 model, but got;

isTuple()INTERNAL ASSERT FAILED at “…/aten/src/ATen/core/ivalue_inl.h”:1916, please report a bug to PyTorch. Expected Tuple but got String Exception raised from toTupleRef at …/aten/src/ATen/core/ivalue_inl.h:1916 (most recent call first): frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits, std::allocator >) + 0x6b (0x7f02fbb747ab in /home/robb/Desktop/nn/RedisAI/deps/linux-x64-cpu/libtorch/lib/libc10.so) frame #1: c10::detail::torchCheckFail(char const*, char const*, unsigned int, std::__cxx11::basic_string<char, std::char_traits,…

Can you share code snippet

Have you deployed the model on devices or you try the inference in python environment

1 Like

Thank you for reply.

Sure, here it is;

from redisai import Client as rai
import numpy as np
import cv2

model_path = 'yolov5/runs/train/results_15/weights/best.torchscript'

# Connect to Redis
redis_client = rai(host='localhost', port=6379)

img = '/home/robb/Desktop/.../nn/YOLOv5_cars/yolov5/data/images/bus.jpg'
img = cv2.imread(img)
img = np.transpose(img, (2, 0, 1))
img = img[None, :].astype(np.float32) / 255.0  # adds batch size (1, 3, 1080, 810)


with open(model_path, 'rb') as f:
    model = f.read()


redis_client.modelstore('willitwork', 'torch', 'CPU', model)

redis_client.tensorset('my_tensor', img)

redis_client.modelexecute('willitwork', ['my_tensor'], ['output'])  # <-- This line

output_tensor = redis_client.tensorget('output')
output_data = np.array(output_tensor)

I tried also with pt and onnx but modelstore method did not accepted these.

I have same issue. Which dependency did you install to fix the problem?

What was missing for you that you installed? Having the same issue

1 Like

I fixed this by installing the latest pytorch android version 13.1
https://mvnrepository.com/artifact/org.pytorch/pytorch_android

What fixed this issue for me is to use optimize_for_mobile to transform the model before saving the script file.