Exemplo n.º 1
0
def test_SegmentationMapOnImage_deepcopy():
    arr_c0 = np.float32([
        [1.0, 0.0],
        [1.0, 0.0]
    ])
    arr_c1 = np.float32([
        [0.0, 1.0],
        [0.0, 1.0]
    ])
    arr = np.concatenate([arr_c0[..., np.newaxis], arr_c1[..., np.newaxis]], axis=2)
    segmap = ia.SegmentationMapOnImage(arr, shape=(2, 2))
    observed = segmap.deepcopy()
    assert np.allclose(observed.arr, segmap.arr)
    assert observed.shape == (2, 2)
    assert observed.nb_classes == segmap.nb_classes
    assert observed.input_was == segmap.input_was
    segmap.arr[0, 0, 0] = 0.0
    assert not np.allclose(observed.arr, segmap.arr)

    arr = np.int32([
        [0, 1],
        [2, 3]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(2, 2), nb_classes=10)
    observed = segmap.deepcopy()
    assert np.array_equal(observed.get_arr_int(), segmap.get_arr_int())
    assert observed.shape == (2, 2)
    assert observed.nb_classes == 10
    assert observed.input_was == segmap.input_was
    segmap.arr[0, 0, 0] = 0.0
    segmap.arr[0, 0, 1] = 1.0
    assert not np.array_equal(observed.get_arr_int(), segmap.get_arr_int())
Exemplo n.º 2
0
def augmentationImage(img, lbl, depth_map):
    #seq = iaa.Sequential([iaa.Clouds(),iaa.Fog(),iaa.Snowflakes()])
    seq = iaa.Sequential(
        [
            iaa.Fliplr(0.5),  # horizontal flips
            iaa.Crop(percent=(0, 0.1)),  # random crops
            # Small gaussian blur with random sigma between 0 and 0.5.
            # But we only blur about 50% of all images.
            iaa.Sometimes(0.5, iaa.GaussianBlur(sigma=(0, 0.5))),
            # Strengthen or weaken the contrast in each image.
            iaa.ContrastNormalization((0.75, 1.5)),
            # Add gaussian noise.
            # For 50% of all images, we sample the noise once per pixel.
            # For the other 50% of all images, we sample the noise per pixel AND
            # channel. This can change the color (not only brightness) of the
            # pixels.
            iaa.AdditiveGaussianNoise(
                loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5),
            # Make some images brighter and some darker.
            # In 20% of all cases, we sample the multiplier once per channel,
            # which can end up changing the color of the images.
            iaa.Multiply((0.8, 1.2), per_channel=0.2),
            # Apply affine transformations to each image.
            # Scale/zoom them, translate/move them, rotate them and shear them.
            iaa.Affine(scale={
                "x": (0.8, 1.2),
                "y": (0.8, 1.2)
            },
                       translate_percent={
                           "x": (-0.2, 0.2),
                           "y": (-0.2, 0.2)
                       },
                       rotate=(-25, 25),
                       shear=(-8, 8))
        ],
        random_order=True)  # apply augmenters in random order

    segmap = ia.SegmentationMapOnImage(lbl,
                                       shape=img.shape,
                                       nb_classes=1 + np.max(lbl))
    #    images = load_batch(batch_idx)  # you have to implement this function
    depthmap = ia.SegmentationMapOnImage(depth_map,
                                         shape=depth_map.shape,
                                         nb_classes=256)

    seq_det = seq.to_deterministic()
    #    images_aug.append(seq_det.augment_image(image))
    #    segmaps_aug.append(seq_det.augment_segmentation_maps([segmap])[0])

    images_aug = seq_det.augment_images([img])  # done by the library
    labels_aug = seq_det.augment_segmentation_maps([segmap])
    depthmap_aug = seq_det.augment_segmentation_maps([depthmap])
    #    train_on_images(images_aug)  # you have to implement this function

    return [
        images_aug[0], labels_aug[0].get_arr_int(),
        depthmap_aug[0].get_arr_int()
    ]
Exemplo n.º 3
0
def test_SegmentationMapOnImage_get_arr_int():
    arr = np.int32([
        [0, 0, 1],
        [0, 2, 1],
        [1, 3, 1]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3), nb_classes=4)
    observed = segmap.get_arr_int()
    assert observed.dtype.type == np.int32
    assert np.array_equal(arr, observed)

    arr_c0 = np.float32([
        [0.1, 0.1, 0.1],
        [0.1, 0.9, 0.1],
        [0.0, 0.1, 0.0]
    ])
    arr_c1 = np.float32([
        [0.2, 1.0, 0.2],
        [0.2, 0.8, 0.2],
        [0.0, 0.0, 0.0]
    ])
    arr_c2 = np.float32([
        [0.0, 0.0, 0.0],
        [0.3, 0.7, 0.3],
        [0.1, 0.0, 0.0001]
    ])
    arr = np.concatenate([
        arr_c0[..., np.newaxis],
        arr_c1[..., np.newaxis],
        arr_c2[..., np.newaxis]
    ], axis=2)
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3))
    observed = segmap.get_arr_int()
    expected = np.int32([
        [2, 2, 2],
        [3, 1, 3],
        [3, 1, 0]
    ])
    assert observed.dtype.type == np.int32
    assert np.array_equal(observed, expected)

    got_exception = False
    try:
        _ = segmap.get_arr_int(background_class_id=2)
    except Exception as exc:
        assert "The background class id may only be changed if " in str(exc)
        got_exception = True
    assert got_exception

    observed = segmap.get_arr_int(background_threshold=0.21)
    expected = np.int32([
        [0, 2, 0],
        [3, 1, 3],
        [0, 0, 0]
    ])
    assert observed.dtype.type == np.int32
    assert np.array_equal(observed, expected)
Exemplo n.º 4
0
    def data_augmentation(self, image, label):

        crop_size = random.randint(int(0.8*self.sample_height),int(1.2*self.sample_height))
        
        start_h = random.randint(
            0,image.shape[0] - int(1.42*crop_size) - 2)
        start_w = random.randint(
            0,image.shape[1] - int(1.42*crop_size) - 2)
        image = image[start_h:start_h + int(1.42*crop_size), start_w:start_w +
                      int(1.42*crop_size)]
        label = label[start_h:start_h + int(1.42*crop_size), start_w:start_w +
                      int(1.42*crop_size)]


        seq = iaa.Sequential([
            iaa.Affine(
                shear=(-4, 4),
                rotate=(0, 360)),  # rotate by -45 to 45 degrees (affects segmaps)    
        ])
        segmap = ia.SegmentationMapOnImage(
            label.copy(), shape=label.shape, nb_classes=self.num_classes)

        seq_det = seq.to_deterministic()

        image_rotation = seq_det.augment_image(image.copy())
        segmap_aug = seq_det.augment_segmentation_maps(segmap)

        label_rotation = segmap_aug.get_arr_int()

        reduction_pixels = int(0.15*label_rotation.shape[0])
        start_i = reduction_pixels
        stop_i = label.shape[0] - reduction_pixels
        image = image_rotation[start_i:stop_i, start_i:stop_i, :]
        label = label_rotation[start_i:stop_i, start_i:stop_i]

        seq = iaa.Sequential([
            iaa.Resize({"height": self.sample_height,
                         "width": self.sample_width},interpolation='nearest'),
            iaa.Fliplr(0.5),
            iaa.Flipud(0.5),
            iaa.Sometimes(0.8,iaa.HistogramEqualization()),
            iaa.Sometimes(0.8,iaa.CoarseDropout((0.0, 0.05), size_percent=(0.02, 0.25))),
            iaa.AddToHueAndSaturation((-20, 20), per_channel=True),
        ])
        segmap = ia.SegmentationMapOnImage(
            label.copy(), shape=label.shape, nb_classes=self.num_classes)

        seq_det = seq.to_deterministic()

        image_aug = seq_det.augment_image(image.copy())
        segmap_aug = seq_det.augment_segmentation_maps(segmap)

        label_aug = segmap_aug.get_arr_int()

        return image_aug, label_aug
Exemplo n.º 5
0
def test_SegmentationMapOnImage_pad_to_aspect_ratio():
    arr = np.int32([
        [0, 1, 1],
        [0, 2, 1]
    ])
    segmap = ia.SegmentationMapOnImage(arr, shape=(2, 3), nb_classes=3)

    segmap_padded = segmap.pad_to_aspect_ratio(1.0)
    observed = segmap_padded.arr
    expected = np.pad(segmap.arr, ((0, 1), (0, 0), (0, 0)), mode="constant", constant_values=0)
    assert np.allclose(observed, expected)

    segmap_padded = segmap.pad_to_aspect_ratio(1.0, cval=1.0)
    observed = segmap_padded.arr
    expected = np.pad(segmap.arr, ((0, 1), (0, 0), (0, 0)), mode="constant", constant_values=1.0)
    assert np.allclose(observed, expected)

    segmap_padded = segmap.pad_to_aspect_ratio(1.0, mode="edge")
    observed = segmap_padded.arr
    expected = np.pad(segmap.arr, ((0, 1), (0, 0), (0, 0)), mode="edge")
    assert np.allclose(observed, expected)

    segmap_padded = segmap.pad_to_aspect_ratio(0.5)
    observed = segmap_padded.arr
    expected = np.pad(segmap.arr, ((2, 2), (0, 0), (0, 0)), mode="constant", constant_values=0)
    assert np.allclose(observed, expected)

    segmap_padded, pad_amounts = segmap.pad_to_aspect_ratio(0.5, return_pad_amounts=True)
    observed = segmap_padded.arr
    expected = np.pad(segmap.arr, ((2, 2), (0, 0), (0, 0)), mode="constant", constant_values=0)
    assert np.allclose(observed, expected)
    assert pad_amounts == (2, 0, 2, 0)
    def evaluateAll(self,ds, fold:int,stage=-1,negatives="real"):
        folds = self.kfold(ds, range(0, len(ds)))
        vl, vg, test_g = folds.generator(fold, False,negatives=negatives,returnBatch=True);
        indexes = folds.sampledIndexes(fold, False, negatives)
        m = self.load_model(fold, stage)
        num=0
        with tqdm.tqdm(total=len(indexes), unit="files", desc="segmentation of validation set from " + str(fold)) as pbar:
            try:
                for f in test_g():
                    if num>=len(indexes): break
                    x, y, b = f
                    z = m.predict(x)
                    ids=[]
                    augs=[]
                    for i in range(0,len(z)):
                        if num >= len(indexes): break
                        orig=b.images[i]
                        num = num + 1
                        ma=z[i]
                        id=b.data[i]
                        segmentation_maps_aug = [imgaug.SegmentationMapOnImage(ma, ma.shape)];
                        augmented = imgaug.augmenters.Scale(
                                    {"height": orig.shape[0], "width": orig.shape[1]}).augment_segmentation_maps(segmentation_maps_aug)
                        ids.append(id)
                        augs=augs+augmented

                    res=imgaug.Batch(images=b.images,data=ids,segmentation_maps=b.segmentation_maps)
                    res.predicted_maps_aug=augs
                    yield res
                    pbar.update(len(ids))
            finally:
                vl.terminate();
                vg.terminate();
        pass
    def predict_on_directory(self, path, fold=0, stage=0, limit=-1, batchSize=32,ttflips=False):
        mdl = self.load_model(fold, stage)
        if self.crops is not None:
            mdl=BatchCrop(self.crops,mdl)
        ta = self.transformAugmentor()
        for v in datasets.DirectoryDataSet(path, batchSize).generator(limit):
            for z in ta.augment_batches([v]):
                o1=np.array(z.images_aug);
                res = mdl.predict(o1)
                if ttflips:
                    another=imgaug.augmenters.Fliplr(1.0).augment_images(z.images_aug);
                    res1= mdl.predict(np.array(another))
                    res1=imgaug.augmenters.Fliplr(1.0).augment_images(res1)

                    another1 = imgaug.augmenters.Flipud(1.0).augment_images(z.images_aug);
                    res2 = mdl.predict(np.array(another1))
                    res2 = imgaug.augmenters.Flipud(1.0).augment_images(res2)

                    seq=imgaug.augmenters.Sequential([imgaug.augmenters.Fliplr(1.0), imgaug.augmenters.Flipud(1.0)])
                    another2 = seq.augment_images(z.images_aug);
                    res3 = mdl.predict(np.array(another2))
                    res3 = seq.augment_images(res3)

                    res=(res+res1+res2+res3)/4.0
                z.segmentation_maps_aug = [imgaug.SegmentationMapOnImage(x, x.shape) for x in res];
                yield z
    def load(self):
        i = 0
        bx = []
        by = []
        while True:
            if (i == len(self.indeces)):
                i = 0
            try:
                if hasattr(self.dataset, "item"):
                    item = self.dataset.item(self.indeces[i], self.isTrain)
                else:
                    item = self.dataset[self.indeces[i]]
                x, y = item.x, item.y
            except:
                traceback.print_exc()
                i = i + 1
                continue
            i = i + 1
            bx.append(x)
            by.append(y)
            if len(bx) == self.batchSize:

                return imgaug.imgaug.Batch(images=bx,
                                           segmentation_maps=[
                                               imgaug.SegmentationMapOnImage(
                                                   x, shape=x.shape)
                                               for x in by
                                           ])
                bx = []
                by = []
Exemplo n.º 9
0
    def __getitem__(self, index):
        # load image and crop
        img = Image.open(self.image_lists[index])
        img = np.array(img)
        labels = self.label_lists[index]

        # load label
        if self.mode != 'test':
            label = Image.open(self.label_lists[index])
            label = np.array(label)
            label[label != 255] = 0
            label[label == 255] = 1
            # label=np.argmax(label,axis=-1)
            # label[label!=1]=0
            # augment image and label
            if self.mode == 'train':
                seq_det = self.flip.to_deterministic()
                segmap = ia.SegmentationMapOnImage(label,
                                                   shape=label.shape,
                                                   nb_classes=2)

                img = seq_det.augment_image(img)
                label = seq_det.augment_segmentation_maps(
                    [segmap])[0].get_arr_int().astype(np.uint8)

            label = np.reshape(label, (1, ) + label.shape)
            label = torch.from_numpy(label.copy()).float()

            labels = label

        # img=np.reshape(img,img.shape+(1,))       # 如果输入是1通道需打开此注释 ******
        img = self.to_tensor(img.copy()).float()
        return img, labels
def augment_seg(img, seg):
    aug_det = seq.to_deterministic()
    image_aug = aug_det.augment_image(img)
    segmap = ia.SegmentationMapOnImage(seg, nb_classes=2, shape=img.shape)
    segmap_aug = aug_det.augment_segmentation_maps(segmap)
    segmap_aug = segmap_aug.get_arr_int()
    return image_aug, segmap_aug
    def __getitem__(self, index):
        datum = self.data[index]
        datum = datum.copy()

        img = self.loader_fn(datum['path'])
        seg = np.array(Image.open(datum['seg_path']))
        # convert classes
        seg = self.seg_mapping[seg]

        if self.transform is not None:
            # flip transform is outside the pipeline
            # segmentation label flipping is not yet supported
            # do before possible normalization
            num_seg_classes = self.info['num_seg_classes']

            if self.flip_prob > 0:
                # only execute if the probability is greater 0
                # if the image will be flipped is decided by augmenter
                det_flip = self.flip_transform.to_deterministic()
                img = det_flip.augment_image(img)
                seg = ia.SegmentationMapOnImage(seg,
                                                shape=seg.shape,
                                                nb_classes=num_seg_classes)
                seg = det_flip.augment_segmentation_maps(seg).get_arr_int()

            self.transform.to_deterministic()
            img = self.transform.augment_image(img)
            seg = self.transform.augment_segmentation(seg, num_seg_classes)

        datum['img'] = img
        # TODO why long?? Otherwise error in loss
        datum['seg'] = np.array(seg, dtype=np.int64)

        return datum
 def iter():
     for z in self.ta.augment_batches([self.rs]):
         res = self.model.predict(np.array(z.images_aug))
         z.heatmaps_aug = [
             imgaug.SegmentationMapOnImage(x, x.shape) for x in res
         ]
         yield z
Exemplo n.º 13
0
def chapter_examples_segmentation_maps_bool_full():
    import imgaug as ia
    from imgaug import augmenters as iaa
    import imageio
    import numpy as np

    ia.seed(1)

    # Load an example image (uint8, 128x128x3).
    image = ia.quokka(size=(128, 128), extract="square")

    # Create an example mask (bool, 128x128).
    # Here, we just randomly place a square on the image.
    segmap = np.zeros((128, 128), dtype=bool)
    segmap[28:71, 35:85] = True
    segmap = ia.SegmentationMapOnImage(segmap, shape=image.shape)

    # Define our augmentation pipeline.
    seq = iaa.Sequential(
        [
            iaa.Dropout([0.05, 0.2]),  # drop 5% or 20% of all pixels
            iaa.Sharpen((0.0, 1.0)),  # sharpen the image
            iaa.Affine(
                rotate=(-45,
                        45)),  # rotate by -45 to 45 degrees (affects heatmaps)
            iaa.ElasticTransformation(
                alpha=50, sigma=5)  # apply water effect (affects heatmaps)
        ],
        random_order=True)

    # Augment images and heatmaps.
    images_aug = []
    segmaps_aug = []
    for _ in range(5):
        seq_det = seq.to_deterministic()
        images_aug.append(seq_det.augment_image(image))
        segmaps_aug.append(seq_det.augment_segmentation_maps([segmap])[0])

    # We want to generate an image of original input images and heatmaps before/after augmentation.
    # It is supposed to have five columns: (1) original image, (2) augmented image,
    # (3) augmented heatmap on top of augmented image, (4) augmented heatmap on its own in jet
    # color map, (5) augmented heatmap on its own in intensity colormap,
    # We now generate the cells of these columns.
    #
    # Note that we add a [0] after each heatmap draw command. That's because the heatmaps object
    # can contain many sub-heatmaps and hence we draw command returns a list of drawn sub-heatmaps.
    # We only used one sub-heatmap, so our lists always have one entry.
    cells = []
    for image_aug, segmap_aug in zip(images_aug, segmaps_aug):
        cells.append(image)  # column 1
        cells.append(segmap.draw_on_image(image))  # column 2
        cells.append(image_aug)  # column 3
        cells.append(segmap_aug.draw_on_image(image_aug))  # column 4
        cells.append(segmap_aug.draw(size=image_aug.shape[:2]))  # column 5

    # Convert cells to grid image and save.
    grid_image = ia.draw_grid(cells, cols=5)
    #imageio.imwrite("example_segmaps_bool.jpg", grid_image)

    save("examples_segmentation_maps", "bool_full.jpg", grid_image, quality=90)
Exemplo n.º 14
0
    def test(self):
        ia.seed(1)
        # Load an example image (uint8, 128x128x3).
        image = ia.quokka(size=(128, 128), extract="square")

        # Create an example segmentation map (int32, 128x128).
        # Here, we just randomly place some squares on the image.
        # Class 0 is the background class.
        segmap = np.zeros((128, 128), dtype=np.int32)
        segmap[28:71, 35:85] = 1
        segmap[10:25, 30:45] = 2
        segmap[10:25, 70:85] = 3
        segmap[10:110, 5:10] = 4
        segmap[118:123, 10:110] = 5
        segmap = ia.SegmentationMapOnImage(segmap,
                                           shape=image.shape,
                                           nb_classes=1 + 5)

        # Define our augmentation pipeline.
        seq = iaa.Sequential(
            [
                iaa.Dropout([0.05, 0.2]),  # drop 5% or 20% of all pixels
                iaa.Sharpen((0.0, 1.0)),  # sharpen the image
                iaa.Affine(rotate=(
                    -45,
                    45)),  # rotate by -45 to 45 degrees (affects heatmaps)
                iaa.ElasticTransformation(
                    alpha=50, sigma=5)  # apply water effect (affects heatmaps)
            ],
            random_order=True)

        # Augment images and heatmaps.
        images_aug = []
        segmaps_aug = []
        for _ in range(5):
            seq_det = seq.to_deterministic()
            images_aug.append(seq_det.augment_image(image))
            segmaps_aug.append(seq_det.augment_segmentation_maps([segmap])[0])

        # We want to generate an image of original input images and heatmaps before/after augmentation.
        # It is supposed to have five columns: (1) original image, (2) augmented image,
        # (3) augmented heatmap on top of augmented image, (4) augmented heatmap on its own in jet
        # color map, (5) augmented heatmap on its own in intensity colormap,
        # We now generate the cells of these columns.
        #
        # Note that we add a [0] after each heatmap draw command. That's because the heatmaps object
        # can contain many sub-heatmaps and hence we draw command returns a list of drawn sub-heatmaps.
        # We only used one sub-heatmap, so our lists always have one entry.
        cells = []
        for image_aug, segmap_aug in zip(images_aug, segmaps_aug):
            cells.append(image)  # column 1
            cells.append(segmap.draw_on_image(image))  # column 2
            cells.append(image_aug)  # column 3
            cells.append(segmap_aug.draw_on_image(image_aug))  # column 4
            cells.append(segmap_aug.draw(size=image_aug.shape[:2]))  # column 5

        # Convert cells to grid image and save.
        grid_image = ia.draw_grid(cells, cols=5)
        ia.show_grid(cells, cols=5)
Exemplo n.º 15
0
def aug(image, label, num_class):
    mask = ia.SegmentationMapOnImage(label,
                                     shape=image.shape,
                                     nb_classes=num_class)
    image_aug, label_aug = seq(image=image, segmentation_maps=mask)
    label_aug = label_aug.get_arr_int().astype(np.uint8)

    return image_aug, label_aug
Exemplo n.º 16
0
def make_image_gen(in_df, batch_size=train_config.BATCH_SIZE, if_aug=False):
    file = in_df['filename'].values
    mask = in_df['maskname'].values
    ys = in_df['ys'].values
    ye = in_df['ye'].values
    xs = in_df['xs'].values
    xe = in_df['xe'].values
    all_batches = np.stack((file, mask, ys, ye, xs, xe), 1)
    out_rgb = []
    out_mask = []
    seq = augmentation().to_deterministic()
    while True:
        np.random.shuffle(all_batches)
        for data in all_batches:
            file = data_folder + data[0]
            mask = data_folder + data[1]
            c_img = crop_pic(cv2.imread(file),
                             ys=data[2],
                             ye=data[3],
                             xs=data[4],
                             xe=data[5])
            c_mask = crop_mask(cv2.imread(mask, 0),
                               ys=data[2],
                               ye=data[3],
                               xs=data[4],
                               xe=data[5])
            out_rgb += [c_img]
            if if_aug:

                out_mask += [
                    ia.SegmentationMapOnImage(c_mask,
                                              shape=c_img.shape[:2],
                                              nb_classes=1 + 4)
                ]
            else:
                c_mask = np.where(np.equal(c_mask, train_config.class_label),
                                  1, 0)
                c_mask = np.expand_dims(c_mask, axis=-1)
                out_mask += [c_mask]

            if len(out_rgb) >= batch_size:
                if if_aug:
                    images_aug, segmaps_aug = seq.augment(
                        images=out_rgb, segmentation_maps=out_mask)
                    segmaps_aug = [
                        np.expand_dims(
                            segmaps_aug[i].arr[:, :, train_config.class_label],
                            axis=-1) for i in range(len(segmaps_aug))
                    ]

                    out_rgb = np.stack(images_aug, 0)
                    out_mask = segmaps_aug
                else:
                    out_rgb = np.stack(np.array(out_rgb), 0)
                    out_mask = out_mask

                yield out_rgb / 255.0, np.stack(out_mask, 0)
                out_rgb, out_mask = [], []
Exemplo n.º 17
0
def display_image(img, segmap):
    img = np.array(img)
    segmap = np.array(segmap)
    ia_seg_map = ia.SegmentationMapOnImage(segmap,
                                           shape=img.shape,
                                           nb_classes=47)
    colors = ia_seg_map.DEFAULT_SEGMENT_COLORS + ia_seg_map.DEFAULT_SEGMENT_COLORS[
        1:6]
    return Image.fromarray(ia_seg_map.draw_on_image(img, colors=colors))
Exemplo n.º 18
0
    def __getitem__(self, index):
        # load image and crop
        img_cur = Image.open(self.image_lists[index])
        length=len(self.image_lists)
        if index==0:
            pre_index=0
        else:
            pre_index=index-1

        if index>=length-1:
            next_index=index
        else:
            next_index=index+1
        
        if self.image_lists[pre_index].split('/')[-2]==self.image_lists[index].split('/')[-2]:
            img_pre=Image.open(self.image_lists[pre_index])
        else:
            img_pre=img_cur

        if self.image_lists[next_index].split('/')[-2]==self.image_lists[index].split('/')[-2]:
            img_next=Image.open(self.image_lists[next_index])
        else:
            img_next=img_cur


        
        img = np.stack((img_pre,img_cur,img_next),axis=2).astype(np.uint16)  #2.5D
        labels=self.label_lists[index]
        #load label
        if self.mode !='test':
            label_ori = Image.open(self.label_lists[index])  
            label_ori = np.array(label_ori)
            label=np.ones(shape=(label_ori.shape[0],label_ori.shape[1]),dtype=np.uint8)

            #convert RGB  to one hot
            
            for i in range(len(self.COLOR_DICT)):
                equality = np.equal(label_ori, self.COLOR_DICT[i])
                class_map = np.all(equality, axis=-1)
                label[class_map]=i

            # augment image and label
            if self.mode == 'train' or self.mode == 'train_val' :
                seq_det = self.flip.to_deterministic()#固定变换
                segmap = ia.SegmentationMapOnImage(label, shape=label.shape, nb_classes=5)
                img = seq_det.augment_image(img)
                label = seq_det.augment_segmentation_maps([segmap])[0].get_arr_int().astype(np.uint8)

            label_img=torch.from_numpy(label.copy()).float()
            if self.mode == 'val':
                img_num=len(os.listdir(os.path.dirname(labels)))
                labels=label_img,img_num
            else:
                labels=label_img
        imgs=img.transpose(2,0,1)/65535.0
        img = torch.from_numpy(imgs.copy()).float()#self.to_tensor(img.copy()).float()
        return img, labels
Exemplo n.º 19
0
    def __getitem__(self, index):
        """
        Example:
            >>> # DISABLE_DOCTEST
            >>> self = SegmentationDataset.demo(augmenter=True)
            >>> output = self[10]
            >>> # xdoctest: +REQUIRES(--show)
            >>> import kwplot
            >>> plt = kwplot.autoplt()
            >>> colored_labels = self._colorized_labels(output['class_idxs'])
            >>> kwplot.figure(doclf=True)
            >>> kwplot.imshow(output['im'])
            >>> kwplot.imshow(colored_labels, alpha=.4)
        """
        outer, inner = self.subindex.unravel(index)
        gid = self._gids[outer]
        slider = self._sliders[outer]
        slices = slider[inner]

        tr = {'gid': gid, 'slices': slices}
        sample = self.sampler.load_sample(tr, with_annots=['segmentation'])

        imdata = sample['im']
        heatmap = self._sample_to_sseg_heatmap(sample)

        heatmap = heatmap.numpy()
        heatmap.data['class_idx'] = heatmap.data['class_idx'].astype(np.int32)
        cidx_segmap = heatmap.data['class_idx']

        if self.augmenter:
            augdet = self.augmenter.to_deterministic()
            imdata = augdet.augment_image(imdata)
            if hasattr(imgaug, 'SegmentationMapsOnImage'):
                # Oh imgaug, stop breaking.
                cidx_segmap_oi = imgaug.SegmentationMapsOnImage(cidx_segmap, cidx_segmap.shape)
                cidx_segmap_oi = augdet.augment_segmentation_maps(cidx_segmap_oi)
                assert cidx_segmap_oi.arr.shape[2] == 1
                cidx_segmap = cidx_segmap_oi.arr[..., 0]
                cidx_segmap = np.ascontiguousarray(cidx_segmap)
            else:
                cidx_segmap_oi = imgaug.SegmentationMapOnImage(cidx_segmap, cidx_segmap.shape, nb_classes=len(self.classes))
                cidx_segmap_oi = augdet.augment_segmentation_maps([cidx_segmap_oi])[0]
                cidx_segmap = cidx_segmap_oi.arr.argmax(axis=2)

        im_chw = torch.FloatTensor(
            imdata.transpose(2, 0, 1).astype(np.float32) / 255.)

        cidxs = torch.LongTensor(cidx_segmap)
        weight = (1 - (cidxs == 0).float())

        output = {
            'im': im_chw,
            'class_idxs': cidxs,
            'weight': weight,
        }
        return output
Exemplo n.º 20
0
    def __getitem__(self, index):
        datum = self.data[index]
        datum = datum.copy()

        img = self.loader_fn(datum['path'])
        shape = img.shape
        coords = datum['coords']
        # image is a 3 channel png with identical channels
        seg = np.array(self.loader_fn(datum['seg_path']))[:, :, 0]

        if not self.info['seg_mapping'] is None:
            seg = self.info['seg_mapping'][seg]

        if self.transform is not None:
            # flip transform is outside the pipeline
            # segmentation label flipping is not yet supported
            # do before possible normalization
            num_seg_classes = self.info['num_seg_classes']

            if self.flip_prob > 0:
                # only execute if the probability is greater 0
                # if the image will be flipped is decided by augmenter
                det_flip = self.flip_transform.to_deterministic()
                #det_flip = self.flip_transform
                img = det_flip.augment_image(img)
                seg = ia.SegmentationMapOnImage(seg,
                                                shape=seg.shape,
                                                nb_classes=num_seg_classes)
                seg = det_flip.augment_segmentation_maps(seg).get_arr_int()

                keypoints_on_image = ia.KeypointsOnImage.from_coords_array(
                    coords, shape=shape)
                keypoints_on_image = det_flip.augment_keypoints(
                    [keypoints_on_image])
                coords = keypoints_on_image[0].get_coords_array()

            self.transform.to_deterministic()
            img = self.transform.augment_image(img)

            # apply other transformations only for training
            if self.train:
                seg = self.transform.augment_segmentation(seg, num_seg_classes)
            # the shape of the original image
            coords = self.transform.augment_keypoint(coords, shape)
            # the shape of the augmented image
            coords = self.normalize_pose_keypoints(coords, img.shape)

        # we need to save the shape to restore the orginal coordinates
        datum['height'] = shape[0]
        datum['width'] = shape[1]
        datum['coords'] = coords
        datum['img'] = img
        # TODO why long?? Otherwise error in loss
        datum['seg'] = np.array(seg, dtype=np.int64)

        return datum
Exemplo n.º 21
0
def augment_online(image,label):
    """
    the input label is not ont-hot format.
    """
    segmap = ia.SegmentationMapOnImage(label, shape=image.shape, nb_classes=1+5)
    auger=getAuger_online(CONTRAST_RANGE,PERSPECTIVE_RANGE)
    seq_det = auger.to_deterministic() #确定一个数据增强的序列
    images_aug = seq_det.augment_image(image) #将方法应用在原图像上
    segmaps_aug = seq_det.augment_segmentation_maps([segmap]).get_arr_int().astype(np.uint8)
    return images_aug,segmaps_aug
    def augmentor(self, images, targets):
        '''Augments each batch of data with random transformations'''
        sometimes = lambda aug: iaa.Sometimes(0.5, aug)
        seq = iaa.Sequential([
            iaa.Fliplr(0.5, name="Fliplr"),
            iaa.Flipud(0.5, name="Flipud"),
            sometimes(
                iaa.SomeOf((0, 2), [
                    iaa.Affine(translate_percent={
                        "x": (-0.1, 0.1),
                        "y": (-0.1, 0.1)
                    },
                               rotate=(-25, 25),
                               name="Affine"),
                    iaa.ElasticTransformation(alpha=(0.01, 0.1),
                                              sigma=0.15,
                                              name="ElasticTransformation"),
                    iaa.PiecewiseAffine(scale=(0.001, 0.03),
                                        name="PiecewiseAffine"),
                    iaa.PerspectiveTransform(scale=(0.01, 0.05),
                                             name="PerspectiveTransform"),
                ],
                           random_order=True)),
            sometimes(
                iaa.OneOf([
                    iaa.GaussianBlur(sigma=(0, 0.2)),
                    iaa.AverageBlur(k=3),
                    iaa.MedianBlur(k=3),
                ])),
            sometimes(
                iaa.OneOf([
                    iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.01 * 255)),
                    iaa.AddElementwise((-5, 5)),
                ])),
            sometimes(
                iaa.OneOf([
                    iaa.GammaContrast(gamma=(0.75, 1.50)),
                    iaa.HistogramEqualization(),
                    iaa.Multiply((0.80, 1.15)),
                    iaa.Add((-20, 15)),
                    iaa.Sharpen(alpha=(0, 0.5), lightness=(0.7, 1.5)),
                    iaa.Emboss(alpha=(0, 0.5), strength=(0.7, 1.5)),
                ])),
        ],
                             random_order=True)

        seq_det = seq.to_deterministic()
        images = seq_det.augment_images(images)
        targets = seq_det.augment_segmentation_maps([
            ia.SegmentationMapOnImage(t.astype(bool), shape=t.shape)
            for t in targets
        ])
        targets = np.array([t.get_arr_int() for t in targets])

        return images, targets
Exemplo n.º 23
0
def imageAugment(datafolder):

    img_files=[]
    label_files=[]

####find images
    file=os.path.join(datafolder,'img_list.txt')
    fp = open(file)
    lines = fp.readlines()
    fp.close()
    num_img = len(lines)
    for line in lines:
            line = line.strip('/n')           
            img_files.append(line)
####find labels
    file=os.path.join(datafolder,'label_list.txt')
    fp = open(file)
    lines = fp.readlines()
    fp.close()
    for line in lines:
            line = line.strip('/n')           
            label_files.append(line)
    num_label=len(lines)

    if num_img !=num_label:
          return       
####set augment parameters
    ia.seed(1)
    contrast_range=(0.8, 2.0)
    Perspective_range=(0.05, 0.09)

    img_list_file=os.path.join(datafolder,'img_list_au.txt')
    label_list_file=os.path.join(datafolder,'label_list_au.txt')
    f_img = open(img_list_file,'w')
    f_label = open(label_list_file,'w')

    for idx  in range (num_img):
        label=cv2.imread(datafolder+label_files[idx],0)
        image=cv2.imread(datafolder+img_files[idx])
       # label= convertLas2Train(label, params.LABEL_MAPPING_LAS2TRAIN)
        segmap = ia.SegmentationMapOnImage(label, shape=image.shape, nb_classes=1+5)
        for a_id in range(4):###for each path do 4 augment
            auger=getAuger(a_id,contrast_range,Perspective_range)
            seq_det = auger.to_deterministic() #确定一个数据增强的序列
            images_aug = seq_det.augment_image(image) #将方法应用在原图像上
            segmaps_aug = seq_det.augment_segmentation_maps([segmap])[0].get_arr_int().astype(np.uint8)
            img_write_path=img_files[idx][:-4]+'_{}.tif'.format(a_id)
            label_write_path=label_files[idx][:-4]+'_{}.tif'.format(a_id)
            cv2.imwrite(datafolder+img_write_path,images_aug)
            cv2.imwrite(datafolder+label_write_path,segmaps_aug)
            f_img.write('img_patch/'+img_write_path+'/n');
            f_label.write('label_patch/'+label_write_path+'/n');

    f_img.close()
    f_label.close()
Exemplo n.º 24
0
def applyMasksToImage(img, gt_masks, gt_labels, numColors):
    masksShape = list(img.shape[:2]) + [1]
    objColor = 1
    gtMasksArr = np.zeros(masksShape, dtype=np.int)
    for i in range(len(gt_labels)):
        l = gt_labels[i]
        gtm = gt_masks[i]
        gtMasksArr[gtm > 0] = objColor
        objColor = 1 + (objColor + 1) % (numColors - 1)
    gtMaskImg = imgaug.SegmentationMapOnImage(gtMasksArr, img.shape).draw_on_image(img)[0]
    return gtMaskImg
Exemplo n.º 25
0
    def __getitem__(self, index):

        img = io.imread(self.img_paths[index])
        mask = io.imread(self.mask_paths[index])
        mask = mask / mask.max()
        mask = mask.astype(np.uint8)

        corners = self.csv.iloc[index]
        poly = np.zeros([5, 2])
        nodes = np.zeros([self.L, 2])
        for c in range(4):
            poly[c, 0] = np.float(corners[1 + 2 * c])
            poly[c, 1] = np.float(corners[2 + 2 * c])
        poly[4, :] = poly[0, :]
        [tck, u] = interpolate.splprep([poly[:, 0], poly[:, 1]],
                                       s=2,
                                       k=1,
                                       per=1)
        [nodes[:, 0],
         nodes[:, 1]] = interpolate.splev(np.linspace(0, 1, self.L), tck)

        sample = {
            'image': img,
            'label/segmentation': mask,
            'label/nodes': nodes
        }

        # do image augmentations
        if (self.augmentations is not None):
            orig_shape = sample['image'].shape
            aug_det = self.augmentations.to_deterministic()
            sample['image'] = aug_det.augment_image(sample['image'])

            truth = sample['label/segmentation']
            truth = ia.SegmentationMapOnImage(truth,
                                              shape=truth.shape,
                                              nb_classes=2)
            truth = aug_det.augment_segmentation_maps(
                [truth])[0].get_arr_int()[..., np.newaxis]
            sample['label/segmentation'] = truth

            if ('label/nodes' in sample.keys()):
                kp = sample['label/nodes']
                kp = KeypointsOnImage(
                    [Keypoint(x=r[1], y=r[0] - orig_shape[0]) for r in kp],
                    shape=orig_shape)
                sample['label/nodes'] = aug_det.augment_keypoints(
                    kp).to_xy_array()

        # do image normalization
        sample['image_unnormalized'] = sample['image']
        sample['image'] = self.normalization.augment_image(sample['image'])

        return sample
Exemplo n.º 26
0
 def createBatch(self, bx, by, ids):
     if isinstance(by[0],list):
         r=imgaug.augmentables.Batch(data=[ids,by], images=bx)
         return r
     if len(by[0].shape)>1:
         return imgaug.augmentables.Batch(data=ids, images=bx,
                                    segmentation_maps=[imgaug.SegmentationMapOnImage(x, shape=x.shape) for x
                                                       in by])
     else:
         r=imgaug.augmentables.Batch(data=[ids,by], images=bx)
         return r
Exemplo n.º 27
0
    def __getitem__(self, index):
        # load image and crop
        img = Image.open(self.image_lists[index])

        img = np.array(img)  #转化为numpy格式
        labels = self.label_lists[index]

        #load label
        if self.mode != 'test':
            label = Image.open(self.label_lists[index])
            label = np.array(label)

            label[label == 255] = 1
            label[label == 190] = 2
            #            label[label==105]=3

            # label=np.argmax(label,axis=-1)
            # label[label!=1]=0
            # augment image and label

            if self.mode == 'train':

                seq_det = self.flip.to_deterministic()  #确定一个数据增强的序列
                segmap = ia.SegmentationMapOnImage(label,
                                                   shape=label.shape,
                                                   nb_classes=3)
                img = seq_det.augment_image(img)  #将方法应用在原图像上

                #                plt.imshow(img.astype(np.float32))#显示原图
                #                plt.show()

                label = seq_det.augment_segmentation_maps(
                    [segmap])[0].get_arr_int().astype(
                        np.uint8)  # 将方法应用在分割标签上,并且转换成np类型,这里尺度(256,512)


#                plt.imshow(label.astype(np.float32))
#                plt.show()

#            label=np.reshape(label,(1,)+label.shape)
#
#                plt.imshow(label.astype(np.float32))#显示label图片
#                plt.show()
#            label=torch.from_numpy(label.copy()).float()#二分类用float

            labels = torch.from_numpy(label.copy()).long()  #多分类label用long

        img = np.reshape(img, img.shape + (1, ))  # 如果输入是1通道需打开此注释 ******

        img = self.to_tensor(img.copy()).float()

        return img, labels
Exemplo n.º 28
0
    def __getitem__(self, index):
        seq_det = self.aug_seq.to_deterministic()

        color = Image.open(
            os.path.join(self.path, 'color-input',
                         self.sample_list[index] + '.png'))
        depth = Image.open(
            os.path.join(self.path, 'depth-input',
                         self.sample_list[index] + '.png'))
        label = Image.open(
            os.path.join(self.path, 'label', self.sample_list[index] + '.png'))

        if self.mode == 'train':
            color_img = np.asarray(color, dtype=np.float32) / 255
            color_img = seq_det.augment_image(color_img).clip(0.0, 1.0)
            color_img = self.normalize(self.to_tensor(color_img.copy()))

            depth_img = (np.asarray(depth, dtype=np.float64) / 8000).astype(
                np.float32)
            depth_img = np.stack([depth_img, depth_img, depth_img], axis=2)
            depth_img = seq_det.augment_image(depth_img).clip(0.0, 1.5)
            depth_img = self.normalize(self.to_tensor(depth_img.copy()))

            label = (np.asarray(label, dtype=np.float32) * 2 / 255).astype(
                np.uint8)
            label_segmap = ia.SegmentationMapOnImage(label,
                                                     shape=color_img.shape,
                                                     nb_classes=3)
            label_segmap = seq_det.augment_segmentation_maps([label_segmap])[0]
            label_img = Image.fromarray(
                (label_segmap.get_arr_int() * 255 / 2).astype(np.uint8))
            label_img = self.to_tensor(label_img.copy())

        elif self.mode == 'val':
            color_img = self.normalize(self.to_tensor(color))

            depth_img = (np.asarray(depth, dtype=np.float64) / 8000).astype(
                np.float32).clip(0.0, 1.5)
            depth_img = np.stack([depth_img, depth_img, depth_img], axis=2)
            depth_img = self.to_tensor(depth_img)
            depth_img = self.normalize(depth_img)

            label_img = self.to_tensor(label)

        label_img = torch.round(label_img * 2).long()
        if self.encode_label:
            label_img = torch.nn.functional.one_hot(label_img)
            label_img = label_img.permute(0, 3, 1, 2).squeeze()
        else:
            label_img = label_img.view(self.img_height, -1)
        return [color_img, depth_img], label_img
Exemplo n.º 29
0
    def __getitem__(self, idx):
        # at this point all transformations are applied and we expect to work with raw tensors
        image = np.array(io.imread(
            os.path.join(self.input_root, self.input_ids[idx])),
                         dtype=np.float32)
        mask = np.array(
            Image.open(os.path.join(self.target_root,
                                    self.target_ids[idx])).convert("L")) / 255
        ndsm = np.load(
            os.path.join(self.ndsm_root,
                         self.target_ids[idx]).replace(".png", ".npy"))
        ndsm = (ndsm + 1.5) / 22 * 255
        h, w, c = image.shape
        temp = np.zeros((h, w, c + 1))
        temp[:, :, :3] = image
        temp[:, :, 3] = ndsm
        image = temp

        if self.mode == "train":
            mask = np.reshape(mask, (h, w, 1))

            seq_det = self.transform1.to_deterministic()  #
            segmap = ia.SegmentationMapOnImage(mask,
                                               shape=mask.shape,
                                               nb_classes=2)
            image = seq_det.augment_image(image)
            mask = seq_det.augment_segmentation_maps(
                [segmap])[0].get_arr_int().astype(np.uint8)

            mask = np.reshape(mask, (h, w))
            image, mask = image.copy(), mask.copy()

            if self.weight:
                dwm = distranfwm(mask, beta=9)
                uwm = unetwm(mask)
                wm = 0.3 * dwm + 0.7 * uwm
                mask = self.mask_transform(mask)
                image = self.image_transform(image)
                wm = self.mask_transform(wm)

                return image, mask, wm
            else:
                mask = self.mask_transform(mask)
                image = self.image_transform(image)
                return image, mask
        else:
            image, mask = image.copy(), mask.copy()

            mask = self.mask_transform(mask)
            image = self.image_transform(image)
            return image, mask
Exemplo n.º 30
0
def test_SegmentationMapOnImage_bool():
    # Test for #189 (boolean mask inputs into SegmentationMapOnImage not working)
    arr = np.array([
        [0, 0, 0],
        [0, 1, 0],
        [0, 0, 0]
    ], dtype=bool)
    assert arr.dtype.type == np.bool_
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3))
    observed = segmap.get_arr_int()
    assert observed.dtype.type == np.int32
    assert np.array_equal(arr, observed)

    arr = np.array([
        [0, 0, 0],
        [0, 1, 0],
        [0, 0, 0]
    ], dtype=np.bool)
    assert arr.dtype.type == np.bool_
    segmap = ia.SegmentationMapOnImage(arr, shape=(3, 3))
    observed = segmap.get_arr_int()
    assert observed.dtype.type == np.int32
    assert np.array_equal(arr, observed)