def __getitem__(self, idx):
        """
        Gets batch at position `index`.
        Arguments:
            index: position of the batch in the Sequence.
        Returns:
            A batch
        """
        indexes = self.indexes[idx * self.batch_size: (idx + 1) * self.batch_size]

        image_batch = self.images[indexes]

        image_batch = self.__load_image(image_batch)

        if self.resize_shape:
            # Resize images interpolation choice
            # ``nearest`` (identical to ``cv2.INTER_NEAREST``)
            # ``linear`` (identical to ``cv2.INTER_LINEAR``)
            # ``area`` (identical to ``cv2.INTER_AREA``)
            # ``cubic`` (identical to ``cv2.INTER_CUBIC``)
            image_batch = ia.imresize_many_images(image_batch, self.resize_shape, interpolation='linear')

        label_batch = image_batch
        aug_batch = image_batch
        if self.augment == True:
            label_batch = self.aug_seq.augment_images(image_batch)

            # aug_batch = random_mask.augment_images(label_batch)


        return label_batch,label_batch
Exemplo n.º 2
0
    def __getitem__(self, idx):
        """
        Gets batch at position `index`.
        Arguments:
            index: position of the batch in the Sequence.
        Returns:
            A batch
        """
        indexes = self.indexes[idx * self.batch_size:(idx + 1) *
                               self.batch_size]

        image_batch = self.images[indexes]

        image_batch = self.__cache_data(indexes, image_batch)

        # One-hot label
        label_batch = np_utils.to_categorical(self.labels[indexes],
                                              self.class_num)

        if self.resize_shape:
            # Resize images interpolation choice
            # ``nearest`` (identical to ``cv2.INTER_NEAREST``)
            # ``linear`` (identical to ``cv2.INTER_LINEAR``)
            # ``area`` (identical to ``cv2.INTER_AREA``)
            # ``cubic`` (identical to ``cv2.INTER_CUBIC``)
            image_batch = ia.imresize_many_images(image_batch,
                                                  self.resize_shape,
                                                  interpolation='linear')
            image_batch = image_batch

        if self.augment == True:
            image_batch = self.aug_seq.augment_images(image_batch)

        return image_batch, label_batch
Exemplo n.º 3
0
    def encode(self, item, encode_y=False, treshold=0.5):
        if isinstance(item, PredictionItem):
            raise NotImplementedError(
                "Instance segmentation is only capable to encode datasets")
        if isinstance(item, DataSet):
            res = []
            for i in tqdm.tqdm(range(len(item)), "Encoding dataset"):
                q = item[i]
                imageId = q.id
                if encode_y:
                    vl = q.y
                else:
                    vl = q.prediction
                labels = vl[0]
                probs = vl[1]
                masks = vl[3]
                if masks.shape[1] != self.maskShape[0] or masks.shape[
                        2] != self.maskShape[1]:
                    masks = imgaug.imresize_many_images(
                        masks.astype(np.uint16), self.maskShape,
                        cv.INTER_CUBIC)
                if len(labels) != len(masks):
                    raise Exception(
                        f"{imageId} does not have same ammount of masks and labels"
                    )
                for i in range(len(masks)):
                    if probs[i] < treshold:
                        continue
                    mask = masks[i]
                    label = str(int(labels[i] + 0.5))
                    if label in self.classes:
                        rle = self._to_rle(mask)
                        res.append({
                            self.imColumn: imageId,
                            self.rleColumn: rle,
                            self.clazzColumn: label
                        })
            res = self._recode(res)

            clns = []
            for c in self.splitColumns:
                if not self.splitColumns[c] in clns:
                    clns.append(self.splitColumns[c])
            r = [self.imColumn, self.clazzColumn, self.rleColumn]
            for c in r:
                if not c in self.splitColumns:
                    clns.append(c)
            return pd.DataFrame(res, columns=clns)
Exemplo n.º 4
0
def main():
    parser = argparse.ArgumentParser(description="Contrast check script")
    parser.add_argument("--per_channel", dest="per_channel", action="store_true")
    args = parser.parse_args()

    augs = []
    for p in [0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("GammaContrast " + str(p), iaa.GammaContrast(p, per_channel=args.per_channel)))

    for cutoff in [0.25, 0.5, 0.75]:
        for gain in [5, 10, 15, 20, 25]:
            augs.append(("SigmoidContrast " + str(cutoff) + " " + str(gain), iaa.SigmoidContrast(gain, cutoff, per_channel=args.per_channel)))

    for gain in [0.0, 0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LogContrast " + str(gain), iaa.LogContrast(gain, per_channel=args.per_channel)))

    for alpha in [-1.0, 0.5, 0, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LinearContrast " + str(alpha), iaa.LinearContrast(alpha, per_channel=args.per_channel)))

    augs.append(("AllChannelsHistogramEqualization", iaa.AllChannelsHistogramEqualization()))
    augs.append(("HistogramEqualization (Lab)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.Lab)))
    augs.append(("HistogramEqualization (HSV)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.HSV)))
    augs.append(("HistogramEqualization (HLS)", iaa.HistogramEqualization(to_colorspace=iaa.HistogramEqualization.HLS)))

    for clip_limit in [0.1, 1, 5, 10]:
        for tile_grid_size_px in [3, 7]:
            augs.append(("AllChannelsCLAHE %d %dx%d" % (clip_limit, tile_grid_size_px, tile_grid_size_px),
                         iaa.AllChannelsCLAHE(clip_limit=clip_limit, tile_grid_size_px=tile_grid_size_px,
                                              per_channel=args.per_channel)))

    for clip_limit in [1, 5, 10, 100, 200]:
        for tile_grid_size_px in [3, 7, 15]:
            augs.append(("CLAHE %d %dx%d" % (clip_limit, tile_grid_size_px, tile_grid_size_px),
                         iaa.CLAHE(clip_limit=clip_limit, tile_grid_size_px=tile_grid_size_px)))

    images = [data.astronaut()] * 16
    images = ia.imresize_many_images(np.uint8(images), (128, 128))
    for name, aug in augs:
        print("-----------")
        print(name)
        print("-----------")
        images_aug = aug.augment_images(images)
        images_aug[0] = images[0]
        grid = ia.draw_grid(images_aug, rows=4, cols=4)
        ia.imshow(grid)
Exemplo n.º 5
0
    def __getitem__(self, idx):
        """
        Gets batch at position `index`.
        Arguments:
            index: position of the batch in the Sequence.
        Returns:
            A batch
        """
        indexes = self.indexes[idx * self.batch_size:(idx + 1) *
                               self.batch_size]

        image_batch = self.images[indexes]

        image_batch = self.__cache_data(indexes, image_batch)

        # One-hot label, E.g. if self.class_num = 5
        # Before: [[1,2,...],[3,4,...]...]
        # After: [[[0,1,0,0,0],[0,0,1,0,0]...] , [[0,0,0,1,0],[0,0,0,0,1]...]...]
        label_batch = np_utils.to_categorical(self.labels[indexes],
                                              self.class_num)

        # For the multi label fixed-length OCR problem, the label shape must be [BATCH,FIXED_LENGTH*CLASS]
        label_batch = np.reshape(label_batch,
                                 (-1, self.fixed_length * self.class_num))

        if self.resize_shape:
            # Resize images interpolation choice
            # ``nearest`` (identical to ``cv2.INTER_NEAREST``)
            # ``linear`` (identical to ``cv2.INTER_LINEAR``)
            # ``area`` (identical to ``cv2.INTER_AREA``)
            # ``cubic`` (identical to ``cv2.INTER_CUBIC``)
            image_batch = ia.imresize_many_images(image_batch,
                                                  self.resize_shape,
                                                  interpolation='linear')
            image_batch = image_batch

        if self.augment == True:
            image_batch = self.aug_seq.augment_images(image_batch)

        return image_batch, label_batch
Exemplo n.º 6
0
def main():
    parser = argparse.ArgumentParser(description="Contrast check script")
    parser.add_argument("--per_channel",
                        dest="per_channel",
                        action="store_true")
    args = parser.parse_args()

    augs = []
    for p in [0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("GammaContrast " + str(p),
                     iaa.GammaContrast(p, per_channel=args.per_channel)))

    for cutoff in [0.25, 0.5, 0.75]:
        for gain in [5, 10, 15, 20, 25]:
            augs.append(("SigmoidContrast " + str(cutoff) + " " + str(gain),
                         iaa.SigmoidContrast(gain,
                                             cutoff,
                                             per_channel=args.per_channel)))

    for gain in [-1.0, 0.0, 0.25, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LogContrast " + str(gain),
                     iaa.LogContrast(gain, per_channel=args.per_channel)))

    for alpha in [-1.0, 0.5, 0, 0.5, 1.0, 2.0, (0.5, 1.5), [0.5, 1.0, 1.5]]:
        augs.append(("LinearContrast " + str(alpha),
                     iaa.LinearContrast(alpha, per_channel=args.per_channel)))

    images = [data.astronaut()] * 16
    images = ia.imresize_many_images(np.uint8(images), (128, 128))
    for name, aug in augs:
        print("-----------")
        print(name)
        print("-----------")
        images_aug = aug.augment_images(images)
        grid = ia.draw_grid(images_aug, rows=4, cols=4)
        ia.imshow(grid)