Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

parser = argparse.ArgumentParser() parser.addargument('--restoreckpt', help="restore…

  1. User

    parser = argparse.ArgumentParser() parser.add_argument('--restore_ckpt', help="restore checkpoint", default=r"D:\jx\ai\stereo_match_dl\IGEV-main\IGEV-main\IGEV-Stereo\ckpt\110000_igev-stereo.pth") parser.add_argument('-l', '--left_imgs', help="path to all first (left) frames", default=r"D:\jx\ai\stereo_match_dl\test_image\bak\SV1-02_20190416_L1B0000976744_8041900187280009_01-PAN2_epi.tif") parser.add_argument('-r', '--right_imgs', help="path to all second (right) frames", default=r"D:\jx\ai\stereo_match_dl\test_image\bak\SV1-02_20190416_L1B0000976744_8041900187280009_01-PAN1_epi.tif") # parser.add_argument('-l', '--left_imgs', help="path to all first (left) frames", default=r"D:\jx\ai\stereo_match_dl\test_image\bak\SV1_PAN1_down_half.tif") # parser.add_argument('-r', '--right_imgs', help="path to all second (right) frames", default=r"D:\jx\ai\stereo_match_dl\test_image\bak\SV1_PAN2_down_half.tif")

    # parser.add_argument('-l', '--left_imgs', help="path to all first (left) frames", default=r"D:\jx\ai\stereo_match_dl\test_image\right\3_6_right.tif")
    # parser.add_argument('-r', '--right_imgs', help="path to all second (right) frames", default=r"D:\jx\ai\stereo_match_dl\test_image\left\3_6_left.tif")
    #parser.add_argument('-l', '--left_imgs', help="path to all first (left) frames", default=r"D:\jx\ai\stereo_match_dl\train_image\experimental data\with ground truth\test\left\HY_left_0.tiff")
    #parser.add_argument('-r', '--right_imgs', help="path to all second (right) frames", default=r"D:\jx\ai\stereo_match_dl\train_image\experimental data\with ground truth\test\right\HY_right_0.tiff")
    
    parser.add_argument('--output_directory', help="directory to save output", default=r"D:\jx\ai\stereo_match_dl\IGEV-main\IGEV-main\IGEV-Stereo\core\demo-output\test_patch")
    parser.add_argument('--mixed_precision', action='store_true', help='use mixed precision')
    # Architecture choices
    parser.add_argument('--hidden_dims', nargs='+', type=int, default=[128]*3, help="hidden state and context dimensions")
    parser.add_argument('--corr_implementation', choices=["reg", "alt", "reg_cuda", "alt_cuda"], default="reg", help="correlation volume implementation")
    parser.add_argument('--shared_backbone', action='store_true', help="use a single backbone for the context and feature encoders")
    parser.add_argument('--corr_levels', type=int, default=2, help="number of levels in the correlation pyramid")
    parser.add_argument('--corr_radius', type=int, default=4, help="width of the correlation pyramid")
    parser.add_argument('--n_downsample', type=int, default=2, help="resolution of the disparity field (1/2^K)")
    parser.add_argument('--slow_fast_gru', action='store_true', help="iterate the low-res GRUs more frequently")
    parser.add_argument('--n_gru_layers', type=int, default=3, help="number of hidden GRU levels")
    parser.add_argument('--max_disp', type=int, default=192, help="max disp of geometry encoding volume")
    args = parser.parse_args()
    Path(args.output_directory).mkdir(exist_ok=True, parents=True)
    # 初始化模型
    model = torch.nn.DataParallel(IGEVStereo(args), device_ids=[0])
    

    这个能改成别的的写法吗?我不喜欢这种写法

  2. Assistant

    当然,可以将您的代码改写为另一种方式,使其更符合您的偏好。下面是一个重写的示例,使用了配置字典来代替 argparse,并对模型的初始化进行了简化处理。

    import torch
    from pathlib import Path
    from IGEV import IGEVStereo  # 请确保正确导入您的 IGEVStereo 模型
    
    # 定义配置字典
    config = {
        'restore_ckpt': r"D:\jx\ai\stereo_match_dl\IGEV-main\IGEV-main\IGEV-Stereo\ckpt\110000_igev-stereo.pth",
        'left_imgs': r"D:\jx\ai\stereo_match_dl\test_image\bak\SV1-02_20190416_L1B0000976744_8041900187280009_01-PAN2_epi.tif",
        'right_imgs': r"D:\jx\ai\stereo_match_dl\test_image\bak\SV1-02_20190416_L1B0000976744_8041900187280009_01-PAN1_epi.tif",
        'output_directory': r"D:\jx\ai\stereo_match_dl\IGEV-main\IGEV-main\IGEV-Stereo\core\demo-output\test_patch",
        'mixed_precision': False,
        'hidden_dims': [128, 128, 128],
        'corr_implementation': "reg",
        'shared_backbone': False,
        'corr_levels': 2,
        'corr_radius': 4,
        'n_downsample': 2,
        'slow_fast_gru': False,
        'n_gru_layers': 3,
        'max_disp': 192,
        'device_ids': [0],  # 如果您有多个GPU,可以在此修改
    }
    
    # 创建输出目录
    Path(config['output_directory']).mkdir(exist_ok=True, parents=True)
    
    # 初始化模型参数
    model_args = {
        'mixed_precision': config['mixed_precision'],
        'hidden_dims': config['hidden_dims'],
        'corr_implementation': config['corr_implementation'],
        'shared_backbone': config['shared_backbone'],
        'corr_levels': config['corr_levels'],
        'corr_radius': config['corr_radius'],
        'n_downsample': config['n_downsample'],
        'slow_fast_gru': config['slow_fast_gru'],
        'n_gru_layers': config['n_gru_layers'],
        'max_disp': config['max_disp'],
    }
    
    # 检查是否有可用的GPU
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    
    # 实例化模型并移动到设备
    model = IGEVStereo(**model_args).to(device)
    
    # 如果有多个GPU,使用DataParallel
    if torch.cuda.device_count() > 1:
        model = torch.nn.DataParallel(model, device_ids=config['device_ids'])
    
    # 加载检查点(如果需要)
    if config['restore_ckpt']:
        checkpoint = torch.load(config['restore_ckpt'], map_location=device)
        model.load_state_dict(checkpoint['state_dict'])
    
    # 后续代码...
    

    在这个重写的版本中,我们做了以下改动:

    1. 使用配置字典替代 argparse:直接将参数放入一个字典 config 中,更加直观,也更易于修改和管理。

    2. 简化模型初始化:

      • 使用解包运算符 ** 将 model_args 传递给 IGEVStereo,使代码更清晰。
      • 检查是否有可用的 GPU,将模型移动到相应的设备上。
      • 仅在检测到多于一个 GPU 时,才使用 torch.nn.DataParallel。如果只有一个 GPU,直接使用模型即可,不需要包裹在 DataParallel 中。
    3. 可选地加载检查点:如果您需要加载预训练模型的权重,可以根据 config['restore_ckpt'] 的值来决定是否加载。

    这样的改写有以下优点:

    • 代码更清晰易读:配置参数集中管理,修改方便,避免了散落的 parser.add_argument 调用。
    • 更好的扩展性:如果以后需要增加或修改配置,只需更改 config 字典即可。
    • 避免不必要的并行包装:仅在需要时才使用 DataParallel,简化了模型的使用。

    希望这能满足您的需求。如果您有其他偏好的代码风格,或者需要进一步的修改,请告诉我!