python-3.x - 我正在尝试生成一个数据集,但得到值错误 "ValueError: ' a' 不能为空,除非没有采样”

标签 python-3.x deep-learning transfer-learning

我正在使用一个包含 3000 多个图像的数据集进行迁移学习。
这是代码的一部分:

import glob
import numpy as np
import os
import shutil

np.random.seed(42)
files = glob.glob('train/*')

cat_files = [fn for fn in files if 'cat' in fn]
dog_files = [fn for fn in files if 'dog' in fn]
len(cat_files), len(dog_files)

cat_train = np.random.choice(cat_files, size=1500, replace=False)

最佳答案

如果没有来自 train/ 的一些样本数据,很难确切地知道发生了什么。 ,但是从 np.random.choice() 的源代码中,通过谷歌搜索您的错误消息发现了这一点。 :

    def choice(self, a, size=None, replace=True, p=None):

    ...

    Raises
    -------
    ValueError
        If a is an int and less than zero, if a or p are not 1-dimensional,
        if a is an array-like of size 0, if p is not a vector of
        probabilities, if a and p have different lengths, or if
        replace=False and the sample size is greater than the population
        size

    ...

    # Format and Verify input
    a = np.array(a, copy=False)
    if a.ndim == 0:
        try:
            # __index__ must return an integer by python rules.
            pop_size = operator.index(a.item())
        except TypeError:
            raise ValueError("'a' must be 1-dimensional or an integer")
        if pop_size <= 0 and np.prod(size) != 0:
            raise ValueError("'a' must be greater than 0 unless no samples are taken")

看来可能是cat_files为空,或类型不正确。在将其传递给 np.random.choice() 之前,您是否已验证其内容? ?

关于python-3.x - 我正在尝试生成一个数据集,但得到值错误 "ValueError: ' a' 不能为空,除非没有采样”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54734427/

相关文章:

python - 将文本文件读入字典

python - 如何在 Python 3 中使用过滤器、映射和归约

python - PyQt5 QTextEdit自动补全

machine-learning - 如何在 mxnet.gluon 中定义每层学习率?

python - pytorch中引入nn.Parameter的目的

python - 选择要训练的层并在 Keras 中训练的 Inception 模型中添加跳过连接

python-3.x - 如何在列 Pandas 中找到连续零的最大计数?

python - 数据集映射表中的 Tensorflow 特征列已初始化问题

Tensorflow 对象检测——增加批量大小会导致失败

machine-learning - 以YOLO为例进行微调和迁移学习