xiaomoguhzz's picture
Add files using upload-large-folder tool
17f1221 verified
#!/usr/bin/env python
"""
单次腐蚀+评估调试脚本:只跑一次,不做重试,方便定位报错。
"""
import argparse
import os
import sys
import subprocess
from pathlib import Path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
from robustness_eval.prepare_corrupted_images import ( # noqa: E402
corrupt_and_save_images,
cleanup_tmp_directory,
)
from robustness_eval.run_robustness_eval_multi_gpu import ( # noqa: E402
get_dataset_info_from_config,
get_eval_script_path,
)
def parse_args():
parser = argparse.ArgumentParser(
description='单次腐蚀+评估调试(无重试,方便查看报错)')
parser.add_argument('--config', type=str, required=True, help='配置文件路径')
parser.add_argument('--corruption', type=str, default='defocus_blur',
help='corruption 类型,默认 defocus_blur')
parser.add_argument('--severity', type=int, default=1,
help='严重程度,默认 1')
parser.add_argument('--work-dir', type=str,
default='robustness_eval/results/debug_single',
help='输出目录(会自动按 corruption/severity 归档)')
parser.add_argument('--gpu', type=int, default=0, help='使用的 GPU ID')
parser.add_argument('--source-data-root', type=str, default=None,
help='可选,显式指定数据根目录')
parser.add_argument('--source-img-prefix', type=str, default=None,
help='可选,显式指定图片前缀,如 val2017 或 leftImg8bit/val')
return parser.parse_args()
def main():
args = parse_args()
config_path = Path(args.config)
if not config_path.is_absolute():
config_path = project_root / config_path
if not config_path.exists():
print(f'配置文件不存在: {config_path}')
sys.exit(1)
work_dir_base = Path(args.work_dir)
if not work_dir_base.is_absolute():
work_dir_base = project_root / work_dir_base
# 读取数据集信息(若未手动指定)
data_root = args.source_data_root
img_prefix = args.source_img_prefix
if data_root is None or img_prefix is None:
cfg_root, cfg_prefix = get_dataset_info_from_config(config_path)
if data_root is None:
data_root = cfg_root
if img_prefix is None:
img_prefix = cfg_prefix
# 组织工作目录与临时目录
if args.corruption.lower() == 'clean':
work_dir = work_dir_base / 'clean'
tmp_dir = work_dir_base / 'tmp_corrupted_images' / 'clean'
else:
work_dir = work_dir_base / args.corruption / f'severity_{args.severity}'
tmp_dir = (work_dir_base / 'tmp_corrupted_images' /
args.corruption / f'severity_{args.severity}')
work_dir.mkdir(parents=True, exist_ok=True)
print('======= 调试参数 =======')
print(f'config: {config_path}')
print(f'corruption: {args.corruption}, severity: {args.severity}')
print(f'gpu: {args.gpu}')
print(f'data_root: {data_root}')
print(f'img_prefix: {img_prefix}')
print(f'work_dir: {work_dir}')
print(f'tmp_dir: {tmp_dir}')
print('========================')
try:
# 预处理(无重试)
target_data_root = corrupt_and_save_images(
source_data_root=data_root,
source_img_prefix=img_prefix,
target_tmp_root=str(tmp_dir),
corruption_name=args.corruption,
severity=args.severity,
)
# 组装环境变量
env = os.environ.copy()
env['CORRUPTED_DATA_ROOT'] = target_data_root
env['CORRUPTION_NAME'] = args.corruption
env['SEVERITY'] = str(args.severity if args.corruption != 'clean' else 1)
env['CUDA_VISIBLE_DEVICES'] = str(args.gpu)
cmd = [
sys.executable,
str(get_eval_script_path()),
'--config', str(config_path),
'--work-dir', str(work_dir),
]
print('\n======= 开始评估(单次,无重试) =======')
print('命令:', ' '.join(cmd))
proc = subprocess.run(
cmd,
env=env,
cwd=project_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
print('------- eval stdout -------')
print(proc.stdout)
print('------- eval stderr -------')
print(proc.stderr)
print('---------------------------')
if proc.returncode != 0:
print(f'评估失败,返回码: {proc.returncode}')
sys.exit(proc.returncode)
print('✓ 评估完成(未发生重试)')
print(f'结果目录: {work_dir}')
except Exception as e:
print(f'✗ 调试脚本异常: {e}')
sys.exit(1)
finally:
# 无论成功失败,都清理临时目录,避免脏数据影响后续
if tmp_dir.exists():
cleanup_tmp_directory(str(tmp_dir))
if __name__ == '__main__':
main()