ubuntu18.04编译使用 caffe cpu 及生成神经网络图

Posted Hero_HL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ubuntu18.04编译使用 caffe cpu 及生成神经网络图相关的知识,希望对你有一定的参考价值。

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 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(默认的版本报错)

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

2.1 获取测试数据

进入caffe获取data测试的目录

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

2.2 环境依赖和准备文件

apt-get install graphviz
pip install pydot


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

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

以上是关于ubuntu18.04编译使用 caffe cpu 及生成神经网络图的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu18.04编译使用 caffe cpu 使用工具示例 训练示例

ubuntu18.04编译使用 caffe cpu 及生成神经网络图

Ubuntu 18.04+VS Code +Python + Anaconda + caffe(CPU)配置

Ubuntu 18.04+VS Code +Python + Anaconda + caffe(CPU)配置

ubuntu18.04+ opencv3.1+caffe-ssd安装

Ubuntu16.04安装Caffe详细教程(CPU)