首页 > 解决方案 > 如何在tensorflow数据管道中使用cv2应用函数将路径转换为数组?

问题描述

任何帮助将不胜感激

我正在尝试加载两个包含图像路径及其相应标签的列表。像这样的东西:

p0 = ['a','b',....] #paths to images .tif format
p1 = [1,2,3,......] #paths to images .tif format
labels = [0,1,1,...] #corresponding labels w.r.t both the lists

我以下列方式使用了 tf.data:

def TFData(p_0, p_1, batch_size, labels=None, is_train=True):
  dset = tf.data.Dataset.from_tensor_slices((p_0,p_1))

  if labels is not None:
    label = tf.data.Dataset.from_tensor_slices(labels)
  
  AUTO = tf.data.experimental.AUTOTUNE
  final_dset = tf.data.Dataset.zip((dset, label))
  final_dset = final_dset.batch(batch_size, drop_remainder=is_train).prefetch(AUTO)
  return final_dset

这将返回:

<PrefetchDataset shapes: (((64,), (64,)), (64,)), types: ((tf.string, tf.string), tf.int32)>

我的问题是如何应用函数来使用 cv2 将路径转换为数组,因为图像是 .tif 文件?这样结果将是:

<PrefetchDataset shapes: (((64,256,256,3), (64,256,256,3)), (64,)), types: ((tf.float64, tf.float64), tf.int32)>

我正在使用dataset.map。但是它抛出错误:

def to_array(p_0):
  im_1 = cv2.imread(p_0,1)
  #im = tfio.experimental.image.decode_tiff(paths)
  im_1 = cv2.resize(im_1,(img_w,img_h)) #img_w=img_h=256
  im_1 = np.asarray(im_1, dtype=np.float64)
  im_1 /= 255
  return im_1

def parse_fn(p_0):
  [p_0,] = tf.py_function(to_array, [p_0], [tf.float64])
  return p_0

def TFData(p_0, p_1, batch_size, labels=None, is_train=True):
  
  dset_1 = tf.data.Dataset.from_tensor_slices(p_0)
  dset_1 = dset_1.map(parse_fn)
  dset_2 = tf.data.Dataset.from_tensor_slices(p_1)
  dset_2 = dset_2.map(parse_fn)

  if labels is not None:
    label = tf.data.Dataset.from_tensor_slices(labels)
  
  AUTO = tf.data.experimental.AUTOTUNE
  
  final_dset = tf.data.Dataset.zip((dset_1, dset_2, label))
  final_dset = final_dset.batch(batch_size, drop_remainder=is_train).prefetch(AUTO)
  return final_dset
print(train_data) #where train_data is defined as TFData()
<PrefetchDataset shapes: ((<unknown>, <unknown>), (64,)), types: ((tf.float64, tf.float64), tf.int32)>

这会引发错误:

for (t,p),l in train_data.as_numpy_iterator():
  print(t)
  print(p)
  print(l)
  print(type(t))
  break
SystemError: <built-in function imread> returned NULL without setting an error


     [[{{node EagerPyFunc}}]] [Op:IteratorGetNext]

任何帮助将不胜感激

标签: python-3.xtensorflowdeep-learningdata-pipelinetf.data.dataset

解决方案


我认为您的问题出在 cv2.imread 中。您是否在函数外部检查过它是否正在相应地读取和绘制数据?

请尝试使用 -1 代替:

im_1 = cv2.imread(p_0,-1)

推荐阅读