【发布时间】:2022-11-12 22:15:57
【问题描述】:
我正在尝试使用tensorflow.data.data API 创建自定义 Tensorflow 数据集。但是,我的原始数据由许多称为图块的较小图像组成,必须将它们连接起来以形成更大的图像。这些图块也在进行图像增强。出于这个原因,正在使用os.path.join。但是,os.path.join 不适用于 tensorflow 张量。错误信息:
main_image_path = os.path.join(INDIVIDUAL_TILE_PATH, image_id)
File "C:\ProgramData\Anaconda3\envs\3.9\lib\ntpath.py", line 117, in join *
genericpath._check_arg_types('join', path, *paths)
File "C:\ProgramData\Anaconda3\envs\3.9\lib\genericpath.py", line 152, in _check_arg_types *
raise TypeError(f'{funcname}() argument must be str, bytes, or '
TypeError: join() argument must be str, bytes, or os.PathLike object, not 'Tensor'
Process finished with exit code 1
显而易见的解决方案是将张量转换为字符串,但 str(image_id) 似乎不起作用。这是我的代码:
def createDynamicDatasetFromIDsLabels(ID, labels, mode="train"):
dataset = (
tf.data.Dataset
.from_tensor_slices((ID, labels))
.map(decodeImages, num_parallel_calls=AUTO)
#.repeat()
#.shuffle(BATCH_SIZE * 5)
#.batch(BATCH_SIZE)
#.prefetch(AUTO)
)
return dataset
def decodeImages(image_id, label):
main_image_path = os.path.join(INDIVIDUAL_TILE_PATH, image_id)
tiles_list_paths = glob.glob(main_image_path + "*")
augmentedTiles = map(DataAugmentation.data_augment, tiles_list_paths) ##DATA AUGMENT READS TILES AND AUGMENTS
tile_list_images = list(augmentedTiles)
concat_image = glue_to_one(tile_list_images)
plt.imshow(concat_image)
plt.show()
return concat_image, label
def glue_to_one(imgs_seq):
first_row= tf.concat((imgs_seq[0], imgs_seq[1],imgs_seq[2],imgs_seq[3]), 0)
second_row = tf.concat((imgs_seq[4], imgs_seq[5], imgs_seq[6], imgs_seq[7]), 0)
third_row = tf.concat((imgs_seq[8], imgs_seq[9], imgs_seq[10], imgs_seq[11]), 0)
fourth_row = tf.concat((imgs_seq[12], imgs_seq[13], imgs_seq[14], imgs_seq[15]), 0)
img_glue = tf.stack((first_row, second_row, third_row, fourth_row), axis=1)
img_glue = tf.reshape(img_glue, [512,512,3])
return img_glue```
【问题讨论】:
标签: python tensorflow