将张量流转换为jpeg。 Python

问题描述 投票:0回答:1

我正在尝试将Tfrecord文件转换为JPEG,但是我不知道如何解决此错误。我在这里和Python中都是新手,如果做错了事,对不起。如果有人可以帮助我,请提前谢谢给出的错误Tensor(“ DecodeJpeg:0”,shape =(?,?,1),dtype = uint8)预期的图像(JPEG,PNG或GIF),文件为空[[node DecodeJpeg(定义为:38)]]

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from google.colab import drive
drive.mount('/content/drive')
c = 0
totalFile=0
tfrco="/content/drive/My Drive/ColabNotebooks/ddsm- mammography/training10_0/training10_0.tfrecords"
output_path = "/content/drive/My Drive/ColabNotebooks/ddsm-mammography/training10_0/Images10_0"
for record in tf.python_io.tf_record_iterator(tfrco):
        c += 1

totalFiles=c

tf.reset_default_graph()

fq = tf.train.string_input_producer([tfrco], num_epochs=totalFiles)
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)
imagem = tf.image.decode_jpeg(ex['image/encoded'], channels=1)
label = tf.cast(ex['image/class/synset'], tf.string)
fileName = tf.cast(ex['image/filename'], tf.string)

init_op = tf.group(tf.global_variables_initializer(),
        tf.local_variables_initializer())

sess = tf.Session()
sess.run(init_op)

coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord, sess = sess)

num_images=c
print("VAI RESTAURAR {} ARQUIVOS ".format(num_images))
for i in range(num_images):
try:
         im_,lbl,fName = sess.run([imagem,label,fileName])


except Exception as e:
             print(e)
             break
   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])
   cv2.imwrite(fName_ , im_)
   print(fName)
   coord.request_stop()
   coord.join(threads)

您能帮我吗?

python tensorflow jpeg tfrecord
1个回答
0
投票

据我所见,问题出在这一行:ex = tf.parse_single_example(v, fk),但是如果没有更多细节很难说。

无论如何,我建议使用tf.data.Dataset模块提取并解析.tfrecord文件:

raw_dataset = tf.data.TFRecordDataset(["record.tfrecord", "record1.tfrecord"])
dataset = raw_dataset.map(self._parse_dataset)

map()函数针对_parse_dataset()中的每个条目调用它作为参数(在这种情况下为tfrecord)接收的函数。 _parse_datatset()需要看起来像这样:

def _parse_dataset(example_proto):
    keys_to_features = {
                 '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='')
            }

    parsed_features = tf.io.parse_single_example(example_proto, keys_to_features)

    return parsed_features['image/encoded'], (parsed_features['image/class/synset'], parsed_features['image/filename'])

现在,您可以遍历dataset的元素并将其转换回JPEG格式:

for raw_image, features in dataset:
    imagem = tf.image.decode_jpeg(raw_image, channels=1)
© www.soinside.com 2019 - 2024. All rights reserved.