作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下结构的数据集:
Dataset/
|
|
-----Pothole/
| |
| ------ umm001.jpg
| |
| ------ abd.jpg
| |
| ------
| |
|
|
----Road/
|
------road005.jpg
|
------ummm.jpg
|
------
|
我想将此数据集拆分为 X_train、y_train、X_test、y_test
。
这样:
### data: shuffled and split between train and test
(X_train, y_train), (X_test, y_test) = mnist.load_data()
或者,
(X_train, y_train), (X_test, y_test) = train_test_split(X,y, test_size=0.20)
我该怎么做?
最佳答案
您可以使用 os
模块构建 X
和 y
数组:
import os
X = []
y = []
base_dir = '<full path to dataset folder>/'
for f in sorted(os.listdir(base_dir)):
if os.path.isdir(base_dir+f):
print(f"{f} is a target class")
for i in sorted(os.listdir(base_dir+f)):
print(f"{i} is an input image path")
X.append(base_dir+f+'/'+i)
y.append(f)
print(X)
print(y)
然后您可以使用 train_test_split(X,y, test_size=0.20)
来获取您需要的内容,但请记住,您必须使用其他库(例如 pillow)打开图像
或 scikit-image
或类似的。
如果您打算使用pytorch
来训练神经网络,您可以使用他们的ImageFolder
class创建您的数据集。
关于python - 如何在 X_train、y_train、X_test、y_test 中分割图像数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54333202/
我是一名优秀的程序员,十分优秀!