如何解决 ValueError:样本大于总体或为负?

问题描述

我正在尝试学习如何使用带有 GPU 的 kaggle 内核。我从这个 kaggle 教程开始,用于“asl 字母表”数据集分类

https://www.kaggle.com/dansbecker/running-kaggle-kernels-with-a-gpu?scriptVersionId=3558833

但是在第一行代码中(在实现模型之前,当我们只想在训练数据集中显示一些样本时),我得到了这个错误

ValueError: Sample larger than population or is negative.

似乎 glob 函数返回一个空列表。

这是代码的那部分:

import cv2
from glob import glob
from matplotlib import pyplot as plt
from numpy import floor
import random

def plot_three_samples(letter):
    print("Samples images for letter " + letter)
    base_path = '../input/asl_alphabet_train/asl_alphabet_train/'
    img_path = base_path + letter + '/**'
    path_contents = glob(img_path)

    plt.figure(figsize=(16,16))
    imgs = random.sample(path_contents,3)
    plt.subplot(131)
    plt.imshow(cv2.imread(imgs[0]))
    plt.subplot(132)
    plt.imshow(cv2.imread(imgs[1]))
    plt.subplot(133)
    plt.imshow(cv2.imread(imgs[2]))
    return

plot_three_samples('A')

这是错误

ValueError                                Traceback (most recent call last)
<ipython-input-1-bcd03e4a0bce> in <module>
     35     return
     36 
---> 37 plot_three_samples('A')

<ipython-input-1-bcd03e4a0bce> in plot_three_samples(letter)
     26 
     27     plt.figure(figsize=(16,16))
---> 28     imgs = random.sample(path_contents,3)
     29     plt.subplot(131)
     30     plt.imshow(cv2.imread(imgs[0]))

/opt/conda/lib/python3.7/random.py in sample(self,population,k)
    319         n = len(population)
    320         if not 0 <= k <= n:
--> 321             raise ValueError("Sample larger than population or is negative")
    322         result = [None] * k
    323         setsize = 21        # size of a small set minus size of an empty list

ValueError: Sample larger than population or is negative

kaggle 内核已经有了数据,我只是按照他们的教程规则来使用数据以及如何使用 glob 等来处理它。

有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)