图像生成器中的错误:要求检索元素 0,但序列的长度为 0
Posted
技术标签:
【中文标题】图像生成器中的错误:要求检索元素 0,但序列的长度为 0【英文标题】:Error in Image generator : Asked to retrieve element 0, but the Sequence has length 0 【发布时间】:2021-02-04 15:28:44 【问题描述】:我正在尝试计算二进制类彩色图像分类问题中的真阳性、真阴性、假阳性、假阴性比率。
我有二进制类、人脸和背景彩色图像,我必须使用 MLP 对它们进行分类。
我的问题是:我得到错误:
ValueError: 要求检索元素 0,但序列的长度为 0
编辑:完整追溯
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
128 raise ValueError(' is not supported in multi-worker mode.'.format(
129 method.__name__))
--> 130 return method(self, *args, **kwargs)
131
132 return tf_decorator.make_decorator(
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1577 use_multiprocessing=use_multiprocessing,
1578 model=self,
-> 1579 steps_per_execution=self._steps_per_execution)
1580
1581 # Container that configures and calls `tf.keras.Callback`s.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
1115 use_multiprocessing=use_multiprocessing,
1116 distribution_strategy=ds_context.get_strategy(),
-> 1117 model=model)
1118
1119 strategy = ds_context.get_strategy()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, shuffle, workers, use_multiprocessing, max_queue_size, model, **kwargs)
914 max_queue_size=max_queue_size,
915 model=model,
--> 916 **kwargs)
917
918 @staticmethod
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in __init__(self, x, y, sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
784 # Since we have to know the dtype of the python generator when we build the
785 # dataset, we have to look at a batch to infer the structure.
--> 786 peek, x = self._peek_and_restore(x)
787 peek = self._standardize_batch(peek)
788 peek = _process_tensorlike(peek)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _peek_and_restore(x)
918 @staticmethod
919 def _peek_and_restore(x):
--> 920 return x[0], x
921
922 def _handle_multiprocessing(self, x, workers, use_multiprocessing,
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/iterator.py in __getitem__(self, idx)
55 'but the Sequence '
56 'has length length'.format(idx=idx,
---> 57 length=len(self)))
58 if self.seed is not None:
59 np.random.seed(self.seed + self.total_batches_seen)
ValueError: Asked to retrieve element 0, but the Sequence has length 0
在尝试分别预测 2 个类中的每个文件夹时(而不是包含 2 个文件夹的根文件夹,每个类一个用于训练)。
我产生错误的代码是:
test_face_dir = "/content/test/TESTSET/face"
test_background_dir = "/content/test/TESTSET/background"
# Face DG
test_datagen_face = ImageDataGenerator(rescale=1./255)
test_generator_face = test_datagen_face.flow_from_directory(
test_face_dir,
target_size=img_window[:2],
batch_size=batch_size,
class_mode='binary',
color_mode='rgb'
)
# Background DG
test_datagen_background = ImageDataGenerator(rescale=1./255)
test_generator_background = test_datagen_background.flow_from_directory(
test_background_dir,
target_size=img_window[:2],
batch_size=batch_size,
class_mode='binary',
color_mode='rgb'
)
#-----------------------------------------
prediction_face = simpleMLP.predict(test_generator_face)
prediction_background = simpleMLP.predict(test_generator_background)
#-----------------------------------------
# th = 0.5 #threshold
# Face
prediction_face[prediction_face>=th]=1
prediction_face[prediction_face<th]=0
pred_face = np.squeeze(prediction_face)
print('pred shape: ', pred_face.shape,int(np.sum(pred_face)))
# Background
prediction_background[prediction_background>=th]=1
prediction_background[prediction_background<th]=0
pred_background = np.squeeze(prediction_background)
print('pred shape: ', pred_background.shape,int(np.sum(pred_background)))
【问题讨论】:
请注意,我们无法运行您的代码,因为我们没有输入数据。帮助我们帮助您并包含完整的回溯,格式为代码。 @MisterMiyagi Full Traceback 添加。 回溯似乎只显示库函数。它不包含您的任何代码。这真的是整个追溯吗? @MisterMiyagi 是的,完整的追溯。我选择了另一种方法来完成这项工作,但是这种将数据生成器用于 1 个文件夹而不是不同的 2 个文件夹的方法不起作用。 【参考方案1】:生成错误是因为我使用的是 class_mode='binary'
,而我指定的是一个图像文件夹而不是两个文件夹 faces 和 backgrounds ,当我选择包含它们的父文件夹时,这个问题就消失了。
无论如何计算我得到的 True Positive、True Negative、False Positive、False Negative 比率基本事实如下:
# Get Y_GroundTruth
y_label = (np.expand_dims(test_generator_eval.classes, axis=1).ravel()).astype(int)
【讨论】:
以上是关于图像生成器中的错误:要求检索元素 0,但序列的长度为 0的主要内容,如果未能解决你的问题,请参考以下文章
错误:Django 1.4 上的“字典更新序列元素 #0 的长度为 1;需要 2”
Elastic Beanstalk:字典更新序列元素 #0 的长度为 1; 2 是必需的
Django - 字典更新序列元素#0 的长度为 1; 2是必需的[重复]