{"id":1375,"url":"https://github.com/JacopoMangiavacchi/SwiftCoreMLTools","last_synced_at":"2025-08-02T04:30:58.705Z","repository":{"id":84573043,"uuid":"237129650","full_name":"JacopoMangiavacchi/SwiftCoreMLTools","owner":"JacopoMangiavacchi","description":"A Swift library for creating and exporting CoreML Models in Swift","archived":false,"fork":false,"pushed_at":"2020-06-05T23:39:04.000Z","size":357,"stargazers_count":161,"open_issues_count":3,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-12-06T08:34:35.274Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JacopoMangiavacchi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-01-30T03:11:45.000Z","updated_at":"2024-07-30T03:26:50.000Z","dependencies_parsed_at":"2024-01-02T20:59:15.674Z","dependency_job_id":null,"html_url":"https://github.com/JacopoMangiavacchi/SwiftCoreMLTools","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/JacopoMangiavacchi/SwiftCoreMLTools","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacopoMangiavacchi%2FSwiftCoreMLTools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacopoMangiavacchi%2FSwiftCoreMLTools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacopoMangiavacchi%2FSwiftCoreMLTools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacopoMangiavacchi%2FSwiftCoreMLTools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JacopoMangiavacchi","download_url":"https://codeload.github.com/JacopoMangiavacchi/SwiftCoreMLTools/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JacopoMangiavacchi%2FSwiftCoreMLTools/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268334609,"owners_count":24233793,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:45.018Z","updated_at":"2025-08-02T04:30:58.304Z","avatar_url":"https://github.com/JacopoMangiavacchi.png","language":"Swift","funding_links":[],"categories":["Machine Learning"],"sub_categories":["Other Hardware"],"readme":"# SwiftCoreMLTools\n\n![Swift](https://github.com/JacopoMangiavacchi/SwiftCoreMLTools/workflows/Swift/badge.svg)\n[![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)\n[![ver](https://img.shields.io/github/v/release/JacopoMangiavacchi/SwiftCoreMLTools?include_prereleases\u0026label=version)](https://github.com/JacopoMangiavacchi/SwiftCoreMLTools)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) \n![Documentation](https://github.com/JacopoMangiavacchi/SwiftCoreMLTools/workflows/Documentation/badge.svg)\n\nA Swift Library for creating CoreML models in Swift.\n\n**Work in progress**\n\nThis library expose a (function builder based) DSL as well as a programmatic API (see examples below).\n\nThe library also implement Codable protocol allowing to print and edit CoreML model in JSON format.\n\n**The library is not \"official\" - it is not part of Apple CoreML and it is not maintained.**\n\nThis library use the Apple Swift Protocol Buffer package and compile and import to Swift the CoreML ProtoBuf datastructures defined from the GitHub Apple CoreMLTools repo - https://github.com/apple/coremltools/tree/master/mlmodel/format\n\nThis package could be used to export Swift For TensorFlow models or to generate new CoreML models from scratch providing a much swifty interface compared to directly using the Swift compiled CoreML protobuf data structures.\n\nCoreML models generated with this library could be potentially personalized (trained) partially or entirely using the CoreML runtime.\n\nCoreML support much more then Neural Network models but this experimental library is only focused, at the moment, on Neural Network support.\n\n\u003e End to end test case exporting a real S4TF model at:\n\u003e https://github.com/JacopoMangiavacchi/TestSwiftCoreMLTools\n\n## Sample projects using this library to create and train Core ML models on device\n\n### LeNet Convolutional Neural Network for MNIST dataset\n\n- GitHub project: https://github.com/JacopoMangiavacchi/MNIST-CoreML-Training\n- Documentation: https://medium.com/@JMangia/mnist-cnn-core-ml-training-c0f081014fa6\n\n### Transfer Learning with Categorical Embedding\n\n- GitHub project: https://github.com/JacopoMangiavacchi/CoreML-TransferLearning-Demo\n- Documentation: https://heartbeat.fritz.ai/core-ml-on-device-training-with-transfer-learning-from-swift-for-tensorflow-models-1264b444e18d\n\n\n## Documentation\n\n[Documentation](https://jacopomangiavacchi.github.io/SwiftCoreMLTools/Documentation/)\n\n\n## Neural Network Support (work in progress)\n\n### Layers\n\n- InnerProduct\n- Convolution\n- Embedding\n- Flatten\n- Pooling\n- Permute\n- Concat\n\n### Activation Functions\n\n- Linear\n- ReLu\n- LeakyReLu\n- ThresholdedReLu\n- PReLu\n- Tanh\n- ScaledTanh\n- Sigmoid\n- SigmoidHard\n- Elu\n- Softsign\n- Softplus\n- ParametricSoftplus\n- Softmax\n\n### Loss Functions\n\n- MSE\n- CategoricalCrossEntropy\n\n### Optimizers\n\n- SGD\n- Adam\n\n## Export Swift for TensorFlow sample scenario\n\n### Trivial Swift for TensorFlow model\n\n```swift\nstruct LinearRegression: Layer {\n    var layer1 = Dense\u003cFloat\u003e(inputSize: 1, outputSize: 1, activation: identity)\n\n    @differentiable\n    func callAsFunction(_ input: Tensor\u003cFloat\u003e) -\u003e Tensor\u003cFloat\u003e {\n        return layer1(input)\n    }\n}\n\nvar s4tfModel = LinearRegression()\n// Training Loop ...\n```\n\n### Export to CoreML using DSL approach\n\n```swift\nlet coremlModel = Model(version: 4,\n                        shortDescription: \"Trivial linear classifier\",\n                        author: \"Jacopo Mangiavacchi\",\n                        license: \"MIT\",\n                        userDefined: [\"SwiftCoremltoolsVersion\" : \"0.1\"]) {\n    Input(name: \"dense_input\", shape: [1])\n    Output(name: \"output\", shape: [1])\n    NeuralNetwork {\n        InnerProduct(name: \"dense_1\",\n                     input: [\"dense_input\"],\n                     output: [\"output\"],\n                     weight: s4tfModel.layer1.weight.transposed().flattened().scalars,\n                     bias: s4tfModel.layer1.bias.flattened().scalars,\n                     inputChannels: 1,\n                     outputChannels: 1)\n    }\n}\n```\n\n### Export a CoreML personalizable (re-trainable) model using DSL approach\n\n```swift\nlet coremlModel = Model(version: 4,\n                        shortDescription: \"Trivial linear classifier\",\n                        author: \"Jacopo Mangiavacchi\",\n                        license: \"MIT\",\n                        userDefined: [\"SwiftCoremltoolsVersion\" : \"0.1\"]) {\n    Input(name: \"dense_input\", shape: [1])\n    Output(name: \"output\", shape: [1])\n    TrainingInput(name: \"dense_input\", shape: [1])\n    TrainingInput(name: \"output_true\", shape: [1])\n    NeuralNetwork(losses: [MSE(name: \"lossLayer\",\n                               input: \"output\",\n                               target: \"output_true\")],\n                  optimizer: SGD(learningRateDefault: 0.01,\n                                 learningRateMax: 0.3,\n                                 miniBatchSizeDefault: 5,\n                                 miniBatchSizeRange: [5],\n                                 momentumDefault: 0,\n                                 momentumMax: 1.0),\n                  epochDefault: 2,\n                  epochSet: [2],\n                  shuffle: true) {\n        InnerProduct(name: \"dense_1\",\n                     input: [\"dense_input\"],\n                     output: [\"output\"],\n                     weight: s4tfModel.layer1.weight.transposed().flattened().scalars,\n                     bias: s4tfModel.layer1.bias.flattened().scalars,\n                     inputChannels: 1,\n                     outputChannels: 1,\n                     updatable: true)\n    }\n}\n```\n\n## Example code to export and save to a CoreML model data file\n\n```swift\nlet model = Model(...){ ... }\nlet coreMLData = model.coreMLData\ntry! coreMLData.write(to: URL(fileURLWithPath: \"model.mlmodel\"))\n```\n\n## CoreML model creation with programmatic API\n\n```swift\nvar model = Model(version: 4,\n                  shortDescription: \"Trivial linear classifier\",\n                  author: \"Jacopo Mangiavacchi\",\n                  license: \"MIT\",\n                  userDefined: [\"SwiftCoremltoolsVersion\" : \"0.1\"])\n\nmodel.addInput(Input(name: \"dense_input\", shape: [1]))\nmodel.addOutput(Output(name: \"output\", shape: [1]))\nmodel.addTrainingInput(TrainingInput(name: \"dense_input\", shape: [1]))\nmodel.addTrainingInput(TrainingInput(name: \"output_true\", shape: [1]))\nmodel.neuralNetwork = NeuralNetwork(losses: [MSE(name: \"lossLayer\",\n                                                 input: \"output\",\n                                                 target: \"output_true\")],\n                                    optimizer: SGD(learningRateDefault: 0.01,\n                                                   learningRateMax: 0.3,\n                                                   miniBatchSizeDefault: 5,\n                                                   miniBatchSizeRange: [5],\n                                                   momentumDefault: 0,\n                                                   momentumMax: 1.0),\n                                    epochDefault: 2,\n                                    epochSet: [2],\n                                    shuffle: true)\n\nmodel.neuralNetwork.addLayer(InnerProduct(name: \"layer1\",\n                                         input: [\"dense_input\"],\n                                         output: [\"output\"],\n                                         weight: [0.0],\n                                         bias: [0.0],\n                                         inputChannels: 1,\n                                         outputChannels: 1,\n                                         updatable: true))\n```\n\n## YAML / JSON Format model persistence (Codable)\n\n### Example CoreML model in YAML format\n\n```yaml\nversion: 4\nshortDescription: Trivial linear classifier\nauthor: Jacopo Mangiavacchi\nlicense: MIT\nuserDefined:\n  SwiftCoremltoolsVersion: '0.1'\ninputs:\n  dense_input:\n    name: dense_input\n    shape:\n    - 1\n    featureType: float\noutputs:\n  output:\n    name: output\n    shape:\n    - 1\n    featureType: float\ntrainingInputs:\n  dense_input:\n    name: dense_input\n    shape:\n    - 1\n    featureType: float\n  output_true:\n    name: output_true\n    shape:\n    - 1\n    featureType: float\nneuralNetwork:\n  losses:\n  - type: mse\n    base:\n      name: lossLayer\n      input: output\n      target: output_true\n  optimizer:\n    type: sgd\n    base:\n      learningRateDefault: 1e-2\n      learningRateMax: 3e-1\n      miniBatchSizeDefault: 5\n      miniBatchSizeRange:\n      - 5\n      momentumDefault: 0e+0\n      momentumMax: 1e+0\n  epochDefault: 2\n  epochSet:\n  - 2\n  shuffle: true\n  layers:\n  - type: innerProduct\n    base:\n      name: layer1\n      input:\n      - dense_input\n      output:\n      - output\n      weight:\n      - 0e+0\n      bias:\n      - 0e+0\n      inputChannels: 1\n      outputChannels: 1\n      updatable: true\n```\n\n### Example CoreML model in JSON format\n\n```json\n{\n  \"author\" : \"Jacopo Mangiavacchi\",\n  \"shortDescription\" : \"Trivial linear classifier\",\n  \"version\" : 4,\n  \"license\" : \"MIT\",\n  \"userDefined\" : {\n    \"SwiftCoremltoolsVersion\" : \"0.1\"\n  },\n  \"inputs\" : {\n    \"dense_input\" : {\n      \"name\" : \"dense_input\",\n      \"shape\" : [1],\n      \"featureType\" : \"float\"\n    }\n  },\n  \"outputs\" : {\n    \"output\" : {\n      \"name\" : \"output\",\n      \"shape\" : [1],\n      \"featureType\" : \"float\"\n    }\n  },\n  \"trainingInputs\" : {\n    \"output_true\" : {\n      \"name\" : \"output_true\",\n      \"shape\" : [1],\n      \"featureType\" : \"float\"\n    },\n    \"dense_input\" : {\n      \"name\" : \"dense_input\",\n      \"shape\" : [1],\n      \"featureType\" : \"float\"\n    }\n  },\n  \"neuralNetwork\" : {\n    \"layers\" : [\n      {\n        \"type\" : \"innerProduct\",\n        \"base\" : {\n          \"output\" : [\n            \"output\"\n          ],\n          \"outputChannels\" : 1,\n          \"weight\" : [0],\n          \"input\" : [\n            \"dense_input\"\n          ],\n          \"bias\" : [0],\n          \"inputChannels\" : 1,\n          \"updatable\" : true,\n          \"name\" : \"layer1\"\n        }\n      }\n    ],\n    \"optimizer\" : {\n      \"type\" : \"sgd\",\n      \"base\" : {\n        \"momentumDefault\" : 0,\n        \"momentumMax\" : 1,\n        \"learningRateMax\" : 0.3,\n        \"miniBatchSizeRange\" : [5],\n        \"miniBatchSizeDefault\" : 5,\n        \"learningRateDefault\" : 0.01\n      }\n    },\n    \"losses\" : [\n      {\n        \"type\" : \"mse\",\n        \"base\" : {\n          \"name\" : \"lossLayer\",\n          \"input\" : \"output\",\n          \"target\" : \"output_true\"\n        }\n      }\n    ],\n    \"shuffle\" : true,\n    \"epochDefault\" : 2,\n    \"epochSet\" : [2]\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJacopoMangiavacchi%2FSwiftCoreMLTools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJacopoMangiavacchi%2FSwiftCoreMLTools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJacopoMangiavacchi%2FSwiftCoreMLTools/lists"}