abdelaziz-mahdy / pytorch_lite

flutter package to help run pytorch lite models classification and YoloV5 and YoloV8.
MIT License
49 stars 18 forks source link

Pytorch Issues #46

Closed tnghieu closed 9 months ago

tnghieu commented 9 months ago

I'm on the latest 4.2.0+2

The following operation failed in the TorchScript interpreter. Traceback of TorchScript, serialized code (most recent call last): File "code/torch/models/yolo/___torch_mangle1054.py", line 80, in forward input31 = torch.silu(_38) _39 = torch.upsample_nearest2d(input31, None, [2., 2.]) input32 = torch.cat([_39, input20], 1)


    _40 = ops.prepacked.conv2d_clamp_run(input32, CONSTANTS.c34)
    _41 = ops.prepacked.conv2d_clamp_run(input32, CONSTANTS.c35)

Traceback of TorchScript, original code (most recent call last):
/Users/me/github_repos/yolov5/models/common.py(324): forward
/Users/me/github_repos/yolov5/yoloEnv/lib/python3.9/site-packages/torch/nn/modules/module.py(1488): _slow_forward
/Users/me/github_repos/yolov5/yoloEnv/lib/python3.9/site-packages/torch/nn/modules/module.py(1501): _call_impl
/Users/me/github_repos/yolov5/models/yolo.py(121): _forward_once
/Users/me/github_repos/yolov5/models/yolo.py(209): forward
/Users/me/github_repos/yolov5/yoloEnv/lib/python3.9/site-packages/torch/nn/modules/module.py(1488): _slow_forward
/Users/me/github_repos/yolov5/yoloEnv/lib/python3.9/site-packages/torch/nn/modules/module.py(1501): _call_impl
/Users/me/github_repos/yolov5/yoloEnv/lib/python3.9/site-packages/torch/jit/_trace.py(1056): trace_module
/Users/me/github_repos/yolov5/yoloEnv/lib/python3.9/site-packages/torch/jit/_trace.py(794): trace
/Users/me/github_repos/yolov5/export.py(141): export_torchscript
/Users/me/github_repos/yolov5/export.py(125): outer_func
/Users/me/github_repos/yolov5/export.py(768): run
/Users/me/github_repos/yolov5/yoloEnv/lib/python3.9/site-packages/torch/utils/_contextlib.py(115): decorate_context
/Users/me/github_repos/yolov5/export.py(858): main
/Users/me/github_repos/yolov5/export.py(863): <module>
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 24 but got size 23 for tensor number 1 in the list.
PytorchLitePlugin result length before processing 0
PytorchLitePlugin result length after processing 0

I am using object detection and testing it on a single image.

*** SETUP ***
objectModel = await PytorchLite.loadObjectDetectionModel(
        "assets/models/weights640.torchscript",
        1,
        640,
        318,
        labelPath: "assets/labels/sharklabel.txt",
        objectDetectionModelType: ObjectDetectionModelType.yolov5,
      );

*** Execution ***
 var x = (await rootBundle.load('assets/labels/sharkpic.jpg'))
            .buffer
            .asUint8List();

        List<ResultObjectDetection> res =
            await objectModel.getImagePrediction(x);
abdelaziz-mahdy commented 9 months ago

Did you choose the right model?

tnghieu commented 9 months ago

My coworker is generating a .pt weights file made from a yoloV5 object detection model.

What's the best use of this library? I'm assuming it's using the PytorchLite.loadObjectDetectionModel().

I've tried both the .pt and the exported .torchscript versions and they don't work as the model to load.

abdelaziz-mahdy commented 9 months ago

!python export.py --weights "the weights of your model" --include torchscript --img 640 --optimize

this is using yolov5 repo for example

tnghieu commented 9 months ago

Yes I used that to convert the .pt to torch script.

abdelaziz-mahdy commented 9 months ago

That's weird the model I have works, and I am using latest version of pytorch mobile, maybe because it's a pytorch v2 model?

tnghieu commented 9 months ago

What does 640 represent in the --img flag? Height or Width?

If my images are 640 width by 480 height, what should I use?

Also for Model setup as well: await PytorchLite.loadObjectDetectionModel( "assets/models/weights1280.torchscript", 1, 640, 640, labelPath: "assets/labels/sharklabel.txt", objectDetectionModelType: ObjectDetectionModelType.yolov5, );

abdelaziz-mahdy commented 9 months ago

640 represents model input size, width and height, for yolov5 it's the same, it doesn't matter your image width and height, the code will resize it to work with your model

tnghieu commented 9 months ago

Thanks, I have it working now. The issue was with these dimensions. Closing this.