windows配置caffe及matlab/python接口编译和调用(cpu/gpu)

 

环境:windows 7+matlab2016a+vs2013

caffe下载地址:https://github.com/BVLC/caffe/tree/windows

1 进入caffe-windows的windows文件夹,Copy .windowsCommonSettings.props.example to .windowsCommonSettings.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 FilesMATLABR2016a</MatlabDir>
        <LibraryPath>$(MatlabDir)externlibwin64microsoft;$(LibraryPath)</LibraryPath>
        <IncludePath>$(MatlabDir)externinclude;$(IncludePath)</IncludePath>
    </PropertyGroup>

3  选择matcaffe项目,点击编译(会自动去下载第三方库),在Buildx64Release会生成相应的文件

4 将上面Buildx64Release绝对路径加入到系统环境path变量中,同时将Buildx64Releasematcaffe加入到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
原文地址:https://www.cnblogs.com/linyuanzhou/p/5913505.html