Using Caffe with your own dataset

  1. Dataset preparation
  2. Caffe network files preparation
  3. From scratch training and finetuning

Preparing dataset

Usual situation looks like this — you have pictures downloaded from Internet, or from other source. Let’s say we solve classification problem and I hope you at least saved your different class pictures to different folders. Almost the same structure has Stanford Dogs Breeds Dataset. You can download it from http://vision.stanford.edu/aditya86/ImageNetDogs/.

Afghan hound
...
63_n02101006-Gordon_setter138.jpg 63
37_n02095314-wire-haired_fox_terrier75.jpg 37
16_n02089973-English_foxhound133.jpg 16
67_n02102177-Welsh_springer_spaniel105.jpg 67
32_n02093859-Kerry_blue_terrier28.jpg 32
48_n02097298-Scotch_terrier137.jpg 48
25_n02091831-Saluki52.jpg 25
....
Images moving to one folder and train.txt, val.txt generation

Converting to LMDB

To feed Caffe with large images dataset it’s good choice to use LMDB format for our dataset. We already have an example of a script in Caffe folder (I suppose you have Caffe built on your machine) here caffe/examples/imagenet/create_imagenet.sh.

  • EXAMPLE=examples/dogs : where we are going to store LMDB
  • DATA=data/dogs/dogs_data : folder with dogs train.txt, val.txt
  • TRAIN_DATA_ROOT : folder with train images
  • VAL_DATA_ROOT : folder with test images (with script above it’s same folder)
  • RESIZE=true : we need to resize all photos to same size
  • And following piece of code:
GLOG_logtostderr=1 $TOOLS/convert_imageset \
— resize_height=$RESIZE_HEIGHT \
— resize_width=$RESIZE_WIDTH \
— shuffle \
$TRAIN_DATA_ROOT \
$DATA/train.txt \
$EXAMPLE/dogs_train_lmdb
echo “Creating val lmdb…”
GLOG_logtostderr=1 $TOOLS/convert_imageset \
— resize_height=$RESIZE_HEIGHT \
— resize_width=$RESIZE_WIDTH \
— shuffle \
$VAL_DATA_ROOT \
$DATA/val.txt \
$EXAMPLE/dogs_val_lmdb
  • Above we set $DATA/train.txt, $DATA/val.txt and $EXAMPLE/dogs_train_lmdb, $EXAMPLE/dogs_val_lmdb

Preparation network architecture

Let’s open network architecture files in caffe/models. We should change bold things in the next snippet: size of images (from RESIZE=true) and paths to LMDB images (from $EXAMPLE/dogs_train_lmdb, $EXAMPLE/dogs_val_lmdb) in file train_val.prototxt. In our case, let’s try with GoogleNet.

name: "GoogleNet"
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size: 224
mean_value: 104
mean_value: 117
mean_value: 123
}
data_param {
source: "examples/dogs/dogs_train_lmdb"
batch_size: 32
backend: LMDB
}
}
layer {
name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
mirror: false
crop_size: 224
mean_value: 104
mean_value: 117
mean_value: 123
}
data_param {
source: "examples/dogs/dogs_val_lmdb"
batch_size: 40
backend: LMDB
}
}
layer {
name: "loss1/classifier"
type: "InnerProduct"
bottom: "loss1/fc"
top: "loss1/classifier"
param {
lr_mult: 1
decay_mult: 1
}
param {
lr_mult: 2
decay_mult: 0
}
inner_product_param {
num_output: 120
weight_filler {
type: "xavier"
std: 0.0009765625
}
bias_filler {
type: "constant"
value: 0
}
}
}
layer {
name: "loss1/loss"
type: "SoftmaxWithLoss"
bottom: "loss1/classifier"
bottom: "label"
top: "loss1/loss1"
loss_weight: 0.3
}
layer {
name: "loss1/top-1"
type: "Accuracy"
bottom: "loss1/classifier"
bottom: "label"
top: "loss1/top-1"
include {
phase: TEST
}
}
layer {
name: "loss1/top-5"
type: "Accuracy"
bottom: "loss1/classifier"
bottom: "label"
top: "loss1/top-5"
include {
phase: TEST
}
accuracy_param {
top_k: 5
}
}
layer {
name: "loss1/loss"
type: "SoftmaxWithLoss"
bottom: "loss1/classifier_dog"
bottom: "label"
top: "loss1/loss1"
loss_weight: 0.3
}

Training and finetuning

For training a network from scratch we already prepared everything we need. Following line from ./caffe will run your training process.

./build/tools/caffe train -solver models/bvlc_googlenet/quick_solver.prototxt -gpu 0
./build/tools/caffe train -solver models/bvlc_googlenet/quick_solver.prototxt -weights models/bvlc_googlenet/bvlc_googlenet.caffemodel -gpu 0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store