zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    [转帖]Mootools源码分析03 Hash
    iphone的手势与触摸编程学习笔记
    怎样使项目中的cocos2d默认模板支持ARC内存管理
    xCode4.2下添加TableViewController会出现”Prototype cells“警告
    关于31天App教程示例中一些因SDK版本而出现的问题
    带你掌握二进制SCA检测工具的短板及应对措施
    HDZ城市行深圳站|AIoT时代,如何抓住智联生活的战略机会点?
    分析内部运行机制,教你解决Redis性能问题
    今天谈谈用户故事地图,不是用户故事
    云图说|ModelArts Pro:让AI开发更简单
  • 原文地址:https://www.cnblogs.com/linyuanzhou/p/5913505.html
Copyright © 2011-2022 走看看