PaddleSeg通过读取pascal_voc12_urls_extracted.txt里面的model连接,一次性训练完所有模型

读取PaddleSeg里面的configs里面的README.md文件model的连接,并分别保存在cityscapes_urls_extracted.txt,pascal_voc12_urls_extracted.txt,ade20k_urls_extracted.txt三个文件中。

import os
import re

# 文件夹路径
folder_path = "/home/aistudio/work/PaddleSeg-release-2.7/configs"

# 创建一个文件来保存匹配到的内容
output_file_matched_links = "matched_links.txt"
output_file_cityscapes_urls = "/home/aistudio/work/PaddleSeg-release-2.7/voc/cityscapes_urls_extracted.txt"
output_file_pascal_voc12_urls = "/home/aistudio/work/PaddleSeg-release-2.7/voc/pascal_voc12_urls_extracted.txt"
output_file_ade20k_urls = "/home/aistudio/work/PaddleSeg-release-2.7/voc/ade20k_urls_extracted.txt"

# 正则表达式匹配链接
pattern = re.compile(r'https?:\/\/.*\/model\.pdparams')

# 遍历文件夹,寻找匹配链接并提取包含 pascal_voc12、cityscapes、ade20k 的 URL
with open(output_file_matched_links, 'w') as matched_links_file:
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.lower() == "readme.md":
                readme_path = os.path.join(root, file)
                # 读取 README.md 文件
                with open(readme_path, 'r') as f:
                    content = f.read()
                    # 使用正则表达式匹配内容
                    matches = pattern.findall(content)
                    if matches:
                        matched_links_file.write(f"Matches found in {readme_path}:\n")
                        for match in matches:
                            matched_links_file.write(f"{match}\n")
                    else:
                        matched_links_file.write(f"No matches found in {readme_path}\n")

# 从匹配的链接文件中提取包含 pascal_voc12、cityscapes、ade20k 的 URL,并分别保存到不同文件
with open(output_file_matched_links, 'r') as file:
    content = file.readlines()

pascal_voc12_urls = [line.strip() for line in content if 'pascal_voc12' in line]
cityscapes_urls = [line.strip() for line in content if 'cityscapes' in line]
ade20k_urls = [line.strip() for line in content if 'ade20k' in line]

# 将提取的 URLs 分别写入不同文件
with open(output_file_pascal_voc12_urls, 'w') as file:
    file.write('\n'.join(pascal_voc12_urls))

with open(output_file_cityscapes_urls, 'w') as file:
    file.write('\n'.join(cityscapes_urls))

with open(output_file_ade20k_urls, 'w') as file:
    file.write('\n'.join(ade20k_urls))

print(f"Matched links saved in {output_file_matched_links}")
print(f"Pascal VOC12 URLs extracted and saved in {output_file_pascal_voc12_urls}")
print(f"Cityscapes URLs extracted and saved in {output_file_cityscapes_urls}")
print(f"ADE20K URLs extracted and saved in {output_file_ade20k_urls}")

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/10945f88a01846dc80fb99c19c383af9.png![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/cdc9280eabc94a8b8a5fe981c8a22366.png

我们这里取以上代码保存的pascal_voc12_urls_extracted.txt文件,读取里面的model链接实现一键式训练所有的model。

from urllib.parse import urlparse

base_command = "python predict.py \
       --config configs/{}/{}.yml \
       --model_path {} \
       --image_path /home/aistudio/work/PaddleSeg-release-2.7/voctestimg \
       --save_dir output/{}"

# 从文件中读取URLs
with open('/home/aistudio/work/PaddleSeg-release-2.7/configs/pascal_voc12_urls_extracted.txt', 'r') as file:
    urls = file.read().splitlines()

for url in urls:
    parsed_url = urlparse(url)
    path_components = parsed_url.path.split('/')
    print(path_components,"1")
    model_name = path_components[-2]  # 获取模型名称部分,比如 ocrnet_hrnetw18_voc12aug_512x512_40k
    model_name_without_extension = os.path.splitext(path_components[-1])[0]  # 去除模型文件扩展名,得到模型名称部分
    print(model_name_without_extension,"2")
    name = model_name_without_extension.replace(model_name, "")
    print(name,"3")

    config_name = model_name_without_extension.replace(model_name, "") + ".yml"  # 生成配置文件名,比如 ocrnet_hrnetw18_voc12aug_512x512_40.yml
    print(config_name,"4")
    save_dir = model_name_without_extension  # 保存路径的文件夹名,比如 ocrnet_hrnetw18_voc12aug_512x512_40
    print(model_name,"5")
    config_folder = model_name.split("_")[0] 

    command = base_command.format(config_folder, model_name, url, save_dir)
    print(model_name,"6")
    print(command,"7")
    os.system(command)  # 执行命令

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值