1

I'm getting a problem when building my ios app with CoreML model in it. I've used this example code for implementing coreML in my project. I can run the example code when cloning it from github with the given model in the repo, but when I paste in my own model I get an error:

lexical preprocessor issue
    source file is not valid UTF-8
        SSD_model.mlmodel

My model is an SSD model for generating bounding boxes, (and not just object detection like the repo's model), so that could make a problem maybe. I trained it using tensorflow object detection API, and exported it to coreML using this helpful blog post . I've verified that my model works well by testing it in another script made for SSD models. Here's a snippet from the first example how I import the model now:

#import <CoreML/CoreML.h>
#import <Vision/Vision.h>
#import "SSD_model.mlmodel"

- (void)viewDidLoad {
    [super viewDidLoad];

    model = [[[SSD_model alloc] init] model]
    m = [VNCoreMLModel modelForMLModel: model error:nil];

Is there anything I need to change to this objective C code to make it work? Or any project settings that I might be forgetting? Thanks!

1
  • source file is not valid UTF-8 sounds like an encoding issue which is not necessarily related to the model itself but to the file format. Did you copy the model from another platform? Jan 30, 2019 at 12:25

1 Answer 1

2

You are getting this error is because of this => #import "SSD_model.mlmodel"

Essentially the compiler tries to import some source code and instead finds something that is not.

You'll need to compile this model first in order to use it. You can compile it either via Xcode (it will autogenerate an interface for you) or by using APIs that compile the model in runtime (so you can update your models without needing to release a new version of the app).


Note: In the example you have linked, the author imports SomeModel.h (which I presume is the autogen header for ObjC) and not SomeModel.mlmodel. So I guess this was what you probably intended to do as well.

5
  • Thanks for pointing that out. After changing SSD_model import to .h, autogenerating it via xcode is still failing with error message: clang: error: linker command failed with exit code 1: Undefined symbols for architecture arm64: "_OBJC_CLASS_$_MLModel", referenced from: objc-class-ref in SSD_model.o Any thoughts on that? Jan 31, 2019 at 9:50
  • Did you link CoreML framework in your target settings?
    – Alladinian
    Jan 31, 2019 at 10:10
  • 1
    Tip: You can use @import CoreML; instead of #import <CoreML/CoreML.h> and if the setting for framework autolinking (CLANG_MODULES_AUTOLINK) is enabled (which is by default) the framework will be linked automatically for you.
    – Alladinian
    Jan 31, 2019 at 10:17
  • Did all you said and it worked!:) The last step of changing #import <CoreML/CoreML.h> to @import CoreML; , combined with changing settings to 'enable modules (under Apple Clang)' to Yes was necessary as well interestingly enough. Jan 31, 2019 at 10:49
  • Glad that helped you :)
    – Alladinian
    Jan 31, 2019 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.