def load_idl_tf(idlfile, H, jitter):
    """Take the idlfile 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 = []
    for anno in annolist:
        anno.imageName = os.path.join(
            os.path.dirname(os.path.realpath(idlfile)), anno.imageName)
        annos.append(anno)
    random.seed(0)

    if H['data']['truncate_data']:
        annos = annos[:10]
    for epoch in itertools.count():
        random.shuffle(annos)
        for origin_anno in annos:
            tiles = preprocess_image(deepcopy(origin_anno), H)
            for I, anno in tiles:
                if jitter:
                    jitter_scale_min=0.9
                    jitter_scale_max=1.1
                    jitter_offset=16
                    I, anno = annotation_jitter(I,
                                                anno, target_width=H["image_width"],
                                                target_height=H["image_height"],
                                                jitter_scale_min=jitter_scale_min,
                                                jitter_scale_max=jitter_scale_max,
                                                jitter_offset=jitter_offset)

                boxes, flags = annotation_to_h5(H, anno)

                yield {"image": I, "boxes": boxes, "flags": flags}
Esempio n. 2
0
def load_idl_tf(idlfile, H, jitter):
    """Take the idlfile 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 = []
    for anno in annolist:
        anno.imageName = os.path.join(
            os.path.dirname(os.path.realpath(idlfile)), anno.imageName)
        annos.append(anno)
    random.seed(0)
    if H['data']['truncate_data']:
        annos = annos[:10]
    for epoch in itertools.count():
        random.shuffle(annos)
        for anno in annos:
            try:
                if 'grayscale' in H and 'grayscale_prob' in H:
                    I = cv2.imread(anno.imageName)
                    cv2.cvtColor(I, cv2.COLOR_BGR2RGB)
                    if len(I.shape) < 3:
                        I = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB)
                else:
                    if len(I.shape) < 3:
                        continue
                    I = cv2.imread(anno.imageName)
                    cv2.cvtColor(I, cv2.COLOR_BGR2RGB)
                if I.shape[0] != H["image_height"] or I.shape[1] != H[
                        "image_width"]:
                    if epoch == 0:
                        anno = rescale_boxes(I.shape, anno, H["image_height"],
                                             H["image_width"])
                    I = cv2.resize(I, (H["image_width"], H["image_height"]),
                                   interpolation=cv2.INTER_CUBIC)
                if jitter:
                    jitter_scale_min = 0.9
                    jitter_scale_max = 1.1
                    jitter_offset = 16
                    I, anno = annotation_jitter(
                        I,
                        anno,
                        target_width=H["image_width"],
                        target_height=H["image_height"],
                        jitter_scale_min=jitter_scale_min,
                        jitter_scale_max=jitter_scale_max,
                        jitter_offset=jitter_offset)

                boxes, flags = annotation_to_h5(H, anno, H["grid_width"],
                                                H["grid_height"], H["rnn_len"])

                yield {"image": I, "boxes": boxes, "flags": flags}
            except Exception as exc:
                print(exc)
Esempio n. 3
0
def load_idl_tf(idlfile, H, jitter):
    """Take the idlfile 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 = []
    for anno in annolist:
        anno.imageName = os.path.join(
            os.path.dirname(os.path.realpath(idlfile)), anno.imageName)
        annos.append(anno)
    random.seed(0)
    if H['data']['truncate_data']:
        annos = annos[:10]
    for epoch in itertools.count():
        random.shuffle(annos)
        for anno in annos:
            I = imread(anno.imageName)
            if I.shape[2] == 4:
                I = I[:, :, :3]
            if I.shape[0] != H["image_height"] or I.shape[1] != H[
                    "image_width"]:
                if epoch == 0:
                    anno = rescale_boxes(I.shape, anno, H["image_height"],
                                         H["image_width"])
                I = imresize(I, (H["image_height"], H["image_width"]),
                             interp='cubic')
            if jitter:
                jitter_scale_min = 0.9
                jitter_scale_max = 1.1
                jitter_offset = 16
                I, anno = annotation_jitter(I,
                                            anno,
                                            target_width=H["image_width"],
                                            target_height=H["image_height"],
                                            jitter_scale_min=jitter_scale_min,
                                            jitter_scale_max=jitter_scale_max,
                                            jitter_offset=jitter_offset)

            boxes, flags = annotation_to_h5(H, anno, H["grid_width"],
                                            H["grid_height"], H["rnn_len"])

            yield {"image": I, "boxes": boxes, "flags": flags}
Esempio n. 4
0
def load_idl_tf(idlfile, H, jitter):
    """Take the idlfile 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,
        root_dir=H['data']['root_dir'] if 'root_dir' in H['data'] else './')

    augmenter = Augmentation()

    annos = []
    for anno in annolist:
        anno.imageName = os.path.join(
            os.path.dirname(os.path.realpath(idlfile)), anno.imageName)
        annos.append(anno)
    random.seed(0)
    if H['data']['truncate_data']:
        annos = annos[:10]
    for epoch in itertools.count():
        random.shuffle(annos)
        for anno in annos:
            try:
                if 'grayscale' in H and 'grayscale_prob' in H:
                    I = imread(anno.imageName,
                               mode='RGB' if
                               random.random() < H['grayscale_prob'] else 'L')
                    if len(I.shape) < 3:
                        I = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB)
                else:
                    if len(I.shape) < 3:
                        continue
                    I = imread(anno.imageName, mode='RGB')

                labels = np.array([[r.x1, r.y1, r.x2, r.y2]
                                   for r in anno.rects])
                if len(labels) == 0:
                    labels = np.zeros((0, 4))

                I, labels, _ = augmenter(I, labels, np.zeros((len(labels), 1)))

                new_rects = []
                for box in labels:
                    r = al.AnnoRect()
                    r.x1, r.y1, r.x2, r.y2 = box
                    new_rects.append(r)

                new_anno = al.Annotation()
                new_anno.imageName = anno.imageName
                new_anno.imagePath = anno.imagePath
                new_anno.rects = new_rects
                anno = new_anno

                # img = I[:, :, (2,1,0)].copy()
                # for r in anno.rects:
                #     cv2.rectangle(img, tuple(map(int, (r.x1, r.y1))), tuple(map(int, (r.x2, r.y2))), (0,0,255))

                # cv2.imwrite('test.jpg', img)
                # pdb.set_trace()

                if I.shape[0] != H["image_height"] or I.shape[1] != H[
                        "image_width"]:
                    anno = rescale_boxes(I.shape, anno, H["image_height"],
                                         H["image_width"])
                    I = imresize(I, (H["image_height"], H["image_width"]),
                                 interp='cubic')
                if jitter:
                    jitter_scale_min = 0.9
                    jitter_scale_max = 1.1
                    jitter_offset = 16
                    I, anno = annotation_jitter(
                        I,
                        anno,
                        target_width=H["image_width"],
                        target_height=H["image_height"],
                        jitter_scale_min=jitter_scale_min,
                        jitter_scale_max=jitter_scale_max,
                        jitter_offset=jitter_offset)

                boxes, flags = annotation_to_h5(H, anno, H["grid_width"],
                                                H["grid_height"], H["rnn_len"])

                yield {"image": I, "boxes": boxes, "flags": flags}
            except Exception as exc:
                print(exc)