def detection(args: Arguments, proc_id: int, cache_dir: Union[(Path, str)]):
    (classes, labels_IO, labels_attribute, W_attribute) = load_labels(cache_dir)
    model = load_model(cache_dir)
    hook = Hooker(model)
    params = list(model.parameters())
    if args.visualize:
        weight_softmax = params[(- 2)].data.numpy()
        weight_softmax[(weight_softmax < 0)] = 0
    else:
        weight_softmax = None
    photos = load_photos(args.images, cache_dir)
    print('The dataset contains a total of', len(photos))
    photos = photos[proc_id::args.num_splits]
    print('The split', proc_id, 'over', args.num_splits, 'contains', len(photos), 'photos')
    dataset = ImageDataset(photos)
    dataloader = DataLoader(dataset, batch_size=args.batch_size, num_workers=args.num_workers, collate_fn=collate_fn)
    model = model.cuda()
    filename = (args.outfile.parent / f'{args.outfile.stem}.{proc_id}.tsv')
    print(f'Start split {proc_id} on {len(dataset)} photos')
    with open(filename, 'wt') as tsvfile:
        writer = csv.DictWriter(tsvfile, delimiter='\t', fieldnames=TSV_FIELDNAMES)
        for batch in tqdm(dataloader):
            if (batch is None):
                continue
            detections = run_model(batch, model, hook, classes, labels_IO, labels_attribute, W_attribute, num_cat=args.num_cat, num_attr=args.num_attr, weight_softmax=weight_softmax)
            for d in detections:
                writer.writerow(d)
def detection(args: Arguments, proc_id: int, cache_dir: Union[(Path, str)]):
    (classes, labels_IO, labels_attribute, W_attribute) = load_labels(cache_dir)
    model = load_model(cache_dir)
    hook = Hooker(model)
    params = list(model.parameters())
    if args.visualize:
        weight_softmax = params[(- 2)].data.numpy()
        weight_softmax[(weight_softmax < 0)] = 0
    else:
        weight_softmax = None
    photos = load_photos(args.images, cache_dir)
    print('The dataset contains a total of', len(photos))
    photos = photos[proc_id::args.num_splits]
    print('The split', proc_id, 'over', args.num_splits, 'contains', len(photos), 'photos')
    dataset = ImageDataset(photos)
    dataloader = DataLoader(dataset, batch_size=args.batch_size, num_workers=args.num_workers, collate_fn=collate_fn)
    model = model.cuda()
    filename = (args.outfile.parent / f'{args.outfile.stem}.{proc_id}.tsv')
    print(f'Start split {proc_id} on {len(dataset)} photos')
    with open(filename, 'wt') as tsvfile:
        writer = csv.DictWriter(tsvfile, delimiter='\t', fieldnames=TSV_FIELDNAMES)
        for batch in tqdm(dataloader):
            if (batch is None):
                continue
            detections = run_model(batch, model, hook, classes, labels_IO, labels_attribute, W_attribute, num_cat=args.num_cat, num_attr=args.num_attr, weight_softmax=weight_softmax)
            for d in detections:
                writer.writerow(d)