pytorch 移動端架構 thnets 附c範例程式碼

來源:互聯網
上載者:User

標籤:net   mod   flow   caffe   vs編譯   tor   ima   分配   phone   

前年年前做一個手機移動端Image Recognition項目的時候,

先後嘗試了mxnet,thnets,caffe,tensorflow.

當時的情況是,mxnet記憶體管理奇差,記憶體經常由於模型運算分派不足,app掛掉。

後來調研了下caffe發現也很不友好。

最後發現thnets相對比較輕巧,

經過演算法調優之後,效能還不錯,

特別是在ios下啟用了Accelerate加速庫。

後來tensorflow快速發展,就切到tensorflow上了。

最近看了下thnets,作者 mvitez 看來不怎麼上心了。

至今為止,改進的不多。

thnets的移動端範例代碼,可以參考:

https://github.com/e-lab/apps-iOs

https://github.com/e-lab/apps-android

有一段時間nnpack加速庫起來了,就想著把thnets給patch一下nnpack.

但是由於項目太趕,沒那個時間去做。

後來也因為切換到tensorflow上了。

thnets就被雪藏了。

向作者提交了兩個建議,1,改用stb_image載入圖片  2, 支援windows平台

這個兩個工作,我都做了。

作者合了1。

2 我關閉了。幾天前去看記錄,作者當時問我關閉的原因,我沒回。

真正的原因是。。。thnets被我遺忘了,而windows 版本的存在意義並不大。

今天稍微花了點時間,在windows寫個thnets的demo範例,給有需要的網友~

項目地址:

https://github.com/cpuimage/thnets

程式碼範例見:demo.c

#include <string.h> #include <stdlib.h> #include <stdio.h> #include "thnets.h"// http://tntmonks.cnblogs.com/THNETWORK * net;char * labels[] = { "lamp"," bottle"," watch"," pen"," grass"," shoe"," wall"," chair"," mug"," fork"," table"," book"," tablet"," bookcase"," pencil"," door"," face"," ceiling"," sofa"," bicycle"," aluminum - can"," window"," road"," stairs"," floor"," painting"," toy"," remote"," computer"," plant"," television"," dog"," laptop"," microwave"," cat"," tree"," knife"," car"," motorcycle"," person"," cup"," sidewalk"," telephone"," spoon"," hand"," sofabed" };int main(int argc, char ** argv) {    img_t image = { 0 };    //test.jpg     char * pic_file = argv[1];    //model    char * model_path = argv[2];    int dropclassifier = 0;    loadimage(pic_file, &image);    THInit();    printf("init_thnets.");    net = THLoadNetwork(model_path);    if (net) {        THUseSpatialConvolutionMM(net, 2);        if (dropclassifier == 1) {            if (net->net->modules[net->net->nelem - 1].type == MT_SoftMax)                net->net->nelem--;            if (net->net->modules[net->net->nelem - 1].type == MT_Linear)                net->net->nelem--;            if (net->net->modules[net->net->nelem - 1].type == MT_View)                net->net->nelem--;        }    }    else {        printf("Shiiiiit went down.");        return -1;    }    float * percentages = 0;    int outwidth, outheight;    if (!net) {        return 0;    }    int i = 0;    int nbatch = 1;    unsigned char *bitmaps[256];    for (i = 0; i < nbatch; i++)        bitmaps[i] = image.bitmap;    int size = THProcessImages(net, bitmaps, nbatch, image.width, image.height, image.cp * image.width, &percentages, &outwidth, &outheight, 0);    for (i = 0; i < nbatch; i++)        if (bitmaps[i])            free(bitmaps[i]);    if (percentages)    {        float max[3] = { 0 };        int maxPos[3] = { 0 };        for (int j = 0; j < size; j++) {            if (percentages[j] > max[0]) {                maxPos[0] = j;                max[0] = percentages[j];            }            else if (percentages[j] > max[1]) {                maxPos[1] = j;                max[1] = percentages[j];            }            else if (percentages[j] > max[2]) {                maxPos[2] = j;                max[2] = percentages[j];            }        }        for (int index = 0; index < 3; index += 1) {            const float predictionValue = max[index];            if (predictionValue > 0.05f) {                printf(" %f %s  \n", predictionValue, labels[maxPos[index] % size]);            }        }        free(percentages);    }    getchar();}

 

採用vs編譯的話,需要下載openblas:

https://jaist.dl.sourceforge.net/project/openblas/v0.2.19/

lib庫連結檔案:libopenblas.dll.a 即可編譯。

附帶的模型 來自e-lab的項目。

樣本輸出得分最高並且高於0.05的三個結果。

樣本圖片:test.jpg

樣本模型:  model

對應的標籤,見代碼或模型檔案夾下的categories.txt

以上,待有精力再對thnets進行效能調優。

對於前向傳播而言,最好的代碼學習資料莫過於:darknet

https://github.com/pjreddie/darknet

darknet 代碼寫得十分耐看,邏輯清晰。

darknet+nnpack:

https://github.com/digitalbrain79/darknet-nnpack

 

若有其他相關問題或者需求也可以郵件聯絡俺探討。

郵箱地址是: 
[email protected]

 

pytorch 移動端架構 thnets 附c範例程式碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.