Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export of cls model #41

Open
JulieBender opened this issue Jul 21, 2024 · 3 comments
Open

Export of cls model #41

JulieBender opened this issue Jul 21, 2024 · 3 comments

Comments

@JulieBender
Copy link

When exporting a custom yolov8 cls model, the classification doesn't run in the app, it just opens the camera as have no detections. also says 0.0ms - 0.0FPS. I have tested the model in python code, and here it works just fine.

I have used this for exporting in a python script: model.export(format="mlmodel", imgsz=[320, 192], nms=True)

I get this error inside med x-code:

Thread 5: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=com.apple.mlassetio Code=1 "Failed to parse the model specification. Error: Unable to parse ML Program: at unknown location: Could not open /var/mobile/Containers/Data/Application/0157029F-2294-42DF-9422-4E1E0C40F43A/Library/Application Support/assets/weights/weight.bin" UserInfo={NSLocalizedDescription=Failed to parse the model specification. Error: Unable to parse ML Program: at unknown location: Could not open /var/mobile/Containe

You didn't provide any specific code snippets for exporting the cls models to ios in the readme section (as you did with all the other options). I assume that this error is because of a wrong model, as the app works fine with your model from the example.

I also tried to convert your yolov8m-cls, but that didn't work either.

@pderrenger
Copy link
Member

@JulieBender hi there,

Thank you for reaching out and providing detailed information about the issue you're encountering with exporting and running your custom YOLOv8 classification model in an iOS app.

It seems like the error you're experiencing is related to the model file not being correctly parsed or located by the app. Here are a few steps you can take to troubleshoot and potentially resolve this issue:

  1. Verify Model Export: Ensure that the model export process completes successfully and that the exported model files are correctly placed in the expected directory. You can try exporting the model again with the following command:

    from ultralytics import YOLO
    
    # Load your custom model
    model = YOLO("path/to/your/custom_model.pt")
    
    # Export the model to CoreML format
    model.export(format="coreml", imgsz=[320, 192], nms=True)
  2. Check File Paths: The error message indicates that the app is unable to open the weight.bin file. Ensure that all necessary files are correctly bundled with your app and that the paths are correctly specified. Double-check the file paths in your Xcode project to make sure they match the locations of the exported model files.

  3. Test with Pretrained Model: Since you mentioned that the app works fine with the example model, try using a pretrained YOLOv8 classification model to see if the issue persists. This can help determine if the problem is specific to your custom model or a more general issue with the export process:

    from ultralytics import YOLO
    
    # Load a pretrained model
    model = YOLO("yolov8n-cls.pt")
    
    # Export the pretrained model to CoreML format
    model.export(format="coreml", imgsz=[320, 192], nms=True)
  4. Update Packages: Ensure that you are using the latest versions of the Ultralytics package and other dependencies. Sometimes, issues are resolved in newer releases, so it's always a good idea to keep your packages up to date.

  5. Consult Documentation: For additional details on exporting models, you can refer to the Export section of the Ultralytics documentation.

If the issue persists after trying these steps, please let us know. Providing any additional error messages or logs can also help us diagnose the problem more effectively.

Thank you for your patience and for being a part of the YOLO community! 😊

@JulieBender
Copy link
Author

I have already tried steps 1,3,4,5

Maybe I don't put the files in the right places, can you guide me on where to place the bin file, and specify it? In the example, I can't find your bin file or the path to it in the scripts. I also looked trough the documentation and can't really find any guides to the placement of these.

  1. Check File Paths: The error message indicates that the app is unable to open the weight.bin file. Ensure that all necessary files are correctly bundled with your app and that the paths are correctly specified. Double-check the file paths in your Xcode project to make sure they match the locations of the exported model files.

I have this LocalModel, and if I switch my custom cls model out with yours it works fine. my mlmodel is placed in the assets folder. Is it necessary to specify the bin file aswell?

Future<ImageClassifier> _initObjectClassifierWithLocalModel() async { final modelPath = await _copy('assets/model.mlmodel'); final model = LocalYoloModel( id: '', task: Task.classify, format: Format.coreml, modelPath: modelPath, );

@pderrenger
Copy link
Member

Hi @JulieBender,

Thank you for providing additional details. It sounds like you're on the right track, and I appreciate your efforts in troubleshooting the issue. Let's address the specific concerns regarding the placement and specification of the weight.bin file.

File Placement and Specification

When exporting a YOLOv8 model to CoreML format, the exported model typically includes multiple files, such as the .mlmodel and associated weight files (e.g., weight.bin). These files need to be correctly bundled with your app and referenced appropriately.

Here are some steps to ensure proper placement and specification:

  1. Bundle All Exported Files: Ensure that all files generated during the export process (e.g., model.mlmodel, weight.bin) are included in your Xcode project. Place them in the same directory within your app's bundle.

  2. Update Xcode Project: Add the exported files to your Xcode project. You can do this by dragging and dropping the files into the appropriate group in your Xcode project navigator. Ensure that the files are included in the app target.

  3. Specify File Paths in Code: When initializing the model in your app, ensure that the paths to the model and weight files are correctly specified. Here's an example of how you might do this in your Flutter app:

    Future<ImageClassifier> _initObjectClassifierWithLocalModel() async {
        // Copy the model file to a location accessible by the app
        final modelPath = await _copy('assets/model.mlmodel');
        
        // Ensure the weight file is also copied if necessary
        final weightPath = await _copy('assets/weights/weight.bin');
        
        // Initialize the LocalYoloModel with the correct paths
        final model = LocalYoloModel(
            id: '',
            task: Task.classify,
            format: Format.coreml,
            modelPath: modelPath,
            weightPath: weightPath,  // Specify the weight file path if required
        );
        
        return model;
    }
    
    Future<String> _copy(String assetPath) async {
        final byteData = await rootBundle.load(assetPath);
        final file = File('${(await getApplicationDocumentsDirectory()).path}/$assetPath');
        await file.create(recursive: true);
        await file.writeAsBytes(byteData.buffer.asUint8List());
        return file.path;
    }
  4. Verify File Paths: Double-check the paths to ensure they match the locations of the files within your app bundle. The weightPath should point to the correct location of the weight.bin file.

Additional Considerations

  • Latest Versions: Ensure you are using the latest versions of the Ultralytics package and other dependencies. This can help avoid issues that may have been resolved in newer releases.
  • Model Compatibility: Verify that the custom model you are using is compatible with the CoreML format and that it has been exported correctly.

If you continue to experience issues, please provide any additional error messages or logs that may help diagnose the problem further.

Thank you for your patience and for being a part of the YOLO community! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants