7.caffe:create_lmdb.sh(數據預處理轉換成lmdb格式)


個人實踐代碼如下:

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

EXAMPLE=/home/wp/CAFFE/caffe-master/myself/00b
DATA=/home/wp/CAFFE/caffe-master/myself/00b
TOOLS=build/tools

TRAIN_DATA_ROOT=/home/wp/CAFFE/caffe-master/myself/00b/train/
VAL_DATA_ROOT=/home/wp/CAFFE/caffe-master/myself/00b/val/

# 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=101
  RESIZE_WIDTH=101
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.txt \
    $EXAMPLE/00b_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/00b_val_lmdb

echo "Done."

# cd CAFFE/caffe-master
# sh ./myself/00b/create_lmdb.sh

結果生成兩個文件:00b_train_lmdb.sh; 00b_val_lmdb.sh

參考一:

由於參數比較多,因此我們可以編寫一個sh腳本來執行命令:

首先,創建sh腳本文件:

# sudo vi examples/images/create_lmdb.sh

編輯,輸入下面的代碼並保存

[cpp]
#!/usr/bin/en sh
DATA=examples/images
rm -rf $DATA/img_train_lmdb
build/tools/convert_imageset --shuffle \
--resize_height=256 --resize_width=256 \
/home/xxx/caffe/examples/images/ $DATA/train.txt  $DATA/img_train_lmdb

 注釋:/convert_imageset --shuffle \  //使用shuffle  

  • --resize_height=256 --resize_width=256 \ //圖片的大小都會調用opencv來獲得固定的大小  
  • /opt/modules/caffe-master/examples/images/ \ // 圖片的絕對存儲路徑  
  • /opt/modules/caffe-master/examples/images/train.txt \ // 文件的列表信息  
  • /opt/modules/caffe-master/examples/images/img_train_lmdb \ //最終生成的數據庫保存的路徑

設置參數-shuffle,打亂圖片順序。設置參數-resize_height和-resize_width將所有圖片尺寸都變為256*256.

/home/xxx/caffe/examples/images/ 為圖片保存的絕對路徑。

最后,運行這個腳本文件

[cpp]
# sudo sh examples/images/create_lmdb.sh

就會在examples/images/ 目錄下生成一個名為 img_train_lmdb的文件夾,里面的文件就是我們需要的db文件了

上面就將圖像數據轉換成db(leveldb/lmdb)文件了。

 

參考二:

create_filelist.sh后 接着再編寫一個腳本文件create_lmdb.sh,調用convert_imageset命令來轉換數據格式。

# sudo vi examples/myfile/create_lmdb.sh

插入:

 
#!/usr/bin/env sh MY=examples/myfile echo "Create train lmdb.." rm -rf $MY/img_train_lmdb build/tools/convert_imageset \ --shuffle \ --resize_height=256 \ --resize_width=256 \ /home/xxx/caffe/data/re/ \ $MY/train.txt \ $MY/img_train_lmdb echo "Create test lmdb.." rm -rf $MY/img_test_lmdb build/tools/convert_imageset \ --shuffle \ --resize_width=256 \ --resize_height=256 \ /home/xxx/caffe/data/re/ \ $MY/test.txt \ $MY/img_test_lmdb echo "All Done.."

因為圖片大小不一,因此我統一轉換成256*256大小。運行成功后,會在 examples/myfile下面生成兩個文件夾img_train_lmdb和img_test_lmdb,分別用於保存圖片轉換后的lmdb文件。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM