1. 程式人生 > >caffe 13 caffe vs2015 在tools中新增自己程式碼

caffe 13 caffe vs2015 在tools中新增自己程式碼

01 使用cmake編譯的新增程式碼方式

閱讀caffe\tools\CMakeLists.txt

# Collect source files
file(GLOB_RECURSE srcs ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

# Build each source file independently
foreach(source ${srcs})
  get_filename_component(name ${source} NAME_WE)

  # caffe target already exits
  if(name MATCHES "caffe"
) set(name ${name}.bin) endif() # target add_executable(${name} ${source}) target_link_libraries(${name} ${Caffe_LINK}) caffe_default_properties(${name})

可以看出caffe\tools下面的cpp檔案除了caffe.cpp被命名為caffe.bin工程外,其他的cpp檔案都以檔名稱命名為工程。
所以只要把我們的類tools程式碼,放到caffe\tools檔案下,執行cmake-gui,【configure】、【generate】,重新生成工程檔案,這樣工程會自動載入include、和lib依賴。

02 新增測試程式碼

在caffe\tools\my_first_test.cpp檔案,這裡使用《深度學習-21天實戰caffe》中的第八天《Caffe資料結構》中的程式碼。

#include <vector>
#include <iostream>
#include <caffe/blob.hpp>
#include <caffe/util/io.hpp> // test03()
#include <caffe/net.hpp> // TestNet()
using namespace caffe;
using namespace std
; void test01() { Blob<float> a; // 預設維度0 cout << "Size: " << a.shape_string() << endl; a.Reshape(1, 2, 3, 4); // 重置維度 cout << "Size: " << a.shape_string() << endl; // 設定data值 float* p = a.mutable_cpu_data(); for (int i = 0; i < a.count(); i++) { p[i] = i; } // 列印data值 for (int u = 0; u < a.num(); u++) { for (int v = 0; v < a.channels(); v++) { for (int w = 0; w < a.height(); w++) { for (int x = 0; x < a.width(); x++) { cout << "a[" << u << "][" << v << "][" << w << "][" << x << "] = " << a.data_at(u, v, w, x) << endl; } } } } cout << "ASUM = " << a.asum_data() << endl; // 元素絕對值和(L1-範數) cout << "SUMQ = " << a.sumsq_data() << endl; // 平方和(L2-範數) } void test02(Blob<float> &b) { cout << "Size: " << b.shape_string() << endl; b.Reshape(1, 2, 3, 4); cout << "Size: " << b.shape_string() << endl; float* p = b.mutable_cpu_data(); float* q = b.mutable_cpu_diff(); for (int i = 0; i < b.count(); i++) { p[i] = i; // 將data初始化為 1,2,3... q[i] = b.count() - 1 - i; // 將diff初始化為23,22,21... } b.Update(); // 執行Update操作,將diff與data融合 // 這也是CNN權值更新步驟的最終實施者 for (int u = 0; u < b.num(); u++) { for (int v = 0; v < b.channels(); v++) { for (int w = 0; w < b.height(); w++) { for (int x = 0; x < b.width(); x++) { cout << "b[" << u << "][" << v << "][" << w << "][" << x << "] = " << b.data_at(u, v, w, x) << endl; } } } } cout << "ASUM = " << b.asum_data() << endl; cout << "SUMSQ = " << b.sumsq_data() << endl; } void test03(Blob<float> &a) { BlobProto bp; // 構造一個BlobProto物件 a.ToProto(&bp, true); // 將a 序列化,連同diff(預設不帶) WriteProtoToBinaryFile(bp, "a.blob"); // 寫入磁碟檔案"a.blog" BlobProto bp2; // 構造一個新的 BlobProto物件 ReadProtoFromBinaryFileOrDie("a.blob", &bp2); // 讀取磁碟檔案 Blob<float> b; b.FromProto(bp2, true); // 從序列化物件bp2中克隆b(連同形狀) cout << "output a.blob" << endl; for (int u = 0; u < b.num(); u++) { for (int v = 0; v < b.channels(); v++) { for (int w = 0; w < b.height(); w++) { for (int x = 0; x < b.width(); x++) { cout << "b[" << u << "][" << v << "][" << w << "][" << x << "] = " << b.data_at(u, v, w, x) << endl; } } } } } void TestNet() { // deploy.prototxt 來自models\bvlc_reference_caffenet\deploy.prototxt // debug除錯時把depoly.prototxt放在my_first_test工程同目錄下,直接執行exe時,放在exe同目錄下 std::string proto("deploy.prototxt"); Net<float> nn(proto, caffe::TEST); vector<string> bn = nn.blob_names(); // 獲取Net中所有blob物件名稱 for (int i = 0; i < bn.size(); i++) { cout << "Blob #" << i << " : " << bn[i] << endl; } } int main(void) { test01(); Blob<float> b; test02(b); test03(b); TestNet(); return 0; }

03 重新執行cmake-gui,重新生成vs工程

這裡寫圖片描述

04 執行my_first_test工程

設定my_first_test工程為啟動項,設定斷點,執行除錯。
這裡寫圖片描述