【发布时间】:2021-06-09 14:01:50
【问题描述】:
数据集文件包含汽车和卡车的图像。汽车的所有图像都在一个文件中,卡车的所有图像都在另一个文件中。包含汽车图像的文件称为 Car,包含卡车图像的文件称为 truck。我正在尝试创建子文件夹 test 和 train。火车文件应包含数据集中 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