-
Notifications
You must be signed in to change notification settings - Fork 607
Android App crash after click DETECT button #102
Comments
cc @jeffxtang |
Looks like your model's output is a tensor list instead of a tuple that the model that comes with the demo app outputs. Below is from IValue.java which you can enter from Android Studio by holding down the Cmd key (on Mac) and click the
You need to make the output of your model the same type as the output of the model used for the demo app. |
First I downloaded the repository zip from https://github.com/jeffxtang/yolov5 and extract it then I open the anaconda prompt python models/export.py --weights best.pt --img 512 --batch 16 After showing some error messages then it shows that 'TorchScript export success, saved as best.torchscript.pt'. Need solution for this problem. @jeffxtang |
We don't know how your custom model is trained, but most likely your custom model inference result |
Actually I train my model in kaggle notebook. After finishes training best.pt file is generated from the output. If my custom model inference result y is a list then is there any way to convert to tuple? If you want I can share my custom trained model best.pt file with you and then you see and give me some solutions to integrate it in this app. @jeffxtang |
The reasons you see
The error you see when running on Android (BTW, what you see when running the export.py is not error but warning messages) is because your model's forward method output a list of tensors instead of a tuple. You can either change your model code or in Android MainActivity.java, change |
I train my model in the 640 image size then after running this command 'python models/export.py --weights best.pt --img 640 --batch 16', I see torch.Size([16, 25200, 26]) and I change the following values in PrePostProcessor.java to :
And also change in Android MainActivity.java, from IValue[] outputTuple = mModule.forward(IValue.from(inputTensor)).toTuple(); to IValue[] outputList = mModule.forward(IValue.from(inputTensor)).toList(); but again the same type of error is showing. |
If your model's output is a tensor list then So looks like you have changed your model's output type - then you should change your Android code back to If you still have the problem, you can send a downloadable link to your model file and I'll give it a try. |
It works. Thank you. Now I am going to check the camera view portion. @jeffxtang |
Glad it works for you! What was the cause and fix for the problem? |
|
I export my best.pt model file to best.torchscript.pt as img size 512(torch.Size([16, 16128, 26]). Then I integrate it in the app. I change the following values in PrePostProcessor.java to : // model output is of size 1612826 And also change in Android MainActivity.java, from IValue[] outputTuple = mModule.forward(IValue.from(inputTensor)).toTuple(); to IValue[] outputList = mModule.forward(IValue.from(inputTensor)).toList(); but an error is showing which I upload it before https://drive.google.com/file/d/1yh1uycn-40TXKFd8z9cOIInJRzRFYt2n/view?usp=sharing |
This is a similar but different error message - you used to have "expected IValue type 7, actual type 11" and "expected IValue type 12, actual type 7" and now you're having "expected IValue type 12, actual type 11", meaning your model's output is now
So you need to change |
When I change from toList() to toTensorList(). There is a red line shown under this line. @jeffxtang Then I change it to: Tensor[] outputList = mModule.forward(IValue.from(inputTensor)).toTensorList(); and there is a red color in line no. 224 toTensor() |
You need to find out the shape of your model's outputList - how many tensors are in it? What's the size of outputTensor (outputList[0])? Is the first element the right output to pass to the method in PrePosProcessor? |
@navidnayyem @jeffxtang I encounter the same error
After adding the code above, this problem went away. |
I love you all! Thanks, I had the same issue |
I also faced the same issue of ArrayIndexOutBoundException even though I was using 3 classes and also I had mOutputColumn =8. I did some analysis and found that the problem was I was running the export file with --batch 1 so |
hello, I am using the same method of the error message here: E/AndroidRuntime: FATAL EXCEPTION: main |
Hello,if torch.Size([1, 80, 80,15]), not 3 dimensin,how do? Thanks @jeffxtang |
It works for me. |
@navidnayyem I guess you were able to use this object detection app with img-size other 640, would you provide some suggestions on this issue 233? Thanks! |
Hi As mentioned by you, I tried to print the shape of "a" (attached screenshot), I got - From my understanding from the mentioned comment is that the 2nd number decides mOutputRow (25200) and 3rd number decides mOutputColumn (since i have 5 classes so it's value would be = 10). (Please correct me if I interpreted it wrong) I don't know what does first number decides from this shape (value of which is 1 in my case, while it was 16 in the mentioned comment). If you help me understand it, that would be a great help. Also, in my case I trained my model with image size of 320, then why I am getting 2nd value in |
If somebody still has issues with a custom image size and custom dataset see my comment in another issue. |
Hello, I have a different error on my end. Actually, we are looking for a new solution. |
Hello, according to you,changed IValue[] outputTuple = mModule.forward(IValue.from(inputTensor)).toTuple(); to IValue[] outputList = mModule.forward(IValue.from(inputTensor)).toList();
What should I do? |
We are working on a custom dataset model and also trained our model in a kaggle notebook. From the kaggle notebook, we got our model weights file in the outputs which is best.pt and last.pt after finishes training. Now, we are trying to integrate best.pt file into the pytorch android demo app (Github Link: https://github.com/pytorch/android-demo-app/tree/master/ObjectDetection). I converted best.pt file to best.torchscipt.pt by following the link (ultralytics/yolov5#251) and after export I copy it to asset file and also changes the name of our classes in the classes.txt file. The problem is that first of all, the apps runs fine but when I click on the DETECT button after sometime ,the app closes and shut down. I then watch the logcat of Android Studio and see some error messages which I attached it below. Actually, we want a solution for this error.
The text was updated successfully, but these errors were encountered: