Win10 Caffe CUDA10.2 从零配置,支持Python接口

首先需要说明得是Caffe这个玩意已经停更很多年了,建议不要自己编译,Caffe源代码有问题,需要有改代码的能力

从我的下一篇文章,取编译好的玩玩就行了https://blog.csdn.net/u010039305/article/details/110926672

Caffe有些loss函数实现不完善,需要用户重新编写,但是python接口支持loss函数,很方便

如果打赏可以联系个人分享我用得pythonloss函数

1.Visual Studio 2013 或者 Visual Studio 2015

电脑上有哪个用哪个

2.Anaconda安装

这玩意用来虚拟环境的,可以各个环境切换,同时很方便的添加各种包

国内镜像下载位置,anaconda python 版本对应关系 https://blog.csdn.net/yuejisuo1948/article/details/81043823

Anaconda3-4.2.0 能勾选的都勾选(all user) next next

没有报错最好,有报错很烦;

打开Anaconda Prompt,

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ 添加源 非必要

conda config --set show_channel_urls yes 

conda config --remove channels defaults 还原源

conda create -n caffe python=3.5 等完成

activate caffe

conda install --yes cmake ninja numpy scipy protobuf==3.1.0 six scikit-image pyyaml pydotplus graphviz

conda install git

网速差很慢,已经打包到百度网盘https://pan.baidu.com/s/1ngz8h7JwdscryAt_1I8nbw yt8r

解压位置 *\Anaconda3\pkgs

(胳膊拧不过大腿,Microsoft Caffe 用python2.7 建议这里python=2.7

conda create -n py27 python=2.7

activate py27

conda install --yes numpy scipy matplotlib scikit-image pip

pip install protobuf

pip install numpy -i 源地址(最好指定国内源,不然速度非常慢)

pip install opencv-python==4.2.0.32)

3.CUDA cuDNN

根据驱动装个10.2

CUDA :https://developer.nvidia.com/cuda-toolkit-archive

cuDNN:https://developer.nvidia.com/rdp/cudnn-download

其实用这个版本,需要改caffe的源码,也不难,百度google信息整合下就可以了;

4.Caffe

git clone https://github.com/microsoft/caffe.git

下载比较慢 推荐直接下载ZIP,然后解压到你想放置的位置(Ps:git clone 的位置在C:\Users\"你的用户名"\)

以下内容等同https://github.com/Microsoft/caffe README.md

打开官方链接,下载caffe-master并解压保存在你的电脑中,如:C:\Users\"你的用户名"\caffe-master

进入你刚才保存windows_caffe的文件夹下,复制 .\windows\CommonSettings.props.example,在原地黏贴,并重命名 CommonSettings.props,说白了,这一步骤就是修改工程的属性文件,里面CUDA和  cuDNN库是默认被要求用到的,cuDNN和CPU_ONLY两个只能二选一,Python和matlab是被默认false的,也就是不用,你可以通过修改这个属性文件来个性化设置。

CUDA 如果不想安装,或者不能安装(没有显卡,或是显卡太旧),也可以用CUP_ONLY,只需在工程的属性文件里面修改一下即可,设置 CpuOnlyBuild为 true 并设置 UseCuDNN为false.

cuDNN Unpack downloaded zip to %CUDA_PATH% (environment variable set by CUDA installer). Alternatively, you can unpack zip to any location and set CuDnnPath to point to this location in .\windows\CommonSettings.propsCuDnnPath defined in .\windows\CommonSettings.props. Also, you can disable cuDNN by setting UseCuDNN to false in the property file

Python 把第2步的虚拟的环境路径 (我的 C:\Program Files\Anaconda3\envs\caffe\)放到.\windows\CommonSettings.props <PythonDir>
       matlab To build Caffe Matlab wrapper set MatlabSupport to true and MatlabDir to the root of your Matlab installation in .\windows\CommonSettings.props

下面是我的CommonSettings.props属性文件,仅供参考,建议使用Notepad++ 的 Compare 插件对比一下

NVIDA CUDA CudaArchitecture https://blog.csdn.net/jieleiping/article/details/53107254

Build

Now, you should be able to build .\windows\Caffe.sln 其实这只是入坑的开始

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ImportGroup Label="PropertySheets" />
    <PropertyGroup Label="UserMacros">
        <BuildDir>$(SolutionDir)..\Build</BuildDir>
        <!--NOTE: CpuOnlyBuild and UseCuDNN flags can't be set at the same time.-->
        <CpuOnlyBuild>false</CpuOnlyBuild>
        <UseCuDNN>true</UseCuDNN>
        <CudaVersion>10.2</CudaVersion>
        <!-- NOTE: If Python support is enabled, PythonDir (below) needs to be
         set to the root of your Python installation. If your Python installation
         does not contain debug libraries, debug build will not work. -->
        <PythonSupport>true</PythonSupport>
        <!-- NOTE: If Matlab support is enabled, MatlabDir (below) needs to be
         set to the root of your Matlab installation. -->
        <MatlabSupport>false</MatlabSupport>
        <CudaDependencies></CudaDependencies>

        <!-- Set CUDA architecture suitable for your GPU.
         Setting proper architecture is important to mimize your run and compile time. -->
        <CudaArchitecture>compute_35,sm_35;compute_61,sm_61</CudaArchitecture>

        <!-- CuDNN 4 and 5 are supported -->
        <CuDnnPath></CuDnnPath>
        <ScriptsDir>$(SolutionDir)\scripts</ScriptsDir>
    </PropertyGroup>
    <PropertyGroup Condition="'$(CpuOnlyBuild)'=='false'">
        <CudaDependencies>cublas.lib;cuda.lib;curand.lib;cudart.lib</CudaDependencies>
    </PropertyGroup>

    <PropertyGroup Condition="'$(UseCuDNN)'=='true'">
        <CudaDependencies>cudnn.lib;$(CudaDependencies)</CudaDependencies>
    </PropertyGroup>
    <PropertyGroup Condition="'$(UseCuDNN)'=='true' And $(CuDnnPath)!=''">
        <LibraryPath>$(CuDnnPath)\cuda\lib\x64;$(LibraryPath)</LibraryPath>
        <IncludePath>$(CuDnnPath)\cuda\include;$(IncludePath)</IncludePath>
    </PropertyGroup>

    <PropertyGroup>
        <OutDir>$(BuildDir)\$(Platform)\$(Configuration)\</OutDir>
        <IntDir>$(BuildDir)\Int\$(ProjectName)\$(Platform)\$(Configuration)\</IntDir>
    </PropertyGroup>
    <PropertyGroup>
        <LibraryPath>$(OutDir);$(CUDA_PATH)\lib\$(Platform);$(LibraryPath)</LibraryPath>
        <IncludePath>$(SolutionDir)..\include;$(SolutionDir)..\include\caffe\proto;$(CUDA_PATH)\include;$(IncludePath)</IncludePath>
    </PropertyGroup>
    <PropertyGroup Condition="'$(PythonSupport)'=='true'">
        <PythonDir>C:\Program Files\Anaconda3\envs\caffe\</PythonDir>
        <LibraryPath>$(PythonDir)\libs;$(LibraryPath)</LibraryPath>
        <IncludePath>$(PythonDir)\include;$(IncludePath)</IncludePath>
    </PropertyGroup>
    <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>
    <ItemDefinitionGroup Condition="'$(CpuOnlyBuild)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>CPU_ONLY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(UseCuDNN)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>USE_CUDNN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
        <CudaCompile>
            <Defines>USE_CUDNN</Defines>
        </CudaCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(PythonSupport)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>WITH_PYTHON_LAYER;BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(MatlabSupport)'=='true'">
        <ClCompile>
            <PreprocessorDefinitions>MATLAB_MEX_FILE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup>
        <ClCompile>
            <MinimalRebuild>false</MinimalRebuild>
            <MultiProcessorCompilation>true</MultiProcessorCompilation>
            <PreprocessorDefinitions>_SCL_SECURE_NO_WARNINGS;USE_OPENCV;USE_LEVELDB;USE_LMDB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
            <TreatWarningAsError>false</TreatWarningAsError>
        </ClCompile>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
        <ClCompile>
            <Optimization>Full</Optimization>
            <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
            <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
            <FunctionLevelLinking>true</FunctionLevelLinking>
        </ClCompile>
        <Link>
            <EnableCOMDATFolding>true</EnableCOMDATFolding>
            <GenerateDebugInformation>true</GenerateDebugInformation>
            <LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
            <OptimizeReferences>true</OptimizeReferences>
        </Link>
    </ItemDefinitionGroup>
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
        <ClCompile>
            <Optimization>Disabled</Optimization>
            <PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
            <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
        </ClCompile>
        <Link>
            <GenerateDebugInformation>true</GenerateDebugInformation>
        </Link>
    </ItemDefinitionGroup>
</Project>

NugetPackages\OpenCV.2.4.10\build\native\OpenCV.props 中$(MSBuildThisFileDirectory)\private的斜杠一定要去掉

NuGet Error:Unable to find version '2.4.10' of package 'OpenCV.overlay-x64_v120_Release'

类似这种错误给出全网唯一有效的解决办法:

 Install-Package OpenCV.overlay-x64_v120_Release -Version 2.4.10

注意红色部分 缺啥 下载啥 一般都是Realease

把 NugetPackages\OpenCV.overlay-x64_v120.2.4.10 下的build文件夹整个拷贝到NugetPackages\OpenCV.2.4.10

把NugetPackages\OpenCV.overlay-x64_v120_Release.2.4.10 下的build整个拷贝到NugetPackages\OpenCV.2.4.10

把NugetPackages\glog.overlay-x64_v120_Release_dynamic.0.3.3.0 下的build 整个拷贝到NugetPackages\glog.0.3.3.0

后面需要添加修改cudnn.hpp cudnn* 使得支持cuda10.2,这部分改变较多以后有时间传github也不难

conda create -n py27 python=2.7 用这个版本编译吧,3.5的window caffe就不支持!!!

https://blog.csdn.net/tsyccnh/article/details/51673474

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值