读取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')
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)
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")
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]
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}")
我们这里取以上代码保存的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/{}"
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]
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"
print(config_name,"4")
save_dir = model_name_without_extension
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)