当前位置:网站首页>Dataset partitioning script for classification tasks
Dataset partitioning script for classification tasks
2022-07-01 11:41:00 【Jiangxiaobai JLJ】
This script realizes the data set division of classification tasks , With a little modification , It can also be used for target detection to divide training sets 、 Verification set 、 Test set .
import os
from shutil import copy, rmtree
import random
def mk_file(file_path: str):
if os.path.exists(file_path):
# If the folder exists , First delete the original folder and all the files it contains , And re create a new folder
rmtree(file_path)
os.makedirs(file_path)
def main():
# Make sure that random repeatable
random.seed(0)
# Centralize data 10% Divide the data into validation sets
split_rate = 0.1
# Point to the main directory where classified image data is stored
cwd = os.getcwd()
data_root = os.path.join(cwd, "flower_data")
origin_flower_path = os.path.join(data_root, "flower_photos")
assert os.path.exists(origin_flower_path), "path '{}' does not exist.".format(origin_flower_path)
# Get the address information of all folders under the home directory
flower_class = [cla for cla in os.listdir(origin_flower_path)
if os.path.isdir(os.path.join(origin_flower_path, cla))]
# Saved training set folder
train_root = os.path.join(data_root, "train")
mk_file(train_root)
for cla in flower_class:
# Create a folder for each category
mk_file(os.path.join(train_root, cla))
# Create a folder to save the validation set
val_root = os.path.join(data_root, "val")
mk_file(val_root)
for cla in flower_class:
# Create a folder for each category
mk_file(os.path.join(val_root, cla))
# Classification according to data set category
for cla in flower_class:
cla_path = os.path.join(origin_flower_path, cla)
images = os.listdir(cla_path)
num = len(images)
# Index of random sampling verification set
eval_index = random.sample(images, k=int(num*split_rate))
for index, image in enumerate(images):
if image in eval_index:
# Copy the files assigned to the validation set to the corresponding directory
image_path = os.path.join(cla_path, image)
new_path = os.path.join(val_root, cla)
copy(image_path, new_path) # copy( The original path , New path )
else:
# Copy the files assigned to the training set to the corresponding directory
image_path = os.path.join(cla_path, image)
new_path = os.path.join(train_root, cla)
copy(image_path, new_path)
print("\r[{}] processing [{}/{}]".format(cla, index+1, num), end="") # processing bar
print()
print("processing done!")
if __name__ == '__main__':
main()
边栏推荐
- Can servers bundled with flask be safely used in production- Is the server bundled with Flask safe to use in production?
- Brief explanation of the working principle, usage scenarios and importance of fingerprint browser
- Kafuka learning path (I) Kafuka installation and simple use
- Web foundation of network security note 02
- 博途V15添加GSD文件
- Value/string in redis
- Flip the array gracefully
- 商汤进入解禁期:核心管理层自愿禁售 强化公司长期价值信心
- CPU 上下文切换的机制和类型 (CPU Context Switch)
- 自定義 grpc 插件
猜你喜欢

Neo4j 中文开发者月刊 - 202206期

Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode

Network security learning notes 01 network security foundation

Tempest HDMI leak receive 3

博途V15添加GSD文件

Compile and debug net6 source code

Several cases of index failure

ACLY与代谢性疾病

Tianrunyun, invested by Tian Suning, was listed: its market value was 2.2 billion Hong Kong, and its first year profit decreased by 75%

图的理论基础
随机推荐
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
Harbor webhook from principle to construction
Redis的攻击手法
如何看懂开发的查询语句
Why must we move from Devops to bizdevops?
妙啊!MarkBERT
redis中value/String
redis常识
Learning summary on June 29, 2022
开发说,“ 这个不用测,回归正常流程就行 “,测试人员怎么办?
Kafuka learning path (I) Kafuka installation and simple use
S7-1500PLC仿真
Acly and metabolic diseases
openinstall:微信小程序跳转H5配置业务域名教程
Y48. Chapter III kubernetes from introduction to mastery -- pod status and probe (21)
Ultra detailed black apple installation graphic tutorial sent to EFI configuration collection and system
证券账户销户后果 开户安全吗
Can I open an account today and buy stocks today? Is it safe to open an account online?
TMUX usage
Binary stack (I) - principle and C implementation