天天看點

windows配置caffe及matlab/python接口編譯和調用(cpu/gpu)

環境:windows 7+matlab2016a+vs2013

caffe下載下傳位址:https://github.com/BVLC/caffe/tree/windows

1 進入caffe-windows的windows檔案夾,Copy 

.\windows\CommonSettings.props.example

 to 

.\windows\CommonSettings.props

2 打開caffe工程,編輯CommonSettings.props檔案,以下是cpu版本設定

        <CpuOnlyBuild>true</CpuOnlyBuild>

        <UseCuDNN>false</UseCuDNN>

        <CudaVersion>7.5</CudaVersion>

        <PythonSupport>false</PythonSupport>

        <MatlabSupport>true</MatlabSupport>

        <CudaDependencies></CudaDependencies>

    <PropertyGroup Condition="\'$(MatlabSupport)\'==\'true\'">

        <MatlabDir>C:\Program Files\MATLAB\R2016a</MatlabDir>

        <LibraryPath>$(MatlabDir)\extern\lib\win64\microsoft;$(LibraryPath)</LibraryPath>

        <IncludePath>$(MatlabDir)\extern\include;$(IncludePath)</IncludePath>

    </PropertyGroup>

3  選擇matcaffe項目,點選編譯(會自動去下載下傳第三方庫),在Build\x64\Release會生成相應的檔案

4 将上面Build\x64\Release絕對路徑加入到系統環境path變量中,同時将Build\x64\Release\matcaffe加入到matlab路徑中。

5 重新啟動matlab,調用caffe.reset_all(),則說明ok。

>> caffe.reset_all();

Cleared 0 solvers and 0 stand-alone nets

>>

python使用

下載下傳安裝anaconda

安裝protobuf:在指令行輸入:pip install protobuf

使用spyder,并且設定Python路徑

import caffe

caffe在matlab中使用:

function train()
solver_def_file = \'model/lenet_solver.prototxt\';
caffe.set_mode_cpu();
caffe.reset_all();
solver = caffe.Solver(solver_def_file);
% solver.solve();%一次性疊代

close all;
hold on%畫圖用的 
iter_ = solver.iter();
while iter_<10000
    solver.step(1);%一步一步疊代
    iter_ = solver.iter();    
    loss=solver.net.blobs(\'loss\').get_data();%取訓練集的loss  
    if iter_==1
        loss_init = loss;
    else
        y_l=[loss_init loss];
        x_l=[iter_-1, iter_];     
        plot(x_l, y_l, \'r-\');
        drawnow
        loss_init = loss;
    end
    
    if mod(iter_, 100) == 0
        accuracy=solver.test_nets.blobs(\'accuracy\').get_data();%取驗證集的accuracy       
        if iter_/100 == 1
            accuracy_init = accuracy;
        else 
            x_l=[iter_-100, iter_];
            y_a=[accuracy_init accuracy];
            plot(x_l, y_a,\'g-\');
            drawnow
            accuracy_init=accuracy;
        end
    end
end      

測試

function test()
net = init_net();
im_data = 255-caffe.io.load_image(\'image/00082.png\');
res = net.forward({im_data});
[~, idx ]= max(res{1});
disp(idx-1);


function net = init_net()
caffe.set_mode_cpu();
caffe.reset_all();
deploy = \'model/lenet_deploy.prototxt\';
caffe_model = \'snapshot/lenet_iter_10000.caffemodel\';
net = caffe.Net(deploy, caffe_model, \'test\');      

微調

function retrain()
caffe.set_mode_cpu();
caffe.reset_all();
caffe_model = \'snapshot/lenet_iter_10000.caffemodel\';
solver = caffe.Solver(\'model/lenet_solver.prototxt\');
solver.net.copy_from(caffe_model);
% solver.solve();

close all;
hold on%畫圖用的 
iter_ = solver.iter();
while iter_<10000
    solver.step(1);
    iter_ = solver.iter();    
    loss=solver.net.blobs(\'loss\').get_data();%取訓練集的loss  
    if iter_==1
        loss_init = loss;
    else
        y_l=[loss_init loss];
        x_l=[iter_-1, iter_];     
        plot(x_l, y_l, \'r-\');
        drawnow
        loss_init = loss;
    end
    
    if mod(iter_, 100) == 0
        accuracy=solver.test_nets.blobs(\'accuracy\').get_data();%取驗證集的accuracy       
        if iter_/100 == 1
            accuracy_init = accuracy;
        else 
            x_l=[iter_-100, iter_];
            y_a=[accuracy_init accuracy];
            plot(x_l, y_a,\'g-\');
            drawnow
            accuracy_init=accuracy;
        end
    end
end