splitdata模块
划分yolo数据集¶
splitdata
¶
splitdata(
imgpath: str | Path,
txtpath: str | Path,
new_imgpath: str | Path,
new_txtpath: str | Path,
val_size: float = 0.1,
test_size: float = 0.1,
postfix: str = ".png",
) -> None
将数据集拆分为训练集、val集和测试集,并相应地复制文件。(主要用于yolo目标检测数据的划分)
主要用于yolo目标检测数据的划分,分成训练集、验证集和测试集。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
imgpath
|
str | Path
|
原始图片路径的根目录 (这个目录下包含了要处理的图片) |
required |
txtpath
|
str | Path
|
原始标签路径的根目录 (txt文件, 图片文件和txt文件的stem要一样,没有重复样本且数量也要一致,否则可能报错) |
required |
new_imgpath
|
str | Path
|
新的图片路径 |
required |
new_txtpath
|
str | Path
|
新的标签路径 |
required |
val_size
|
float
|
验证集所占比例. Defaults to 0.1. |
0.1
|
test_size
|
float
|
测试集所占比例. Defaults to 0.1. |
0.1
|
postfix
|
str
|
图片后缀名. Defaults to ".png". |
'.png'
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
如果原始图片路径或标签路径不存在,抛出异常。 |
AssertionError
|
如果txt文件和图片文件的stem不一致 或者数量不一致,抛出异常。 |
ValueError
|
如果val_size和test_size不在0到1之间,抛出异常。 |
ValueError
|
如果val_size + test_size >= 1,抛出异常。 |
Example
Source code in src/cfun/yolo/splitdata.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
check_image_and_json
¶
check_image_and_json(
json_dir: Path | str,
label_key: str,
image_dir: Path | str,
image_suffix: str,
) -> dict
检查json_dir下的json文件 和 image_dir下的图片文件是否一致,并返回类别映射文件
Note
检查三个点:
- json_dir下的json文件, image_dir下的图片文件, 以及json文件中的imagePath字段,这三者的 stem 数量和名字要相同,不能重复.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_dir
|
Path | str
|
JSON 标注文件所在目录 |
required |
label_key
|
str
|
用于分类的键, 如 "label", 这个key在json文件中的shapes字段下才行 |
required |
image_dir
|
Path | str
|
图像文件所在目录, |
required |
image_suffix
|
str
|
图像文件后缀名, |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
如果json_dir或image_dir不是有效的目录,抛出异常。 |
AssertionError
|
如果json_dir下的json文件数量和image_dir下的图片文件数量不一致,抛出异常。 |
AssertionError
|
如果json文件的stem和图片文件的stem不一致,抛出异常。 |
AssertionError
|
如果json文件中的imagePath字段和图片文件名不一致,抛出异常。 |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
类别映射文件,格式为 |
Example
Source code in src/cfun/yolo/splitdata.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
json_to_yolo_detect_data
¶
json_to_yolo_detect_data(
json_dir: Path | str,
label_key: str,
image_dir: Path | str,
image_suffix: str,
) -> None
将json数据直接转为yolo训练数据集,且已划分为训练集、验证集和测试集。主要用于yolo目标检测数据的划分(一步到位版本)
会在当且文件夹下创建一个imgs_split的文件夹,里面包含了train、val和test三个文件夹,分别对应训练集、验证集和测试集。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_dir
|
Path | str
|
JSON 标注文件所在目录 |
required |
label_key
|
str
|
用于分类的键,如 "label",这个key在json文件中的shapes字段下才行 |
required |
image_dir
|
Path | str
|
图像文件所在目录, |
required |
image_suffix
|
str
|
图像文件后缀名, |
required |