(0) INVALID_ARGUMENT: 未知的图像文件格式。需要其中之一:JPEG、PNG、GIF、BMP。

3
我看到了这些问题:Tensorflow Keras错误:未知的图像文件格式。需要其中之一:JPEG,PNG,GIF,BMP未知的图像文件格式。 需要其中之一:JPEG,PNG,GIF,BMP,但它们并没有完全帮助我。
我正在Google Colab中构建一个简单的CNN。
Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-29-a98bc2c91ee1> in <module>
----> 1 history = model_1.fit(train_data, epochs=5, steps_per_epoch=len(train_data), validation_data=test_data, validation_steps=int(0.25 * len(test_data)))

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     53     ctx.ensure_initialized()
     54     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55                                         inputs, attrs, num_outputs)
     56   except core._NotOkStatusException as e:
     57     if name is not None:

InvalidArgumentError: Graph execution error:

2 root error(s) found.
  (0) INVALID_ARGUMENT:  Unknown image file format. One of JPEG, PNG, GIF, BMP required.
     [[{{node decode_image/DecodeImage}}]]
     [[IteratorGetNext]]
     [[categorical_crossentropy/softmax_cross_entropy_with_logits/Shape_2/_10]]
  (1) INVALID_ARGUMENT:  Unknown image file format. One of JPEG, PNG, GIF, BMP required.
     [[{{node decode_image/DecodeImage}}]]
     [[IteratorGetNext]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_31356]

我是一个有用的助手,可以为您进行文本翻译。下面是需要翻译的内容:

我遇到了上述错误。这个错误出现在我尝试拟合模型时。 通过我链接的以前的答案,我已经确认我的文件夹中没有不适当的图像。所有的图像都是jpeg格式。

我的代码:

import tensorflow as tf

# Create training and test directory paths

train_dir = 'Dataset/train'
test_dir = 'Dataset/test'
IMG_SIZE = (224,224)
BATCH_SIZE=32
# Set up data loaders

import tensorflow as tf

IMG_SIZE = (224,224)
BATCH_SIZE=32
train_data = tf.keras.preprocessing.image_dataset_from_directory(directory=train_dir,
                                                                            image_size=IMG_SIZE,
                                                                            label_mode='categorical',
                                                                            batch_size=BATCH_SIZE)


test_data = tf.keras.preprocessing.image_dataset_from_directory(directory=test_dir, image_size=IMG_SIZE, batch_size=BATCH_SIZE, label_mode='categorical')


import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing


data_augmentation = keras.Sequential([
        preprocessing.RandomFlip('horizontal'),
        preprocessing.RandomRotation(0.2),
        preprocessing.RandomZoom(0.2),
        preprocessing.RandomHeight(0.2),
        preprocessing.RandomWidth(0.2),
        # preprocessing.Rescale(1/255.) Keep this model for ResNet. Efficient Net has rescaling buit in
], name='data_augmentation')



input_shape = (224,224,3)
base_model = tf.keras.applications.EfficientNetB0(include_top=False)
base_model.trainable=False

# Create the input layer

inputs = layers.Input(shape=input_shape, name='input_layer')
x=data_augmentation(inputs)

# Give base model the inputs after augmentation.. Dont train it
x = base_model(x,training=False)

x = layers.GlobalAveragePooling2D()(x)

# Add a dense layer for output

outputs = layers.Dense(9, activation='softmax', name='output_layer')(x)


# Make a model using the inputs and outputs

model_1 = keras.Model(inputs,outputs)

# Compile the model

model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

 history = model_1.fit(train_data, epochs=5, steps_per_epoch=len(train_data), validation_data=test_data, validation_steps=int(0.25 * len(test_data)))

我只从Google搜索下载了所有图像。

数据集链接: https://drive.google.com/file/d/1dKgzyq2lUF87ggZQ80KUhINhmtVrC_p-/view?usp=sharing
2个回答

2

仅仅因为一张图片的后缀名是.jpeg或.png并不代表这张图片就好。有些图片虽然后缀名正确,但却是坏的。除了后缀名,图片可能依旧含有损坏二进制数据,这才是导致图片损坏的原因。在预处理期间,你需要一个代码库来正确查找出目录中的损坏图片。


1

我在删除一些目录中的一些.DS*文件和.*.jpg文件后解决了问题。

压缩文件包含一些以.OOO开头命名的无效图形文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接