Utility function to split a mlpackage model into two mlpackages of approximately same file size. Parameters ---------- model: str or MLModel Path to the mlpackage file, or a Core ML model, to be split into two mlpackages of approximately same file size. output_dir: str
(
model: _Union[str, "_ct.models.MLModel"],
output_dir: str,
merge_chunks_to_pipeline: _Optional[bool] = False,
check_output_correctness: _Optional[bool] = True,
)
| 1865 | |
| 1866 | |
| 1867 | def bisect_model( |
| 1868 | model: _Union[str, "_ct.models.MLModel"], |
| 1869 | output_dir: str, |
| 1870 | merge_chunks_to_pipeline: _Optional[bool] = False, |
| 1871 | check_output_correctness: _Optional[bool] = True, |
| 1872 | ): |
| 1873 | """ |
| 1874 | Utility function to split a mlpackage model into two mlpackages of approximately same file size. |
| 1875 | |
| 1876 | Parameters |
| 1877 | ---------- |
| 1878 | model: str or MLModel |
| 1879 | Path to the mlpackage file, or a Core ML model, to be split into two mlpackages of approximately same file size. |
| 1880 | |
| 1881 | output_dir: str |
| 1882 | Path to output directory where the two model chunks / pipeline model would be saved. |
| 1883 | |
| 1884 | If the `model` is `{path}/{model_name}.mlpackage`, the chunk models are going to be saved as: |
| 1885 | 1. first chunk model: `{output_dir}/{model_name}_chunk1.mlpackage` |
| 1886 | 2. second chunk model: `{output_dir}/{model_name}_chunk2.mlpackage` |
| 1887 | 3. chunked pipeline model: `{output_dir}/{model_name}_chunked_pipeline.mlpackage` |
| 1888 | |
| 1889 | If the `model` is type of `MLModel`, the chunk models are saved as: |
| 1890 | 1. first chunk model: `{output_dir}/chunk1.mlpackage` |
| 1891 | 2. second chunk model: `{output_dir}/chunk2.mlpackage` |
| 1892 | 3. chunked pipeline model: `{output_dir}/chunked_pipeline.mlpackage` |
| 1893 | |
| 1894 | merge_chunks_to_pipeline: bool |
| 1895 | If True, model chunks are managed inside a single pipeline model for easier asset maintenance. |
| 1896 | |
| 1897 | check_output_correctness: bool |
| 1898 | - If True, compares the outputs of original Core ML model with that of pipelined CoreML model chunks and reports PSNR in dB. |
| 1899 | - Enabling this feature uses more memory. Disable it if your machine runs out of memory. |
| 1900 | |
| 1901 | Examples |
| 1902 | -------- |
| 1903 | .. sourcecode:: python |
| 1904 | |
| 1905 | import coremltools as ct |
| 1906 | |
| 1907 | model_path = "my_model.mlpackage" |
| 1908 | output_dir = "./output/" |
| 1909 | |
| 1910 | # The following code will produce two smaller models: |
| 1911 | # `./output/my_model_chunk1.mlpackage` and `./output/my_model_chunk2.mlpackage` |
| 1912 | # It also compares the output numerical of the original Core ML model with the chunked models. |
| 1913 | ct.models.utils.bisect_model( |
| 1914 | model_path, |
| 1915 | output_dir, |
| 1916 | ) |
| 1917 | |
| 1918 | # The following code will produce a single pipeline model `./output/my_model_chunked_pipeline.mlpackage` |
| 1919 | ct.models.utils.bisect_model( |
| 1920 | model_path, |
| 1921 | output_dir, |
| 1922 | merge_chunks_to_pipeline=True, |
| 1923 | ) |
| 1924 |