tf.data.Dataset InvalidArgumentError:预期的图像(JPEG,PNG或GIF),文件为空

时间:2018-11-13 05:09:50

标签: python tensorflow-datasets

import os
from PIL import Image, ImageFile
import tensorflow as tf
import numpy as np


ImageFile.LOAD_TRUNCATED_IMAGES = True

CWD = '/home/qinlong/PycharmProjects/ProgNEU/NEU/cai_nl/classes'

def convert_to_tfrecord(cwd, output):
    classes = os.listdir(cwd)
    writer = tf.python_io.TFRecordWriter(output)
    for index, name in enumerate(classes):
        class_path = cwd + '/' + name + '/'
        for img_name in os.listdir(class_path):
            img_path = class_path + img_name 

            img = Image.open(img_path)
            img = img.resize((64, 64))
            img_raw = img.tobytes()     
            example = tf.train.Example(features=tf.train.Features(feature={
                'label': tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
                'img_raw': tf.train.Feature(bytes_list = tf.train.BytesList(value=[img_raw]))
            }))    
            writer.write(example.SerializeToString())   
    writer.close()
    return output


def dataset_input_fn(batch_size, epoch, buffer_size=2048):
    filenames = ['/home/qinlong/PycharmProjects/ProgNEU/NEU/cai_nl/a_4.tfrecord']
    dataset = tf.data.TFRecordDataset(filenames)

    def parser(record):
        keys_to_features = {
            "image_data": tf.FixedLenFeature((), tf.string, default_value=""),
            "date_time": tf.FixedLenFeature((), tf.int64, default_value=0),
            "label": tf.FixedLenFeature((), tf.int64,
                                        default_value=tf.zeros([], dtype=tf.int64)),
        }
        parsed = tf.parse_single_example(record, keys_to_features)

        # Perform additional preprocessing on the parsed data.
        image = tf.image.decode_jpeg(parsed["image_data"])
        image = tf.reshape(image, [64, 64, 3])
        label = tf.cast(parsed["label"], tf.int32)

        return {"image_data": image, "date_time": parsed["date_time"]}, label

    dataset = dataset.map(parser)
    dataset = dataset.shuffle(buffer_size=10000)
    dataset = dataset.batch(batch_size=batch_size)
    dataset = dataset.repeat(epoch)

    iterator = dataset.make_one_shot_iterator()

    features, labels = iterator.get_next()
    return features, labels
  

InvalidArgumentError:预期的图像(JPEG,PNG或GIF)为空   文件[[{{node DecodeJpeg}} = DecodeJpegacceptable_fraction = 1,   频道= 0,dct_method =“”,fancy_upscaling = true,比率= 1,   try_recover_truncated = false]]      [[node IteratorGetNext(在   /home/qinlong/PycharmProjects/ProgNEU/NEU/cai_nl/main.py:23)=   IteratorGetNextoutput_shapes = [[?],[?, 64,64,3],[?]],   output_types = [DT_INT64,DT_UINT8,DT_INT32],   _device =“ / job:localhost /副本:0 /任务:0 /设备:CPU:0”]]

0 个答案:

没有答案