KITTI Road 数据集训练验证集划分
KITTI Road数据集是一个用于道路场景理解的计算机视觉数据集。该数据集由德国卡尔斯鲁厄理工学院和丰田美国技术中心联合发布,包含了来自汽车行驶中的短视频序列和对应的传感器数据。KITTI Road数据集是现今最受欢迎和广泛使用的道路场景理解数据集之一,常用于道路检测、车道线检测、车辆检测等任务。我们在运用KITTI Road数据集时候,经常需要将数据集划分为以下格式,但是KITTI官方的数据集只提供了training和testing部分,并没提供validation部分,因此需要我们写代码自己划分训练集和验证集。
|-- datasets
| |-- kitti
| | |-- training
| | | |-- calib
| | | |-- depth_u16
| | | |-- gt_image_2
| | | |-- image_2
| | |-- validation
| | | |-- calib
| | | |-- depth_u16
| | | |-- gt_image_2
| | | |-- image_2
| | |-- testing
| | | |-- calib
| | | |-- depth_u16
| | | |-- image_2
当我们把训练集和测试集按照上述放到指定位置后,再运行下面代码就可以了,代码如下:
import os
import random
import shutil
# 定义training和validation子目录的路径
training_dir = 'datasets/kitti/training'
validation_dir = 'datasets/kitti/validation'
# 定义需要抽样的子目录名称
subdir_name = 'calib'
# 获取需要抽样的子目录的路径和其中的所有文件
subdir_path = os.path.join(training_dir, subdir_name)
files = os.listdir(subdir_path)
# 随机选择58个文件
selected_files = random.sample(files, 58)
# 去除文件名后缀
selected_files = [i.split('.')[0] for i in selected_files]
# 遍历training子目录下的所有子目录
for subdir in os.listdir(training_dir):
subdir_path = os.path.join(training_dir, subdir)
if os.path.isdir(subdir_path):
# 如果子目录中包含已选中的文件,则将这些文件移动到validation子目录下的同名子目录中
dst_subdir_path = os.path.join(validation_dir, subdir)
os.makedirs(dst_subdir_path, exist_ok=True)
for file in selected_files:
if subdir == 'image_2':
file = file + '.png'
src_path = os.path.join(subdir_path, file)
if os.path.isfile(src_path):
dst_path = os.path.join(dst_subdir_path, file)
shutil.move(src_path, dst_path)
elif subdir == 'gt_image_2':
# 把字符串根据_切分成两部分
string, number=file.split('_')
if string == 'um':
filename_road = string + '_road_' + number + '.png'
filename_lane = string + '_lane_' + number + '.png'
# file = file + '.png'
src_path_road = os.path.join(subdir_path, filename_road)
src_path_lane = os.path.join(subdir_path,filename_lane)
if os.path.isfile(src_path_road):
dst_path_road = os.path.join(dst_subdir_path, filename_road)
shutil.move(src_path_road, dst_path_road)
if os.path.isfile(src_path_lane):
dst_path_lane = os.path.join(dst_subdir_path, filename_lane)
shutil.move(src_path_lane, dst_path_lane)
else:
filename = string + "_road_" + number + '.png'
src_path = os.path.join(subdir_path, filename)
if os.path.isfile(src_path):
dst_path = os.path.join(dst_subdir_path, filename)
shutil.move(src_path, dst_path)
elif subdir == 'calib':
file = file + '.txt'
src_path = os.path.join(subdir_path, file)
if os.path.isfile(src_path):
dst_path = os.path.join(dst_subdir_path, file)
shutil.move(src_path, dst_path)
elif subdir == 'depth_u16':
file = file + '.png'
src_path = os.path.join(subdir_path, file)
if os.path.isfile(src_path):
dst_path = os.path.join(dst_subdir_path, file)
shutil.move(src_path, dst_path)
# 打印移动的文件数以及已经完成的子目录数
print(f'Moved {len(selected_files)} files from {subdir} to {dst_subdir_path}')