{"id":20037978,"url":"https://github.com/perfectlysoft/perfect-tensorflow","last_synced_at":"2025-05-05T06:31:52.873Z","repository":{"id":63920185,"uuid":"94277830","full_name":"PerfectlySoft/Perfect-TensorFlow","owner":"PerfectlySoft","description":"TensorFlow C API Class Wrapper in Server Side Swift.","archived":false,"fork":false,"pushed_at":"2020-07-07T21:00:46.000Z","size":642,"stargazers_count":168,"open_issues_count":0,"forks_count":15,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-10-30T17:17:25.461Z","etag":null,"topics":["artificial-intelligence","artificial-neural-networks","deep-learning","machine-learning","matrix-library","perfect","swift","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PerfectlySoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-14T02:06:53.000Z","updated_at":"2024-09-01T21:17:49.000Z","dependencies_parsed_at":"2023-01-14T14:00:20.111Z","dependency_job_id":null,"html_url":"https://github.com/PerfectlySoft/Perfect-TensorFlow","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-TensorFlow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-TensorFlow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-TensorFlow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-TensorFlow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerfectlySoft","download_url":"https://codeload.github.com/PerfectlySoft/Perfect-TensorFlow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224429197,"owners_count":17309666,"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","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":["artificial-intelligence","artificial-neural-networks","deep-learning","machine-learning","matrix-library","perfect","swift","tensorflow"],"created_at":"2024-11-13T10:24:29.246Z","updated_at":"2024-11-13T10:24:29.752Z","avatar_url":"https://github.com/PerfectlySoft.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Perfect TensorFlow [简体中文](README.zh_CN.md)\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"http://perfect.org/get-involved.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.org/assets/github/perfect_github_2_0_0.jpg\" alt=\"Get Involved with Perfect!\" width=\"854\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/PerfectlySoft/Perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg\" alt=\"Star Perfect On Github\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://stackoverflow.com/questions/tagged/perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/perfect_gh_button_2_SO.jpg\" alt=\"Stack Overflow\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"https://twitter.com/perfectlysoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg\" alt=\"Follow Perfect on Twitter\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg\" alt=\"Join the Perfect Slack\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Swift-4.1-orange.svg?style=flat\" alt=\"Swift 4.1\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat\" alt=\"Platforms OS X | Linux\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.org/licensing.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat\" alt=\"License Apache\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://twitter.com/PerfectlySoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat\" alt=\"PerfectlySoft Twitter\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.ly/badge.svg\" alt=\"Slack Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\nThis project is an experimental wrapper of TensorFlow C API which enables Machine Learning in Server Side Swift.\n\nThis package builds with Swift Package Manager and is part of the [Perfect](https://github.com/PerfectlySoft/Perfect) project but can also be used as an independent module.\n\nEnsure you have installed and activated the latest Swift 4.1.1 / Xcode 9.3\n\n## Project Status\n\nThe framework conforms to TensorFlow v1.8.0 C API functionality.\n\n## Development Notes\n\nThese files are the key part of Perfect-TensorFlow:\n\n```\nSources\n├── PerfectTensorFlow\n│   ├── APILoader.swift (1000+ lines, translated from tensorflow/c/c_api.h)\n│   ├── PerfectTensorFlow.swift (2700+ lines)\n└── TensorFlowAPI\n    ├── TensorFlowAPI.c (72 lines)\n    └── include\n        └── TensorFlowAPI.h (138 lines)\n```\n\nAll other Swift sources named as 'pb.*.swift', which is totally up to 45,000+ lines of code, are automatically generated by `updateprotos.sh` in the root directory. Unfortunately, if using such a script, you still need to manually edit the `public typealias` part listed in the **PerfectTensorFlow.swift**.\n\nUp to now there is no such a plan to generate these protocol buffer files dynamically in the Swift Source since Perfect-TensorFlow is a part of Perfect, although it can run independently, all features of Perfect framework are built by Swift Package Manager for consistency consideration. However, since the project is also fast growing, all pull request, ideas, suggestions and comments are welcome!\n\n## API Guide\n\nAPI programming topics can be found in [Perfect TensorFlow Guide](GUIDE.md).\n\nAlso, there are many features that has already embedded in the testing script, such as **TensorFlow event** and **summary** for **TensorBoard** report and benchmarking. Please check the [Perfect TensorFlow Testing Script](Tests/PerfectTensorFlowTests/PerfectTensorFlowTests.swift) for detail.\n\n## Quick Start\n\n### TensorFlow C API Library Installation\n\nPerfect-TensorFlow is based on TensorFlow C API, i.e., `libtensorflow.so` and `libtensorflow_framework.so` on runtime.\nThis project contains an express CPU version installation script for this module on both macOS / Ubuntu Linux, and will install both dynamic libraries into path `/usr/local/lib`. You can download \u0026 run [`install.sh`](https://github.com/PerfectlySoft/Perfect-TensorFlow/blob/master/install.sh). Before running this script, please make sure that `curl` has been installed onto your computer.\n\nFor more installation options, such as GPU/CPU and multiple versions on the same machine, please check TensorFlow website: [Installing TensorFlow for C](https://www.tensorflow.org/install/install_c)\n\n### Perfect TensorFlow Application\n\nTo use this library, add dependencies to your project's Package.swift with the **LATEST TAG**:\n\n``` swift\n.package(url: \"https://github.com/PerfectlySoft/Perfect-TensorFlow.git\", from: \"1.4.0\")\n```\n\nand it also requires a dependency declaration in the same file, `target` section:\n\n``` swift\ndependencies: [\"PerfectTensorFlow\"]\n```\n\nThen declare the library:\n\n``` swift\n// TensorFlowAPI contains most API functions defined in libtensorflow.so\nimport TensorFlowAPI\n\n// This is the Swift version of TensorFlow classes and objects\nimport PerfectTensorFlow\n\n// To keep the naming consistency with TensorFlow in other languages such as\n// Python or Java, making an alias of `TensorFlow` Class is a good idea:\npublic typealias TF = TensorFlow\n```\n\n### Library Activation\n\n⚠️NOTE⚠️ Prior to use **ANY ACTUAL FUNCTIONS** of Perfect TensorFlow framework, `TF.Open()` must be called first:\n\n``` swift\n// this action will load all api functions defined\n// in /usr/local/lib/libtensorflow.so\ntry TF.Open()\n```\n\nPlease also note that you can active the library with a specific path, alternatively, especially in case of different versions or CPU/GPU library adjustment required:\n\n``` swift\n// this action will load the library with the path\ntry TF.Open(\"/path/to/DLL/of/libtensorflow.so\")\n```\n\n### \"Hello, Perfect TensorFlow!\"\n\nHere is the Swift version of \"Hello, TensorFlow!\":\n\n``` swift\n// define a string tensor\nlet tensor = try TF.Tensor.Scalar(\"Hello, Perfect TensorFlow! 🇨🇳🇨🇦\")\n\n// declare a new graph\nlet g = try TF.Graph()\n\n// turn the tensor into an operation\nlet op = try g.const(tensor: tensor, name: \"hello\")\n\n// run a session\nlet o = try g.runner().fetch(op).addTarget(op).run()\n\n// decode the result      \nlet decoded = try TF.Decode(strings: o[0].data, count: 1)\n\n// check the result\nlet s2 = decoded[0].string\nprint(s2)\n```\n\n### Matrix Operations\n\n\nAs you can see, Swift version of TensorFlow keeps the same principals of the original one, i.e., create tensors, save tensors into graph, define the operations and then run the session \u0026 check the result.\n\nHere is an other simple example of matrix operations in Perfect TensorFlow:\n\n``` swift\n/* Matrix Multiply:\n| 1 2 |   |0 1|   |0 1|\n| 3 4 | * |0 0| = |0 3|\n*/\n// input the matrix.\nlet tA = try TF.Tensor.Matrix([[1, 2], [3, 4]])\nlet tB = try TF.Tensor.Matrix([[0, 0], [1, 0]])\n\n// adding tensors to graph\nlet g = try TF.Graph()\nlet A = try g.const(tensor: tA, name: \"Const_0\")\nlet B = try g.const(tensor: tB, name: \"Const_1\")\n\n// define matrix multiply operation\nlet v = try g.matMul(l: A, r: B, name: \"v\", transposeB: true)\n\n// run the session\nlet o = try g.runner().fetch(v).addTarget(v).run()\nlet m:[Float] = try o[0].asArray()\nprint(m)\n// m shall be [0, 1, 0, 3]\n```\n\n### Load a Saved Artificial Neural Network Model\n\nBesides building graph \u0026 sessions in code, Perfect TensorFlow also provides a handy method to load models into runtime, i.e, generate a new session by loading a model file:\n\n``` swift\nlet g = try TF.Graph()\n\n// the meta signature info defined in a saved model\nlet metaBuf = try TF.Buffer()\n\n// load the session\nlet session = try g.load(\n\texportDir: \"/path/to/saved/model\",\n\ttags: [\"tag1\", \"tag2\", ...],\n\tmetaGraphDef: metaBuf)\n```\n\n### Computer Vision Demo\n\nA detailed example of Perfect TensorFlow for Computer Vision can be found in this repo: [Perfect TensorFlow Demo](https://github.com/PerfectExamples/Perfect-TensorFlow-Demo-Vision), where you can upload any local images or draw a scribble online to test if the server can recognize the picture content:\n\n\u003cimg src='https://github.com/PerfectExamples/Perfect-TensorFlow-Demo-Vision/blob/master/scrshot2.png?raw=true'\u003e\u003c/img\u003e\n\n\n## Further Information\nFor more information on the Perfect project, please visit [perfect.org](http://perfect.org).\n\n\n## Now WeChat Subscription is Available (Chinese)\n\u003cp align=center\u003e\u003cimg src=\"https://raw.githubusercontent.com/PerfectExamples/Perfect-Cloudinary-ImageUploader-Demo/master/qr.png\"\u003e\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-tensorflow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectlysoft%2Fperfect-tensorflow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-tensorflow/lists"}