Compile coreml model and return the compiled model path.
(model, model_name="main", out_dir=".")
| 169 | |
| 170 | |
| 171 | def compile_coreml(model, model_name="main", out_dir="."): |
| 172 | """Compile coreml model and return the compiled model path.""" |
| 173 | mlmodel_path = os.path.join(out_dir, model_name + ".mlmodel") |
| 174 | mlmodelc_path = os.path.join(out_dir, model_name + ".mlmodelc") |
| 175 | metadata = {"inputs": list(model.input_description), "outputs": list(model.output_description)} |
| 176 | # Use the description field to send info to CoreML runtime |
| 177 | model.short_description = json.dumps(metadata) |
| 178 | model.save(mlmodel_path) |
| 179 | |
| 180 | res = xcrun(["coremlcompiler", "compile", mlmodel_path, out_dir]) |
| 181 | if not os.path.isdir(mlmodelc_path): |
| 182 | raise RuntimeError(f"Compile failed: {res}") |
| 183 | |
| 184 | return mlmodelc_path |