コード例 #1
0
def test_get_all_models():
    cfg = get_cfg_defaults()
    names = get_model_list()
    for name in names:
        cfg.CONFIG.MODEL.NAME = name
        net = get_model(cfg)
        assert isinstance(net, nn.Module), '{}'.format(name)
コード例 #2
0
ファイル: pose_model.py プロジェクト: yinweisu/gluon-cv
 def load_model(self, model_prefix, device_ctx):
     if not self.torch:
         with open('{}.json'.format(model_prefix), 'r') as f:
             model_json = f.read()
         model_lib = tvm.runtime.load_module('{}.so'.format(model_prefix))
         with open('{}.params'.format(model_prefix), "rb") as f:
             model_params = bytearray(f.read())
         module = graph_runtime.create(model_json, model_lib, device_ctx)
         module.load_params(model_params)
         return module
     import torch
     from gluoncv.torch import model_zoo
     from gluoncv.torch.engine.config import get_cfg_defaults
     torch.set_grad_enabled(False)
     device = torch.device('cuda')
     cfg = get_cfg_defaults(name='directpose')
     # cfg.merge_from_file('./configurations/ms_dla_34_4x_syncbn.yaml')
     # net = model_zoo.dla34_fpn_directpose(cfg).to(device).eval()
     # model = torch.load('model_final.pth')['model']
     cfg.merge_from_file('./configurations/ms_aa_resnet50_4x_syncbn.yaml')
     net = model_zoo.directpose_resnet_lpf_fpn(cfg).to(device).eval()
     model = torch.load('model_final_resnet.pth')['model']
     # _ = net(torch.zeros((1,3, self.image_height, self.image_width)).cuda())
     net.load_state_dict(model, strict=False)
     return net
コード例 #3
0
def _test_model_list(model_list, use_cuda, x, pretrained, num_classes, **kwargs):
    cfg = get_cfg_defaults()
    for model in model_list:
        cfg.CONFIG.MODEL.NAME = model
        cfg.CONFIG.MODEL.PRETRAINED = pretrained
        cfg.CONFIG.DATA.NUM_CLASSES = num_classes
        net = get_model(cfg)

        if use_cuda:
            net.cuda()
            x = x.cuda()

        net(x)
コード例 #4
0
        # step lr scheduler
        scheduler.step_rop(det_best_field_current, True)
        logger.info(f"ROP: model improved: {is_best}, "
                    f"value {det_best_field_current:.3f},"
                    f"new LR: {optimizer.param_groups[0]['lr']:5.3e}")

        if epoch % cfg.CONFIG.LOG.SAVE_FREQ == 0:
            if cfg.DDP_CONFIG.GPU_WORLD_RANK == 0 or cfg.DDP_CONFIG.DISTRIBUTED == False:
                model.save_model(optimizer, epoch, cfg)

        # check if model did not improve for too long
        term_after = 15
        if epoch - best_epoch > term_after:
            logger.info(f"NO improvements for {term_after} epochs (current "
                        f"{epoch} best {best_epoch}) STOP training.")
            break

    if writer is not None:
        writer.close()
    if logger is not None:
        close_logger(logger)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Train Coot model.')
    parser.add_argument('--config-file', type=str, help='path to config file.')
    args = parser.parse_args()
    cfg = get_cfg_defaults()
    cfg.merge_from_file(args.config_file)
    main_worker(cfg)
コード例 #5
0
ファイル: train_pytorch.py プロジェクト: yinweisu/gluon-cv
        # step lr scheduler
        scheduler.step_rop(det_best_field_current, True)
        logger.info(f"ROP: model improved: {is_best}, "
                    f"value {det_best_field_current:.3f},"
                    f"new LR: {optimizer.param_groups[0]['lr']:5.3e}")

        if epoch % cfg.CONFIG.LOG.SAVE_FREQ == 0:
            if cfg.DDP_CONFIG.GPU_WORLD_RANK == 0 or cfg.DDP_CONFIG.DISTRIBUTED == False:
                model.save_model(optimizer, epoch, cfg)

        # check if model did not improve for too long
        term_after = 15
        if epoch - best_epoch > term_after:
            logger.info(f"NO improvements for {term_after} epochs (current "
                        f"{epoch} best {best_epoch}) STOP training.")
            break

    if writer is not None:
        writer.close()
    if logger is not None:
        close_logger(logger)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Train Coot model.')
    parser.add_argument('--config-file', type=str, help='path to config file.')
    args = parser.parse_args()
    cfg = get_cfg_defaults(name='coot')
    cfg.merge_from_file(args.config_file)
    main_worker(cfg)
コード例 #6
0
    parser.add_argument("opts",
                        help="Modify config options using the command-line",
                        default=None,
                        nargs=argparse.REMAINDER)
    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()
    torch.set_grad_enabled(False)
    if args.use_gpu:
        device = torch.device('cuda')
    else:
        device = torch.device('cpu')
    cfg = get_cfg_defaults(name='directpose')
    if args.model_name:
        cfg.CONFIG.MODEL.PRETRAINED = True
        cfg.CONFIG.MODEL.NAME = args.model_name
        cfg.CONFIG.MODEL.TVM_MODE = True
        net = model_zoo.get_model(cfg)
    else:
        assert os.path.isfile(args.config_file)
        assert os.path.isfile(cfg.CONFIG.MODEL.PRETRAINED_PATH)
        cfg.merge_from_file(args.config_file)
        cfg.CONFIG.MODEL.TVM_MODE = True
        net = model_zoo.directpose_resnet_lpf_fpn(cfg).to(device).eval()
        load_model = torch.load(cfg.CONFIG.MODEL.PRETRAINED_PATH)
        net.load_state_dict(load_model, strict=False)
    images, orig_image = get_image(
        'soccer.png',
コード例 #7
0
                                         cfg,
                                         writer=writer)
        if cfg.CONFIG.TRAIN.USE_WARMUP:
            scheduler_warmup.step()
        else:
            scheduler.step()

        # evaluation
        if epoch % cfg.CONFIG.VAL.FREQ == 0 or epoch == cfg.CONFIG.TRAIN.EPOCH_NUM - 1:
            validation_classification(model, val_loader, epoch, criterion, cfg,
                                      writer)

        # save model
        if epoch % cfg.CONFIG.LOG.SAVE_FREQ == 0 or epoch == cfg.CONFIG.TRAIN.EPOCH_NUM - 1:
            if cfg.DDP_CONFIG.GPU_WORLD_RANK == 0 or cfg.DDP_CONFIG.DISTRIBUTED == False:
                save_model(model, optimizer, epoch, cfg)

    if writer is not None:
        writer.close()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Train video action recognition models.')
    parser.add_argument('--config-file', type=str, help='path to config file.')
    args = parser.parse_args()

    cfg = get_cfg_defaults(name='action_recognition')
    cfg.merge_from_file(args.config_file)
    spawn_workers(main_worker, cfg)
コード例 #8
0
def test_model_zoo_tvm_export_directpose():
    from gluoncv.torch.utils.tvm_utils.nms import nms
    from tvm.contrib.download import download_testdata

    def get_image(img_name='street_small.jpg',
                  img_url=None,
                  img_width=1280,
                  img_height=736):
        def get_single_image_input(img_url):
            img_name = img_url.split("/")[-1]
            img_path = download_testdata(img_url, img_name, module="data")
            orig_img = Image.open(img_path)
            orig_img = orig_img.resize((img_width, img_height), Image.LANCZOS)
            img = np.array(orig_img)[:, :, (0, 1, 2)].astype('uint8')
            return img, orig_img, img_path

        def get_transforms():
            tforms = T.Compose([
                T.ToTensor(),
                T.Normalize(mean=[0.485, 0.456, 0.406],
                            std=[0.229, 0.224, 0.225])
            ])
            return tforms

        if img_url is None:
            img_url = f"https://raw.githubusercontent.com/dmlc/web-data/master/gluoncv/detection/{img_name}"
        img, orig_img, img_path = get_single_image_input(img_url)

        tforms = get_transforms()
        input_data = tforms(img).unsqueeze(0)
        return input_data, orig_img

    images, orig_img = get_image()
    model_list = ['directpose_resnet50_lpf_fpn_coco']

    cfg = get_cfg_defaults(name='directpose')
    torch.set_grad_enabled(False)
    use_cuda = True
    if torch.cuda.device_count() == 0:
        use_cuda = False
    if use_cuda:
        device = torch.device('cuda')
        target = "cuda"
        ctx = tvm.gpu(0)
    else:
        device = torch.device('cpu')
        target = "llvm"
        ctx = tvm.cpu()
    for model in model_list:
        cfg.CONFIG.MODEL.PRETRAINED = True
        cfg.CONFIG.MODEL.NAME = model
        cfg.CONFIG.MODEL.TVM_MODE = True
        net = model_zoo.get_model(cfg).to(device).eval()

        # y = net(images.to(device))
        with torch.no_grad():
            scripted_model = torch.jit.trace(net.forward,
                                             images.to(device)).eval()

        input_name = "input0"
        shape_list = [(input_name, images.shape)]
        mod, params = relay.frontend.from_pytorch(scripted_model, shape_list,
                                                  {"torchvision::nms": nms})
        target_host = "llvm"
        with tvm.transform.PassContext(opt_level=3):
            lib = relay.build(mod,
                              target=target,
                              target_host=target_host,
                              params=params)