我有一个图片文件夹,下面有两个文件夹分别是300张窃电用户图片文件夹和300张非窃电用户图片文件夹。请帮用python我写出按照8:1:1比例划分数据集生成训练集、验证集和测试集,并保存在相应文件夹下,每个文件夹下有窃电用户和非窃电用户两个子文件夹。
时间: 2024-03-05 09:51:29 浏览: 25
好的,我可以为您提供代码。请注意,在运行代码之前,您需要将图片文件夹路径修改为您自己的路径。
```python
import os
import shutil
import random
# 设置随机数种子
random.seed(42)
# 设置文件夹路径
data_dir = '/path/to/image/folder'
train_dir = os.path.join(data_dir, 'train')
val_dir = os.path.join(data_dir, 'val')
test_dir = os.path.join(data_dir, 'test')
# 创建训练集、验证集和测试集文件夹
if not os.path.exists(train_dir):
os.makedirs(os.path.join(train_dir, 'steal'))
os.makedirs(os.path.join(train_dir, 'non_steal'))
if not os.path.exists(val_dir):
os.makedirs(os.path.join(val_dir, 'steal'))
os.makedirs(os.path.join(val_dir, 'non_steal'))
if not os.path.exists(test_dir):
os.makedirs(os.path.join(test_dir, 'steal'))
os.makedirs(os.path.join(test_dir, 'non_steal'))
# 获取所有窃电用户和非窃电用户图片的路径
steal_dir = os.path.join(data_dir, 'steal')
steal_images = [os.path.join(steal_dir, f) for f in os.listdir(steal_dir) if os.path.isfile(os.path.join(steal_dir, f))]
non_steal_dir = os.path.join(data_dir, 'non_steal')
non_steal_images = [os.path.join(non_steal_dir, f) for f in os.listdir(non_steal_dir) if os.path.isfile(os.path.join(non_steal_dir, f))]
# 打乱图片顺序
random.shuffle(steal_images)
random.shuffle(non_steal_images)
# 划分数据集
train_steal = steal_images[:int(len(steal_images) * 0.8)]
val_steal = steal_images[int(len(steal_images) * 0.8):int(len(steal_images) * 0.9)]
test_steal = steal_images[int(len(steal_images) * 0.9):]
train_non_steal = non_steal_images[:int(len(non_steal_images) * 0.8)]
val_non_steal = non_steal_images[int(len(non_steal_images) * 0.8):int(len(non_steal_images) * 0.9)]
test_non_steal = non_steal_images[int(len(non_steal_images) * 0.9):]
# 将数据集复制到相应文件夹下
for image in train_steal:
shutil.copy(image, os.path.join(train_dir, 'steal'))
for image in train_non_steal:
shutil.copy(image, os.path.join(train_dir, 'non_steal'))
for image in val_steal:
shutil.copy(image, os.path.join(val_dir, 'steal'))
for image in val_non_steal:
shutil.copy(image, os.path.join(val_dir, 'non_steal'))
for image in test_steal:
shutil.copy(image, os.path.join(test_dir, 'steal'))
for image in test_non_steal:
shutil.copy(image, os.path.join(test_dir, 'non_steal'))
```
该代码将按照8:1:1的比例划分数据集,并将训练集、验证集和测试集保存在相应文件夹下,每个文件夹下有窃电用户和非窃电用户两个子文件夹。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![sb3](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)