1. 程式人生 > >caffe21天書上的例子--qt讀取caffe deply網路層名

caffe21天書上的例子--qt讀取caffe deply網路層名

主要功能是讀取deply的層名字,程式碼如下:

#include <vector>
#include <iostream>
#include "caffe/net.hpp"


using  namespace std;
using namespace caffe;
int main(int argc, char *argv[])
{
    string proto("deploy.prototxt");
    Net<float> nn(proto,caffe::TEST);
    vector<string> bn = nn.blob_names();
    for(int i=0;i<bn.size();i++)
    {
        cout<<"Blob #"<<i<<" : "<<bn[i]<<endl;
    }
    
    return 0;
}

程式碼好寫,環境難配,一開始的.pro如下

QT += core
QT -= gui

CONFIG += c++11

TARGET = 1113_test_caffe_net
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe
INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe/debug_caffe/src
INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe/include
INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe/debug_caffe/include

LIBS += -L/usr/local/cuda-8.0/lib64/
LIBS += -L/usr/lib/x86_64-linux-gnu/hdf5/serial
LIBS += -L/usr/lib/
LIBS += -L/usr/local/cuda-8.0/lib64
LIBS += -L/lib/x86_64-linux-gnu/
LIBS += -L/media/d_2/everyday/1105_caffe/caffe/debug_caffe/lib

LIBS += -lboost_serialization
LIBS += -lboost_system
LIBS += -lboost_filesystem
LIBS += -lglog
LIBS += -lcaffe-d
LIBS += -lhdf5
LIBS += -lhdf5_hl
LIBS += -lboost_thread
LIBS += -lprotobuf
LIBS += -latlas
LIBS += -lcublas_static
LIBS += -lcudart
LIBS += -lculibos
LIBS += -lcurand_static
LIBS += -lssl3

LIBS += -lpthread
LIBS += -ldl
LIBS += -lrt
LIBS += /usr/lib/x86_64-linux-gnu/libgflags.a


SOURCES += main.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

報錯:

fatal error: cublas_v2.h: 沒有那個檔案或目錄

然後看了之前的工程.pro的配置:

加上了

INCLUDEPATH += /usr/local/cuda/include
INCLUDEPATH += /usr/local/include/node

 

好了,又報錯:

usr/bin/ld: 找不到 -lcaffe

Makefile:242: recipe for target '1113_test_caffe_net' failed

 我知道我編譯的caffe是debug模式,tool目錄下面的caffe都是caffe-d,同樣的lib目錄下面的是_caffe-d.so 、libcaffe-d.so 、libcaffe-d.so.1.0.0 所以,我的應該寫成這樣LIBS += -lcaffe-d。。正確的.pro如下:

QT += core
QT -= gui

CONFIG += c++11

TARGET = 1113_test_caffe_net
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe
INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe/debug_caffe/src
INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe/include
INCLUDEPATH += /media/d_2/everyday/1105_caffe/caffe/debug_caffe/include
INCLUDEPATH += /usr/local/cuda/include
INCLUDEPATH += /usr/local/include/node


LIBS += -L/usr/local/cuda-8.0/lib64/
LIBS += -L/usr/lib/x86_64-linux-gnu/hdf5/serial
LIBS += -L/usr/lib/
LIBS += -L/usr/local/cuda-8.0/lib64
LIBS += -L/lib/x86_64-linux-gnu/
LIBS += -L/media/d_2/everyday/1105_caffe/caffe/debug_caffe/lib

LIBS += -lboost_serialization
LIBS += -lboost_system
LIBS += -lboost_filesystem
LIBS += -lglog
LIBS += -lcaffe-d
LIBS += -lhdf5
LIBS += -lhdf5_hl
LIBS += -lboost_thread
LIBS += -lprotobuf
LIBS += -latlas
LIBS += -lcublas_static
LIBS += -lcudart
LIBS += -lculibos
LIBS += -lcurand_static
LIBS += -lssl3

LIBS += -lpthread
LIBS += -ldl
LIBS += -lrt
LIBS += /usr/lib/x86_64-linux-gnu/libgflags.a

SOURCES += main.cpp

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

所以,程式碼雖簡單,但是它通過呼叫caffe的一些底層程式碼,都需要正確地址的庫和標頭檔案的支援,要不然找不到,比如,這個簡單的程式碼會呼叫cuda和protobuf等,執行結果如下: