Water's Home

Just another Life Style

0%

MNIST : Caffe

Prepare

wget http://deeplearning.net/data/mnist/mnist.pkl.gz

Loading data from pkl

import os
import pickle, gzip
from matplotlib import pyplot

print(‘Loading data from mnist.pkl.gz …’)
with gzip.open(‘mnist.pkl.gz’, ‘rb’) as f:
train_set, valid_set, test_set = pickle.load(f)

imgs_dir = ‘mnist’
os.system(‘mkdir -p {}’.format(imgs_dir))
datasets = {‘train’: train_set, ‘val’: valid_set, ‘test’: test_set}

for dataname, dataset in datasets.items():
print(‘Converting {} dataset …’.format(dataname))
data_dir = os.sep.join([imgs_dir, dataname])

os.system('mkdir -p {}'.format(data\_dir))

for i, (img, label) in enumerate(zip(\*dataset)):

    filename = '{:0>6d}\_{}.jpg'.format(i, label)

    filepath = os.sep.join(\[data\_dir, filename\])

    img = img.reshape((28, 28))

    pyplot.imsave(filepath, img, cmap='gray')
    if (i + 1) % 10000 == 0:
        print('{} images converted!'.format(i + 1))

Prepare imglist for Caffe

import os
import sys

input_path = sys.argv[1].rstrip(os.sep)

output_path = sys.argv[2]

filenames = os.listdir(input_path)
with open(output_path, ‘w’) as f:
for filename in filenames:

    filepath = os.sep.join(\[input\_path, filename\])

    label = filename\[: filename.rfind('.')\].split('\_')\[1\]

    line = '{} {}\\n'.format(filepath, label)
    f.write(line)

Convert to LMDB

python gen_caffe_imglist.py mnist/train train.txt
python gen_caffe_imglist.py mnist/val val.txt
python gen_caffe_imglist.py mnist/test test.txt

/home/d/Documents/caffe/build/tools/convert_imageset ./ train.txt train_lmdb –gray –shuffle
/home/d/Documents/caffe/build/tools/convert_imageset ./ val.txt val_lmdb –gray –shuffle
/home/d/Documents/caffe/build/tools/convert_imageset ./ test.txt test_lmdb –gray –shuffle

Train (LeNet-5)

/home/d/Documents/caffe/build/tools/caffe train -solver lenet_solver.prototxt -log_dir ./

Log Visualization

loss_iters

python /home/d/Documents/caffe/tools/extra/plot_training_log.py.example 2 loss_iters.png caffe.ubuntu.d.log

accuracy_iters

python /home/d/Documents/caffe/tools/extra/plot_training_log.py.example 0 accuracy_iters.png caffe.ubuntu.d.log

Test Model Accuracy

/home/d/Documents/caffe/build/tools/caffe test -model lenet_test.prototxt -weights mnist_lenet_iter_36000.caffemodel -iterations 100

Test Model Time

/home/d/Documents/caffe/build/tools/caffe time -model lenet.prototxt

Augmented, Train

loss_iters

python /home/d/Documents/caffe/tools/extra/plot_training_log.py.example 2 loss_iters_aug.png mnist_train.log mnist_train_with_augmentation.log

accuracy_iters

python /home/d/Documents/caffe/tools/extra/plot_training_log.py.example 0 accuracy_iters_aug.png mnist_train.log mnist_train_with_augmentation.log

Update solver max_iter, Train

/home/d/Documents/caffe/build/tools/caffe train -solver lenet_solver_aug.prototxt -snapshot mnist_aug_lenet_iter_36000.solverstate -log_dir ./