【问题标题】:How to create own dataset for FCN with caffe?如何使用 caffe 为 FCN 创建自己的数据集?
【发布时间】:2017-10-17 02:25:02
【问题描述】:

如何将图像转换为 lmdb for fcn with caffe?你知道,用caffe创建自己的图像分类数据集很容易,但是如何为fcn创建自己的语义片段数据集呢?

【问题讨论】:

    标签: dataset caffe lmdb


    【解决方案1】:

    使用此代码。进行必要的路径更改。使用前请仔细阅读代码。

    import caffe
    import lmdb
    from PIL import Image
    import numpy as np
    import glob
    from random import shuffle
    
    # Initialize the Image set:
    
    NumberTrain = 1111 # Number of Training Images
    
    NumberTest = 1112 # Number of Testing Images
    
    Rheight = 380 # Required Height
    
    Rwidth = 500 # Required Width
    
    LabelHeight = 380 # Downscaled height of the label
    
    LabelWidth = 500 # Downscaled width of the label
    
    
    # Read the files in the Data Folder
    
    inputs_data_train = sorted(glob.glob("/home/<user>/caffe-with_crop/examples/fcn-32s/TrainData/*.jpg"))
    inputs_data_valid = sorted(glob.glob("/home/<user>/caffe-with_crop/examples/fcn-32s/ValData/*.jpg"))
    inputs_label = sorted(glob.glob("/home/<user>/caffe-with_crop/examples/fcn-32s/VOC2011/SegmentationClass/*.png"))
    
    shuffle(inputs_data_train) # Shuffle the DataSet
    shuffle(inputs_data_valid) # Shuffle the DataSet
    
    inputs_Train = inputs_data_train[:NumberTrain] # Extract the training data from the complete set
    
    inputs_Test = inputs_data_valid[:NumberTest] # Extract the testing data from the complete set
    
    # Creating LMDB for Training Data
    
    print("Creating Training Data LMDB File ..... ")
    
    in_db = lmdb.open('/home/<user>/caffe-with_crop/examples/fcn-32s/TrainVOC_Data_lmdb',map_size=int(1e14))
    
    with in_db.begin(write=True) as in_txn:
    
        for in_idx, in_ in enumerate(inputs_Train):
            print in_idx
            im = np.array(Image.open(in_)) # or load whatever ndarray you need
            Dtype = im.dtype
            im = im[:,:,::-1]
            im = Image.fromarray(im)
            im = im.resize([Rheight, Rwidth], Image.ANTIALIAS)
            im = np.array(im,Dtype)     
    
            im = im.transpose((2,0,1))
            im_dat = caffe.io.array_to_datum(im)
            in_txn.put('{:0>10d}'.format(in_idx),im_dat.SerializeToString())
    
    in_db.close()
    
    # Creating LMDB for Training Labels
    
    print("Creating Training Label LMDB File ..... ")
    
    in_db = lmdb.open('/home/<user>/caffe-with_crop/examples/fcn-32s/TrainVOC_Label_lmdb',map_size=int(1e14))
    
    with in_db.begin(write=True) as in_txn:
    
        for in_idx, in_ in enumerate(inputs_Train):
            print in_idx
            in_label = in_[:-25]+'VOC2011/SegmentationClass/'+in_[-15:-3]+'png' # Change the numbers as per requirement
            L = np.array(Image.open(in_)) # or load whatever ndarray you need
            Dtype = L.dtype
            L = L[:,:,::-1]
            Limg = Image.fromarray(L)
            Limg = Limg.resize([LabelHeight, LabelWidth],Image.NEAREST) # To resize the Label file to the required size 
    
            L = np.array(Limg,Dtype)
    
            L = L.reshape(L.shape[0],L.shape[1],1)
    
            L = L.transpose((2,0,1))
    
            L_dat = caffe.io.array_to_datum(L)
            in_txn.put('{:0>10d}'.format(in_idx),L_dat.SerializeToString())
    
    in_db.close()
    
    # Creating LMDB for Testing Data
    
    print("Creating Testing Data LMDB File ..... ")
    
    in_db = lmdb.open('/home/<user>/caffe-with_crop/examples/fcn-32s/TestVOC_Data_lmdb',map_size=int(1e14))
    
    with in_db.begin(write=True) as in_txn:
    
        for in_idx, in_ in enumerate(inputs_Test):
            print in_idx    
            im = np.array(Image.open(in_)) # or load whatever ndarray you need
            Dtype = im.dtype
            im = im[:,:,::-1]
            im = Image.fromarray(im)
            im = im.resize([Rheight, Rwidth], Image.ANTIALIAS)
            im = np.array(im,Dtype)     
    
            im = im.transpose((2,0,1))
            im_dat = caffe.io.array_to_datum(im)
            in_txn.put('{:0>10d}'.format(in_idx),im_dat.SerializeToString())
    
    in_db.close()
    
    # Creating LMDB for Testing Labels
    
    print("Creating Testing Label LMDB File ..... ")
    
    in_db = lmdb.open('/home/<user>/caffe-with_crop/examples/fcn-32s/TestVOC_Label_lmdb',map_size=int(1e14))
    
    with in_db.begin(write=True) as in_txn:
    
        for in_idx, in_ in enumerate(inputs_Test):
            print in_idx    
            in_label = in_[:-25]+'VOC2011/SegmentationClass/'+in_[-15:-3]+'png' # Change the numbers as per requirement
            L = np.array(Image.open(in_)) # or load whatever ndarray you need
            Dtype = L.dtype
            L = L[:,:,::-1]
            Limg = Image.fromarray(L)
            Limg = Limg.resize([LabelHeight, LabelWidth],Image.NEAREST) # To resize the Label file to the required size 
    
            L = np.array(Limg,Dtype)
    
            L = L.reshape(L.shape[0],L.shape[1],1)
    
            L = L.transpose((2,0,1))
    
            L_dat = caffe.io.array_to_datum(L)
            in_txn.put('{:0>10d}'.format(in_idx),L_dat.SerializeToString())
    
    in_db.close()
    

    【讨论】:

    • 嗨,伙计,我该如何解决这个问题,我知道标签图像应该是单通道的,但它报告了这个错误。回溯(最后一次调用):文件“/home/ubuntu/eclipse-workspace/trainDataset/train.py”,第 79 行,在 L = L.reshape(L.shape[0],L.shape[ 1],1) ValueError: 新数组的总大小必须保持不变@Harsh Wardhan
    • 您是否检查了图像尺寸和脚本中给出的尺寸?
    • 我已经检查过了,我所有的图像尺寸都是512*512,但是我已经用NIVIDIA DIGITS简单地解决了这个问题,谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    相关资源
    最近更新 更多