機器學習 Tensorflow 筆記(四):iOS 通過 Core ML 使用 Tensorflow 訓練模型
Tensorflow是Google推出的人工智慧框架,而Core ML是蘋果推出的人工智慧框架,兩者是有很大的區別,其中Tensorflow是包含了訓練模型和評估模型,Core ML只支援在裝置上評估模型,不能訓練模型。
通常而言我們通過Tensorflow訓練模型,匯出我們訓練的模型pb,然後轉換了Core ML的格式mlmodel,從而實現在iOS實現評估模型。
系列文章
《機器學習Tensorflow筆記1:Hello World到MNIST實驗》
《機器學習Tensorflow筆記2:超詳細剖析MNIST實驗》
《機器學習Tensorflow筆記3:Python訓練MNIST模型,在Android上實現評估》
《機器學習Tensorflow筆記4:iOS通過Core ML使用Tensorflow訓練模型》匯出MNIST訓練模型
我們在上一篇文章介紹瞭如何匯出.pb格式的解決,但是這種格式只適合在Tensorflow框架上使用,如果要在iOS上使用,那麼就要轉換到CoreML能夠使用的.mlmodel格式。如何匯出.pb格式的教程請看我的另一篇文章
《機器學習Tensorflow筆記3:Python訓練MNIST模型,在Android上實現評估》安裝tfcoreml
安裝tfcoreml很簡單, github 上也有詳細的介紹。我的Tensorflow是通過Virtualenv安裝的,如果安裝tfcoreml,那麼需要先啟用環境,通過pip安裝即可。
$ cd targetDirectory $ source ./bin/activate (targetDirectory) $ pip install -U tfcoreml
把pb轉換成mlmodel
import tfcoreml as tf_converter tf_converter.convert(tf_model_path='model/mnist2.pb', mlmodel_path='my_model.mlmodel', output_feature_names=['Softmax:0'],input_name_shape_dict={"input/x_input:0":[1,784]})
專案新增mlmodel模型檔案
把.mlmodel檔案新增到專案中

image.png
需要保證檔案已經新增到Compile Sources

image.png
分析原理
我們可以點選.mlmodel檔案的箭頭,就可以看到一個程式碼檔案,實際上mlmodel是保護了三個類, Mnist
, MnistInput
, MnistOutput
,通過這幾個類就可以實現模型的評估。

image.png
評估模型
do{ let image = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,18,18,18,126,136,175,26,166,255,247,127,0,0,0,0,0,0,0,0,0,0,0,0,30,36,94,154,170,253,253,253,253,253,225,172,253,242,195,64,0,0,0,0,0,0,0,0,0,0,0,49,238,253,253,253,253,253,253,253,253,251,93,82,82,56,39,0,0,0,0,0,0,0,0,0,0,0,0,18,219,253,253,253,253,253,198,182,247,241,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,156,107,253,253,205,11,0,43,154,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,1,154,253,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,139,253,190,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,190,253,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,35,241,225,160,108,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,81,240,253,253,119,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,45,186,253,253,150,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,93,252,253,187,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,253,249,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,46,130,183,253,253,207,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,148,229,253,253,253,250,182,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,114,221,253,253,253,253,201,78,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,66,213,253,253,253,253,198,81,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,171,219,253,253,253,253,195,80,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,172,226,253,253,253,253,244,133,11,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,136,253,253,253,212,135,132,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] // print shape for i in 0...783{ if(i % 28 == 0){ print("") } print("\(String(format: "% 2x", image[i]))",terminator: "") } print("") let array = try MLMultiArray(shape: [784], dataType: MLMultiArrayDataType.float32) for i in 0...(image.count-1) { let value = Double(image[i]) / 255.0 array[i]= NSNumber(floatLiteral: value) } let mnistInput = MnistInput(input__x_input__0: array) let result = try mnist.prediction(input: mnistInput) for i in 0...(result.Softmax__0.count - 1){ let item = result.Softmax__0[i] print("\(i) possibility : \(String(format:"%.2f",item.floatValue))") } }catch{ print(error) }
結果:
最可能是值是 5
0 possibility : 0.00 1 possibility : 0.00 2 possibility : 0.00 3 possibility : 0.14 4 possibility : 0.00 5 possibility : 0.86 6 possibility : 0.00 7 possibility : 0.00 8 possibility : 0.00 9 possibility : 0.00
評估模型(改進)
上面是直接使用陣列進行評估模型,下面我們直接讀取圖片檔案來實現評估模型。
let imagePath = Bundle.main.path(forResource: "test_image", ofType: "png") let imageData:UIImage = UIImage(contentsOfFile: imagePath!)! imageView.image = imageData let width = imageData.cgImage?.width let height = imageData.cgImage?.height let data:UnsafePointer<UInt8> = CFDataGetBytePtr(imageData.cgImage?.dataProvider?.data!) var image = Array<Int>() for i in 0...(width! * height! - 1){ let postion = i*4 image.append(Int(data[postion])) } let mnist = Mnist() do{ // print shape for i in 0...783{ if(i % 28 == 0){ print("") } print("\(String(format: "% 2x", image[i]))",terminator: "") } print("") let array = try MLMultiArray(shape: [784], dataType: MLMultiArrayDataType.float32) for i in 0...(image.count-1) { let value = Double(image[i]) / 255.0 array[i]= NSNumber(floatLiteral: value) } let mnistInput = MnistInput(input__x_input__0: array) let result = try mnist.prediction(input: mnistInput) var text = "" for i in 0...(result.Softmax__0.count - 1){ let item = result.Softmax__0[i] text += "\(i) possibility : \(String(format:"%.2f",item.floatValue))\n" } label.text = text }catch{ print(error) }
新增圖片

test_image.png

image.png

image.png
測試結果

image.png