コード例 #1
0
ファイル: benchmarks.py プロジェクト: lilujunai/ZCls
def main(data_shape, config_file, mobile_name):
    np.random.seed(cfg.RNG_SEED)
    torch.manual_seed(cfg.RNG_SEED)
    torch.backends.cudnn.deterministic = False
    torch.backends.cudnn.benchmark = True

    cfg.merge_from_file(config_file)

    gpu_device = torch.device('cuda:0')
    cpu_device = torch.device('cpu')

    model = build_recognizer(cfg, cpu_device)
    model.eval()
    data = torch.randn(data_shape).to(device=cpu_device, non_blocking=True)

    GFlops, params_size = compute_num_flops(model, data)
    print(f'{mobile_name} ' + '*' * 10)
    print(f'device: {cpu_device}')
    print(f'GFlops: {GFlops:.3f}G')
    print(f'Params Size: {params_size:.3f}MB')

    model = build_recognizer(cfg, cpu_device)
    model.eval()
    print(f'compute cpu infer time')
    compute_model_time(data_shape, model, cpu_device)
    print(f'compute gpu infer time')
    compute_model_time(data_shape, model, gpu_device)

    del model
    torch.cuda.empty_cache()
コード例 #2
0
ファイル: rotnet.py プロジェクト: ZJCV/RotNet
def rotnet(pretrained=False, **kwargs):
    cfg.merge_from_file(config_file)
    model = build_recognizer(cfg, torch.device('cpu'))
    if pretrained:
        checkpoint = torch.load('weights/model.pth')
        model.load_state_dict(checkpoint)
    return model
コード例 #3
0
def test_mnasnet():
    cfg.merge_from_file(
        'configs/benchmarks/lightweight/mnasnet_a1_1_3_cifar100_224_e100.yaml')
    data = torch.randn(1, 3, 224, 224)

    for wm in [0.5, 0.75, 1.0, 1.3]:
        cfg.MODEL.COMPRESSION.WIDTH_MULTIPLIER = wm
        model = MNASNet(cfg)
        print(model)

        outputs = model(data)[KEY_OUTPUT]
        print(outputs.shape)

        assert outputs.shape == (1, 100)

    cfg.merge_from_file(
        'configs/benchmarks/lightweight/mnasnet_b1_1_3_torchvision_cifar100_224_e100_sgd.yaml'
    )
    for wm in [0.5, 0.75, 1.0, 1.3]:
        cfg.MODEL.COMPRESSION.WIDTH_MULTIPLIER = wm
        model = build_torchvision_mnasnet(cfg)
        print(model)

        outputs = model(data)[KEY_OUTPUT]
        print(outputs.shape)

        assert outputs.shape == (1, 100)
コード例 #4
0
def test_sknet():
    config_file = 'configs/benchmarks/resnet/sknet50_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (3, 3, 224, 224), (3, 100))
コード例 #5
0
ファイル: parser.py プロジェクト: vivektalwar13071999/ZCls
def load_config(args):
    if args.config_file:
        cfg.merge_from_file(args.config_file)
    if args.pretrained:
        cfg.MODEL.RECOGNIZER.PRELOADED = args.pretrained
    if args.output_dir:
        cfg.OUTPUT_DIR = args.output_dir

    if args.log_step != -1:
        cfg.TRAIN.LOG_STEP = args.log_step
    if args.save_step != -1:
        cfg.TRAIN.SAVE_STEP = args.save_step
    if args.eval_step != -1:
        cfg.TRAIN.EVAL_STEP = args.eval_step

    if args.resume:
        cfg.TRAIN.RESUME = True
    if not args.use_tensorboard:
        cfg.TRAIN.USE_TENSORBOARD = False

    if args.gpus != -1:
        cfg.NUM_GPUS = args.gpus
    if args.nodes != -1:
        cfg.NUM_NODES = args.nodes
    if args.nr != -1:
        cfg.RANK_ID = args.nr
    if args.init_method:
        cfg.INIT_METHOD = args.init_method

    cfg.merge_from_list(args.opts)
    cfg.freeze()
    return cfg
コード例 #6
0
def load_train_config(args):
    if args.config_file:
        cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    if args.log_step != -1:
        cfg.TRAIN.LOG_STEP = args.log_step
    if args.save_step != -1:
        cfg.TRAIN.SAVE_STEP = args.save_step
    if args.eval_step != -1:
        cfg.TRAIN.EVAL_STEP = args.eval_step

    if args.resume:
        cfg.TRAIN.RESUME = True
    if not args.use_tensorboard:
        cfg.TRAIN.USE_TENSORBOARD = False

    if args.gpus != -1:
        cfg.NUM_GPUS = args.gpus
    if args.nodes != -1:
        cfg.NUM_NODES = args.nodes
    if args.nr != -1:
        cfg.RANK_ID = args.nr

    cfg.freeze()

    if not os.path.exists(cfg.OUTPUT_DIR):
        os.makedirs(cfg.OUTPUT_DIR)

    return cfg
コード例 #7
0
ファイル: test_imagenet.py プロジェクト: lovecove/ZCls
def test_dataloader():
    config_file = 'configs/imagenet/rxtd50_32x4d_imagenet_224_e100_sgd_mslr_e100_g2.yaml'
    cfg.merge_from_file(config_file)

    dataloader = build_dataloader(cfg, is_train=True)
    print(dataloader)
    te = iter(dataloader)
    print(te)

    images, targets = te.__next__()
    print(images.shape)
    print(targets)
コード例 #8
0
def test_config():
    config_file = 'configs/benchmarks/rxd50_32x4d_avg_custom_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = build_resnet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))

    config_file = 'configs/benchmarks/rxd50_32x4d_fast_avg_custom_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = build_resnet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))
コード例 #9
0
def test_resnetd():
    config_file = 'configs/benchmarks/resnet/rd50_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))

    config_file = 'configs/benchmarks/resnet/rxtd50_32x4d_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))

    config_file = 'configs/benchmarks/resnet/rxtd50_32x4d_fast_avg_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))

    config_file = 'configs/benchmarks/resnet/rxtd50_32x4d_avg_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))
コード例 #10
0
def test_resnet():
    config_file = 'configs/benchmarks/resnet/r50_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))

    config_file = 'configs/benchmarks/resnet/rxt50_32x4d_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))

    config_file = 'configs/benchmarks/resnet/r50_torchvision_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = build_torchvision_resnet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))

    config_file = 'configs/benchmarks/resnet/rxt50_32x4d_torchvision_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = build_torchvision_resnet(cfg)
    print(model)
    test_data(model, (1, 3, 224, 224), (1, 100))
コード例 #11
0
def test_shufflenet_v2():
    cfg.merge_from_file(
        'configs/benchmarks/lightweight/sfv2_x2_0_cifar100_224_e100.yaml')
    print(cfg)
    model = ShuffleNetV2(cfg)
    print(model)

    test_data(model)

    cfg.merge_from_file(
        'configs/benchmarks/lightweight/sfv2_torchvision_cifar100_224_e100.yaml'
    )
    model = build_torchvision_sfv2(cfg)
    print(model)
    test_data(model)
コード例 #12
0
def test_cfg():
    cfg_file = 'configs/cifar/mbv3_large_se_hsigmoid_c100_224_e100_rmsprop_mslr_g1.yaml'
    cfg.merge_from_file(cfg_file)
    print(cfg.TRANSFORM)

    res = build_transform(cfg, is_train=True)
    data = Image.fromarray(torch.randn(234, 134, 3).numpy().astype(np.uint8))
    print(res(data).shape)

    cfg_file = 'configs/cifar/rxtd50_32x4d_c100_224_e100_sgd_mslr_g1.yaml'
    cfg.merge_from_file(cfg_file)
    print(cfg.TRANSFORM)

    res = build_transform(cfg, is_train=True)
    data = Image.fromarray(torch.randn(234, 134, 3).numpy().astype(np.uint8))
    print(res(data).shape)
コード例 #13
0
def test_prefetcher():
    config_file = 'configs/cifar/mbv3_large_se_hsigmoid_c10_224_e100_rmsprop_mslr_g1.yaml'
    cfg.merge_from_file(config_file)
    data_loader = build_dataloader(cfg, is_train=False)
    print(data_loader.__len__())

    print('enumerate')
    model = Prefetcher(data_loader)
    print(model)
    for i, item in enumerate(model):
        print(i)

    print('tqdm')
    model = Prefetcher(data_loader)
    print(model)
    for images, targets in tqdm(model):
        time.sleep(0.1)
    print('done')
コード例 #14
0
def test_mobilenet_v2():
    for s in [224, 192, 160, 128]:
        for wm in [1.0, 0.75, 0.5, 0.25]:
            cfg.merge_from_file('configs/benchmarks/lightweight/mbv2_cifar100_224_e100.yaml')
            cfg.MODEL.COMPRESSION.WIDTH_MULTIPLIER = wm
            round_nearest = cfg.MODEL.COMPRESSION.ROUND_NEAREST
            feature_dims = make_divisible(cfg.MODEL.HEAD.FEATURE_DIMS * wm, round_nearest)
            cfg.MODEL.HEAD.FEATURE_DIMS = feature_dims

            print(f's: {s}, wn: {wm}, feature_dims: {feature_dims}')
            model = MobileNetV2(cfg)
            # print(model)

            data = torch.randn(1, 3, s, s)
            outputs = model(data)[KEY_OUTPUT]
            print(outputs.shape)

            assert outputs.shape == (1, 100)
コード例 #15
0
def test_regvgg():
    cfg.merge_from_file('configs/benchmarks/repvgg/repvgg_b2g4_cifar100_224_e100_sgd_calr.yaml')
    model = RepVGG(cfg)
    model.eval()
    print(model)

    data = torch.randn(1, 3, 224, 224)
    insert_repvgg_block(model)
    model.eval()
    train_outputs = model(data)[KEY_OUTPUT]
    print(model)

    fuse_repvgg_block(model)
    model.eval()
    eval_outputs = model(data)[KEY_OUTPUT]
    print(model)

    print(torch.sqrt(torch.sum((train_outputs - eval_outputs) ** 2)))
    print(torch.allclose(train_outputs, eval_outputs, atol=1e-8))
    assert torch.allclose(train_outputs, eval_outputs, atol=1e-8)
コード例 #16
0
def load_test_config(args):
    if not os.path.isfile(args.config_file) or not os.path.isfile(
            args.pretrained):
        raise ValueError('需要输入配置文件和预训练模型路径')

    cfg.merge_from_file(args.config_file)
    cfg.MODEL.RECOGNIZER.PRELOADED = args.pretrained
    cfg.OUTPUT_DIR = args.output

    if args.gpus != -1:
        cfg.NUM_GPUS = args.gpus
    if args.nodes != -1:
        cfg.NODES = args.nodes
    if args.nr != -1:
        cfg.RANK = args.nr
    cfg.freeze()

    if not os.path.exists(cfg.OUTPUT_DIR):
        os.makedirs(cfg.OUTPUT_DIR)

    return cfg
コード例 #17
0
def main():
    args = parse_args()
    print(args)

    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    if args.ckpt is not None:
        cfg.MODEL.RECOGNIZER.PRELOADED = args.ckpt

    cfg.freeze()

    if args.config_file:
        print("Loaded configuration file {}".format(args.config_file))
        with open(args.config_file, "r") as cf:
            config_str = "\n" + cf.read()
            print(config_str)
    # print("Running with config:\n{}".format(cfg))

    run_demo(cfg=cfg,
             images_dir=args.images_dir,
             rotate_dir=args.rotate_dir,
             output_dir=args.output_dir)
コード例 #18
0
def test_config():
    data = torch.randn(1, 3, 224, 224)

    config_file = 'configs/benchmarks/mnasnet_a1_1_3_se_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mnasnet(cfg)
    print(model)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mnasnet_a1_1_3_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mnasnet(cfg)
    print(model)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mnasnet_b1_1_3_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mnasnet(cfg)
    print(model)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mnasnet_b1_1_3_torchvision_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mnasnet(cfg)
    print(model)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)
コード例 #19
0
def test_resnest():
    config_file = 'configs/benchmarks/resnet/rstd50_2s2x40d_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (3, 3, 224, 224), (3, 100))

    config_file = 'configs/benchmarks/resnet/rstd50_2s2x40d_fast_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (3, 3, 224, 224), (3, 100))

    config_file = 'configs/benchmarks/resnet/rstd50_2s2x40d_fast_official_cifar100_224_e100_rmsprop.yaml'
    cfg.merge_from_file(config_file)

    model = ResNet(cfg)
    print(model)
    test_data(model, (3, 3, 224, 224), (3, 100))
コード例 #20
0
def test_config():
    data = torch.randn(1, 3, 224, 224)

    config_file = 'configs/benchmarks/mbv3_large_se_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mobilenet_v3(cfg)
    print(model)
    outputs = model(data)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mbv3_large_se_hsigmoid_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mobilenet_v3(cfg)
    print(model)
    outputs = model(data)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mbv3_large_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mobilenet_v3(cfg)
    print(model)
    outputs = model(data)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mbv3_small_se_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mobilenet_v3(cfg)
    print(model)
    outputs = model(data)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mbv3_small_se_hsigmoid_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mobilenet_v3(cfg)
    print(model)
    outputs = model(data)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)

    config_file = 'configs/benchmarks/mbv3_small_custom_cifar100_224_e50.yaml'
    cfg.merge_from_file(config_file)
    model = build_mobilenet_v3(cfg)
    print(model)
    outputs = model(data)
    outputs = model(data)[KEY_OUTPUT]
    print(outputs.shape)

    assert outputs.shape == (1, 100)
コード例 #21
0
def test_config():
    config_file = "configs/benchmarks/mbv1_custom_cifar100_224.yaml"
    cfg.merge_from_file(config_file)

    model = build_mobilenet_v1(cfg)
    print(model)
コード例 #22
0
def test_config_file():
    data = torch.randn(3, 3, 224, 224)

    print('repvgg_b2g4_custom_cifar100_224_e100_sgd')
    config_file = "configs/benchmarks/repvgg/repvgg_b2g4_cifar100_224_e100_sgd_calr.yaml"
    cfg.merge_from_file(config_file)

    device = torch.device('cpu')
    model = build_recognizer(cfg, device)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)
    fuse_repvgg_block(model)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)

    print('repvgg_b2g4_acb_custom_cifar100_224_e100_sgd')
    config_file = "configs/benchmarks/repvgg/repvgg_b2g4_acb_cifar100_224_e100_sgd_calr.yaml"
    cfg.merge_from_file(config_file)

    device = torch.device('cpu')
    model = build_recognizer(cfg, device)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)
    # 注意:如果在RepVGG中嵌入了ACBlock,融合时应该先acb再regvgg
    fuse_acblock(model)
    print(model)
    fuse_repvgg_block(model)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)

    print('acb_repvgg_b2g4_custom_cifar100_224_e100_sgd')
    config_file = "configs/benchmarks/repvgg/acb_repvgg_b2g4_cifar100_224_e100_sgd_calr.yaml"
    cfg.merge_from_file(config_file)

    device = torch.device('cpu')
    model = build_recognizer(cfg, device)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)
    # 注意:如果先嵌入ACBlock再嵌入RepVGGBlock,那么融合时应该先repvgg_block再acblock
    fuse_repvgg_block(model)
    print(model)
    fuse_acblock(model)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)

    print('rxtd50_32x4d_acb_rvb_custom_cifar100_224_e100_sgd')
    config_file = "configs/benchmarks/repvgg/rxtd50_32x4d_acb_rvb_cifar100_224_e100_sgd_calr.yaml"
    cfg.merge_from_file(config_file)

    device = torch.device('cpu')
    model = build_recognizer(cfg, device)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)
    # 注意:如果先嵌入ACBlock再嵌入RepVGGBlock,那么融合时应该先repvgg_block再acblock
    fuse_repvgg_block(model)
    print(model)
    fuse_acblock(model)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)

    print('rxtd50_32x4d_rvb_acb_custom_cifar100_224_e100_sgd')
    config_file = "configs/benchmarks/repvgg/rxtd50_32x4d_rvb_acb_cifar100_224_e100_sgd_calr.yaml"
    cfg.merge_from_file(config_file)

    device = torch.device('cpu')
    model = build_recognizer(cfg, device)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)
    # 注意:如果先嵌入RepVGGBlock再嵌入ACBlock,那么逆序融合
    fuse_acblock(model)
    print(model)
    fuse_repvgg_block(model)
    print(model)
    outputs = model(data)[KEY_OUTPUT]

    assert outputs.shape == (3, 100)
コード例 #23
0
def test_regvgg():
    data = torch.randn(1, 3, 224, 224)
    for key in arch_settings.keys():
        print('*' * 10, key)
        cfg.merge_from_file(
            'configs/benchmarks/repvgg/repvgg_b2g4_cifar100_224_e100_sgd_calr.yaml'
        )
        model = RepVGG(cfg)
        # print(model)
        outputs = model(data)[KEY_OUTPUT]
        assert outputs.shape == (1, 100)

        print('insert_regvgg_block -> fuse_regvgg_block')
        insert_repvgg_block(model)
        # print(model)
        model.eval()
        outputs_insert = model(data)[KEY_OUTPUT]
        fuse_repvgg_block(model)
        # print(model)
        model.eval()
        outputs_fuse = model(data)[KEY_OUTPUT]

        # print(outputs_insert)
        # print(outputs_fuse)
        print(torch.sqrt(torch.sum((outputs_insert - outputs_fuse)**2)))
        print(torch.allclose(outputs_insert, outputs_fuse, atol=1e-8))
        assert torch.allclose(outputs_insert, outputs_fuse, atol=1e-8)

        print(
            'insert_regvgg_block -> insert_acblock -> fuse_acblock -> fuse_regvgg_block'
        )
        insert_repvgg_block(model)
        insert_acblock(model)
        # print(model)
        model.eval()
        outputs_insert = model(data)[KEY_OUTPUT]
        fuse_acblock(model)
        fuse_repvgg_block(model)
        # print(model)
        model.eval()
        outputs_fuse = model(data)[KEY_OUTPUT]

        print(torch.sqrt(torch.sum((outputs_insert - outputs_fuse)**2)))
        print(torch.allclose(outputs_insert, outputs_fuse, atol=1e-6))
        assert torch.allclose(outputs_insert, outputs_fuse, atol=1e-6)

        print(
            'insert_acblock -> insert_regvgg_block -> fuse_regvgg_block -> fuse_acblock'
        )
        insert_repvgg_block(model)
        insert_acblock(model)
        # print(model)
        model.eval()
        outputs_insert = model(data)[KEY_OUTPUT]
        fuse_acblock(model)
        fuse_repvgg_block(model)
        # print(model)
        model.eval()
        outputs_fuse = model(data)[KEY_OUTPUT]

        print(torch.sqrt(torch.sum((outputs_insert - outputs_fuse)**2)))
        print(torch.allclose(outputs_insert, outputs_fuse, atol=1e-6))
        assert torch.allclose(outputs_insert, outputs_fuse, atol=1e-6)