Ejemplo n.º 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
            }
Ejemplo n.º 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}
Ejemplo n.º 3
0
def classify(net, im, config):
    image_mean = load_image_mean_from_binproto(config["idl_mean"])
    jit_image = cv2.resize(im, (config['new_width'], config['new_height']))
    image = image_to_h5(jit_image, data_mean, crop_size=config['crop_size'], image_scaling=1.0)
    input_en = {"image":image}
    probs = forward(net, input_en, deploy=True)
    pred_class, value = get_max_index(probs[0,:])
    return pred_class, value
Ejemplo n.º 4
0
def load_txt(txtfile, net_config, data_mean):
    lines = open(txtfile, 'r').readlines()
    for line in lines:
        img_name = line.strip().split()[0]
        img_name = 'dishes_315/' + img_name
        jit_image = imread(img_name)
        jit_image = cv2.resize(jit_image, (net_config['new_width'], net_config['new_height']))
        image = image_to_h5(jit_image, data_mean, crop_size=net_config['crop_size'], image_scaling=1.0)
        yield {"imname": img_name, "raw": jit_image, "image": image}
Ejemplo n.º 5
0
def load_txt(txtfile, net_config, data_mean, 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."""

    lines = open(txtfile, 'r').readlines()

    img_name_batch = []
    raw_image_batch = np.zeros(
        (net_config['batch_size'], net_config['new_width'],
         net_config['new_height'], 3))
    image_batch = np.zeros((net_config['batch_size'], 3,
                            net_config['crop_size'], net_config['crop_size']))
    label_batch = np.zeros((net_config['batch_size'], 1))
    cnt = 0
    while True:
        random.shuffle(lines)
        for line in lines:
            line_segs = line.strip().split()
            img_name = ' '.join(line_segs[:-1])
            img_label = int(line_segs[-1])
            # print img_name, img_label
            if jitter:
                jit_image = image_jitter(
                    img_name,
                    target_width=net_config['new_width'],
                    target_height=net_config['new_height'])
                # plt.imshow(jit_image)
                # plt.show()
            else:
                jit_image = imread(img_name)
                jit_image = cv2.resize(
                    jit_image,
                    (net_config['new_width'], net_config['new_height']))
            image = image_to_h5(jit_image,
                                data_mean,
                                crop_size=net_config['crop_size'],
                                image_scaling=1.0)

            img_name_batch.append(img_name)
            raw_image_batch[cnt, :, :, :] = jit_image
            image_batch[cnt, :, :, :] = image
            label_batch[cnt, :] = img_label
            cnt += 1
            if cnt == net_config['batch_size']:
                yield {
                    "imname": img_name_batch,
                    "raw": raw_image_batch,
                    "image": image_batch,
                    "label": label_batch
                }
                img_name_batch = []
                cnt = 0
Ejemplo n.º 6
0
def classify(net, impath, regions_list, image_mean):
    im = imread(impath)
    batch_size = len(regions_list) / 4
    image_batch = np.zeros((batch_size, 3, 227, 227))
    for i in range(batch_size):
        x0 = int(regions_list[i * 4 + 0])
        y0 = int(regions_list[i * 4 + 1])
        x1 = int(regions_list[i * 4 + 2])
        y1 = int(regions_list[i * 4 + 3])
        sub_image = im[x0:x1, y0:y1, :]
        jit_image = cv2.resize(sub_image, (256, 256))
        image = image_to_h5(jit_image,
                            image_mean,
                            crop_size=227,
                            image_scaling=1.0)
        image_batch[i, :, :, :] = image
    input_en = {"image": image_batch}
    probs = forward(net, input_en, deploy=True)
    res = ''
    for i in range(probs.shape[0]):
        index, value = get_max_index(probs[i, :])
        res += "%d %.03f" % (index, value)
    return res
Ejemplo n.º 7
0
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
            }
Ejemplo n.º 8
0
def generate_input_en(imname, data_mean, net_config):
    raw_image = imread(imname)
    image = image_to_h5(raw_image, data_mean, image_scaling=1.0)
    return {"imname": imname, "raw": raw_image, "image": image}
Ejemplo n.º 9
0
def prepocessd_image(raw_img, data_mean):
    img_mean = image_to_h5(raw_img, data_mean, image_scaling=1.0)
    return {"raw": raw_img, "image": img_mean}