1、在Mac上使用Homebrew包管理器安装Python 3.6很简单,如果还没有安装Homebrew,可以在线安装。然后打开终端(Terminal)输入如下命令安装最新版Python:
brew install python3
2、然后使用Python的包管理器pip来安装所需要的包,在终端输入如下命令:
pip3 install numpy
pip3 install scipy
pip3 install scikit-learn
pip3 install pandas
pip3 install tensorflow==1.3 #最新tensorflow1.7训练出来的模型在ios camera工程上有问题,不能识别模型的某些操作符号, 大概camera工程的tensorflow版本过低
3、首先你得安装好Xcode 8,确定开发者目录指向你安装Xcode的位置并且已经被激活。(如果你在安装Xcode之前已经安装了Homebrew,这可能会指向错误的地址,导致TensorFlow安装失败):
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer
4、我们将使用名为bazel的工具来安装TensorFlow。先使用Homebrew安装所需要的包:
//brew cask install java //安装最新java,现在是java10
brew install brew-cask brew update
brew tap caskroom/versions # 安装cask,如果已安装了cask可以省略。
brew cask install caskroom/versions/java8
#brew cask install java8 # 使用cask安装其他的工具
brew install bazel #要求java8 brew install automake brew install libtool
5、下载tensorflow 1.3源码:
sudo git clone https://github.com/tensorflow/tensorflow -b r1.3
6、下载花朵图片,在tensorflow目录下执行:
cd ..
sudo mkdir tf_files
sudo curl -O http://download.tensorflow.org/example_images/flower_photos.tgz
sudo tar xzf flower_photos.tgz
cd ../tensorflow
7、训练mobilenet模型:
yp@ubuntu:/home/tensorflow1.3/tensorflow$ more buildretrain.sh
python3 tensorflow/examples/image_retraining/retrain.py \ --bottleneck_dir=../tf_files/bottlenecks/ \ --how_many_training_steps 20 \ --model_dir=../tf_files/inception \ --output_graph=../tf_files/retrained_graph.pb \ --output_labels=../tf_files/retrained_labels.txt \ --image_dir=../tf_files/flower_photos \ --architecture=mobilenet_1.0_224
sudo ./buildretrain.sh
测试mobilenet模型:
yp@ubuntu:/home/tensorflow1.3/tensorflow$ more testlabel.py python3 tensorflow/examples/label_image/label_image.py \ --graph=../tf_files/retrained_graph.pb \ --labels=../tf_files/retrained_labels.txt \ --image=../tf_files/flower_photos/roses/99383371_37a5ac12a3_n.jpg \ --input_layer=input \ --output_layer=final_result \ --input_mean=128 \ --input_std=128 \ --input_width=224 \ --input_height=224
sudo ./testlabel.py
8、在ios camera工程中加载模型:
参考上一篇文章:https://mp.csdn.net/postedit/79838931
sudo cp ../tf_files/retrained_graph.pb ./tensorflow/examples/ios/camera/data/
sudo cp ../tf_files/retrained_labels.txt ./tensorflow/examples/ios/camera/data/
修改:ios/camera/CameraExampleViewController.mm
static NSString* model_file_name = @"retrained_graph";
static NSString* model_file_type = @"pb"; // This controls whether we'll be loading a plain GraphDef proto, or a // file created by the convert_graphdef_memmapped_format utility that wraps a // GraphDef and parameter file that can be mapped into memory from file to // reduce overall memory usage. const bool model_uses_memory_mapping = false; // If you have your own model, point this to the labels file. static NSString* labels_file_name = @"retrained_labels"; static NSString* labels_file_type = @"txt"; // These dimensions need to match those the model was trained with. const int wanted_input_width = 224; const int wanted_input_height = 224; const int wanted_input_channels = 3; const float input_mean = 117.0f; const float input_std = 1.0f; const std::string input_layer_name = "input"; const std::string output_layer_name = "final_result";
9、最后: 运行真机测试