深度學習之在iOS上運行CNN

來源:互聯網
上載者:User

深度學習之在iOS上運行CNN
1 引言

作為曾經的iOS開發人員,在研究深度學習的時候,總有一個想法就是在iPhone上運行深度學習,不管是在手機上訓練還是利用訓練好的資料進行測試。
因為iOS的開發環境支援C++,因此,只要你的代碼是C/C++,本質上就可以在iOS上運行。
怎麼才能更快更好地在iOS上運行CNN呢?

2 方法1:通過Matlab轉碼

Matlab內建轉成c的工具,如果你研究過UFLDL的深度學習教程,就知道如何在Matlab上使用CNN,那麼,轉換成c後,放到iOS的開發環境中,然後將參數存成txt格式再讀取分割,也就是可以實現。
如就是已經將matlab代碼轉換為c後匯入的結果:

開啟predict.h檔案,可以看到可以調用的介面:

/* Function Declarations */extern real_T predict(const real_T Theta1[10025], const real_T Theta2[260], const real_T X[400]);

這是訓練MNIST的一個神經網路,我這邊用了測試手寫數位識別。

因此,接下來需要對圖片進行處理,從而轉換為x[400]的向量格式。

這個只要能讀取圖片的像素,進行轉換就可以。可以考慮用opencv來實現。


這裡我的方法是在用手畫出數字之後,將圖片轉換為20*20像素的圖片,如右下角所示,再將右下角的圖片轉換為400的數組,輸入predict得到的結果。

3 方法2:使用DeepBeliefSDK

https://github.com/jetpacapp/DeepBeliefSDK
這個是別人專門寫的一個用於iOS的深度學習的SDK。可以使用,但是存在的問題就是如果要自己訓練的話很受限制。

4 方法3:使用tinyCNN

https://github.com/nyanp/tiny-cnn
這個很不錯,它對比Caffe,Theano等架構最大的特點就是不需要安裝,只要能用C++ 11.然后里面的例子使用了boost庫。因此,為了運行它,我們需要在xcode安裝ios的boost庫。

網上找到了一個編譯好的boost庫:
https://github.com/danoli3/ofxiOSBoost

匯入boost庫的方法非常簡單:

In Xcode Build Settings for your project:Add to Library Search Paths ( LIBRARY_SEARCH_PATHS ) $(SRCROOT)/../../../addons/ofxiOSBoost/libs/boost/lib/iosAdd to Header Search Paths ( HEADER_SEARCH_PATHS )$(SRCROOT)/../../../addons/ofxiOSBoost/libs/boost/includeIn the Target under Build PhasesAdd to 'Link Binary With Libraries' the boost.a found in the ofxiOSBoost/libs/boost/lib/ios directory.If not openFrameworks just add the libs/boost/include to Header Search Paths and the libs/boost/ios to Library Search Paths

那麼具體在建立iOS應用的時候,這裡使用作者提供的訓練MNIST的例子,那麼要注意在使用資料時,要更改路徑:

NSString *trainLabels = [[NSBundle mainBundle] pathForResource:@train-labels ofType:@idx1-ubyte];    NSString *trainImages = [[NSBundle mainBundle] pathForResource:@train-images ofType:@idx3-ubyte];    NSString *t10kLabels = [[NSBundle mainBundle] pathForResource:@t10k-labels ofType:@idx1-ubyte];    NSString *t10kImages = [[NSBundle mainBundle] pathForResource:@t10k-images ofType:@idx3-ubyte];    parse_mnist_labels([trainLabels cStringUsingEncoding:NSUTF8StringEncoding], &train_labels);    parse_mnist_images([trainImages cStringUsingEncoding:NSUTF8StringEncoding], &train_images);    parse_mnist_labels([t10kLabels cStringUsingEncoding:NSUTF8StringEncoding], &test_labels);    parse_mnist_images([t10kImages cStringUsingEncoding:NSUTF8StringEncoding], &test_images);

基本上這樣就可以運行開始訓練了。

如果想在Mac上訓練,同樣需要安裝boost庫。這個只要在官網下載boost,我用的是1.58版本。然後在terminal中安裝,cd到路徑,然後./boostrap.sh 然後./b2 安裝就可以。然後在xcode引入路徑:

The Boost C++ Libraries were successfully built!The following directory should be added to compiler include paths:    /Users/.../.../.../boostThe following directory should be added to linker library paths:    /Users/.../.../.../boost/stage/lib

路徑初始為自己boost的檔案夾地址。

4 小結

上面說了一些很方便的方法來實現在iOS下運行CNN。當然,我們更多需要就是進行映像的識別。相信大家自己測試會覺得很有趣。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.