Exemple #1
0
def factory_from_args(args):

    # Data
    if args.glob:
        args.images += glob.glob(args.glob)
    if not args.images:
        raise Exception("no image files given")

    if args.path_gt is None:
        args.show_all = True

    # Models
    dic_models = download_checkpoints(args)
    args.checkpoint = dic_models['keypoints']

    logger.configure(args, LOG)  # logger first

    # Devices
    args.device = torch.device('cpu')
    args.pin_memory = False
    if torch.cuda.is_available():
        args.device = torch.device('cuda')
        args.pin_memory = True
    LOG.debug('neural network device: %s', args.device)

    # Add visualization defaults
    args.figure_width = 10
    args.dpi_factor = 1.0

    if args.mode == 'stereo':
        args.batch_size = 2
        args.images = sorted(args.images)
    else:
        args.batch_size = 1

    # Patch for stereo images with batch_size = 2
    if args.batch_size == 2 and not args.long_edge:
        args.long_edge = 1238
        LOG.info("Long-edge set to %i", args.long_edge)

    # Make default pifpaf argument
    args.force_complete_pose = True
    LOG.info("Force complete pose is active")

    # Configure
    decoder.configure(args)
    network.Factory.configure(args)
    show.configure(args)
    visualizer.configure(args)

    return args, dic_models
Exemple #2
0
def factory_from_args(args):

    # Data
    if args.glob:
        args.images += glob.glob(args.glob)
    if not args.images:
        raise Exception("no image files given")

    # Model
    if not args.checkpoint:
        if os.path.exists(OPENPIFPAF_PATH):
            args.checkpoint = OPENPIFPAF_PATH
        else:
            LOG.info("Checkpoint for OpenPifPaf not specified and default model not found in 'data/models'. "
                  "Using a ShuffleNet backbone")
            args.checkpoint = 'shufflenetv2k30'

    logger.configure(args, LOG)  # logger first

    # Devices
    args.device = torch.device('cpu')
    args.pin_memory = False
    if torch.cuda.is_available():
        args.device = torch.device('cuda')
        args.pin_memory = True
    LOG.debug('neural network device: %s', args.device)

    # Add visualization defaults
    args.figure_width = 10
    args.dpi_factor = 1.0

    if args.net == 'monstereo':
        args.batch_size = 2
    else:
        args.batch_size = 1

    # Make default pifpaf argument
    args.force_complete_pose = True
    LOG.info("Force complete pose is active")

    # Configure
    decoder.configure(args)
    network.Factory.configure(args)
    show.configure(args)
    visualizer.configure(args)

    return args
    def __init__(self,
                 checkpoint=None,
                 basenet=None,
                 headnets=None,
                 pretrained=True,
                 two_scale=False,
                 multi_scale=False,
                 multi_scale_hflip=True,
                 cross_talk=0.0,
                 download_progress=True,
                 head_dropout=0.0,
                 head_quad=1,
                 seed_threshold=0.5,
                 instance_threshold=0.1,
                 keypoint_threshold=None,
                 decoder_workers=None,
                 dense_connections=False,
                 dense_coupling=0.01,
                 caf_seeds=False,
                 force_complete_pose=True,
                 profile_decoder=None,
                 cif_th=0.1,
                 caf_th=0.1,
                 connection_method="blend",
                 greedy=False,
                 video_output="",
                 xls_output="",
                 quiet=False,
                 debug=False,
                 skip_frames=1,
                 max_frames=False,
                 start_frame=0,
                 confidence_threshold=0.1):

        self.checkpoint = checkpoint
        self.basenet = basenet
        self.headnets = headnets
        self.pretrained = pretrained
        self.two_scale = two_scale
        self.multi_scale = multi_scale
        self.multi_scale_hflip = multi_scale_hflip
        self.cross_talk = cross_talk
        self.download_progress = download_progress
        self.head_dropout = head_dropout
        self.head_quad = head_quad
        self.seed_threshold = seed_threshold
        self.instance_threshold = instance_threshold
        self.keypoint_threshold = keypoint_threshold
        self.decoder_workers = decoder_workers
        self.dense_connections = dense_connections
        self.dense_coupling = dense_coupling
        self.caf_seeds = caf_seeds
        self.force_complete_pose = force_complete_pose
        self.profile_decoder = profile_decoder
        self.cif_th = cif_th
        self.caf_th = caf_th
        self.connection_method = connection_method
        self.greedy = greedy
        self.quiet = quiet
        self.debug = debug
        self.skip_frames = skip_frames
        self.max_frames = max_frames
        self.start_frame = start_frame
        self.confidence_threshold = confidence_threshold

        class InternalConfig:
            def __init__(self):
                self.debug_cifhr = None
                self.debug_cif_c = None
                self.debug_cif_v = None
                self.debug_cifdet_c = None
                self.debug_cifdet_v = None
                self.debug_caf_c = None
                self.debug_caf_v = None
                self.debug_indices = []
                self.debug_images = False
                self.show_box = False,
                self.show_joint_scales = False,
                self.show_decoding_order = False,
                self.show_frontier_order = False,
                self.show_only_decoded_connections = False,
                self.show_joint_confidences = False,

        # configure logging
        self.log_level = logging.INFO
        if quiet:
            self.log_level = logging.WARNING
        if debug:
            self.log_level = logging.DEBUG
        logging.basicConfig()
        logging.getLogger('openpifpaf').setLevel(self.log_level)
        LOG.setLevel(self.log_level)

        internal_config = InternalConfig()
        network.configure(self)
        pifpaf.show.configure(internal_config)
        visualizer.configure(internal_config)

        # add args.device
        self.device = torch.device('cpu')
        if torch.cuda.is_available():
            self.device = torch.device('cuda')
        LOG.debug('neural network device: %s', self.device)

        self.model, _ = network.factory_from_args(self)
        self.model = self.model.to(self.device)
        self.processor = decoder.factory_from_args(self, self.model)
Exemple #4
0
def cli():
    parser = argparse.ArgumentParser(
        prog='python3 -m openpifpaf.train',
        usage='%(prog)s [options]',
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        '--version',
        action='version',
        version='OpenPifPaf {version}'.format(version=__version__))
    parser.add_argument('-o', '--output', default=None, help='output file')
    parser.add_argument('--disable-cuda',
                        action='store_true',
                        help='disable CUDA')
    parser.add_argument('--ddp',
                        default=False,
                        action='store_true',
                        help='[experimental] DistributedDataParallel')
    parser.add_argument('--local_rank',
                        default=None,
                        type=int,
                        help='[experimental] for torch.distributed.launch')
    parser.add_argument('--no-sync-batchnorm',
                        dest='sync_batchnorm',
                        default=True,
                        action='store_false',
                        help='[experimental] in ddp, to not use syncbatchnorm')

    logger.cli(parser)
    network.Factory.cli(parser)
    network.losses.Factory.cli(parser)
    network.Trainer.cli(parser)
    encoder.cli(parser)
    optimize.cli(parser)
    datasets.cli(parser)
    show.cli(parser)
    visualizer.cli(parser)

    args = parser.parse_args()

    logger.configure(args, LOG)
    if args.log_stats:
        logging.getLogger('openpifpaf.stats').setLevel(logging.DEBUG)

    # DDP with SLURM
    slurm_process_id = os.environ.get('SLURM_PROCID')
    if args.ddp and slurm_process_id is not None:
        if torch.cuda.device_count() > 1:
            LOG.warning(
                'Expected one GPU per SLURM task but found %d. '
                'Try with "srun --gpu-bind=closest ...". Still trying.',
                torch.cuda.device_count())

        # if there is more than one GPU available, assume that other SLURM tasks
        # have access to the same GPUs and assign GPUs uniquely by slurm_process_id
        args.local_rank = (int(slurm_process_id) % torch.cuda.device_count()
                           if torch.cuda.device_count() > 0 else 0)

        os.environ['RANK'] = slurm_process_id
        if not os.environ.get('WORLD_SIZE') and os.environ.get('SLURM_NTASKS'):
            os.environ['WORLD_SIZE'] = os.environ.get('SLURM_NTASKS')

        LOG.info('found SLURM process id: %s', slurm_process_id)
        LOG.info(
            'distributed env: master=%s port=%s rank=%s world=%s, '
            'local rank (GPU)=%d', os.environ.get('MASTER_ADDR'),
            os.environ.get('MASTER_PORT'), os.environ.get('RANK'),
            os.environ.get('WORLD_SIZE'), args.local_rank)

    # add args.device
    args.device = torch.device('cpu')
    args.pin_memory = False
    if not args.disable_cuda and torch.cuda.is_available():
        args.device = torch.device('cuda')
        args.pin_memory = True
    LOG.info('neural network device: %s (CUDA available: %s, count: %d)',
             args.device, torch.cuda.is_available(), torch.cuda.device_count())

    # output
    if args.output is None:
        args.output = default_output_file(args)
        os.makedirs('outputs', exist_ok=True)

    network.Factory.configure(args)
    network.losses.Factory.configure(args)
    network.Trainer.configure(args)
    encoder.configure(args)
    datasets.configure(args)
    show.configure(args)
    visualizer.configure(args)

    return args
Exemple #5
0
def cli():
    parser = argparse.ArgumentParser(
        prog='python3 -m openpifpaf.predict',
        usage='%(prog)s [options] images',
        description=__doc__,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument('--version', action='version',
                        version='OpenPifPaf {version}'.format(version=__version__))

    decoder.cli(parser)
    logger.cli(parser)
    network.Factory.cli(parser)
    show.cli(parser)
    visualizer.cli(parser)

    parser.add_argument('images', nargs='*',
                        help='input images')
    parser.add_argument('--glob',
                        help='glob expression for input images (for many images)')
    parser.add_argument('-o', '--image-output', default=None, nargs='?', const=True,
                        help='Whether to output an image, '
                             'with the option to specify the output path or directory')
    parser.add_argument('--json-output', default=None, nargs='?', const=True,
                        help='Whether to output a json file, '
                             'with the option to specify the output path or directory')
    parser.add_argument('--batch-size', default=1, type=int,
                        help='processing batch size')
    parser.add_argument('--long-edge', default=None, type=int,
                        help='rescale the long side of the image (aspect ratio maintained)')
    parser.add_argument('--loader-workers', default=None, type=int,
                        help='number of workers for data loading')
    parser.add_argument('--precise-rescaling', dest='fast_rescaling',
                        default=True, action='store_false',
                        help='use more exact image rescaling (requires scipy)')
    parser.add_argument('--disable-cuda', action='store_true',
                        help='disable CUDA')
    args = parser.parse_args()

    logger.configure(args, LOG)  # logger first

    # add args.device
    args.device = torch.device('cpu')
    args.pin_memory = False
    if not args.disable_cuda and torch.cuda.is_available():
        args.device = torch.device('cuda')
        args.pin_memory = True
    LOG.info('neural network device: %s (CUDA available: %s, count: %d)',
             args.device, torch.cuda.is_available(), torch.cuda.device_count())

    decoder.configure(args)
    network.Factory.configure(args)
    show.configure(args)
    visualizer.configure(args)

    if args.loader_workers is None:
        args.loader_workers = args.batch_size

    # glob
    if args.glob:
        args.images += glob.glob(args.glob)
    if not args.images:
        raise Exception("no image files given")

    return args