Miscellaneous Things about Caffe




Media IC & System Lab
Presenter: Yu Sheng Lin
Instructor: Shao Yi Chien

Outlines

  • Libraries used in Caffe
  • Caffe Design
  • How to use Caffe

Libraries Used in Caffe

glog, protobuf, gflag, snappy, leveldb, gtest

All are open libraries by Google
(Wow, they like Google very much)

There are still benchmark library and so on

glog

Google LOGging library

A very simple tutorial

Initialize


#include <glog/logging.h>
int main(int argc, char* argv[]) {
	google::InitGoogleLogging(argv[0]);
				

glog API (1/3)

Simple Usage


// INFO, WARNING, ERROR, FETAL
LOG(INFO) << "Your code is running";
			

Condition


LOG_IF(WARNING, speedup < 1.0)
	<< "Your median filter is "
	<< speedup << "x slower";
			

glog API (2/3)

Log with Counter/Limitation


LOG_FIRST_N(INFO, 20) << google::COUNTER;
LOG_IF_EVERY_N(INFO, (size > 1024), 10) << google::COUNTER;
			

Debug


DLOG(INFO) << "Won't appear using NDEBUG";
			

glog API (3/3)

Shortcut


CHECK(random() <= RAND_MAX) << "Your library has bug";
CHECK_NE(1, 2) << "Your compiler has bug";
CHECK_NOTNULL(some_ptr); // Return the pointer itself, so do not use <<
			

Verbosity


FLAGS_v = 1;
VLOG(0) << "This will not be printed";
VLOG(100) << "This will be printed";
			

You may usually set FLAGS_* variables

gprotobuf

Data serializer

Here, we use the term "serialization" to mean the reversible deconstruction of an arbitrary set of C++ data structures to a sequence of bytes.
From boost serialization library

  • Cross platform - boost
  • Cross language

(Caffe use it to store network)

Cross Language


struct Member {
  void from_file(FILE *fp) {
    fread(...
					

Language 1


class Member:
  def from_file(self, fp):
    fp.read...
					

Language 2



package tutorial;

message Person {
	required string name = 1;
	required int32 id = 2;
	optional bool isMale = 3;
	repeated string email = 4;
}
				

Compile to different target language

Usage

Reading/Writing file


using namespace tutorial; // Correspond to "package tutorial;"
fstream in("in", ios::in | ios::binary);
ostream out("out", ios::out | ios::binary);
Person person;
person.ParseFromIstream(&in);
person.SerializeToOstream(&out);
			

Generated code will has (but is not limited to) these APIs


person.id();
person.clear_id();
person.set_id();
person.has_id();
person.email(); // return a vector like object
person.email(123); // repeated only
person.email_size(); // repeated only
person.add_email(); // repeated only
			

gtest (1/2)

An Unittest Tool

  • A person claims:
    • I have writen many small modules (units)
    • They will work as long as I assemble them
    • Do you believe?
  • Write test
    • After each time you modify the code
    • Test every unit you modified
  • Test-driven design (TDD)
    • Write testing then write your code
    • Implicit architecture design at first

gtest (2/2)

A simple example


TEST(PrimeTest, Positive) {
    EXPECT_TRUE(IsPrime(2));
    EXPECT_TRUE(IsPrime(199));
    EXPECT_FALSE(IsPrime(2000));
}
TEST(PrimeTest, Invalid) {
    EXPECT_FALSE(IsPrime(0));
    EXPECT_FALSE(IsPrime(-9999));
}
int main() {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
			

self-explanatory, I think

snappy, leveldb and gflags

  • snappy
    • Very fast file (de)compress library (~10x zlib)
    • But zipped files are larger
  • leveldb
    • Database, but no SQL, use it with API
    • Very fast
    • Can use snappy to compress
    • Caffe input data format
  • gflags
    • Command line parser
    • More convinient and flexible than getopt
    • Lightweight compared to boost

Caffe Design (1/3)

  • Layer
    • Data, loss, ReLU...
    • Modularity
  • Blob
    • Copy-on-write, GPU-CPU synchronized
    • Holds data $f(x)$ and gradient $f'(x)$
    • 4D matrix for holding layer parameter, gradient

Caffe Design (2/3)

Training Steps

  • Each layer implements these functions:
    1. Setup: Uh, setup.
    2. Forward: Bottom data → top data
    3. Backward: Top gradient → gradient
      1. of its parameter and
      2. passed to its bottom

Caffe Design (3/3)

Backward and Forward

OK I like to draw by myself.

  • Yellow: forward
  • Red: backward
  • Green/Blue: data reuse

How to Use Caffe

  • Generally speaking, use Caffe means:
    1. Used a trained model
    2. Train a model


1 is included in 2 so I will only talk about 2

Reference: 1, 2

A Working Flow

  • Preprocessing
    • Data-augumentation (noise, flip...)
    • Normalize (size, zero mean, uniform variance...)
  • Database preparation
    • Prepare dataset description files
    • Convert to leveldb format
  • Network design
    • Describe in protobuf format
  • Train
  • Use network

Preprocessing

  • Data-augumentation
    • A "regularization" step (See ML slides)
    • +noise, projective transform, JPEG and whatever
    • Tell to machine "hey, this is also a good data"
    • The more, the better, but also slower
  • Normalize
    • To the same size
    • Zero mean, uniform variance
      • Caffe can do it

Define Your Network

Define in protobuf text format


name: "LogReg"
layers {
  name: "mnist"
  type: DATA
  top: "data"
  top: "label"
  data_param {
    source: "input_leveldb"
    batch_size: 64
  }
}
...
				
  • top: output blobs
  • bottom: input blobs

Data Preparation

For both training and testing

Description text file


Kongou.jpg 1
Yamato.jpg 1
Bismarck.jpg 1
Akagi.jpg 2
Shoukaku.jpg 2
Yukikaze.jpg 3
Hibiki.jpg 3
Fubuki.jpg 3
...
			

And put these images into a directory

← If we want to classify: (1)battleship, (2)aircraft carrier or (3)destroyer


convert_imageset \
    <dir> <txt> <output> \
    [1] [-backend leveldb] [w h]
			
  • 1: shuffle
  • backend: leveldb or lmdb
  • w&h: resize
  • The tool is provided by Caffe

Training! (1/2)


caffe train -solver your_solver.prototxt
			
  • Solver is another prototxt file
  • Use net: to specify your net file
  • Also defines gradient paramters here
  • You can also assign iterations count and finetune a model
  • You can "snapshot" the training (why?)



Training!

I Wanna Test (Validate) my NN

  • Use include: { phase: TEST/TRAIN }
    to notate your layer
  • Train phase only
    • A data layer
  • Test phase only
    • Another data layer (you must split them)
    • An accuracy layer


Caffe run 1 test phase after several train phases as long as you define in your solver

Deployment

Hey My NN is Good

See Alexnet for example

  • Another NN prototxt file
  • Slightly differs from your original net prototxt
    • Input data layer format is changed
    • No more initialization parameters
    • No softmax_loss/accuracy layers, but a softmax layer
  • (To be confirmed) Will Caffe generate 1 for you from your solver?

The End

Fast review

  • Libraries used in Caffe
  • Caffe Design
  • How to use Caffe