【发布时间】:2017-07-04 03:55:49
【问题描述】:
我想使用 TFRecords 序列化一堆 PNG 文件。因为我查看了 inception repository。给出的例子是RGB JPEG 文件,因为我的文件是灰度的,我不得不更改代码。
但我设法生成了记录文件。
问题是当我尝试阅读它们时:
def getImage(filename):
with tf.device('/cpu:0'):
# convert filenames to a queue for an input pipeline.
filenameQ = tf.train.string_input_producer([filename],num_epochs=None)
# object to read records
recordReader = tf.TFRecordReader()
# read the full set of features for a single example
key, fullExample = recordReader.read(filenameQ)
# parse the full example into its' component features.
features = tf.parse_single_example(
fullExample,
features={
'image/height': tf.FixedLenFeature([], tf.int64),
'image/width': tf.FixedLenFeature([], tf.int64),
'image/colorspace': tf.FixedLenFeature([], dtype=tf.string,default_value=''),
'image/channels': tf.FixedLenFeature([], tf.int64),
'image/class/label': tf.FixedLenFeature([],tf.int64),
'image/class/text': tf.FixedLenFeature([], dtype=tf.string,default_value=''),
'image/format': tf.FixedLenFeature([], dtype=tf.string,default_value=''),
'image/filename': tf.FixedLenFeature([], dtype=tf.string,default_value=''),
'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='')
})
# now we are going to manipulate the label and image features
label = features['image/class/label']
image_buffer = features['image/encoded']
# Decode the PNG
with tf.name_scope('decode_img',[image_buffer], None):
# decode
image = tf.image.decode_png( image_buffer, channels=1)
# and convert to single precision data type
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
# cast image into a single array, where each element corresponds to the greyscale
# value of a single pixel.
image =tf.reshape([None, img_height*img_width])# here is the problem
label=tf.stack(tf.one_hot(label-1, numberOFclasses))
return label, image
问题是重塑线,当我尝试这个时程序崩溃了。 这是它的使用方式:
with tf.name_scope('decode_img',[image_buffer], None):
# decode
image = tf.image.decode_jpeg(image_buffer, channels=3)
# and convert to single precision data type
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
# cast image into a single array, where each element corresponds to the greyscale
# value of a single pixel.
# the "1-.." part inverts the image, so that the background is black.
image=tf.reshape(1-tf.image.rgb_to_grayscale(image),[img_height*img_width])
这是有道理的,因为当文件是 RGB 时。但我只有 1 个通道,所以文件已经是灰度的。
【问题讨论】:
-
应该是:image =tf.reshape(image, [img_height*img_width])
标签: python tensorflow