The Visionary Researcher

기본 틀 구성 및 DB 다운로드

1. 새로운 폴더 생성

2. Traffic sign dataset 주소 : http://benchmark.ini.rub.de/?section=gtsrb&subsection=dataset

3. Training, Test dataset download 후 압축 풀어서 폴더로 이동


The training data set contains 39,209 training images in 43 classes.

The test dataset contains 12,630 test images or the corresponding pre-calculated features in random order.

  • Extended annotations including class ids: Download (98 kB)

4. imagenet example 파일

cp 새로운폴더주소 caffe-dir/example/imagenet/

(create_imagenet.sh, create_imagenet_mean.sh, readme.md, resume_training.sh, train_caffenet.sh)

5. bvlc reference 파일

cp 새로운폴더주소 caffe-dir/models/bvlc_reference_caffenet/train_val.prototxt

cp 새로운폴더주소 caffe-dir/models/bvlc_reference_caffenet/solver.prototxt



LMDB를 이용하여 DB 구성

이유 : caffe에서 data format 3가지

(LMDB, LevelDB, hdf5->matlab사용가능)

LMDB 구성방법 : http://stackoverflow.com/questions/31427094/guide-to-use-convert-imageset-cpp

[create_imagenet.sh 변경]

1. 폴더, 이미지, label txt파일 구성

>새로운 폴더

>>위에에서 다운받은 imagen일et, blvc 총 7개 파일

>>database

>>>images

>>>>train_images

>>>>>train 이미지들

(폴더 안에 보통 여러개의 세부폴더로 클래스가 나뉘면서 존재)

[train db를 옮겨줍니다.]

>>>>test_images

>>>>>test 이미지들

(폴더 안에 일정한 포맷으로 존재)

[test db를 옮겨줍니다.]

>>>labels

>>>>train_labels.txt

(00000.ppm 16 와 같은 포맷으로 구성)

[matlab을 이용하여 txt파일을 구성합니다]

>>>>test_labels.txt

(00000/00000_00000.ppm 0 와 같은 포맷으로 구성)

[matlab을 이용하여 txt파일을 구성합니다]

>>>lmdb

>>>>train_lmdb(절대 만들어 놓지 말 것-오류발생)

>>>>test_lmdb(절대 만들어 놓지 말 것-오류발생)


*.label txt파일 구하는 매틀랩 코드

clear, close all
%%make test DB

%%make train DB
train_dir = dir('./database/images/train_images/');
train_dir(1:2) = [];
train_txt = fopen('train.txt', 'w+');

for i = 1 : length(train_dir)
    temp = train_dir(i).name;
    train_dir1 = dir(['./database/images/train_images/',temp, '/*.ppm']);
    for j = 1 : length(train_dir1)
        fprintf(train_txt, '%s%c%s %d\n', temp,'/',train_dir1(j).name, i-1);
    end
end

fclose(train_txt);

test db는 label과 같이 존재하는 csv파일을 이용하여 바로 구성


2. create_imagenet.sh 수정

#!/usr/bin/env sh
# Create the imagenet lmdb inputs
# N.B. set the path to the imagenet train + val data dirs

EXAMPLE=./database/lmdb
DATA=./database/labels
TOOLS=../caffe-master/build/tools

TRAIN_DATA_ROOT=./database/images/train_images/
VAL_DATA_ROOT=./database/images/test_images/

# Set RESIZE=true to resize the images to 256x256. Leave as false if images have
# already been resized using another tool.
RESIZE=true
if $RESIZE; then
  RESIZE_HEIGHT=256
  RESIZE_WIDTH=256
else
  RESIZE_HEIGHT=0
  RESIZE_WIDTH=0
fi

if [ ! -d "$TRAIN_DATA_ROOT" ]; then
  echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"
  echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet training data is stored."
  exit 1
fi

if [ ! -d "$VAL_DATA_ROOT" ]; then
  echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"
  echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \
       "where the ImageNet validation data is stored."
  exit 1
fi

echo "Creating train lmdb..."

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/train_labels.txt \
    $EXAMPLE/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/test_labels.txt \
    $EXAMPLE/test_lmdb/

echo "Done."

주의 할 점은 RESIZE=true로 바꿀 것(나중에 make_imagenet_mean.sh할 때 오류)

example의 위치는 lmdb 위치(lmdb 내부에 폴더 생성하지 말것)

data의 위치는 label txt파일이 있는 위치

tools의 위치는 원래 caffe-dir로 설정(이 곳에 convert_imageset함수 존재)

모든 이미지를 LMDB형태로 구성합니다.


3. ./create_imagenet.sh 수행




Generate mean file

1. make_imagenet_mean.sh 수정

#!/usr/bin/env sh
# Compute the mean image from the imagenet training lmdb
# N.B. this is available in data/ilsvrc12

EXAMPLE=./database/lmdb
DATA=./database/labels
TOOLS=../caffe-master/build/tools

$TOOLS/compute_image_mean $EXAMPLE/train_lmdb \
  $DATA/imagenet_mean.binaryproto

echo "Done."

주의 할 점은 lmdb폴더 내부를 지울 것

또한 이 과정이 지나면 imagenet_mean.binaryproto 파일이 생성된다.


2. ./make_imagenet_mean.sh 수행




Train and Test

1. train_caffenet.sh 수정

#!/usr/bin/env sh

../caffe-master/build/tools/caffe train \
    --solver=./solver.prototxt


2. train_val.prototxt 수정

crop size : 227로 수정

mean_file : 주소 수정

source_file : 주소 수정

num_output : 1000->43(fc8 layer)

(class 43개이므로)

batch_size : 20으로 조정

(nvidia-smi 이용하여 테스트 할 때 gpu memory check하면서

batch size 올릴 수 있으면 올린다)

[한번에 읽어오는 이미지 수]


3. solver.prototxt 수정

base_lr : 0.001 (수렴 안되면 더 낮출걸)

snapshot_prefix : 주소 수정(snapshot 저장)

solver_mode : GPU (CPU로 할거면 CPU)


4. ./train_caffenet.sh



로그 저장하는 방법

./train_caffenet.sh 2>&1 | tee -a path/to/text/log/file.txt



Matcaffe 이용한 Test class accuracy

1. compile matcaffe wrapper

cd caffe-dir

sudo gedit Makefile.config

-> MATLAB_DIR := /home/seokeon/MATLAB/R2015b

mat all matcaffe

make mattest

matlab

2. 데모 실행

caffe-master/matlab/demo/classification_demo.m 이용하여 수정

function [scores, maxlabel] = classification_demo(im, use_gpu)
% [scores, maxlabel] = classification_demo(im, use_gpu)
%
% Image classification demo using BVLC CaffeNet.
%
% IMPORTANT: before you run this demo, you should download BVLC CaffeNet
% from Model Zoo (http://caffe.berkeleyvision.org/model_zoo.html)
%
% ****************************************************************************
% For detailed documentation and usage on Caffe's Matlab interface, please
% refer to Caffe Interface Tutorial at
% http://caffe.berkeleyvision.org/tutorial/interfaces.html#matlab
% ****************************************************************************
%
% input
%   im       color image as uint8 HxWx3
%   use_gpu  1 to use the GPU, 0 to use the CPU
%
% output
%   scores   1000-dimensional ILSVRC score vector
%   maxlabel the label of the highest score
%
% You may need to do the following before you start matlab:
%  $ export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:/usr/local/cuda-5.5/lib64
%  $ export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6
% Or the equivalent based on where things are installed on your system
%
% Usage:
%  im = imread('../../examples/images/cat.jpg');
%  scores = classification_demo(im, 1);
%  [score, class] = max(scores);
% Five things to be aware of:
%   caffe uses row-major order
%   matlab uses column-major order
%   caffe uses BGR color channel order
%   matlab uses RGB color channel order
%   images need to have the data mean subtracted

% Data coming in from matlab needs to be in the order
%   [width, height, channels, images]
% where width is the fastest dimension.
% Here is the rough matlab for putting image data into the correct
% format in W x H x C with BGR channels:
%   % permute channels from RGB to BGR
%   im_data = im(:, :, [3, 2, 1]);
%   % flip width and height to make width the fastest dimension
%   im_data = permute(im_data, [2, 1, 3]);
%   % convert from uint8 to single
%   im_data = single(im_data);
%   % reshape to a fixed size (e.g., 227x227).
%   im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');
%   % subtract mean_data (already in W x H x C with BGR channels)
%   im_data = im_data - mean_data;

% If you have multiple images, cat them with cat(4, ...)

% Add caffe/matlab to you Matlab search PATH to use matcaffe
if exist('../+caffe', 'dir')
  addpath('..');
else
  error('Please run this demo from caffe/matlab/demo');
end

% Set caffe mode
if exist('use_gpu', 'var') && use_gpu
  caffe.set_mode_gpu();
  gpu_id = 0;  % we will use the first gpu in this demo
  caffe.set_device(gpu_id);
else
  caffe.set_mode_cpu();
end

% Initialize the network using BVLC CaffeNet for image classification
% Weights (parameter) file needs to be downloaded from Model Zoo.
model_dir = '../../models/bvlc_reference_caffenet/';
net_model = [model_dir 'deploy.prototxt'];
net_weights = [model_dir 'bvlc_reference_caffenet.caffemodel'];
phase = 'test'; % run with phase test (so that dropout isn't applied)
if ~exist(net_weights, 'file')
  error('Please download CaffeNet from Model Zoo before you run this demo');
end

% Initialize a network
net = caffe.Net(net_model, net_weights, phase);

if nargin < 1
  % For demo purposes we will use the cat image
  fprintf('using caffe/examples/images/cat.jpg as input image\n');
  im = imread('../../examples/images/cat.jpg');
end

% prepare oversampled input
% input_data is Height x Width x Channel x Num
tic;
input_data = {prepare_image(im)};
toc;

% do forward pass to get scores
% scores are now Channels x Num, where Channels == 1000
tic;
% The net forward function. It takes in a cell array of N-D arrays
% (where N == 4 here) containing data of input blob(s) and outputs a cell
% array containing data from output blob(s)
scores = net.forward(input_data);
toc;

scores = scores{1};
scores = mean(scores, 2);  % take average scores over 10 crops

[~, maxlabel] = max(scores);

% call caffe.reset_all() to reset caffe
caffe.reset_all();

% ------------------------------------------------------------------------
function crops_data = prepare_image(im)
% ------------------------------------------------------------------------
% caffe/matlab/+caffe/imagenet/ilsvrc_2012_mean.mat contains mean_data that
% is already in W x H x C with BGR channels
d = load('../+caffe/imagenet/ilsvrc_2012_mean.mat');
mean_data = d.mean_data;
IMAGE_DIM = 256;
CROPPED_DIM = 227;

% Convert an image returned by Matlab's imread to im_data in caffe's data
% format: W x H x C with BGR channels
im_data = im(:, :, [3, 2, 1]);  % permute channels from RGB to BGR
im_data = permute(im_data, [2, 1, 3]);  % flip width and height
im_data = single(im_data);  % convert from uint8 to single
im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear');  % resize im_data
im_data = im_data - mean_data;  % subtract mean_data (already in W x H x C, BGR)

% oversample (4 corners, center, and their x-axis flips)
crops_data = zeros(CROPPED_DIM, CROPPED_DIM, 3, 10, 'single');
indices = [0 IMAGE_DIM-CROPPED_DIM] + 1;
n = 1;
for i = indices
  for j = indices
    crops_data(:, :, :, n) = im_data(i:i+CROPPED_DIM-1, j:j+CROPPED_DIM-1, :);
    crops_data(:, :, :, n+5) = crops_data(end:-1:1, :, :, n);
    n = n + 1;
  end
end
center = floor(indices(2) / 2) + 1;
crops_data(:,:,:,5) = ...
  im_data(center:center+CROPPED_DIM-1,center:center+CROPPED_DIM-1,:);
crops_data(:,:,:,10) = crops_data(end:-1:1, :, :, 5);


수정해야 할 것

+caffe가 있는 dir

addpath dir

model_dir

net_model

net_weights

d

mean_data

cropped_dim




mean_im.mat

compute_mean.m




댓글 0

http://caffe.berkeleyvision.org/gathered/examples/mnist.html

wget 혹은 gunzip 설치 (sudo apt-get install xx이용)


데이터 준비

1. cd caffe-master(caffe가 설치된 dir)

2. ./data/mnist/get_mnist.sh

3. ./examples/mnist/create_mnist.sh


Training and Test

1. ./examples/mnist/train_lenet.sh

아래와 같은 결과 생성

99.05%의 정확도

binary proto file 생성 (examples/mnist/lenet_iter_10000.solverstate)

I0429 13:06:59.745952 19500 sgd_solver.cpp:273] Snapshotting solver state to binary proto file examples/mnist/lenet_iter_10000.solverstate
I0429 13:06:59.748658 19500 solver.cpp:317] Iteration 10000, loss = 0.00336479
I0429 13:06:59.748695 19500 solver.cpp:337] Iteration 10000, Testing net (#0)
I0429 13:06:59.840243 19500 solver.cpp:404]     Test net output #0: accuracy = 0.9905
I0429 13:06:59.840282 19500 solver.cpp:404]     Test net output #1: loss = 0.0290998 (* 1 = 0.0290998 loss)
I0429 13:06:59.840291 19500 solver.cpp:322] Optimization Done.
I0429 13:06:59.840297 19500 caffe.cpp:222] Optimization Done.


LeNet이란?

1. MNIST classification model

2. caffe-master/examples/mnist/lenet_train_test.prototxt의 내용

name: "LeNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "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: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  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"
    }
  }
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"
  bottom: "pool2"
  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: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  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
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

3. caffe-master/examples/mnist/lenet_solver.prototxt의 내용

# The train/test net protocol buffer definition
net: "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.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU


댓글 0

BVLC caffe

1. https://github.com/BVLC/caffe에서 Download ZIP이용해서 save

2. 압축해제

3. cd Downloads/caffe-master (압축 해제)

4. caffe-master folder를 home으로 이동

5. cd caffer-master

6. sudo cp Makefile.config.example Makefile.config

(이름 변경)

7. sudo gedit Makefile.config

8. 원하는 주석 해제

CUDNN사용할 것이므로 USE_CUDNN = 1 주석 제거

9. make all -j4(CPU가 쿼드코어인경우, 옥타코어면 -j8)

10. make test -j4

11. make runtest -j4 (모두 pass가 나오는지 확인)


lisa-caffe

1. https://github.com/LisaAnne/lisa-caffe-public/tree/lstm_video_deploy에서 Download ZIP이용해서 save

2. 압축해제

3. cd Downloads/lisa-caffe-public-lstm_video_deploy (압축 해제)

4. lisa-caffe-public-lstm_video_deploy folder를 home으로 이동

5. cd lisa-caffe-public-lstm_video_deploy

6. sudo cp Makefile.config.example Makefile.config

(이름 변경)

7. sudo gedit Makefile.config

8. 원하는 주석 해제

CUDNN사용할 것이므로 USE_CUDNN = 1 주석 제거

9. make all -j4(CPU가 쿼드코어인경우, 옥타코어면 -j8)


cblas.h 문제가 있을 경우 3. cudnn에서 아래의 dependency 수행

댓글 0

티스토리 툴바