Tensorflow 从tfrecord还原映像并保存在硬盘中
我有一系列tfrecord文件,这些文件是从图像文件夹中生成的。现在,我想反转这个过程,从tfrecord文件中提取图像文件,并将它们保存在存储器中。有没有办法做到这一点 以下是开始使用的示例代码Tensorflow 从tfrecord还原映像并保存在硬盘中,tensorflow,Tensorflow,我有一系列tfrecord文件,这些文件是从图像文件夹中生成的。现在,我想反转这个过程,从tfrecord文件中提取图像文件,并将它们保存在存储器中。有没有办法做到这一点 以下是开始使用的示例代码 with tf.device('/cpu:0'): tf.reset_default_graph() # here a path to tfrecords file as list fq = tf.train.string_input_producer(tf.convert_t
with tf.device('/cpu:0'):
tf.reset_default_graph()
# here a path to tfrecords file as list
fq = tf.train.string_input_producer(tf.convert_to_tensor([/path/to/tfrecordsfiles]), num_epochs=1)
reader = tf.TFRecordReader()
_, v = reader.read(fq)
fk = {
'image/encoded': tf.FixedLenFeature((), tf.string, default_value='')}
ex = tf.parse_single_example(v, fk)
image = tf.image.decode_jpeg(
ex['image/encoded'], dct_method='INTEGER_ACCURATE')
with tf.Session() as sess:
coord = tf.train.Coordinator()
tf.train.start_queue_runners(coord=coord, sess=sess)
sess.run([tf.global_variables_initializer(),
tf.local_variables_initializer()])
# set the number of images in your tfrecords file
num_images=100
for i in range(num_images):
try:
im_ = sess.run(image)
# chnage the image save path here
cv2.imwrite('/tmp/test' + str(i) + '.jpg', im_)
except Exception as e:
print(e)
break
这是适合我的代码。它最初取自Ishant Mrinal先前的回答。我只是添加了一些修改:
#get the number of records in the tfrecord file
c = 0
for record in tf.python_io.tf_record_iterator(tfrecords_filename):
c += 1
totalFiles+=c
logfile.write(" {} : {}".format(f, c))
logfile.flush()
print("going to restore {} files from {}".format(c,f))
tf.reset_default_graph()
# here a path to tfrecords file as list
fq = tf.train.string_input_producer([tfrecords_filename], num_epochs=fileCount)
reader = tf.TFRecordReader()
_, v = reader.read(fq)
fk = {
'image/encoded': tf.FixedLenFeature((), tf.string, default_value=''),
'image/class/synset': tf.FixedLenFeature([], tf.string, default_value=''),
'image/filename': tf.FixedLenFeature([], tf.string, default_value='')
}
ex = tf.parse_single_example(v, fk)
image = tf.image.decode_jpeg(ex['image/encoded'], dct_method='INTEGER_ACCURATE')
label = tf.cast(ex['image/class/synset'], tf.string)
fileName = tf.cast(ex['image/filename'], tf.string)
# The op for initializing the variables.
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# sess.run([tf.global_variables_initializer(),tf.local_variables_initializer()])
# set the number of images in your tfrecords file
num_images=c
print("going to restore {} files from {}".format(num_images, f))
for i in range(num_images):
im_,lbl,fName = sess.run([image,label,fileName])
lbl_=lbl.decode("utf-8")
savePath=os.path.join(output_path,lbl_)
if not os.path.exists(savePath):
os.makedirs(savePath)
fName_=os.path.join(savePath, fName.decode("utf-8").split('_')[1])
# chnage the image save path here
cv2.imwrite(fName_ , im_)
coord.request_stop()
coord.join(threads)
谢谢你的回答。它进行了一些修改。