ubuntu18.04编译使用caffecpu使用工具示例训练示例(代码片段)

Hero_HL Hero_HL     2022-12-05     651

关键词:

测试使用docker ubuntu18.04 docker使用参考
远程工具为MobaXterm_20.0汉化版本,工具链接ubuntu18.04 镜像获取 基本软件安装 基本设置 QT安装 推荐远程链接软件

1. 安装caffe cpu

1.1 安装依赖

apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
apt-get install --no-install-recommends libboost-all-dev  
apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
apt-get install libatlas-base-dev
apt-get install python-numpy
apt-get install libhdf5-serial-dev
apt-get install python-dev
apt install python-pip
pip install scikit-image

1.2 获取caffe源码

(如果没有安装git)apt-get install git
git clone git://github.com/BVLC/caffe.git

1.3 修改配置文件

cd caffe/
cp Makefile.config.example Makefile.config
vim Makefile.config

左侧代表文件Makefile.config的行号

8 CPU_ONLY := 1
23 OPENCV_VERSION := 3
97 INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
98 LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

1.4 编译

make -j4 pycaffe
make -j4 all
make -j4 test
make -j4 runtest

1.5 测试caffe

vim ~/.bashrc

文件末尾加上
export PYTHONPATH=/root/caffe/python:$PYTHONPATH	# 实际caffe代码编译后的python文件夹

生效刚刚修改的文件
source ~/.bashrc

pip install protobuf==3.17.3(默认的版本报错)

1.6 添加caffe到环境变量

编译后生成目录为build,caffe和其他工具在build/tools目录下


拷贝这个目录,编译bashrc文件

vim ~/.bashrc
export PATH=/root/caffe/build/tools:$PATH
source ~/.bashrc

运行caffe,如图表示成功

2. 测试绘制神经网络结构图

2.1 获取测试数据

2.1.1 获取基础数据

进入caffe获取data测试的目录

apt-get install wget
cd /root/caffe/data/mnist
./get_mnist.sh

2.1.2 将基础数据转换成caffe使用的数据

脚本路径及内容

可以看到目录应该在caffe主目录下执行

此时在examples/mnist目录下生成了两个目录mnist_train_lmdb和mnist_test_lmdb就是我们所需的测试数据

2.2 环境依赖和准备文件

apt-get install graphviz
pip install pydot


创建了一个新目录,加入一个新文件vim hbk_mnist.prototxt 内容如下(两个source为刚刚用脚本获取到的数据实际目录/caffe/examples/mnist/mnist_train_lmdb和/caffe/examples/mnist/mnist_test_lmdb

name: "hbk_mnist"

# train/test lmdb数据层
layer 
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include 
    phase: TRAIN
  
  transform_param 
    scale: 0.00390625
  
  data_param 
    source: "/root/caffe/examples/mnist/mnist_train_lmdb"
    batch_size: 64
    backend: LMDB
  

layer 
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include 
    phase: TEST
  
  transform_param 
    scale: 0.00390625
  
  data_param 
    source: "/root/caffe/examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  


# 全连接层,激活层为ReLU   784->500->10
layer 
  name: "ip1"
  type: "InnerProduct"
  bottom: "data"
  top: "ip1"
  param 
    lr_mult: 1
  
  param 
    lr_mult: 2
  
  inner_product_param 
    num_output: 500
    weight_filler 
      type: "xavier"
    
    bias_filler 
      type: "constant"
    
  


layer 
  name: "relu1"
  type: "ReLU"
  bottom: "ip1"
  top: "re1"

layer 
  name: "ip2"
  type: "InnerProduct"
  bottom: "re1"
  top: "ip2"
  param 
    lr_mult: 1
  
  param 
    lr_mult: 2
  
  inner_product_param 
    num_output: 10
    weight_filler 
      type: "xavier"
    
    bias_filler 
      type: "constant"
    
  


# 测试验证用,不必须,输出准确率
layer 
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include 
    phase: TEST
  


# 代价Cost层
layer 
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"

2.3 使用Caffe自带的工具绘制神经网络结构图

绘制神经网络结构图的工具命令

python ../python/draw_net.py hbk_mnist.prototxt aa.png --rankdir=BT


此时就生成了对应的图像aa.png

python ../python/draw_net.py hbk_mnist.prototxt aa.png --rankdir=LR

3. caffe time 命令

评估模型执行的时间大概是多少(cpu) 如果是gpu需要后面添加 -gpu 0命令 (0代表gpu的id号)
需要先将 1.6 添加caffe到环境变量 设置好

caffe time -model hbk_mnist.prototxt -iterations 100

模型 -model hbk_mnist.prototxt
迭代次数 -iterations 100


可以看到结果100次迭代一共用了451ms

4. caffe train 训练命令


创建hbk_mnist_solver.prototxt文件,文件内容如下

# The train/test net 文件路径
net: "hbk_mnist.prototxt"

# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100

# 训练迭代多少次执行一次Test验证
test_interval: 500


# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005

# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75

# 多少次迭代输出一次信息
display: 100
# The maximum number of iterations
max_iter: 10001
# 存储中间结果
snapshot: 5000
snapshot_prefix: "snapshot"

# solver mode: CPU or GPU
solver_mode: CPU

执行命令开始训练

caffe train -solver hbk_mnist_solver.prototxt

输出结果

输出结果重定向到b.log (2标准输出也视作1错误输出)

caffe train -solver hbk_mnist_solver.prototxt >./b.log 2>&1
caffe train -solver hbk_mnist_solver.prototxt 2>&1 | tee a.log

此时运行后输出的结果就会打印到重定向后的文件中

5 caffe plot_training_log.py.example 将训练的日志导成图片工具使用

将日志打成图,更加直观看到训练结果

5.1 依赖安装(绘图和column命令)

apt install python-tk
apt install tk-dev
apt-get install bsdmainutils  (安装column命令,caffe脚本中需要用到column命令)

5.2 修改工具代码

plot_training_log.py.example文件前两行加上

import matplotlib
matplotlib.use('Agg')


5.3 运行查看使用说明

python plot_training_log.py.example

5.4 使用和结果

5.4.1 迭代次数和损失精度

注意当前目录(6: Train loss vs. Iters)

python ../tools/extra/plot_training_log.py.example 6 plotlog.png a.log


从docker中把图片拷到主机上

docker cp caffe:/root/caffe/mytrain/plotlog.png /mnt/hgfs/share/


可以看到在迭代了2000次以后损失精度已经下降不明显了,当然如果有时间机器不卡次数越多越好。看个人如何去取舍

5.4.2 准确度和迭代次数的关系图

0(Test accuracy vs. Iters)查看训练的结果的日志中的准确度和迭代次数的关系
python …/tools/extra/plot_training_log.py.example 0 plotlog_accuracy_vs_iters.png a.log

5.4.3 生成的其他的一些结果信息


可以看到多生成了文件a.log.test(测试信息)、a.log.train(训练信息)

参考

B站的视频:深度学习框架caffe视频教程
在Ubuntu上安装CPU版本的Caffe

ubuntu18.04编译使用caffecpu使用工具示例训练示例(代码片段)

...取基本软件安装基本设置QT安装推荐远程链接软件1.安装caffecpu1.1安装依赖apt-getinstalllibprotobuf-devlibleveldb-devlibsnappy-devlibopencv-devlibhdf5-serial-devprotobuf-compilerapt-getinstall--no-install-recommendslibboost-all-devapt-getinstalllibgflags-devlibgoogle-glog-devli... 查看详情

ubuntu18.04编译使用caffecpu及生成神经网络图(代码片段)

1安装caffecpu1.1安装依赖apt-getinstalllibprotobuf-devlibleveldb-devlibsnappy-devlibopencv-devlibhdf5-serial-devprotobuf-compilerapt-getinstall--no-install-recommendslibboost-all-devapt-getinstalllibgflags-devlibgoogle-glog-devliblmdb-devapt-getinstalllibatlas-base-devapt-getinstallpython-numpy... 查看详情

ubuntu18.04下cuda.cu在c/c++中的三种使用方式(代码片段)

...式使用cuda函数第三种方式使用cuda函数参考网站操作系统ubuntu18.04前提想要在.c文件中使用cuda的函数,即.cu的内容安装nvcc不是这里的内容,但是确保能使用nvcc,这是保证能编译.cu的前提,查看nvcc的版本命令如下nvc... 查看详情

ubuntu18.04下cuda.cu在c/c++中的三种使用方式(代码片段)

...式使用cuda函数第三种方式使用cuda函数参考网站操作系统ubuntu18.04前提想要在.c文件中使用cuda的函数,即.cu的内容安装nvcc不是这里的内容,但是确保能使用nvcc,这是保证能编译.cu的前提,查看nvcc的版本命令如下nvc... 查看详情

ubuntu18.04下cuda.cu在c/c++中的三种使用方式(代码片段)

...第二种方式使用cuda函数第三种方式使用cuda函数操作系统ubuntu18.04前提想要在.c文件中使用cuda的函数,即.cu的内容安装nvcc不是这里的内容,但是确保能使用nvcc,这是保证能编译.cu的前提,查看nvcc的版本命令如下nvc... 查看详情

ubuntu18.04手动编译安装ffmpeg(代码片段)

...的音频和视频的录影、转换和流处理等场合。这里记录在Ubuntu18.04平台下安装ffmpeg的过程。(注:ffmpeg目前已经可以通过apt直接进行安装,见最后) 安装过程  a.在ffmpeg官网上下载对应的安装压缩包,笔者使用的是ffmpeg-4. 查看详情

ns2基于ubuntu18.04(代码片段)

...、编写terminal环境下的变量参数步骤四、运行样例Ref环境Ubuntu18.04(VMware)安装流程步骤一、安装gcc编译器sudoapt-getinstallg++-4.8(注安装最新版g++会出现一些代码错误。Bing到参考[1],使用4.8版本不会有任何报错)步骤二、解压并编译文... 查看详情

ubuntu18.04编译zlmediakit支持webrtc

...s​​和​​ZlmediaKit​​项目是评价比较高的,今天主要在Ubuntu18.04上编译ZlmediaKit,并支持webrtc协议.准备源码准备下载zlmediakit源码及其依赖组件源码.gitclone--depth1https://github.com/ZLMediaKit/ZLMediaKit.git#下载依赖组件源码gitsubmoduleupdate--in... 查看详情

ubuntu18.04编译fortran出现‘没有f951这个文件’处理

...了f951,然后把他复制到某个位置就可以用了。我在自己的ubuntu里搜,没有f951这个文件。然后,我去安装包里找,也没有这个文件,我就去我另一个电脑(ubuntu16.04)里面找到了f951.把它复制到/usr/bin下面 查看详情

如何从 Ubuntu 18.04 使用 PHP 连接到 MSSQL Server?

】如何从Ubuntu18.04使用PHP连接到MSSQLServer?【英文标题】:HowtoconnecttoMSSQLServerwithPHPfromUbuntu18.04?【发布时间】:2020-03-0617:43:18【问题描述】:我想从Ubunutu18.04建立一个MSSQL连接。进行此设置非常困难,但知道它到目前为止可以使用... 查看详情

ubuntu18.04编译安装指定版本bison(gnu词法分析器)(代码片段)

一、问题描述Ubuntu18.04,腾讯源,无法直接使用apt-get安装bison。换源到官方源:Ubuntu恢复官方默认源。安装m4:sudoapt-getinstallm4安装bison:sudoapt-getinstallbison检查版本:bison-v出错,GLIBC_2.32未找到&#x 查看详情

Ubuntu 18.04 服务器 - 如何检查正在使用的 DNS IP 服务器设置

】Ubuntu18.04服务器-如何检查正在使用的DNSIP服务器设置【英文标题】:Ubuntu18.04Server-howtocheckDNSIPserversettingbeingused【发布时间】:2018-10-2206:42:17【问题描述】:使用Ubuntu18.04ServerLTS。我试图找到一种方法来检查通过DHCP设置时实际使... 查看详情

在 Ubuntu 18.04 上使用 Rocker 脚本构建 R Docker 容器

】在Ubuntu18.04上使用Rocker脚本构建RDocker容器【英文标题】:BuildingRDockerContainerwithRockerScriptsonUbuntu18.04【发布时间】:2021-08-0207:47:38【问题描述】:我遇到了一个可以通过回滚到仿生来解决的软件包问题。使用新的模块化摇杆系统... 查看详情

Ubuntu 18.04:无法使用单个命令提取下载的 tar.xz 文件

】Ubuntu18.04:无法使用单个命令提取下载的tar.xz文件【英文标题】:Ubuntu18.04:Cannotextractdownloadedtar.xzfilewithasinglecommand【发布时间】:2019-12-1223:08:45【问题描述】:我正在尝试使用单行命令下载和提取tar.xz文件。但是,它并非始终... 查看详情

ubuntu18.04lts使用xmanager连接图形桌面

参考技术Asudoapt-getinstallopenssh-serversudovim/etc/ssh/sshd_configsudopasswdrootsudoapt-getinstalllightdmsudoufwdisable或sudoufwallow177/udpsudoufwenable 查看详情

如何在ubuntu18.04中使用privoxy实现ubuntu终端的scientificsurftheinternet(代码片段)

总结一下如何在Ubuntu18.04中使用privoxy实现ubuntu终端的scientificsurftheInternet安装privoxysudoapt-getinstallprivoxy如果安装遇到报错E:Sub-process/usr/bin/dpkgreturnedanerrorcode(1)"的错误可以在/var/lib/dpkg目录下执行sudocpinfoinfo.ba 查看详情

ubuntu 18.04 登录循环

】ubuntu18.04登录循环【英文标题】:ubuntu18.04loginloop【发布时间】:2020-08-1816:23:59【问题描述】:我使用的是ubuntu18.04。我对/etc/default/grub文件进行了更改并导致登录循环。tty命令仍然可以使用。我撤消了未更改的内容,但登录循... 查看详情

qt编译zlib完成文件压缩解压(ubuntu18.04)(代码片段)

...b开源就可以很方便的完成。接下来介绍在zlib在Linux(Ubuntu18.04)下如何利用Qt进行编译,调用,完成zip文件打包生成例子。可以单文件压缩打包,多文件压缩打包,目录压缩打包等等。如果需要在windows下使... 查看详情