Exemplo n.º 1
0
def main(args):
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # parse gpus
    gpus = map(int, args.gpus.split(','))
    assert len(gpus) >= mpi_size, "Number of GPUs must be >= MPI size"
    cfg.GPU_ID = gpus[mpi_rank]

    # parse feature blob names
    blob_names = args.blob_names.split(',')

    print('Using config:')
    pprint.pprint(cfg)

    while not osp.exists(args.caffemodel) and args.wait:
        print('Waiting for {} to exist...'.format(args.caffemodel))
        time.sleep(10)

    # load imdb
    imdb = get_imdb(args.imdb_name)
    root_dir = imdb._root_dir
    images_dir = imdb._data_path
    output_dir = get_output_dir(imdb.name,
                                osp.splitext(osp.basename(args.caffemodel))[0])

    if args.eval_only:

        def _load(fname):
            fpath = osp.join(output_dir, fname)
            assert osp.isfile(fpath), "Must have extracted detections and " \
                                      "features first before evaluation"
            return unpickle(fpath)

        if mpi_rank == 0:
            gboxes = _load('gallery_detections.pkl')
            gfeatures = _load('gallery_features.pkl')
            pfeatures = _load('probe_features.pkl')
    else:
        # setup caffe
        caffe.mpi_init()
        caffe.set_mode_gpu()
        caffe.set_device(cfg.GPU_ID)

        # 1. Detect and extract features from all the gallery images in the imdb
        start, end = mpi_dispatch(len(imdb.image_index), mpi_size, mpi_rank)
        if args.use_gt:
            net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
            gboxes, gfeatures = usegt_and_exfeat(net,
                                                 imdb,
                                                 start=start,
                                                 end=end,
                                                 blob_names=blob_names)
        else:
            net = caffe.Net(args.gallery_def, args.caffemodel, caffe.TEST)
            gboxes, gfeatures = detect_and_exfeat(net,
                                                  imdb,
                                                  start=start,
                                                  end=end,
                                                  blob_names=blob_names)
        gboxes = mpi_collect(mpi_comm, mpi_rank, gboxes)
        gfeatures = mpi_collect(mpi_comm, mpi_rank, gfeatures)
        del net  # to release the cudnn conv static workspace

        # 2. Only extract features from given probe rois
        start, end = mpi_dispatch(len(imdb.probes), mpi_size, mpi_rank)
        net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
        pfeatures = exfeat(net,
                           imdb.probes,
                           start=start,
                           end=end,
                           blob_names=blob_names)
        pfeatures = mpi_collect(mpi_comm, mpi_rank, pfeatures)
        del net

        # Save
        if mpi_rank == 0:
            pickle(gboxes, osp.join(output_dir, 'gallery_detections.pkl'))
            pickle(gfeatures, osp.join(output_dir, 'gallery_features.pkl'))
            pickle(pfeatures, osp.join(output_dir, 'probe_features.pkl'))

    # Evaluate
    if mpi_rank == 0:
        imdb.evaluate_detections(gboxes, det_thresh=args.det_thresh)
        imdb.evaluate_detections(gboxes,
                                 det_thresh=args.det_thresh,
                                 labeled_only=True)
        imdb.evaluate_search(gboxes,
                             gfeatures['feat'],
                             pfeatures['feat'],
                             det_thresh=args.det_thresh,
                             gallery_size=args.gallery_size,
                             dump_json=osp.join(output_dir, 'results.json'))
Exemplo n.º 2
0
#!/usr/bin/env python
"""
Classifier is an image classifier specialization of Net.
"""
import numpy as np
# The caffe module needs to be on the Python path;
#  we'll add it here explicitly.
import sys
caffe_root = '/mnt/lustre/dingyang/cervix_caffe_dingyang/sensenet-release/'  
myself='/mnt/lustre/dingyang/cervix_caffe_dingyang/myself/'
sys.path.insert(0, caffe_root + 'core/python')
import caffe
caffe.mpi_init()
import os




class Classifier(caffe.Net):
    """
    Classifier extends Net for image class prediction
    by scaling, center cropping, or oversampling.
    Parameters
    ----------
    image_dims : dimensions to scale input for cropping/sampling.
        Default is to scale to net input size for whole-image crop.
    mean, input_scale, raw_scale, channel_swap: params for
        preprocessing options.
    """
    def __init__(self, model_file, pretrained_file, image_dims=None,
            mean=None, raw_scale=255,
Exemplo n.º 3
0
def main(args):
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # parse gpus
    gpus = map(int, args.gpus.split(','))
    assert len(gpus) >= mpi_size, "Number of GPUs must be >= MPI size"
    cfg.GPU_ID = gpus[mpi_rank]

    # parse feature blob names
    blob_names = args.blob_names.split(',')

    print('Using config:')
    pprint.pprint(cfg)

    while not osp.exists(args.caffemodel) and args.wait:
        print('Waiting for {} to exist...'.format(args.caffemodel))
        time.sleep(10)

    # load imdb
    imdb = get_imdb(args.imdb_name)
    root_dir = imdb._root_dir
    images_dir = imdb._data_path
    output_dir = get_output_dir(imdb.name,
                                osp.splitext(osp.basename(args.caffemodel))[0])

    if args.eval_only:
        def _load(fname):
            fpath = osp.join(output_dir, fname)
            assert osp.isfile(fpath), "Must have extracted detections and " \
                                      "features first before evaluation"
            return unpickle(fpath)
        if mpi_rank == 0:
            gboxes = _load('gallery_detections.pkl')
            gfeatures = _load('gallery_features.pkl')
            pfeatures = _load('probe_features.pkl')
    else:
        # setup caffe
        caffe.mpi_init()
        caffe.set_mode_gpu()
        caffe.set_device(cfg.GPU_ID)

        # 1. Detect and extract features from all the gallery images in the imdb
        start, end = mpi_dispatch(len(imdb.image_index), mpi_size, mpi_rank)
        if args.use_gt:
            net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
            gboxes, gfeatures = usegt_and_exfeat(net, imdb,
                start=start, end=end, blob_names=blob_names)
        else:
            net = caffe.Net(args.gallery_def, args.caffemodel, caffe.TEST)
            gboxes, gfeatures = detect_and_exfeat(net, imdb,
                start=start, end=end, blob_names=blob_names)
        gboxes = mpi_collect(mpi_comm, mpi_rank, gboxes)
        gfeatures = mpi_collect(mpi_comm, mpi_rank, gfeatures)
        del net # to release the cudnn conv static workspace

        # 2. Only extract features from given probe rois
        start, end = mpi_dispatch(len(imdb.probes), mpi_size, mpi_rank)
        net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
        pfeatures = exfeat(net, imdb.probes,
            start=start, end=end, blob_names=blob_names)
        pfeatures = mpi_collect(mpi_comm, mpi_rank, pfeatures)
        del net

        # Save
        if mpi_rank == 0:
            pickle(gboxes, osp.join(output_dir, 'gallery_detections.pkl'))
            pickle(gfeatures, osp.join(output_dir, 'gallery_features.pkl'))
            pickle(pfeatures, osp.join(output_dir, 'probe_features.pkl'))

    # Evaluate
    if mpi_rank == 0:
        imdb.evaluate_detections(gboxes, det_thresh=args.det_thresh)
        imdb.evaluate_detections(gboxes, det_thresh=args.det_thresh,
                                 labeled_only=True)
        imdb.evaluate_search(gboxes, gfeatures['feat'], pfeatures['feat'],
             det_thresh=args.det_thresh,
             gallery_size=args.gallery_size,
             dump_json=osp.join(output_dir, 'results.json'))
Exemplo n.º 4
0
def main(args):
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # Setup caffe
    if args.gpu >= 0:
        caffe.mpi_init()
        caffe.set_mode_gpu()
        caffe.set_device(cfg.GPU_ID)
    else:
        caffe.mpi_init()
        caffe.set_mode_cpu()

    # Get query image and roi
    query_img = 'demo/query.jpg'
    query_roi = [0, 0, 466, 943]  # [x1, y1, x2, y2]

    # Extract feature of the query person
    net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
    query_feat = demo_exfeat(net, query_img, query_roi)
    del net  # Necessary to release cuDNN conv static workspace

    # Get gallery images
    gallery_imgs = sorted(glob('demo/gallery*.jpg'))

    # Detect and extract feature of persons in each gallery image
    net = caffe.Net(args.gallery_def, args.caffemodel, caffe.TEST)

    # Necessary to warm-up the net, otherwise the first image results are wrong
    # Don't know why. Possibly a bug in caffe's memory optimization.
    # Nevertheless, the results are correct after this warm-up.
    demo_detect(net, query_img)

    for gallery_img in gallery_imgs:
        print gallery_img, '...'
        boxes, features = demo_detect(net,
                                      gallery_img,
                                      threshold=args.det_thresh)
        if boxes is None:
            print gallery_img, 'no detections'
            continue
        # Compute pairwise cosine similarities,
        #   equals to inner-products, as features are already L2-normed
        similarities = features.dot(query_feat)

        # Visualize the results
        fig, ax = plt.subplots(figsize=(16, 9))
        ax.imshow(plt.imread(gallery_img))
        plt.axis('off')
        for box, sim in zip(boxes, similarities):
            x1, y1, x2, y2, _ = box
            ax.add_patch(
                plt.Rectangle((x1, y1),
                              x2 - x1,
                              y2 - y1,
                              fill=False,
                              edgecolor='#4CAF50',
                              linewidth=3.5))
            ax.add_patch(
                plt.Rectangle((x1, y1),
                              x2 - x1,
                              y2 - y1,
                              fill=False,
                              edgecolor='white',
                              linewidth=1))
            ax.text(x1 + 5,
                    y1 - 18,
                    '{:.2f}'.format(sim),
                    bbox=dict(facecolor='#4CAF50', linewidth=0),
                    fontsize=20,
                    color='white')
        plt.tight_layout()
        fig.savefig(gallery_img.replace('gallery', 'result'))
        plt.show()
        plt.close(fig)
    del net
Exemplo n.º 5
0
def main(args):
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # Setup caffe
    if args.gpu >= 0:
        caffe.mpi_init()
        caffe.set_mode_gpu()
        caffe.set_device(cfg.GPU_ID)
    else:
        caffe.mpi_init()
        caffe.set_mode_cpu()

    # Get query image and roi
    query_img = 'demo/query.jpg'
    query_roi = [0, 0, 466, 943]  # [x1, y1, x2, y2]

    # Extract feature of the query person
    net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
    query_feat = demo_exfeat(net, query_img, query_roi)
    del net  # Necessary to release cuDNN conv static workspace

    # Get gallery images
    gallery_imgs = sorted(glob('demo/gallery*.jpg'))

    # Detect and extract feature of persons in each gallery image
    net = caffe.Net(args.gallery_def, args.caffemodel, caffe.TEST)

    # Necessary to warm-up the net, otherwise the first image results are wrong
    # Don't know why. Possibly a bug in caffe's memory optimization.
    # Nevertheless, the results are correct after this warm-up.
    demo_detect(net, query_img)

    for gallery_img in gallery_imgs:
        print gallery_img, '...'
        boxes, features = demo_detect(net, gallery_img,
                                      threshold=args.det_thresh)
        if boxes is None:
            print gallery_img, 'no detections'
            continue
        # Compute pairwise cosine similarities,
        #   equals to inner-products, as features are already L2-normed
        similarities = features.dot(query_feat)

        # Visualize the results
        fig, ax = plt.subplots(figsize=(16, 9))
        ax.imshow(plt.imread(gallery_img))
        plt.axis('off')
        for box, sim in zip(boxes, similarities):
            x1, y1, x2, y2, _ = box
            ax.add_patch(
                plt.Rectangle((x1, y1), x2 - x1, y2 - y1,
                              fill=False, edgecolor='#4CAF50', linewidth=3.5))
            ax.add_patch(
                plt.Rectangle((x1, y1), x2 - x1, y2 - y1,
                              fill=False, edgecolor='white', linewidth=1))
            ax.text(x1 + 5, y1 - 18, '{:.2f}'.format(sim),
                    bbox=dict(facecolor='#4CAF50', linewidth=0),
                    fontsize=20, color='white')
        plt.tight_layout()
        fig.savefig(gallery_img.replace('gallery', 'result'))
        plt.show()
        plt.close(fig)
    del net
Exemplo n.º 6
0
def partseg_train(network, exp_dir, category, args):
    if args.cpu:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.mpi_init()

    if network == 'seq':
        batch_norm = True
        conv_weight_filler = 'xavier'
        network = models.partseg_seq(arch_str=args.arch,
                                     skip_str=args.skips,
                                     dataset=args.dataset,
                                     dataset_params=args.dataset_params,
                                     category=category,
                                     feat_dims_str=args.feat,
                                     lattice_dims_str=args.lattice,
                                     sample_size=args.sample_size,
                                     batch_size=args.batch_size,
                                     batchnorm=batch_norm,
                                     conv_weight_filler=conv_weight_filler,
                                     save_path=os.path.join(
                                         exp_dir, category + '_net.prototxt'))

        models.partseg_seq(deploy=True,
                           arch_str=args.arch,
                           skip_str=args.skips,
                           dataset=args.dataset,
                           dataset_params=args.dataset_params,
                           category=category,
                           feat_dims_str=args.feat,
                           lattice_dims_str=args.lattice,
                           sample_size=args.sample_size,
                           batchnorm=batch_norm,
                           save_path=os.path.join(
                               exp_dir, category + '_net_deploy.prototxt'))
    else:
        assert network.endswith(
            '.prototxt'), 'Please provide a valid prototxt file'
        print('Using network defined at {}'.format(network))

    random_seed = 0
    debug_info = False

    solver = create_solver.standard_solver(
        network,
        network,
        os.path.join(exp_dir, category) + '_' + args.prefix,
        base_lr=args.base_lr,
        gamma=args.lr_decay,
        stepsize=args.stepsize,
        test_iter=args.test_iter,
        test_interval=args.test_interval,
        max_iter=args.num_iter,
        snapshot=args.snapshot_interval,
        solver_type=args.solver_type,
        weight_decay=args.weight_decay,
        iter_size=args.iter_size,
        debug_info=debug_info,
        random_seed=random_seed,
        save_path=os.path.join(exp_dir, category + '_solver.prototxt'))
    solver = caffe.get_solver(solver)

    if args.init_model:
        if args.init_model.endswith('.caffemodel'):
            solver.net.copy_from(args.init_model)
        else:
            solver.net.copy_from(
                os.path.join(
                    exp_dir,
                    '{}_iter_{}.caffemodel'.format(category, args.init_model)))

    if args.init_state:
        if args.init_state.endswith('.solverstate'):
            solver.restore(args.init_state)
        else:
            solver.restore(
                os.path.join(
                    exp_dir,
                    '{}_iter_{}.solverstate'.format(category,
                                                    args.init_state)))

    solver.solve()
    caffe.mpi_fin()
Exemplo n.º 7
0
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # parse gpus
    gpus = map(int, args.gpu.split(','))
    assert len(gpus) >= mpi_size, "Number of GPUs must be >= MPI size"
    cfg.GPU_ID = gpus[mpi_rank]

    print('Using config:')
    pprint.pprint(cfg)

    # set up caffe
    caffe.mpi_init()
    caffe.set_mode_gpu()
    caffe.set_device(cfg.GPU_ID)

    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(cfg.RNG_SEED)
        caffe.set_random_seed(cfg.RNG_SEED)

    imdb, roidb = combined_roidb(args.imdb_name)
    print '{:d} roidb entries'.format(len(roidb))

    output_dir = get_output_dir(imdb.name)
    print 'Output will be saved to `{:s}`'.format(output_dir)

    train_net(args.solver, roidb, output_dir,
Exemplo n.º 8
0
def pytorch2caffe(input_var, output_var, protofile, caffemodel):
    global layer_id
    net_info = pytorch2prototxt(input_var, output_var)
    print_prototxt(net_info)
    save_prototxt(net_info, protofile)
    caffe.set_mode_gpu()
    caffe.mpi_init()
    if caffemodel is None:
        return
    net = caffe.Net('../deploy_crt.prototxt', caffe.TEST)
    net = caffe.Net(protofile, caffe.TEST)
    params = net.params

    layer_id = 1
    seen = set()

    def convert_layer(func):
        if True:
            global layer_id
            parent_type = str(type(func).__name__)

            if hasattr(func, 'next_functions'):
                for u in func.next_functions:
                    if u[0] is not None:
                        child_type = str(type(u[0]).__name__)
                        child_name = child_type + str(layer_id)
                        if child_type != 'AccumulateGrad' and (
                                parent_type != 'AddmmBackward'
                                or child_type != 'TransposeBackward'):
                            if u[0] not in seen:
                                convert_layer(u[0])
                                seen.add(u[0])
                            if child_type != 'ViewBackward':
                                layer_id = layer_id + 1

            parent_name = parent_type + str(layer_id)
            print('converting %s' % parent_name)
            if parent_type == 'ConvNdBackward':
                if func.next_functions[1][0] is not None:
                    weights = func.next_functions[1][0].variable.data
                    if func.next_functions[2][0]:
                        biases = func.next_functions[2][0].variable.data
                    else:
                        biases = None
                    save_conv2caffe(weights, biases, params[parent_name])
            elif parent_type == 'BatchNormBackward':
                running_mean = func.running_mean
                running_var = func.running_var
                bn_name = parent_name + "_bn"
                save_bn2caffe(running_mean, running_var, params[bn_name])

                affine = func.next_functions[1][0] is not None
                if affine:
                    scale_weights = func.next_functions[1][0].variable.data
                    scale_biases = func.next_functions[2][0].variable.data
                    scale_name = parent_name + "_scale"
                    save_scale2caffe(scale_weights, scale_biases,
                                     params[scale_name])
            elif parent_type == 'AddmmBackward':
                biases = func.next_functions[0][0].variable.data
                weights = func.next_functions[2][0].next_functions[0][
                    0].variable.data
                save_fc2caffe(weights, biases, params[parent_name])
            elif parent_type == 'UpsamplingNearest2d':
                print('UpsamplingNearest2d')

    convert_layer(output_var.grad_fn)
    print('save caffemodel to %s' % caffemodel)
    net.save(caffemodel)