apple / turicreate

Turi Create simplifies the development of custom machine learning models.
BSD 3-Clause "New" or "Revised" License
11.19k stars 1.14k forks source link

DecodeError when exporting CoreML model #3404

Open ricmarlene opened 3 years ago

ricmarlene commented 3 years ago

I can't export .mlmodel while it's possible to use save() to save model file.

Here's the code and Error message:

model.export_coreml('myclassifier4.mlmodel')
 ---------------------------------------------------------------------------
DecodeError                               Traceback (most recent call last)
<ipython-input-22-2752c9274acc> in <module>
----> 1 model.export_coreml('myclassifier4.mlmodel')

~/opt/anaconda3/envs/turi/lib/python3.8/site-packages/turicreate/toolkits/image_classifier/image_classifier.py in export_coreml(self, filename)
   1109                 ptModel
   1110             )
-> 1111             coreml_model = feature_extractor.get_coreml_model()
   1112             spec = coreml_model.get_spec()
   1113             nn_spec = spec.neuralNetworkClassifier

~/opt/anaconda3/envs/turi/lib/python3.8/site-packages/turicreate/toolkits/_image_feature_extractor.py in get_coreml_model(self)
    221 
    222         model_path = self.ptModel.get_model_path("coreml")
--> 223         return coremltools.models.MLModel(model_path)

~/opt/anaconda3/envs/turi/lib/python3.8/site-packages/coremltools/models/model.py in __init__(self, model, useCPUOnly)
    193 
    194         if isinstance(model, _string_types):
--> 195             self.__proxy__, self._spec, self._framework_error = _get_proxy_and_spec(
    196                 model, useCPUOnly
    197             )

~/opt/anaconda3/envs/turi/lib/python3.8/site-packages/coremltools/models/model.py in _get_proxy_and_spec(filename, use_cpu_only)
     99 
    100     filename = _os.path.expanduser(filename)
--> 101     specification = _load_spec(filename)
    102 
    103     if _MLModelProxy:

~/opt/anaconda3/envs/turi/lib/python3.8/site-packages/coremltools/models/utils.py in load_spec(filename)
    110     with open(filename, "rb") as f:
    111         contents = f.read()
--> 112         spec.ParseFromString(contents)
    113         return spec
    114 

DecodeError: Error parsing message 
TobyRoseman commented 3 years ago

I'm not sure what's going on here. Are you using the most recent version of TuriCreate? What version of coremltools is installed in your environment?

Can you share your model? i.e can you call model.save(<path>) and share that file?

ricmarlene commented 3 years ago

Thank you for your response!

  1. TuriCreate version is 6.4.1
  2. coremltools version is 4.0b3
  3. Yes, I'll attach my model file below.

turi_garbage_4.model.zip

For more information, I tried to update my coremltools to 4.1 after I post this issue and it doesn't work as well.

TobyRoseman commented 3 years ago

Using your model I'm able to run model.export_coreml('myclassifier4.mlmodel') just fine. My version of TuriCreate and coremltools is the same.

What OS are you using? Does this happen every time you try to run model.export_coreml('myclassifier4.mlmodel')?

ricmarlene commented 3 years ago

Sorry for the late reply. I tested the same code many times last week (in different anaconda environment). Weird thing happened that the same code run just fine yesterday. It seems to be a problem with the pre-trained resnet-50 model downloading process when calling model = tc.load_model('garbage_v4'). After days my computer clean the cached pre-trained resnet-50 model and it downloads again.

  1. I'm using macOS Big Sur 11.3.1 (20E241).
  2. Yes, Decode Error happened every time before. Now code runs well except two warning:
    WARNING:root:TensorFlow version 2.5.0 detected. Last version known to be fully compatible is 2.2.0 .
    WARNING:root:Keras version 2.5.0 detected. Last version known to be fully compatible of Keras is 2.2.4 .

I'd like to know if I occur the same problem next time, what should I do to fix it? Thank you for the discuss!

TobyRoseman commented 3 years ago

Those two warnings are from coremltools not TuriCreate. They aren't relevant for this issue and can be ignored here.

I suspect the pretrained ResNet-50 model was not downloaded correctly. If you have this issue in the future, I would recommend clearing the Turi file cache. To do that get the following value: tc.config.get_runtime_config()['TURI_CACHE_FILE_LOCATIONS'] Then delete everything underneath it or at least the ResNet files.

ricmarlene commented 3 years ago

Thank you for your response. It helps me a lot! I guess the model wasn’t downloaded correctly because of the network connection with GitHub in China. Maybe I didn't set up the proxy correctly, which resulted in a poor network status.