这是我编写的用于预处理图像的代码。如果我运行它,则不会创建该文件。我究竟做错了什么? tf.write_file op不是计算图的一部分吗?这是我编写的代码的片段。希望您能在这里理解问题。
def _parse_function(filename, ofile):
tf.Print(ofile,[ofile])
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string,channels=3)
image_resized = tf.image.resize_images(image_decoded, [400, 400])/255.0
image_resized = tf.image.convert_image_dtype(image_resized, tf.uint8)
image_encode = tf.image.encode_jpeg(image_resized)
tf.write_file(ofile, image_encode)
return tf.data.Dataset.from_tensor_slices(([image_encode],[ofile]))
#return (image_resized,label)
def getDataSet(impaths,opaths):
dataset = tf.data.Dataset.from_tensor_slices((impaths,opaths))
dataset = dataset.interleave(_parse_function,32,32)
batched_dataset = dataset.batch(600)
return batched_dataset
........some code.......
with open(self.image_labels_path, 'r') as f:
lines = f.readlines()
dt = [x.strip().split(",") for x in lines]
images = []
opaths = []
for x,y in dt:
pt = os.path.join(self.image_dir ,'image_' + x + '.jpg')
if y =='True':
opt = os.path.join(self.out_dir ,'logo/' + x + '.jpg')
if y =='False':
opt = os.path.join(self.out_dir ,'nologo/' + x + '.jpg')
if (not tf.gfile.Exists(pt)) or tf.gfile.Exists(opt):
continue
images.append(pt)
opaths.append(opt)
print(len(opaths))
dataset = getDataSet(images,opaths)
iterator = dataset.make_one_shot_iterator()
with tf.Session() as sess:
for i in range(1, 18):
print(i)
next_element = iterator.get_next()
res2 = sess.run(next_element)