https://github.com/tobedefined/libtensorflowforiosswift
Modified from emoji-tf-ios: https://github.com/h4x3rotab/emoji-tf-ios
https://github.com/tobedefined/libtensorflowforiosswift
ios swift tensorflow
Last synced: about 1 year ago
JSON representation
Modified from emoji-tf-ios: https://github.com/h4x3rotab/emoji-tf-ios
- Host: GitHub
- URL: https://github.com/tobedefined/libtensorflowforiosswift
- Owner: ToBeDefined
- Created: 2017-07-14T11:11:29.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-08-23T06:05:14.000Z (over 7 years ago)
- Last Synced: 2025-03-25T16:22:07.889Z (about 1 year ago)
- Topics: ios, swift, tensorflow
- Language: Objective-C++
- Homepage:
- Size: 2.66 MB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LibTensorFlowForiOSSwift
[中文文档](./README_CN.md)
This is a TensorFlow demo that can be run on iOS and use swift develop. It implements a text classifier that can predict emoji from short text (like tweets).
Modified from [`emoji-tf-ios`](https://github.com/h4x3rotab/emoji-tf-ios); use `emoji_frozen.pb` model from `emoji-tf-ios`.
### how to run
- open terminal and go to project root folder
- input `sh run.sh`.
In the end, will automatically open Xcode
you can run the project now
### about `run.sh`
it will compile the `TensorFlow for iOS` automatically
### about project setting
> how to import TensorFlow in ios project
- download TensorFlow to the root folder and compile
> befor compile source file, you should change flow TensorFlow Kernel files. (version <= 1.2.1)
>
> in `run.sh`, it will change them automatically
>
> kernel path: `tensorflow/tensorflow/core/kernels`
- `cwise_op_add_1.cc`
> before:
```c++
...
...
#include "tensorflow/core/kernels/cwise_ops_common.h"
namespace tensorflow {
REGISTER5(BinaryOp, CPU, "Add", functor::add, float, Eigen::half, double, int32,
int64);
#if TENSORFLOW_USE_SYCL
...
...
```
> change to:
```c++
...
...
#include "tensorflow/core/kernels/cwise_ops_common.h"
namespace tensorflow {
REGISTER5(BinaryOp, CPU, "Add", functor::add, float, Eigen::half, double, int32,
int64);
// line 21 insert this code
#if defined(__ANDROID_TYPES_SLIM__)
REGISTER(BinaryOp, CPU, "Add", functor::add, int32);
#endif // __ANDROID_TYPES_SLIM__
// insert end
#if TENSORFLOW_USE_SYCL
...
...
```
- `cwise_op_less.cc`
> before:
```c++
...
...
#include "tensorflow/core/kernels/cwise_ops_common.h"
namespace tensorflow {
REGISTER8(BinaryOp, CPU, "Less", functor::less, float, Eigen::half, double,
int32, int64, uint8, int8, int16);
#if GOOGLE_CUDA
REGISTER7(BinaryOp, GPU, "Less", functor::less, float, Eigen::half, double,
int64, uint8, int8, int16);
...
...
```
> change to:
```c++
...
...
#include "tensorflow/core/kernels/cwise_ops_common.h"
namespace tensorflow {
REGISTER8(BinaryOp, CPU, "Less", functor::less, float, Eigen::half, double,
int32, int64, uint8, int8, int16);
// line 21 insert this code
#if defined(__ANDROID_TYPES_SLIM__)
REGISTER(BinaryOp, CPU, "Less", functor::less, int32);
#endif // __ANDROID_TYPES_SLIM__
// insert end
#if GOOGLE_CUDA
REGISTER7(BinaryOp, GPU, "Less", functor::less, float, Eigen::half, double,
int64, uint8, int8, int16);
...
...
```
- about `libtensorflow-core.a`
- in `Other Link Flags` add `$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/lib/libtensorflow-core.a`
- in `Library Search Paths` add `$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/lib`
- about `libprotobuf.a & libprotobuf-lite.a`
- in `Build Phases | Link Binary With Libraries` add `libprotobuf.a & libprotobuf-lite.a` (path: `tensorflow/tensorflow/contrib/makefile/gen/protobuf_ios/lib/`)
- in `Library Search Paths` add `$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/protobuf_ios/lib`
- in `Header Search Paths` add flows
- `$(SRCROOT)/tensorflow/`
- `$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/downloads/protobuf/src/`
- `$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/downloads`
- `$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/downloads/eigen`
- `$(SRCROOT)/tensorflow/tensorflow/contrib/makefile/gen/proto`
- in `Other Link Flags` add `-force_load`
- in `Build Phases | Link Binary With Libraries` add `Accelerate.framework`
- in `C++ Language Dialect` select `GNU++11` or `GNU++14`
- in `C++ Standard Library` select `libc++`
- `Enable Bitcode` set `No`
- remove any `-all_load` ,use `-ObjC` replace it
> Remove any use of the `-all_load` flag in your project. The protocol buffers libraries (full and lite versions) contain duplicate symbols, and the `-all_load` flag will cause these duplicates to become link errors. If you were using `-all_load` to avoid issues with Objective-C categories in static libraries, you may be able to replace it with the `-ObjC` flag.
- suppress TensorFlow warning:
- in `Other C Flags` & `Other C++ Flags` add `-isystem $(SRCROOT)/tensorflow`
### reference:
- compile TensorFlow:
> https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/makefile
- change Kernels error:
> https://github.com/h4x3rotab/emoji-tf-ios/blob/master/README.md
- import TensorFlow to iOS:
> https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/ios/README.md
- suppress warning of TensorFlow:
> https://clang.llvm.org/docs/UsersManual.html#id27