简体   繁体   English

Tensorflow:InvalidArgumentError:预期图像(JPEG、PNG 或 GIF),文件为空

[英]Tensorflow: InvalidArgumentError: Expected image (JPEG, PNG, or GIF), got empty file

I am a beginer.我是初学者。 When I learned the tensorflow's programmer's guide, I tried to define a dataset_input_fn function used for 'estimator'.当我学习tensorflow的程序员指南时,我试图定义一个用于'estimator'的dataset_input_fn函数。 I got a wierd error for me, which shows:我遇到了一个奇怪的错误,它显示:

INFO:tensorflow:Using default config.信息:tensorflow:使用默认配置。

INFO:tensorflow:Using config: {'_model_dir': '/model', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': None, '_cluster_spec': , '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}信息:tensorflow:使用配置:{'_model_dir':'/model','_tf_random_seed':无,'_save_summary_steps':100,'_save_checkpoints_steps':无,'_save_checkpoints_secs':600,'_session_config':无,'_keep_checkpoint_max' : 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_service': 无, '_cluster_spec':, '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}

INFO:tensorflow:Calling model_fn.信息:tensorflow:调用 model_fn。

INFO:tensorflow:Done calling model_fn.信息:tensorflow:完成调用 model_fn。

INFO:tensorflow:Create CheckpointSaverHook.信息:tensorflow:创建 CheckpointSaverHook。

INFO:tensorflow:Graph was finalized. INFO:tensorflow:Graph 已完成。

2018-03-12 10:22:14.699465: IC:\tf_jenkins\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2018-03-12 10:22:14.699465: IC:\tf_jenkins\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:140] 你的 CPU 支持这个 TensorFlow 二进制文件的指令未编译使用:AVX2

INFO:tensorflow:Running local_init_op.信息:tensorflow:正在运行 local_init_op。

INFO:tensorflow:Done running local_init_op.信息:tensorflow:完成运行 local_init_op。

2018-03-12 10:22:15.913858: WC:\tf_jenkins\workspace\rel-win\M\windows\PY\36\tensorflow\core\framework\op_kernel.cc:1202] OP_REQUIRES failed at iterator_ops.cc:870: Invalid argument: Expected image (JPEG, PNG, or GIF), got empty file [[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=0, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false]] 2018-03-12 10:22:15.913858: WC:\tf_jenkins\workspace\rel-win\M\windows\PY\36\tensorflow\core\framework\op_kernel.cc:1202] OP_REQUIRES 在 iterator_ops.cc:870 失败:无效参数:预期图像(JPEG、PNG 或 GIF),得到空文件 [[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=0, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false]]

Traceback (most recent call last): File "F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1361, in _do_call return fn(*args) File "F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1340, in _run_fn target_list, status, run_metadata) File "F:\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 516, in exit c_api.TF_GetCode(self.status.status))回溯(最近调用最后):文件“F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py”,第 1361 行,在 _do_call return fn(*args) 文件“F:\Anaconda3\ lib\site-packages\tensorflow\python\client\session.py”,第 1340 行,在 _run_fn target_list、status、run_metadata) 文件“F:\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py “,第 516 行,在退出c_api.TF_GetCode(self.status.status))

tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected image (JPEG, PNG, or GIF), got empty file [[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=0, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false]] [[Node: IteratorGetNext = IteratorGetNextoutput_shapes=[[?,28,28,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0"]] During handling of the above exception, another exception occurred: tensorflow.python.framework.errors_impl.InvalidArgumentError:预期图像(JPEG、PNG 或 GIF),文件为空 [[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=0, dct_method="", fancy_upscaling=true, ratio=1 , try_recover_truncated=false]] [[节点:IteratorGetNext = IteratorGetNextoutput_shapes=[[?,28,28,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device="/job:localhost/replica:0/ task:0/device:CPU:0"]] 在处理上述异常的过程中,又出现了一个异常:

Traceback (most recent call last): File "F:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev\pydev_run_in_console.py", line 53, in run_file pydev_imports.execfile(file, globals, locals) # execute the script File "F:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "E:/Learning_process/semester2018_spring/deep_learning/meituan/MNIST/demo_cnn_mnist_meituan.py", line 201, in tf.app.run(main) File "F:\Anaconda3\lib\site-packages\tensorflow\python\platform\app.py", line 126, in run _sys.exit(main(argv)) File "E:/Learning_process/semester2018_spring/deep_learning/meituan/MNIST/demo_cnn_mnist_meituan.py", line 195, in main steps=50) File "F:\Anaconda3\lib\site-packages\tensorflow\python\estimator\estimator.py", line 352, in train loss = self._train_model(input_fn, hooks, saving_listeners) File "F:\Anaconda3\lib\site-packages\tensorflow\python\estimator\estimator.py", line回溯(最近调用最后):文件“F:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev\pydev_run_in_console.py”,第 53 行,在 run_file pydev_imports.execfile(file, globals, locals) # 执行脚本文件“F:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev_pydev_imps_pydev_execfile.py”,第 18 行,在 execfile exec(compile(contents+“\n”, file, 'exec'), glob, loc) 文件“E:/Learning_process/semester2018_spring/deep_learning/meituan/MNIST/demo_cnn_mnist_meituan.py”,第201行,在tf.app.run(main)文件“F:\Anaconda3\lib\site-packages\tensorflow\python\platform\ app.py", line 126, in run _sys.exit(main(argv)) File "E:/Learning_process/semester2018_spring/deep_learning/meituan/MNIST/demo_cnn_mnist_meituan.py", line 195, in main steps=50) File " F:\Anaconda3\lib\site-packages\tensorflow\python\estimator\estimator.py", 第 352 行,在 train loss = self._train_model(input_fn, hooks, saving_listeners) File "F:\Anaconda3\lib\site- packages\tensorflow\python\estimator\estimator.py", 行 891, in _train_model _, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss]) File "F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py", line 546, in run run_metadata=run_metadata) File "F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1022, in run run_metadata=run_metadata) File "F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1113, in run raise six.reraise(*original_exc_info) File "F:\Anaconda3\lib\site-packages\six.py", line 693, in reraise raise value File "F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1098, in run return self._sess.run(*args, **kwargs) File "F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1170, in run run_metadata=run_metadata) File "F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py", line 950, in run return self._sess.run(*args, **kwargs) Fil 891,在 _train_model _,loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss]) 文件“F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py”,第 546 行,在运行 run_metadata=run_metadata) 文件“F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py”,第 1022 行,在运行 run_metadata=run_metadata) 文件“F:\Anaconda3\lib\site-packages \tensorflow\python\training\monitored_session.py",第 1113 行,在运行中 raise six.reraise(*original_exc_info) File "F:\Anaconda3\lib\site-packages\six.py",第 693 行,在 reraise raise value文件“F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py”,第 1098 行,在运行中返回 self._sess.run(*args, **kwargs) 文件“F:\Anaconda3\ lib\site-packages\tensorflow\python\training\monitored_session.py",第 1170 行,在运行 run_metadata=run_metadata) 文件“F:\Anaconda3\lib\site-packages\tensorflow\python\training\monitored_session.py”,第 950 行,在 run return self._sess.run(*args, **kwargs) Fil e "F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 905, in run run_metadata_ptr) File "F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1137, in _run feed_dict_tensor, options, run_metadata) File "F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1355, in _do_run options, run_metadata) File "F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1374, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected image (JPEG, PNG, or GIF), got empty file [[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=0, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false]] [[Node: IteratorGetNext = IteratorGetNextoutput_shapes=[[?,28,28,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0"]] PyDev console: using IPython 6.1.0 e "F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 905, in run run_metadata_ptr) File "F:\Anaconda3\lib\site-packages\tensorflow\python\client\ session.py”,第 1137 行,在 _run feed_dict_tensor,options,run_metadata)文件“F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py”,第 1355 行,在 _do_run options,run_metadata)文件“F:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py”,第 1374 行,在 _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError 中:预期图像(JPEG、PNG 或 GIF),得到空文件 [[Node: DecodeJpeg = DecodeJpegacceptable_fraction=1, channels=0, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false]] [[Node: IteratorGetNext = IteratorGetNextoutput_shapes=[[?,28,28,1], [?]], output_types=[DT_FLOAT, DT_INT32], _device="/job:localhost/replica:0/task:0/device:CPU:0" ]] PyDev 控制台:使用 IPython 6.1.0

The code is as follows:代码如下:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Imports
import numpy as np
import os
import tensorflow as tf
import argparse

parser = argparse.ArgumentParser()
# parser.add_argument("--batch_size", default=100, type=int, help='batch_size')
# parser.add_argument("--train_steps", default=1000, type=int, help="train_steps")
parser.add_argument("--model_dir", default='/model', type=str, help='model_dir')
parser.add_argument("--data_dir", default='', type=str, help="data_dir")


def cnn_model(features, labels, mode):
    """

    :param features:
    :param labels:
    :param mode:
    :return:
    """

    # input
    input_layer = tf.reshape(features['image'], [-1, 28, 28, 1])

    conv1 = tf.layers.conv2d(inputs=input_layer,
                             filters = 32,
                             kernel_size=[5, 5],
                             padding='same',
                             activation=tf.nn.relu)

    pool1 = tf.layers.max_pooling2d(inputs=conv1,
                                    pool_size=[2, 2],
                                    strides=2)

    conv2 = tf.layers.conv2d(inputs=pool1,
                             filters=64,
                             kernel_size=[5, 5],
                             padding='same',
                             activation=tf.nn.relu)

    pool2 = tf.layers.max_pooling2d(inputs=conv2,
                                    pool_size=[2, 2],
                                    strides=2)

    pool_flat = tf.reshape(pool2, [-1, 7 * 7 * 64])

    dense = tf.layers.dense(inputs=pool_flat,
                            units=1024,
                            activation=tf.nn.relu)

    dropout = tf.layers.dropout(inputs=dense,
                                rate=0.4,
                                training=mode == tf.estimator.ModeKeys.TRAIN)

    logits = tf.layers.dense(inputs=dropout,
                             units=10,
                             activation=None)

    predictions = {
        'class_ids': tf.argmax(logits, 1),
        'probabilities': tf.nn.softmax(logits, name='softmax_tensor')
    }
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode,
                                          predictions=predictions)

    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

    if mode == tf.estimator.ModeKeys.EVAL:
        eval_metric_ops = {
            'accuracy': tf.metrics.accuracy(labels=labels,
                                            predictions=tf.argmax(logits, 1))
        }
        return tf.estimator.EstimatorSpec(mode,
                                          loss=loss,
                                          eval_metric_ops=eval_metric_ops)

    # train
    assert mode == tf.estimator.ModeKeys.TRAIN
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
    train_op = optimizer.minimize(loss=loss,
                                  global_step=tf.train.get_global_step())
    return tf.estimator.EstimatorSpec(mode,
                                      loss=loss,
                                      train_op=train_op)


def dataset_input_fn(filenames):
    """

    :param filenames: tfrecord file's path
    :return:
    """
    # filenames = ['train.tfrecords', 'test.tfrecords']
    dataset = tf.data.TFRecordDataset(filenames)

    def _parse(record):
        features = {"image": tf.FixedLenFeature((), tf.string, default_value=""),
                    "label": tf.FixedLenFeature((), tf.int64, default_value=0)}
        parsed = tf.parse_single_example(record, features)

        image = tf.image.decode_jpeg(parsed["image"])
        image = tf.cast(image, tf.float32)
        # image = tf.image.convert_image_dtype(image, tf.float32)
        image = tf.reshape(image, [28, 28, 1])
        # image = tf.cast(image, tf.float32)
        # image = tf.decode_raw(features['image'], tf.float64)
        label = tf.cast(parsed['label'], tf.int32)
        return {'image': image}, label

    dataset = dataset.map(_parse)
    dataset = dataset.shuffle(buffer_size=10000)
    dataset = dataset.batch(100)
    dataset = dataset.repeat(1)

    iterator = dataset.make_one_shot_iterator()
    features, labels = iterator.get_next()
    # features = tf.cast(features, tf.float32)
    return features, labels


def main(argv):
    """

    :param argv:
    :return:
    """
    args = parser.parse_args(argv[1:])
    train_path = ['train.tfrecords']
    test_path = ['test.tfrecords']

    print("\ndata has been loaded as 'train_x' and 'train_y'\n")

    classifier = tf.estimator.Estimator(model_fn=cnn_model,
                                        model_dir=args.model_dir)

    classifier.train(
        input_fn=lambda: dataset_input_fn(train_path),
        steps=50)

    print("\ntraining process is done\n")


if __name__ == '__main__':
    tf.app.run(main)

At least one example isn't an image.至少有一个示例不是图像。

You can check the datatype before feeding the image to the Neural Network.您可以在将图像输入神经网络之前检查数据类型。

I'm doing it with the library imghdr:我正在用图书馆 imghdr 做这件事:

Code:代码:

import imghdr
import os

l_FileNames = os.listdir("images_path")

for image in l_FileNames:
    if not imghdr.what(image) == "png":
        l_FileNames.remove(image)

The error seems to be that in some of your examples, there is no actual image.错误似乎是在您的某些示例中没有实际图像。

Basically, when you call image = tf.image.decode_jpeg(parsed["image"]) , parsed["image"] is an empty tensor.基本上,当您调用image = tf.image.decode_jpeg(parsed["image"])时, parsed["image"]是一个空张量。

您需要确保您的图片大于resize()!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 tensorflow.python.framework.errors_impl.InvalidArgumentError:未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - tensorflow.python.framework.errors_impl.InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required Tensorflow DecodeJPEG:预期图像(JPEG、PNG 或 GIF),格式未知,以 '\000\000\000\000\000\000\000\00' 开头 - Tensorflow DecodeJPEG: Expected image (JPEG, PNG, or GIF), got unknown format starting with '\000\000\000\000\000\000\000\00' Tensorflow 预测错误,invalidArgumentError:断言失败:[无法将字节解码为 JPEG、PNG、GIF 或 BMP] - Tensorflow prediciton error, invalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP] Tensorflow Keras 错误:未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - Tensorflow Keras error: Unknown image file format. One of JPEG, PNG, GIF, BMP required 无效参数:预期图像(JPEG、PNG 或 GIF),格式未知,以“RIFF\\320\\025\\000\\000WEBPVP8”开头 - Invalid argument: Expected image (JPEG, PNG, or GIF), got unknown format starting with 'RIFF\320\025\000\000WEBPVP8 ' InvalidArgumentError:断言失败:[无法将字节解码为 JPEG、PNG、GIF 或 BMP] - InvalidArgumentError: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP] 我的训练数据集中的隐藏文件使 tensorflow 返回“未知图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一。” - Hidden file in my training dataset which makes tensorflow return “Unknown image file format. One of JPEG, PNG, GIF, BMP required.” 未知的图像文件格式。 需要 JPEG、PNG、GIF、BMP 之一 - Unknown image file format. One of JPEG, PNG, GIF, BMP required Tensorflow Label_Image用于PNG,GIF等 - Tensorflow Label_Image for PNG, GIF etc InvalidArgumentError:预期维度在 [-1, 1) 范围内,但得到 1 - InvalidArgumentError: Expected dimension in the range [-1, 1) but got 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM