pytorch / android-demo-app

PyTorch android examples of usage in applications
1.48k stars 607 forks source link

Error in Object Detection when changing the input image size to 256 #246

Open atultiwari opened 2 years ago

atultiwari commented 2 years ago

The object detection android app works if the input image size is 640 (mInputWidth & mInputHeight = 640).

But since I don't have a GPU configured computer and Google colab crashes when I train with the size of 640, so I reduced the input image size to 256. I am unable to understand what all changes do I need to make in the android app ( and/or export.py script) for the input size of 256.

When I tried with following (I have 5 classes mOutputColumn =10),

static int mInputWidth = 256;
static int mInputHeight = 256;

// model output is of size 25200*(num_of_class+5)
private static int mOutputRow = 25200; // as decided by the YOLOv5 model for input image of size 640*640
//private static int mOutputColumn = 85; // left, top, right, bottom, score and 80 class probability
private static int mOutputColumn = 10; // left, top, right, bottom, score and 5 class probability
private static float mThreshold = 0.30f; // score above which a detection is generated
private static int mNmsLimit = 15; 

I got following error -

Process: org.pytorch.demo.objectdetection, PID: 30780 com.facebook.jni.CppException: The size of tensor a (52) must match the size of tensor b (80) at non-singleton dimension 3

Sorry if it's very basic question, but I am stuck on this step since a few days now. Searched on issues section too, but I couldn't find the changes that are needed to be done for custom image size.

Edit - 1 In one the issue previously answered I found following reply -

for my yolov5n I've set mOutputRow=6300. with input image=320 it works just fine. I have found this number analysing my model using netron.ai

Originally posted by @masc-it in https://github.com/pytorch/android-demo-app/issues/233#issuecomment-1113271397

But I didnt understand it. I can open my model into Netron but dont know how to find mOutputRow by visualizing the model in Netron.

Thank you

masc-it commented 2 years ago

mOutputRow matches with the final layer of your model. All you need to check on Netron.ai is the shape of the final layer.

Process: org.pytorch.demo.objectdetection, PID: 30780 com.facebook.jni.CppException: The size of tensor a (52) must match the size of tensor b (80) at non-singleton dimension 3 Regarding the error above: you have to fix mOutputColumn according to the number of your classes. Comments on the code are quite explanatory.

atultiwari commented 2 years ago

mOutputRow matches with the final layer of your model. All you need to check on Netron.ai is the shape of the final layer.

Process: org.pytorch.demo.objectdetection, PID: 30780 com.facebook.jni.CppException: The size of tensor a (52) must match the size of tensor b (80) at non-singleton dimension 3 Regarding the error above: you have to fix mOutputColumn according to the number of your classes. Comments on the code are quite explanatory.

Thank you for your reply as you can see in the code i have pasted I have already fixed the mOutputColumn according to my no. of classes as mentioned in the comment. Since i have 5 classes, so I set mOutputColumn = 10.

The mentioned error doesn't occur if i use the default mInputWidth and mInputHeight (i.e.) 640, even though I had trained the model with input size of 416 and 256 in two different attempts. The error comes up when I change the mInputWidth and mInputHeight to 256 or 416.

Regarding checking the final layer for mOutputRow. I have attached the .ptl model and last layer properties.. in this the output shows 380, does that mean i need to set it to 380? or something else?

wbc-1

ss-last

underclocked commented 2 years ago

@atultiwari did you ever figure this out? I'm running into a similar issue.

NicholasZollo commented 2 years ago

To fix this issue, when running the export.py I set the --imgsz to the size that was used in training, then changed the mOutputRow and mOutputColumn after that (version I was on prints the model output size after exporting). By default it will export as a 640x640 model no matter what was used in training.

pdeubel commented 1 year ago

If somebody still has issues with a custom image size and custom dataset see https://github.com/pytorch/android-demo-app/issues/233#issuecomment-1374850143.