无法将张量图像存储在本地驱动器上

时间:2018-07-01 23:04:05

标签: python tensorflow

这是我第一次尝试使用tensorflow进行任何操作。我有一个1920 * 1080的图像,我想从中随机获得一个512 * 512的色块。裁剪完成后,我无法将图像存储在本地驱动器上。

import tensorflow as tf

# An image of format 1920*1080
filename = tf.train.string_input_producer(["/home/rishik/Downloads/Image.jpg"])

reader = tf.WholeFileReader()
key, value = reader.read(filename)

# use png or jpg decoder based on your files.
my_img = tf.image.decode_jpeg(value, channels=3)

random_patch = tf.random_crop(my_img, [512,512,3])

enc = tf.image.encode_jpeg(random_patch)
cropped_file = tf.write_file("/home/rishik/Downloads/Image_2.jpg", enc)

sess = tf.Session()
result = sess.run(cropped_file)

程序一旦执行,终端将显示以下消息,并且不执行其他任何操作:

"2018-07-02 00:50:09.414478: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA".

有人可以让我理解我在这里做错了吗

1 个答案:

答案 0 :(得分:0)

进行一些更改后,我能够成功将图像保存到本地驱动器。虽然,警告消息仍然显示(我认为这是我的设置存在的问题)。请在下面找到解决方案。

import tensorflow as tf

filepath = "<Some filepath>"
filename = "<Some image filename>"
tensors = []

# decode image to RGB format
image_decoded = tf.image.decode_jpeg(tf.read_file(filepath + filename), channels=3)

# crop a random patch from the image of size 512*512
random_patch = tf.random_crop(image_decoded, [512,512,3]) 

# append the tensor to the list of rest of the tensors
tensors.append(random_patch)

# encode the random patch into JPEG format and then write the file to the local drive
enc = tf.image.encode_jpeg(random_patch)

# write the new cropped file at the directory random_patches created
image = filepath + "random_patches/" + filename
cropped_file = tf.constant(image)
fwrite = tf.write_file(cropped_file, enc) 

sess = tf.Session()
result = sess.run(fwrite)