【发布时间】:2018-10-12 19:56:45
【问题描述】:
我目前正在使用 Tensorflow 数据集 api 对指定路径的图像进行一些扩充。文件名本身包含说明是否要扩充文件的信息。所以我想要做的是从数据集中读取文件并为每个文件,在文件名中执行查找,如果我找到特定的子字符串,然后设置一个布尔标志并将子字符串替换为“”。
我得到的错误是:
AttributeError: 'Tensor' 对象没有属性 'find'
我无法在具有 dtype 字符串条目的张量上执行“查找”,因为 find 不是张量的一部分,所以我试图弄清楚如何才能执行上述操作。我在下面分享了一些代码,我认为这些代码展示了我正在尝试做的事情。性能很重要,所以如果有人发现我通过 Dataset API 不正确地执行此操作,我宁愿以正确的方式执行此操作。
def preproc_img(filenames):
def parse_fn(filename):
augment_inst = False
if cfg.SPLIT_INTO_INST:
#*****************************************************
#*** THIS IS WHERE THE LOGIC IS CURRENTLY BREAKING ***
#*****************************************************
if filename.find('_data_augmentation') != -1:
augment_inst = True
filename = filename.replace('_data_augmentation', '')
image_string = tf.read_file(filename)
img = tf.image.decode_image(image_string, channels=3)
return dict(zip([filename], [img]))
dataset = tf.data.Dataset.from_tensor_slices(filenames)
dataset = dataset.map(parse_fn)
iterator = dataset.make_one_shot_iterator()
return iterator.get_next()
def perform_train():
if __name__ == '__main__':
filenames = helper.get_image_paths()
next_batch = preproc_img(filenames)
with tf.Session() as sess:
with sess .graph.as_default():
sess.run(tf.local_variables_initializer())
sess.run(tf.global_variables_initializer())
dat = sess.run(next_batch)
# I would now go about calling any of my tf op code below
【问题讨论】:
标签: tensorflow tensorflow-datasets