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
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值