python-3.x - 如何在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]
任何帮助将不胜感激
解决方案
我认为您的问题出在 cv2.imread 中。您是否在函数外部检查过它是否正在相应地读取和绘制数据?
请尝试使用 -1 代替:
im_1 = cv2.imread(p_0,-1)
推荐阅读
- c++ - TEXT() 在代码块上工作,但不在 Visual Studio 上
- intellij-idea - Intellij 和 Composite 构建配置:我可以影响构建的顺序吗?
- json - JSON Schema:跨数组元素的依赖关系
- css - CSS动画关键帧百分比问题
- swiftui - 无法在横向中将列表的宽度设置为 0
- c++ - `const_cast 可以接受吗
(this)` 如果公共状态没有改变? - javascript - 如何在 Vanilla JS 中创建一个函数,在所选输入之前将选中的属性放入无线电输入?
- r - 使用带有 %like% 运算符的重新编码函数
- ios - 集合视图单元格内的按钮不响应布局更改
- javascript - 如何更改代码来控制某些按钮的名称?