caffe---create自己的数据出现的各种bug

caffe---create自己的数据出现的各种bug

本文转载自:http://blog.csdn.net/dcxhun3/article/details/51966921

目前bug主要是create_imagenet.sh(来源于examples/imagenet)生成lmdb数据时产生的

bug 1  mkdir  *_val_lmdb failed 

这个一般是因为指定路径下已经存在了该文件,导致出现冲突问题,我最开始对于这问题是每次都手动敲码删除该文件,最后发现自己很笨,可以直接加个语句到create_imagenet.sh中:

[python] view plain copy
 caffe---create自己的数据出现的各种bugcaffe---create自己的数据出现的各种bug
  1. rm -rf $EXAMPLE/mytask_train_lmdb  
  2. rm -rf $EXAMPLE/mytask_val_lmdb  

caffe---create自己的数据出现的各种bug 

bug 2 找不到指定路径下的图片could not open or find file

第一个情况是我在windows cmd下生成的txt标签文件,这里路径是反斜杠,我没有注意到。解决的最好办法就是打开txt文件,将反斜杠替换为斜杠。要么就是在linux下运行make_list.py就不会出现这个问题了。

caffe---create自己的数据出现的各种bug

第二种情况,这个着实困扰了我好久,怎么也搞不懂,路径明明对着了,为啥就不对呢?百思不得其解。。。最后才发现是python里面的转义字符 \t 搞的鬼  在图片名和标签之间的空格用\t表示的,解决这个问题的办法是用 ‘ ’代替了,好了:

[python] view plain copy
 caffe---create自己的数据出现的各种bugcaffe---create自己的数据出现的各种bug
  1. #fout.write('%s\t%d\n'%(image_list[i][0], image_list[i][1]))  
  2.        fout.write('%s%s%d\n'%(image_list[i][0], ' ',image_list[i][1]))#space not \t   

caffe---create自己的数据出现的各种bug

正确情况,开始生成lmdb 数据比较大啊 378430图像  比较耗时

caffe---create自己的数据出现的各种bug

代码一

make_list.py

[python] view plain copy
 caffe---create自己的数据出现的各种bugcaffe---create自己的数据出现的各种bug
  1. import fnmatch,os  
  2. import random  
  3. import numpy as np  
  4. import argparse  
  5.   
  6. def list_image(root, recursive, exts):  
  7.     image_list = []  
  8.     if recursive:  
  9.         cat = {}  
  10.         for path, subdirs, files in os.walk(root,True):  
  11.             print path  
  12.             for fname in files:  
  13.                 fpath = os.path.join(path,fname)  
  14.                 suffix = os.path.splitext(fname)[1].lower()  
  15.                 if os.path.isfile(fpath) and (suffix in exts):  
  16.                     if path not in cat:  
  17.                         cat[path] = len(cat)  
  18.                     image_list.append((os.path.relpath(fpath, root), cat[path]))  
  19.                #    print fpath,cat[path]  
  20.     else:  
  21.         for fname in os.listdir(root):  
  22.             fpath = os.path.join(root, fname)  
  23.             suffix = os.path.splitext(fname)[1].lower()  
  24.             if os.path.isfile(fpath) and (suffix in exts):  
  25.                 image_list.append((os.path.relpath(fpath, root), 0))  
  26.     return image_list  
  27.   
  28. def write_list(path_out, image_list):  
  29.     with open(path_out, 'w') as fout:  
  30.         for i in xrange(len(image_list)):  
  31.             #fout.write('%d \t %d \t %s\n'%(i, image_list[i][1], image_list[i][0]))  
  32.             #fout.write('%s\t%d\n'%(image_list[i][0], image_list[i][1]))  
  33.             fout.write('%s%s%d\n'%(image_list[i][0], ' ',image_list[i][1]))#space not \t   
  34. def make_list(prefix_out, root, recursive, exts, num_chunks, train_ratio):  
  35.     image_list = list_image(root, recursive, exts)  
  36.     random.shuffle(image_list)  
  37.     N = len(image_list)  
  38.     chunk_size = (N+num_chunks-1)/num_chunks  
  39.     for i in xrange(num_chunks):  
  40.         chunk = image_list[i*chunk_size:(i+1)*chunk_size]  
  41.         if num_chunks > 1:  
  42.             str_chunk = '_%d'%i  
  43.         else:  
  44.             str_chunk = ''  
  45.         if train_ratio < 1:  
  46.             sep = int(chunk_size*train_ratio)  
  47.             write_list(prefix_out+str_chunk+'_train.txt', chunk[:sep])  
  48.             write_list(prefix_out+str_chunk+'_val.txt', chunk[sep:])  
  49.         else:  
  50.             write_list(prefix_out+str_chunk+'.txt', chunk)  
  51.   
  52. def main():  
  53.     parser = argparse.ArgumentParser(  
  54.         formatter_class=argparse.ArgumentDefaultsHelpFormatter,  
  55.         description='Make image list files that are\  
  56.         required by im2rec')  
  57.     parser.add_argument('root', help='path to folder that contain images.')  
  58.     parser.add_argument('prefix', help='prefix of output list files.')  
  59.     parser.add_argument('--exts', type=list, default=['.bmp','.bmp'],  
  60.         help='list of acceptable image extensions.')  
  61.     parser.add_argument('--chunks', type=int, default=1, help='number of chunks.')  
  62.     parser.add_argument('--train_ratio', type=float, default=1.0,  
  63.         help='Percent of images to use for training.')  
  64.     parser.add_argument('--recursive', type=bool, default=True,  
  65.         help='If true recursively walk through subdirs and assign an unique label\  
  66.         to images in each folder. Otherwise only include images in the root folder\  
  67.         and give them label 0.')  
  68.     args = parser.parse_args()  
  69.       
  70.     make_list(args.prefix, args.root, args.recursive,  
  71.         args.exts, args.chunks, args.train_ratio)  
  72.   
  73. if __name__ == '__main__':  
  74.     main()  
代码二

create_imagenet.sh 

[python] view plain copy
 caffe---create自己的数据出现的各种bugcaffe---create自己的数据出现的各种bug
  1. #!/usr/bin/env sh  
  2. # Create the imagenet lmdb inputs  
  3. # N.B. set the path to the imagenet train + val data dirs  
  4. EXAMPLE=examples/mytask  
  5. DATA=/mnt/hgfs/caffe  
  6. TOOLS=build/tools  
  7. TRAIN_DATA_ROOT=/mnt/hgfs/caffe/train/  
  8. VAL_DATA_ROOT=/mnt/hgfs/caffe/val/  
  9. # Set RESIZE=true to resize the images to 256x256. Leave as false if images have  
  10. # already been resized using another tool.  
  11. RESIZE=true  
  12. if $RESIZE; then  
  13.   RESIZE_HEIGHT=256  
  14.   RESIZE_WIDTH=256  
  15. else  
  16.   RESIZE_HEIGHT=0  
  17.   RESIZE_WIDTH=0  
  18. fi  
  19. if [ ! -d "$TRAIN_DATA_ROOT" ]; then  
  20.   echo "Error: TRAIN_DATA_ROOT is not a path to a directory: $TRAIN_DATA_ROOT"  
  21.   echo "Set the TRAIN_DATA_ROOT variable in create_imagenet.sh to the path" \  
  22.        "where the ImageNet training data is stored."  
  23.   exit 1  
  24. fi  
  25. if [ ! -d "$VAL_DATA_ROOT" ]; then  
  26.   echo "Error: VAL_DATA_ROOT is not a path to a directory: $VAL_DATA_ROOT"  
  27.   echo "Set the VAL_DATA_ROOT variable in create_imagenet.sh to the path" \  
  28.        "where the ImageNet validation data is stored."  
  29.   exit 1  
  30. fi  
  31. echo "Creating train lmdb..."  
  32. rm -rf $EXAMPLE/mytask_train_lmdb  
  33. rm -rf $EXAMPLE/mytask_val_lmdb  
  34. GLOG_logtostderr=1 $TOOLS/convert_imageset \  
  35.     --resize_height=$RESIZE_HEIGHT \  
  36.     --resize_width=$RESIZE_WIDTH \  
  37.     --shuffle \  
  38.     $TRAIN_DATA_ROOT \  
  39.     $DATA/train.txt \  
  40.     $EXAMPLE/mytask_train_lmdb  
  41. echo "Train lmdb done!"  
  42. echo "Creating val lmdb..."  
  43. GLOG_logtostderr=1 $TOOLS/convert_imageset \  
  44.     --resize_height=$RESIZE_HEIGHT \  
  45.     --resize_width=$RESIZE_WIDTH \  
  46.     --shuffle \  
  47.     $VAL_DATA_ROOT \  
  48.     $DATA/val.txt \  
  49.     $EXAMPLE/mytask_val_lmdb  
  50. echo "val lmdb done!"  
  51. echo "Done."