【问题标题】:Error while creating test and train dataset创建测试和训练数据集时出错
【发布时间】:2021-06-09 14:01:50
【问题描述】:

数据集文件包含汽车和卡车的图像。汽车的所有图像都在一个文件中,卡车的所有图像都在另一个文件中。包含汽车图像的文件称为 Car,包含卡车图像的文件称为 truck。我正在尝试创建子文件夹 testtrain。火车文件应包含数据集中 80% 的洗牌汽车和卡车图像,测试文件应包含数据集中 20% 的洗牌汽车和卡车图像。我尝试过使用 os 方法,但弹出错误。相同的代码和错误如下所示。

代码:

import os 
import shutil
import random
import glob         
os.chdir('C:/Users/akash/Downloads/Datasets')
for c in random.sample(glob.glob('car*'),200):
    shutil.move( c, 'train/car' )
for c in random.sample(glob.glob('truck*'),200):
    shutil.move( c, 'train/truck' )
for c in random.sample(glob.glob('car*'),100):
    shutil.move( c, 'test/car' )
for c in random.sample(glob.glob('truck*'),100):
    shutil.move( c, 'test/car' )
os.chdir('../../')

错误:

ValueError                                Traceback (most recent call last)
<ipython-input-11-9e77a796540b> in <module>
----> 1 for c in random.sample(glob.glob('car*'),200):
      2     shutil.move( c, 'train/car' )
      3 for c in random.sample(glob.glob('truck*'),200):
      4     shutil.move( c, 'train/truck' )
      5 for c in random.sample(glob.glob('car*'),100):

C:\ProgramData\Anaconda3\lib\random.py in sample(self, population, k)
    361         n = len(population)
    362         if not 0 <= k <= n:
--> 363             raise ValueError("Sample larger than population or is negative")
    364         result = [None] * k
    365         setsize = 21        # size of a small set minus size of an empty list

ValueError: Sample larger than population or is negative

【问题讨论】:

  • 错误表明您指定了人口规模 (i.e) 200,它大于尝试从 (i.e) images of cars 抽样的人口的实际规模。你有多少汽车的图像?尝试减少样本量。
  • 汽车文件有290张汽车图片。卡车文件也是如此。我还尝试减少样本量,但弹出相同的错误。
  • 根据您的描述All images of cars are in one file,这个文件是什么?是目录吗?是腌制文件吗?我相信这就是导致错误的原因。
  • 文件是指文件夹。很抱歉误解了相同的内容。
  • 你能把你的文件夹结构添加到你的描述中吗?

标签: python tensorflow conv-neural-network


【解决方案1】:

改变这个

for c in random.sample(glob.glob('car*'),200):

到这里

population = glob.glob('car*')
for c in random.sample(population,200):

在第二行设置断点,当它命中时,检查population 的值,尤其是长度。它需要 >= 200。

【讨论】:

  • 嗯,是的,它会的。你没看我回答的最后一句话吗?
【解决方案2】:

出现错误是因为 glob 模块应在其中查找的目录未以正确的格式指定。确保在当前目录中有目录 'train/car''train/truck'test/cartest/truck。把你的代码改成下面的代码sn-p。

代码片段:

import os 
import shutil
import random
import glob         
os.chdir('C:/Users/akash/Downloads/Datasets')

if not os.path.isdir('train/car'):
    os.makedirs('train/car')
if not os.path.isdir('train/truck'):
    os.makedirs('train/truck')

if not os.path.isdir('test/car'):
    os.makedirs('test/car')
if not os.path.isdir('test/truck'):
    os.makedirs('test/truck')

for c in random.sample(glob.glob('./car/*'),200):
    shutil.move( c, 'train/car' )
for c in random.sample(glob.glob('./truck/*'),200):
    shutil.move( c, 'train/truck' )
for c in random.sample(glob.glob('./car/*'),100):
    shutil.move( c, 'test/car' )
for c in random.sample(glob.glob('./truck/*'),100):
    shutil.move( c, 'test/truck' )
os.chdir('../../')

【讨论】:

  • 我也做了同样的事情,但出现了一个不同的错误。错误是FileNotFoundError: [Errno 2] No such file or directory: 'train/car。你能帮帮我吗?是不是因为我没有创建一个名为 train 的文件?
  • 我将正斜杠改为反斜杠。你能再试一次代码吗?
  • 我试过了,但没有用。 SyntaxError: EOL while scanning string literal
  • @user13973948 是的,尝试创建目录和'train/car''train/truck'。请再次尝试该代码。带有正斜杠的那个。
猜你喜欢
  • 2015-07-16
  • 2020-02-08
  • 2022-01-21
  • 2020-07-07
  • 2020-03-09
  • 2023-01-24
  • 2017-09-11
  • 2019-03-21
  • 2018-05-25
相关资源
最近更新 更多