【问题标题】:Convert tensorflow to jpeg. Python将张量流转换为 jpeg。 Python
【发布时间】:2020-06-07 19:29:36
【问题描述】:

我正在尝试将 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】:

    据我所知,问题出在这一行: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)
    

    【讨论】:

    • 感谢 Gal,我设法解决了问题,确实如此,我只需要指定图像的大小。感谢朋友的帮助,我们下期再见
    猜你喜欢
    • 1970-01-01
    • 2016-09-12
    • 2021-11-25
    • 2023-04-09
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    相关资源
    最近更新 更多