Example #1
0
def load_idl(idlfile, data_mean, net_config, jitter=True):
    """Take the idlfile, data mean and net configuration and create a generator
    that outputs a jittered version of a random image from the annolist
    that is mean corrected."""

    annolist = al.parse(idlfile)
    annos = [x for x in annolist]
    for anno in annos:
        anno.imageName = os.path.join(
            os.path.dirname(os.path.realpath(idlfile)), anno.imageName)
    while True:
        random.shuffle(annos)
        for anno in annos:
            if jitter:
                jit_image, jit_anno = annotation_jitter(
                    anno, target_width=net_config["img_width"],
                    target_height=net_config["img_height"])
            else:
                jit_image = imread(anno.imageName)
                jit_anno = anno
            image = image_to_h5(jit_image, data_mean, image_scaling=1.0)
            boxes, box_flags = annotation_to_h5(
                jit_anno, net_config["grid_width"], net_config["grid_height"],
                net_config["region_size"], net_config["max_len"])
            yield {"imname": anno.imageName, "raw": jit_image, "image": image,
                   "boxes": boxes, "box_flags": box_flags, "anno": jit_anno}
Example #2
0
def load_idl(idlfile, data_mean, net_config, jitter=True):
    """Take the idlfile, data mean and net configuration and create a generator
    that outputs a jittered version of a random image from the annolist
    that is mean corrected."""

    annolist = al.parse(idlfile)
    annos = [x for x in annolist]
    for anno in annos:
        anno.imageName = os.path.join(
            os.path.dirname(os.path.realpath(idlfile)), anno.imageName)
    while True:
        random.shuffle(annos)
        for anno in annos:
            if jitter:
                jit_image, jit_anno = annotation_jitter(
                    anno,
                    target_width=net_config["img_width"],
                    target_height=net_config["img_height"])
            else:
                jit_image = imread(anno.imageName)
                jit_anno = anno
            image = image_to_h5(jit_image, data_mean, image_scaling=1.0)
            boxes, box_flags = annotation_to_h5(jit_anno,
                                                net_config["grid_width"],
                                                net_config["grid_height"],
                                                net_config["region_size"],
                                                net_config["max_len"])
            yield {
                "imname": anno.imageName,
                "raw": jit_image,
                "image": image,
                "boxes": boxes,
                "box_flags": box_flags,
                "anno": jit_anno
            }
def convert_deploy_2_train(boot_deploy_list,
                           data_mean,
                           net_config,
                           max_heads=999999,
                           threshold=0.9,
                           jitter=True,
                           if_random=True):
    annos = []
    cnt = 0
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]
    for dic in boot_deploy_list:
        anno = Annotation()
        anno.imageName = dic["imname"]
        bbox_list = dic["bbox"]
        conf_list = dic["conf"]

        all_rects = [[[] for x in range(net_config["grid_width"])]
                     for y in range(net_config["grid_height"])]
        for n in range(len(bbox_list)):
            for k in range(net_config["grid_height"] *
                           net_config["grid_width"]):
                y = int(k / net_config["grid_width"])
                x = int(k % net_config["grid_width"])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k, 1].flatten()[0]
                abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
                abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
                w = bbox[2, 0, 0]
                h = bbox[3, 0, 0]
                if conf < threshold:
                    continue
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

        acc_rects = stitch_rects(all_rects, net_config)
        for rect in acc_rects:
            if rect.true_confidence >= threshold:
                r = AnnoRect()
                r.x1 = int(rect.cx - rect.width / 2.)
                r.x2 = int(rect.cx + rect.width / 2.)
                r.y1 = int(rect.cy - rect.height / 2.)
                r.y2 = int(rect.cy + rect.height / 2.)
                anno.rects.append(r)
        annos.append(anno)
        cnt += len(anno.rects)
        if cnt >= max_heads:
            break
    print 'deployed', len(annos), 'images with', cnt, 'heads'

    while True:
        if if_random:
            random.shuffle(annos)
        for anno in annos:
            if jitter:
                jit_image, jit_anno = annotation_jitter(
                    anno,
                    target_width=net_config["img_width"],
                    target_height=net_config["img_height"])
            else:
                jit_image = imread(anno.imageName)
                jit_anno = anno
            image = image_to_h5(jit_image, data_mean, image_scaling=1.0)
            boxes, box_flags = annotation_to_h5(jit_anno,
                                                net_config["grid_width"],
                                                net_config["grid_height"],
                                                net_config["region_size"],
                                                net_config["max_len"])
            yield {
                "num": len(annos),
                "imname": anno.imageName,
                "raw": jit_image,
                "image": image,
                "boxes": boxes,
                "box_flags": box_flags,
                'anno': jit_anno
            }