COCO数据集缺失文件补全方法

2024-09-02 06:52
文章标签 数据 方法 缺失 补全 coco

本文主要是介绍COCO数据集缺失文件补全方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

COCO2017数据集图片文件缺失自动补全方法

一、前言

本文代码是以目标检测(object detection)和实例分割(instance segmentation)任务的标签文件为例,即instances_train/val/test2017.json文件。

其他任务的标签文件内容略有不同,但是图片来源表示字段完全相同,因此代码可通用。另外如果是非2017版本COCO应该也通用。

1.1 杂谈

本人计算机视觉科研狗一条,生活在在威名赫赫的 汪汪星球球立大学。

某日,本汪以Featurized-QueryRCNN框架为基础,结合自己的模块形成一个新的目标检测模型。当我按下训练的命令,训练了一段时间后,出现了如下报错(因为刚好训练到的那个batch中有缺失的图片):

  ······此处省略一万字File "/liushuai2/PCP/FeatEnHancer-main/detectron2/detectron2/data/detection_utils.py", line 182, in read_imagewith PathManager.open(file_name, "rb") as f:File "/root/anaconda3/envs/FEHR/lib/python3.9/site-packages/iopath/common/file_io.py", line 1012, in openbret = handler._open(path, mode, buffering=buffering, **kwargs)  # type: ignoreFile "/root/anaconda3/envs/FEHR/lib/python3.9/site-packages/iopath/common/file_io.py", line 604, in _openreturn open(  # type: ignore
FileNotFoundError: [Errno 2] No such file or directory: '/liushuai2/PCP/datasets/COCO2017/train2017/000000581831.jpg'

当看到这个我都震惊了,因为数据集我是直接从COCO官方网站下载下来的,竟然有图片缺失!!!

只能想办法解决了,找了半天网络上没啥好用的办法,而且有也是COCO2014而不是COCO2017的。对此我决定自己写一个缺失文件补全代码。

1.2 部分代码逻辑说明

我的代码如果大家细看会发现你没有看到下载链接。这是因为标准COCO数据集的标签文件中对每一张图片都包含了下载链接,如下所示:

{"license": 4,"file_name": "000000060623.jpg","coco_url": "http://images.cocodataset.org/train2017/000000060623.jpg","height": 427,"width": 640,"date_captured": "2013-11-14 17:24:15","flickr_url": "http://farm7.staticflickr.com/6080/6113512699_37b4c98473_z.jpg","id": 60623}

可以看到,在该字典中 coco_url 键的值为图像下载地址。故我在代码中提取了该键值对,并将其值作为下载命令的参数。

另外大家觉得不懂我的代码是怎么解析标签文件(json)的,其实我把COCO数据集的标准格式摆出来大家再去看代码就明白了:

{"info": {"description": "This is stable 1.0 version of the 2014 MS COCO dataset.","url": "http://mscoco.org","version": "1.0","year": 2014,"contributor": "Microsoft COCO group","date_created": "2015-01-27 09:11:52.357475"},"licenses": [{"url": "http://creativecommons.org/licenses/by-nc-sa/2.0/","id": 1,"name": "Attribution-NonCommercial-ShareAlike License"}],"images": [{"license": 3,"file_name": "COCO_val2014_000000391895.jpg","coco_url": "http://mscoco.org/images/391895","height": 360,"width": 640,"date_captured": "2013-11-14 11:18:45","flickr_url": "http://farm9.staticflickr.com/8186/8119368305_4e622c8349_z.jpg","id": 391895}],"annotations": [{"id": 1768,"image_id": 289343,"category_id": 18,"segmentation": [[510.66, 423.01, 511.72, 420.03, 510.45, ...]],"area": 702.1057499999998,"bbox": [473.07, 395.93, 38.65, 28.67],"iscrowd": 0}],"categories": [{"id": 18,"name": "dog","supercategory": "animal"}]
}

上面是一个标签文件的标准格式,不过为了简便每个数字我都只写了一个示例。我们关注的是字典中的images键,这个键所对应的值是一个数组,这个数组包含了数据集中每一张图片的基本信息。

二、实现代码

以下三种方法均可在 Windows 或 Linux 操作系统上运行,已做系统适配,兼容性好。唯一的缺点就是有两种方法需要二外安装依赖包,不过这不是很大的问题啦。

下面三个代码块均需要用到的非标准库是 tqdm 。我用该库来制作好看的进度条,因此需要提前安装:

pip install tqdm

2.1 使用msgspec库(极快 + 操作稍稍麻烦)

2.1.1 食用指南

安装 msgspec

pip install msgspec

拷贝【2.1.2】的代码,并修改 annotations_file_pathimage_directory 为你的COCO数据集相关路径

2.1.2 代码
import os
import subprocess
from tqdm import tqdm
import sys
import platform
import time
import msgspecclass Image(msgspec.Struct):file_name: strcoco_url: strclass ImagesInfo(msgspec.Struct):images: list[Image]def get_missing_files(annotation_file_path, image_directory):# 读取标注文件start_time = time.time()with open(annotation_file_path, 'rb') as f:images_info = msgspec.json.decode(f.read(), type=ImagesInfo)annotation_time = time.time() - start_time# 获取图像列表start_time = time.time()annotation_images_list = images_info.imagesdirectory_images_set = set(os.listdir(image_directory))directory_time = time.time() - start_time# 存放缺失的文件名missing_files = []# 遍历字典,检查每个文件是否存在start_time = time.time()for image in annotation_images_list:# 获取 文件名file_name = image.file_name# 检查文件是否存在if file_name not in directory_images_set:print(f"文件 {file_name} 不存在")download_url = image.coco_urlmissing_files.append({'file_name': file_name,'download_url': download_url})check_time = time.time() - start_timeprint(f"缺失文件数量: {len(missing_files)}")print(f"读取标注文件耗时: {annotation_time:.4f} 秒")print(f"获取图像列表耗时: {directory_time:.4f} 秒")print(f"检查缺失文件耗时: {check_time:.4f} 秒")return missing_filesdef download_Missing_files(missing_files, image_directory):# 检测操作系统类型system = platform.system().lower()# 遍历缺失文件列表并下载with tqdm(missing_files) as pbar:for file in pbar:# 实时更新进度条的描述为当前正在下载的文件名pbar.set_description(f"Downloading {file['file_name']}")if system == "windows":# 构建 curl 命令download_command = f"curl -o {image_directory}\\{file['file_name']} {file['download_url']}"else:# 构建 wget 命令download_command = f"wget -P {image_directory} {file['download_url']}"# 执行 wget 命令,并将输出重定向到DEVNULL以隐藏输出subprocess.run(download_command, shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)if __name__ == '__main__':# 读取标注文件和图像目录路径annotations_file_path = r'/liushuai2/PCP/datasets/COCO2017/annotations/instances_train2017.json'image_directory = r'/liushuai2/PCP/datasets/COCO2017/train2017'# 获取缺失的文件列表missing_files = get_missing_files(annotations_file_path, image_directory)if len(missing_files) == 0:print("没有缺失的文件, Over")sys.exit()# 下载缺失的文件download_Missing_files(missing_files, image_directory)

2.2 使用orjson库(慢 + 操作简单)

2.2.1 食用指南

安装 msgspec

pip install orjson

拷贝【2.2.2】的代码,并修改 annotations_file_pathimage_directory 为你的COCO数据集相关路径

2.2.2 代码
import json
import os
import subprocess
from tqdm import tqdm
import sys
import platform
import time
import orjsondef get_missing_files(annotation_file_path, image_directory):# 读取标注文件start_time = time.time()with open(annotation_file_path, 'rb') as f:images_info = orjson.loads(f.read())annotation_time = time.time() - start_time# 获取图像列表start_time = time.time()annotation_images_list = images_info['images']directory_images_set = set(os.listdir(image_directory))directory_time = time.time() - start_time# 存放缺失的文件名missing_files = []# 遍历字典,检查每个文件是否存在start_time = time.time()for image in annotation_images_list:file_name = image['file_name']if file_name not in directory_images_set:print(f"文件 {file_name} 不存在")download_url = image['coco_url']missing_files.append({'file_name': file_name,'download_url': download_url})check_time = time.time() - start_timeprint(f"缺失文件数量: {len(missing_files)}")print(f"读取标注文件耗时: {annotation_time:.4f} 秒")print(f"获取图像列表耗时: {directory_time:.4f} 秒")print(f"检查缺失文件耗时: {check_time:.4f} 秒")return missing_filesdef download_Missing_files(missing_files, image_directory):# 检测操作系统类型system = platform.system().lower()# 遍历缺失文件列表并下载with tqdm(missing_files) as pbar:for file in pbar:# 实时更新进度条的描述为当前正在下载的文件名pbar.set_description(f"Downloading {file['file_name']}")if system == "windows":# 构建 curl 命令download_command = f"curl -o {image_directory}\\{file['file_name']} {file['download_url']}"else:# 构建 wget 命令download_command = f"wget -P {image_directory} {file['download_url']}"# 执行 wget 命令,并将输出重定向到DEVNULL以隐藏输出subprocess.run(download_command, shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)if __name__ == '__main__':# 读取标注文件和图像目录路径annotations_file_path = r'/liushuai2/PCP/datasets/COCO2017/annotations/instances_train2017.json'image_directory = r'/liushuai2/PCP/datasets/COCO2017/train2017'# 获取缺失的文件列表missing_files = get_missing_files(annotations_file_path, image_directory)if len(missing_files) == 0:print("没有缺失的文件, Over")sys.exit()# 下载缺失的文件download_Missing_files(missing_files, image_directory)

2.3 使用json库(极慢 + 操作最简单)

修改 annotations_file_pathimage_directory 为你的COCO数据集相关路径即可直接食用

import json
import os
import subprocess
from tqdm import tqdm
import sys
import platform
import timedef get_missing_files(annotation_file_path, image_directory):# 读取标注文件start_time = time.time()with open(annotation_file_path, 'r') as f:images_info = json.load(f)annotation_time = time.time() - start_time# 获取图像列表start_time = time.time()annotation_images_list = images_info['images']directory_images_set = set(os.listdir(image_directory))directory_time = time.time() - start_time# 存放缺失的文件名missing_files = []# 遍历字典,检查每个文件是否存在start_time = time.time()for image in annotation_images_list:# 获取文件名file_name = image['file_name']# 将不存在的文件添加到缺失列表if file_name not in directory_images_set:print(f"文件 {file_name} 不存在")download_url = image['coco_url']missing_files.append({'file_name': file_name,'download_url': download_url})check_time = time.time() - start_timeprint(f"缺失文件数量: {len(missing_files)}")print(f"读取标注文件耗时: {annotation_time:.4f} 秒")print(f"获取图像列表耗时: {directory_time:.4f} 秒")print(f"检查缺失文件耗时: {check_time:.4f} 秒")return missing_filesdef download_Missing_files(missing_files, image_directory):# 检测操作系统类型system = platform.system().lower()# 遍历缺失文件列表并下载with tqdm(missing_files) as pbar:for file in pbar:# 实时更新进度条的描述为当前正在下载的文件名pbar.set_description(f"Downloading {file['file_name']}")if system == "windows":# 构建 curl 命令download_command = f"curl -o {image_directory}\\{file['file_name']} {file['download_url']}"else:# 构建 wget 命令download_command = f"wget -P {image_directory} {file['download_url']}"# 执行 wget 命令,并将输出重定向到DEVNULL以隐藏输出subprocess.run(download_command, shell=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)if __name__ == '__main__':# 读取标注文件和图像目录路径annotations_file_path = r'/liushuai2/PCP/datasets/COCO2017/annotations/instances_train2017.json'image_directory = r'/liushuai2/PCP/datasets/COCO2017/train2017'# 获取缺失的文件列表missing_files = get_missing_files(annotations_file_path, image_directory)if len(missing_files) == 0:print("没有缺失的文件, Over")sys.exit()# 下载缺失的文件download_Missing_files(missing_files, image_directory)

三、用到的库的简单介绍

3.1 基本介绍

3.1.1 msgspec

msgspec 是一个高性能的序列化和验证库,支持 JSON、MessagePack、YAML 和 TOML 等多种格式。它的特点包括:

  • 高性能:在常见协议的编码/解码中表现出色,通常比其他库快10-80倍。
  • 零成本的模式验证:使用 Python 类型注解进行模式验证。
  • 轻量级:没有依赖项,适合需要高效处理数据的场景。
  • 结构化数据支持:提供类似 dataclassesStruct 类型,但性能更高。
3.1.2 orjson

orjson 是一个快速且正确的 JSON 库,专为 Python 设计。它的特点包括:

  • 极高的性能:在序列化和反序列化方面表现优异,特别是在处理大型数据结构时。
  • 原生支持多种类型:包括 dataclassdatetimenumpyUUID 实例。
  • 严格的 JSON 和 UTF-8 规范:确保数据的正确性和兼容性。
  • 高效的内存使用:在处理 numpy.ndarray 时,内存使用率仅为其他库的0.3倍。
3.1.3 json

json 是 Python 标准库中的 JSON 编码和解码模块。它的特点包括:

  • 易用性:作为标准库的一部分,无需额外安装。
  • 基本功能:支持将 Python 对象序列化为 JSON 字符串,以及将 JSON 字符串反序列化为 Python 对象。
  • 扩展性:可以通过自定义编码器和解码器来处理复杂类型。

3.2 对比总结

  • 性能
    • msgspec:在编码/解码方面表现出色,通常比其他库快10-80倍。

    • orjson:在序列化和反序列化方面表现优异,特别是在处理大型数据结构时。

    • json:性能较为一般,适合处理小型数据。

  • 功能
    • msgspec:支持 JSON、MessagePack、YAML 和 TOML,多格式支持;零成本的模式验证;提供类似 dataclassesStruct 类型。

    • orjson:原生支持 dataclassdatetimenumpyUUID 实例;严格的 JSON 和 UTF-8 规范。

    • json:基本的 JSON 编码和解码功能;可以通过自定义编码器和解码器来处理复杂类型。

  • 易用性
    • msgspec:需要额外安装,但没有依赖项,轻量级。

    • orjson:需要额外安装,但提供了丰富的功能和高性能。

    • json:作为 Python 标准库的一部分,无需额外安装,使用方便。

  • 内存使用
    • msgspec:高效的内存使用,适合需要高效处理数据的场景。

    • orjson:在处理 numpy.ndarray 时,内存使用率仅为其他库的0.3倍。

    • json:内存使用较为一般。

  • 适用场景
    • msgspec:适合需要高性能和多格式支持的场景,如实时数据处理和大规模数据传输。

    • orjson:适合需要高性能 JSON 处理的场景,特别是涉及大型数据结构和多种数据类型的应用。

    • json:适合一般的 JSON 编码和解码需求,特别是小型项目或不需要高性能的场景。

这篇关于COCO数据集缺失文件补全方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1129303

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

SpringBoot中ResponseEntity的使用方法举例详解

《SpringBoot中ResponseEntity的使用方法举例详解》ResponseEntity是Spring的一个用于表示HTTP响应的全功能对象,它可以包含响应的状态码、头信息及响应体内容,下... 目录一、ResponseEntity概述基本特点:二、ResponseEntity的基本用法1. 创

java中判断json key是否存在的几种方法

《java中判断jsonkey是否存在的几种方法》在使用Java处理JSON数据时,如何判断某一个key是否存在?本文就来介绍三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目http://www.chinasem.cn录第一种方法是使用 jsONObject 的 has 方法