InvalidArgumentError:找到 2 个根错误。 (0) 无效参数:输入为空

我是深度学习的新手。我正在尝试制作一个猫/狗分类器。我有两个猫图像和狗图像的文件夹。我正在尝试使用 mobilenetV2 模型进行迁移学习。但是当第一个时代开始时,我被错误打断了。

import matplotlib.pyplot as plt
import numpy as np
import os
import tensorflow as tf
import tensorflow.keras.layers as tfl

from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.layers.experimental.preprocessing import RandomFlip , RandomRotation

#creating dataset from a directory from our disk and spliting them into train and validation
BATCH_SIZE = 32
IMG_SIZE = (160, 160)
directory = "dataset/PetImages/"
train_dataset = image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset='training',
                                             seed=42)
validation_dataset = image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset='validation',
                                             seed=42)
#seeing some of the pictures
class_names = train_dataset.class_names

plt.figure(figsize=(10, 10))
for images, labels in train_dataset.take(1):
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(class_names[labels[i]])
        plt.axis("off")

AUTOTUNE = tf.data.experimental.AUTOTUNE
train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)

#a function to augment datas
def data_augmenter():
    data_augmentation = tf.keras.Sequential()
    data_augmentation.add(RandomFlip('horizontal'))
    data_augmentation.add(RandomRotation(0.2))

    return data_augmentation

#to show augmented datas
data_augmentation = data_augmenter()

for image, _ in train_dataset.take(1):
    plt.figure(figsize=(10, 10))
    first_image = image[0]
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        augmented_image = data_augmentation(tf.expand_dims(first_image, 0))
        plt.imshow(augmented_image[0] / 255)
        plt.axis('off')

preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input

IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape = IMG_SHAPE,
                                               include_top = True,
                                               weights = 'imagenet')

#printing the model summary
base_model.summary()

nb_layers = len(base_model.layers)
print(base_model.layers[nb_layers - 2].name)
print(base_model.layers[nb_layers - 1].name)

image_batch , label_batch = next(iter(train_dataset))
feature_batch = base_model(image_batch)
print(feature_batch.shape)

#shows the different label probabilities in one tensor
label_batch

base_model.trainable = False
image_var = tf.Variable(image_batch)
pred = base_model(image_var)

tf.keras.applications.mobilenet_v2.decode_predictions(pred.numpy(), top = 2)

#layer freezing with the functional API
def catdog_model(image_shape=IMG_SIZE , data_augmentation = data_augmenter()):

    input_shape = image_shape + (3,)
    base_model = tf.keras.applications.MobileNetV2(input_shape = input_shape,
                                                   include_top = False,
                                                   weights = 'imagenet')
    base_model.trainable = False

    inputs = tf.keras.Input(shape = input_shape)
    x = data_augmentation(inputs)
    x = preprocess_input(x)
    x = base_model(x , training = False)
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    x = tf.keras.layers.Dropout(0.2)(x)
    prediction_layer = tf.keras.layers.Dense(1 , activation = 'linear')(x)

    outputs = prediction_layer
    model = tf.keras.Model(inputs , outputs)

    return model

#make an object from our model
model2 = catdog_model(IMG_SIZE , data_augmentation)

base_learning_rate = 0.001
model2.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = base_learning_rate),
               loss = tf.keras.losses.BinaryCrossentropy(from_logits = True),
               metrics = ['accuracy'])

initial_epochs = 5
history = model2.fit(train_dataset, validation_data = validation_dataset, epochs = initial_epochs)

这是错误:

--------------------------------------------------------------------------- 
InvalidArgumentError                      
Traceback (most recent call last) <ipython-input-13-974c37e3cdfd> in <module>
      9 
     10 initial_epochs = 5
---> 11 history = model2.fit(train_dataset, validation_data = validation_dataset, epochs = initial_epochs)

~AppDataRoamingPythonPython37site-packageskerasenginetraining.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)    1182                 _r=1):    1183            callbacks.on_train_batch_begin(step)
-> 1184               tmp_logs = self.train_function(iterator)    1185               if data_handler.should_sync:    1186                 context.async_wait()

~AppDataRoamingPythonPython37site-packagestensorflowpythonutiltraceback_utils.py in error_handler(*args, **kwargs)
    137     except Exception as e:
    138       filtered_tb = _process_traceback_frames(e.__traceback__)
--> 139       raise e.with_traceback(filtered_tb) from None
    140     finally:
    141       del filtered_tb

~AppDataRoamingPythonPython37site-packagestensorflowpythoneagerexecute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError: 2 root error(s) found.   (0) Invalid argument:  Input is empty.    [[{{node decode_image/DecodeImage}}]]   [[IteratorGetNext]]   (1) Invalid argument:  Input is empty.    [[{{node decode_image/DecodeImage}}]]   [[IteratorGetNext]]     [[IteratorGetNext/_6]] 0 successful operations. 0 derived errors ignored. [Op:__inference_train_function_31277]

Function call stack: train_function -> train_function
stack overflow InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Input is empty
原文答案

答案:

作者头像

您的数据集中可能有零大小的图像。您可以通过执行以下操作来检查并修复它:

如果您在 Windows 中,只需在 Windows 资源管理器中打开数据集的根文件夹,然后在右上角的搜索栏中输入: size:empty

让它运行几分钟直到完成,然后使用 Ctrl+A 选择所有结果,然后删除所有结果 Shift+Delete ...注意所有结果的大小都应该为零。

如果您觉得舒服,您可以在 google 上搜索命令行替代方案,我想列出最简单的解决方案。

如果在 linux 中,您可以使用 find: find /tmp -size 0 -print -delete from top solution in this question

免责声明:应用此修复程序时要小心,在尝试之前进行备份