多个数据集循环运行测试,用python循环调用命令行

 例如测试命令是这样的

CUDA_VISIBLE_DEVICES=X python dehazing/test.py --size ['B' or 'L'] --input_dir [Input path] --target_dir [GT path] --result_dir [Result path] --weights [Model weighting path]
# 因为我电脑只有一个GUP就不需要写CUDA_VISIBLE_DEVICES=X这个命令,没得指定,直接省略就行

 则可以编写下面的python代码循环调用命令行

import subprocess

# 这个是需要测试的多个数据集名称,在下面用{xxx}的方式替换,或者直接替换变量
model_values = ['densehaze', 'ohaze', 'OTS','ITS']
avg_values = ['avg1', 'avg2', 'avg4', 'avg8', 'avg16', 'avg400', 'sim']

# log文件是已经运行过的命令行,如果需要手动编写添加删减,直接用txt打开,将已经运行过的命令行放进去即可
log_file = 'executed_commands.log'


# 读取已执行命令记录文件
def read_executed_commands(log_file):
    try:
        with open(log_file, 'r') as file:
            return set(file.read().splitlines())
    except FileNotFoundError:
        return set()


# 记录已执行命令到文件
def log_executed_command(command_str, log_file):
    with open(log_file, 'a') as file:
        file.write(command_str + '\n')


executed_commands = read_executed_commands(log_file)

# 在这里自行控制需要循环的数据集名称
for model in model_values:
    for avg in avg_values:
        # 一些需要特殊对待的数据集,比如没有gt,数据集size比较大等等
        size = 'B' if avg != 'ITS' else 'L'
        # 命令行正文
        command = [
            'python', 'dehazing\\test.py',
            '--size', size,
            '--input_dir',
            f'E:\\1dataset\\npy_trans_to_bmp\\{avg}',
            '--target_dir',
            f'E:\\1dataset\\npy_trans_to_bmp\\sim',
            '--result_dir',
            f'E:\\1deep_result\\TBdehaze\\result_{model}\\{avg}',
            '--weights',
            f'E:\\project\MB-TaylorFormer\\ICCV-2023-MB-TaylorFormer-main\\ICCV-2023-MB-TaylorFormer-main\\Dehazing\\pretrained_models\\{model}-MB-TaylorFormer-{size}.pth',
        ]
        command_str = ' '.join(command)

        # 检查命令是否已执行过
        if command_str in executed_commands:
            print(f"Skipping already executed command: {command_str}")
            continue

        # 打印并执行命令
        print(f"Executing command: {command_str}")
        try:
            subprocess.run(command, check=True)
            # 记录已执行命令
            log_executed_command(command_str, log_file)
            executed_commands.add(command_str)
        except subprocess.CalledProcessError as e:
            print(f"Command failed with error: {e}")

print("All commands executed.")

再重复单独解释一下

这个是需要测试的多个数据集
log文件是已经运行过的命令行,如果需要手动编写添加删减,直接用txt打开,将已经运行过的命令行放进去即可
model_values = ['densehaze', 'ohaze', 'OTS','ITS']
avg_values = ['avg1', 'avg2', 'avg4', 'avg8', 'avg16', 'avg400', 'sim']
log_file = 'executed_commands.log'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值