我正在尝试将一组灰度图像和地面实况图像转换为LMDB以进行分割任务。我创建了4个文本文件(train.txt
,train_label.txt
,val.txt
,val_label.txt
),指示每个文件夹中每个图片的路径。
很多天,它花了我的时间,但我仍然没有成功。我是python
和caffe
的初学者。如果有人在这里帮助我,我将感激不尽。
我尝试了以下代码:
import os
import numpy as np
from scipy import io
import lmdb
import caffe
from PIL import Image
NUM_IDX_DIGITS = 10
IDX_FMT = '{:0>%d' % NUM_IDX_DIGITS + 'd}'
# src
def img_to_lmdb(paths_src_file, path_dst):
"""
paths_src: src img paths
path_dst: src img lmdb file
"""
print("Creating Training Data LMDB File ..... ")
paths_src = []
with open(paths_src_file) as f:
for line in f.readlines():
#print line
line = line.strip('\n')
paths_src.append(line)
#path_dst = 'train-lmdb'
in_db = lmdb.open(path_dst, map_size=int(1e12))
with in_db.begin(write=True) as in_txn:
for in_idx, in_ in enumerate(paths_src):
print in_idx, in_
# load image:
# - as np.uint8 {0, ..., 255}
# - in BGR (switch from RGB)
# - in Channel x Height x Width order (switch from H x W x C)
im = np.array(Image.open(in_)) # or load whatever ndarray you need
###############
print im.shape
im = np.expand_dims(im, axis=0)
print im.shape
###################
im = im[:,:,::-1]
#im = im[:,:]
im = im.transpose((2,0,1)) # convert to CxHxW
im_dat = caffe.io.array_to_datum(im)
in_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString())
in_db.close()
print 'end.'
# label
def label_to_lmdb(paths_label_file, path_dst):
"""
paths_src: label img paths
path_dst: label lmdb file
"""
print("Creating Training Label LMDB File ..... ")
paths_src = []
with open(paths_label_file) as f:
for line in f.readlines():
line = line.strip('\n')
paths_src.append(line)
in_db = lmdb.open(path_dst, map_size=int(1e12))
with in_db.begin(write=True) as in_txn:
for in_idx, in_ in enumerate(paths_src):
print in_idx, in_
# load image:
# - as np.uint8 {0, ..., 255}
# - in BGR (switch from RGB)
# - in Channel x Height x Width order (switch from H x W x C)
im = np.array(Image.open(in_)) # or load whatever ndarray you need
#
im = im.reshape(im.shape[0], im.shape[1], 1)
im = im.transpose((2,0,1))
##
##im = np.expand_dims(im, axis=0)
##print im.shape
#im = np.expand_dims(im, axis=0)
# create the dataset
im_dat = caffe.io.array_to_datum(im)
in_txn.put('{:0>10d}'.format(in_idx), im_dat.SerializeToString())
in_db.close()
print 'end.'
# train
train_img_paths = 'train.txt'
train_img_lmdb = 'train_img_lmdb'
train_label_paths = 'train_label.txt'
train_label_lmdb = 'train_label_lmdb'
img_to_lmdb(train_img_paths, train_img_lmdb)
label_to_lmdb(train_label_paths, train_label_lmdb)
# val
val_img_paths = 'val.txt'
val_img_lmdb = 'val_img_lmdb'
val_label_paths = 'val_label.txt'
val_label_lmdb = 'val_label_lmdb1'
img_to_lmdb(val_img_paths, val_img_lmdb)
label_to_lmdb(val_label_paths, val_label_lmdb)
我收到了一个错误:
im = im[:,:,::-1]
IndexError: too many indices for array
和
im = im.transpose((2,0,1)) # convert to CxHxW
ValueError: axes don't match array
由于我的图片和标签是灰度的(即只有一个频道),我添加了im = np.expand_dims(im, axis=0)
,现在我收到以下错误:
Creating Training Data LMDB File .....
0 /home/user2/caffe-pascalcontext-fcn32s/dataset/Train/PNG/image-001-001.png
(281, 389)
(1, 281, 389)
Traceback (most recent call last):
File "lmdb_data.py", line 104, in <module>
img_to_lmdb(train_img_paths, train_img_lmdb)
File "lmdb_data.py", line 46, in img_to_lmdb
im_dat = caffe.io.array_to_datum(im)
File "/home/user2/caffe-master/python/caffe/io.py", line 78, in array_to_datum
datum.float_data.extend(arr.flat)
File "/home/user2/anaconda2/envs/caffeenv/lib/python2.7/site-packages/google/protobuf/internal/containers.py", line 275, in extend
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "/home/user2/anaconda2/envs/caffeenv/lib/python2.7/site-packages/google/protobuf/internal/type_checkers.py", line 108, in CheckValue
raise TypeError(message)
TypeError: 66 has type <type 'numpy.int32'>, but expected one of: ((<type 'float'>, <type 'int'>, <type 'long'>),)
convert_imageset
(Caffe内置转换器)
为了这个任务?如果是,请告诉我该怎么做?非常感谢您的帮助。