Go Caffe installation, compilation, and experimentation under Linux

Source: Internet
Author: User

The first part: Caffe introduction

Caffe is a Berkeley visual and Learning Center (BVLC) developed. The author is Dr. Berkeley Jia Yangqing.
Caffe is a deep learning (learning) framework. It has easy-to-read, fast and modular thinking.

Part II: Caffe Installation and Configuration

2.1 Configuring the Environment: Ubuntu 14.04LTS, using homebrew for installation. The GPU is not used, so use the cpu-only mode. No python is used, so there is no support library for Python.

2.2 Support Library

2.2.1 uses homebrew to install the required libraries, including: Boost snappy leveldb protobuf gflags glog szip lmdb HOMEBREW/SCIENCE/OPENCV

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compilersudo apt-get install --no-install-recommends libboost-all-devsudo apt-get install libatlas-base-devsudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

2.2.2:apt-get Introduction:

Apt-get is a Linux command for the Deb package managed operating system that is used to automatically search, install, upgrade, uninstall software, or the operating system from the Internet's software warehouse.

2.3 Introduction to the required libraries:

2.3.1 Snappy

Snappy is a C + + development package for compressing and decompressing. The goal is not to maximize compression or compatibility with other compression formats, but to provide high speed compression and reasonable compression. Snappy is faster than zlib, but files are 20% to 100% larger. On the Core i7 processor in 64-bit mode, the compression speed of 250~500 MB per second is up.

2.3.2 Leveldb

Leveldb is a very efficient KV database implemented by Google, and the current version 1.2 is capable of supporting billion levels of data. There is also a very high performance at this level, thanks largely to its good design. Especially the LSM algorithm.

2.3.3 GFlags

GFlags is an open source for Google to handle command line parameters of the library, using C + + development, with Python interface, can replace the getopt.
GFlags is easier to use than getopt, but it does not support shorthand for parameters.

2.3.4 Glog

Google Glog is a C + + language application-level logging framework that provides C + +-style streaming operations and a variety of helper macros.

2.3.5 Szip

Szip is a fast, excellent, cross-platform open source data compression program.

2.3.6 Lmdb

Lmdb is an embedded (embedded as a library into the host program) storage engine developed by the OPENLDAP project.

2.3.7 HDF5

Hierarchical Data Format (HDF), can store different types of image and digital data file format, and can be transferred on different types of machines, as well as the uniform processing of this file format library. This file format is supported by most ordinary computers.

2.3.8 OpenCV

The full name of OPENCV is: Open Source computer Vision Library. OpenCV is a BSD-licensed (open source) distributed, cross-platform computer Vision library that can run on Linux, Windows, Android, and Mac OS operating systems. It is lightweight and efficient-consisting of a series of C functions and a small number of C + + classes, and provides interfaces to Python, Ruby, Matlab, and many more general-purpose algorithms for image processing and computer vision.

OpenCV is written in the C + + language, and its main interface is the C + + language, but still retains a large number of C-language interfaces. The library also has a large number of interfaces for Python, Java and Matlab/octave (version 2.5). API interface functions for these languages are available through online documentation. Support for C#,ch, Ruby is also available today.

2.3.9 Protobuf

Protocolbuffer is a format for Google's data interchange, which is language independent and platform independent. Google offers implementations in multiple languages: Java, C #, C + +, go and Python, each of which contains compilers and library files for the appropriate language. Because it is a binary format, it is much faster than using XML for data exchange. It can be used in data communication between distributed applications or in heterogeneous environments. As a good efficiency and compatibility of the binary data transmission format, can be used for such as network transport, configuration files, data storage and many other areas.

2.3.10 Boost

The boost library is a generic term for some C + + libraries that provide extensions for the C + + language standard library.

Part III Caffe compiling

3 Compiling Caffe with make

Under the path of the caffe$:

#caffe文件夹中默认含有一个示例Makefile,只要去复制修改这个文件就可以了cp Makefile.config.example Makefile.config

Then open makefile.config to modify, I do not have GPU here, so use cpu-only mode. So the counter-comment dropped cpu_only: = 1

make allmake testmake runtest

After the above Caffe even after the successful compilation!

Part IV uses Caffe to perform lenet training on mnist datasets

4.1 Mnist Introduction

Mnists is a handwritten digital electronic version of the data set, which contains 60000 training sets, 10000 test sets, all in 32*32 format.

4.2 Lenet Introduction

Lenet is a network for handwritten numeral classification, the author is Yann LeCun. Specifically, you can query:

Http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf

When used here, the sigmoid activation function in the original method is changed to the Relu activation function.

4.3 Data Set Preparation

Download the required data and convert it to the desired data format

cd $CAFFE_ROOT./data/mnist/get_mnist.sh./examples/mnist/creat_mnist.sh

Definition of 4.4 mnist Network

The network is defined in the Lenet_train_test.prototxt. The models in Caffe are defined in the Google Protobuf way.

4.4.1 Definition Name

name: "LeNet"

4.4.2 Defining the data layer

layer {  name: "mnist"  type: "Data"  transform_param {    scale: 0.00390625  }  data_param {    source: "mnist_train_lmdb"    backend: LMDB    batch_size: 64  }  top: "data"  top: "label"}

4.4.2 Defining convolutional Layers

layer {  name: "conv1"  type: "Convolution"  param { lr_mult: 1 }  param { lr_mult: 2 }  convolution_param {    num_output: 20    kernel_size: 5    stride: 1    weight_filler {      type: "xavier"    }    bias_filler {      type: "constant"    }  }  bottom: "data"  top: "conv1"}

4.4.3 Defining a pooled layer

layer {  name: "pool1"  type: "Pooling"  pooling_param {    kernel_size: 2    stride: 2    pool: MAX  }  bottom: "conv1"  top: "pool1"}

4.4.4 defining an all-connected layer

layer {  name: "ip1"  type: "InnerProduct"  param { lr_mult: 1 }  param { lr_mult: 2 }  inner_product_param {    num_output: 500    weight_filler {      type: "xavier"    }    bias_filler {      type: "constant"    }  }  bottom: "pool2"  top: "ip1"}

4.4.5 Defining Relu Layers

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

4.4.6 Defining the loss layer

layer {  name: "loss"  type: "SoftmaxWithLoss"  bottom: "ip2"  bottom: "label"}

4.5 Definition Solver

4.5.1 Solver Introduction

Solver the goal of reducing loss by coordinating the forward inference calculation and inverse gradient calculation of net in Caffe to update the parameters.

4.5.2 definition mnist Solver

Defined in $caffe_root/examples/mnist/lenet_solver.prototxt:

# The train/test net protocol buffer definitionnet: "examples/mnist/lenet_train_test.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# Carry out testing every 500 training iterations.test_interval: 500# The base learning rate, momentum and the weight decay of the network.base_lr: 0.01momentum: 0.9weight_decay: 0.0005# The learning rate policylr_policy: "inv"gamma: 0.0001power: 0.75# Display every 100 iterationsdisplay: 100# The maximum number of iterationsmax_iter: 10000# snapshot intermediate resultssnapshot: 5000snapshot_prefix: "examples/mnist/lenet"# solver mode: CPU or GPUsolver_mode: GPU

4.6 Training and test models

Run the script, this script has written all the training and testing

cd $CAFFE_ROOT./examples/mnist/train_lenet.sh

A message similar to the following is flashed on the screen:

I1203 net.cpp:66] Creating Layer conv1I1203 net.cpp:76] conv1 <- dataI1203 net.cpp:101] conv1 -> conv1I1203 net.cpp:116] Top shape: 20 24 24I1203 net.cpp:127] conv1 needs backward computation.

This explains how each layer in the network works.

Finally, the following information is available:

I1203 solver.cpp:84] Testing netI1203 solver.cpp:111] Test score #0: 0.9897I1203 solver.cpp:111] Test score #1: 0.0324599I1203 solver.cpp:126] Snapshotting to lenet_iter_10000I1203 solver.cpp:133] Snapshotting solver state to lenet_iter_10000.solverstateI1203 solver.cpp:78] Optimization Done.

The network iterates 10,000 times, and the final accurary is 0.9897,loss is 0.0324599

Go Caffe installation, compilation, and experimentation under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.