yolov3_spp项目中的各种配置文件读取

目录

1. open 函数

2. cfg文件夹下文档解析

2.1 hyp.yaml 

2.2 my_yolov_3.cfg

3. data文件夹下文档解析

3.1 my_data.data

3.2 其它


后缀名 .ymal   .txt   .json       

            .cfg   .data  .names   .shapes    可以自定义后缀名??

pyhon文件操作大全https://blog.csdn.net/yifengchaoran/article/details/123591190?ops_request_misc=&request_id=&biz_id=102&utm_term=python%20%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C%E5%A4%A7%E5%85%A8&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-123591190.142%5Ev88%5Econtrol_2,239%5Ev2%5Einsert_chatgpt&spm=1018.2226.3001.4187

1. open 函数

with open("test.txt", "r") as f:  # 打开文件。有汉字时加上:encoding='utf-8'
    data = f.read()               # 读取文件
  • 一般都是用  r  或者  w  打开。  

 r:读取文件,若文件不存在则会报错

w:写入文件,若文件不存在则会先创建再写入,会覆盖原文件

  • 3种读取方式

read()   一次性读取文本中全部的内容,以字符串的形式返回结果。

readline()  只读取文本第一行的内容,以字符串的形式返回结果

readlines()  读取所有内容,以数列返回结果,一般配合for in使用

结果见下表

   

  read:      readline:            readlines: 

2. cfg文件夹下文档解析

2.1 hyp.yaml 

该文件存放网络的超参数

import os
import yaml    # 读取 .yaml文件

with open('hyp.yaml') as f:                       # 打开
    hyp = yaml.load(f, Loader=yaml.FullLoader)    # 读取

2.2 my_yolov_3.cfg

该文件存放搭建网络的各个模块,用于搭建模型。

  

    # 解析_模型_配置
def parse_model_cfg(path: str):
    # 读取文件信息
    with open(path, "r") as f:
        lines = f.read().split("\n")
    # 去除空行和注释行
    lines = [x for x in lines if x and not x.startswith("#")]
    # 去除空格符
    lines = [x.strip() for x in lines]
    # 存放读取结果
    mdefs = []  
    for line in lines:
        ...
        ...
    return mdefs

字符串有关操作见3.1末

3. data文件夹下文档解析

3.1 my_data.data

存放内容:

    # 解析 data 配置
def parse_data_cfg(path):
    with open(path, 'r') as f:
        lines = f.readlines()

    options = dict()
    for line in lines:
        line = line.strip()                      # 去空格
        if line == '' or line.startswith('#'):   # 去掉注释行,空行
            continue
        key, val = line.split('=')               # 每一行等号为界分割
        options[key.strip()] = val.strip()       # 存到字典

    return options                               # 返回结果字典

.readlines() 返回字符串列表,之后对每个字符串进行操作。

字符串操作https://blog.csdn.net/m0_51769031/article/details/127322960?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168517863116800192221102%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168517863116800192221102&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-2-127322960-null-null.142%5Ev88%5Econtrol_2,239%5Ev2%5Einsert_chatgpt&utm_term=python%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%93%8D%E4%BD%9C&spm=1018.2226.3001.4187

3.2 其它

  • f.readlines()和f.read().splitlines()的区别

两者都是返回一个list。

f.readlines()后面有加\n;  f.read().splitlines()没有\n

  • os.path.splitext()

文件的格式分开;返回的是一个元组-------后缀  “.”  之前和之后

输入:.../add.png  ————  输出(‘.../add’,‘.png’)

img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng']

with open(path, "r") as f:
    f = f.read().splitlines()
img_files = [x for x in f if os.path.splitext(x)[-1].lower() in img_formats]
                                          # str.lower() 就是变小写
# (./my_yolo_dataset/train/images/2009_004012.jpg) -> 
# (./my_yolo_dataset/train/labels/2009_004012.txt)

self.label_files = 
[x.replace("images", "labels").replace(os.path.splitext(x)[-1], ".txt")
                            for x in self.img_files]

  • str.split()

字符串分割icon-default.png?t=N4P3https://blog.csdn.net/qq_41780295/article/details/88555183?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168532887316800225519319%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168532887316800225519319&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~hot_rank-1-88555183-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=x.split%28%29&spm=1018.2226.3001.4187numpy常用函数:

np.around():保留小数位数

np.floor():向下取整    1.2---1

np.ceil():向上取整      1.2---2

np.where():查找满足条件的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值