Exemplo n.º 1
0
    def process_image(self, line, data_dir, mode):
        """ process_image """
        img1, img2, grt1, grt2, img1_name, img2_name, grt1_name, grt2_name = self.load_image(
            line, data_dir, mode=mode)
        grt1 = grt1 + 1 if grt1 is not None else None
        if mode == ModelPhase.TRAIN:
            img1, img2, grt1, grt2 = aug.resize(img1, img2, grt1, grt2, mode)
            img1, img2, grt1, grt2 = aug.rand_crop(
                img1, img2, grt1, grt2, mode=mode)
            if cfg.AUG.RICH_CROP.ENABLE:
                if cfg.AUG.RICH_CROP.BLUR:
                    if cfg.AUG.RICH_CROP.BLUR_RATIO <= 0:
                        n = 0
                    elif cfg.AUG.RICH_CROP.BLUR_RATIO >= 1:
                        n = 1
                    else:
                        n = int(1.0 / cfg.AUG.RICH_CROP.BLUR_RATIO)
                    if n > 0:
                        if np.random.randint(0, n) == 0:
                            radius = np.random.randint(3, 10)
                            if radius % 2 != 1:
                                radius = radius + 1
                            if radius > 9:
                                radius = 9
                            img1 = cv2.GaussianBlur(img1, (radius, radius), 0,
                                                    0)
                            if img2 is not None:
                                img2 = cv2.GaussianBlur(img2, (radius, radius),
                                                        0, 0)

                img1, img2, grt1, grt2 = aug.random_rotation(
                    img1,
                    img2,
                    grt1,
                    grt2,
                    rich_crop_max_rotation=cfg.AUG.RICH_CROP.MAX_ROTATION,
                    mean_value=cfg.DATASET.PADDING_VALUE)

                img1, img2, grt1, grt2 = aug.rand_scale_aspect(
                    img1,
                    img2,
                    grt1,
                    grt2,
                    rich_crop_min_scale=cfg.AUG.RICH_CROP.MIN_AREA_RATIO,
                    rich_crop_aspect_ratio=cfg.AUG.RICH_CROP.ASPECT_RATIO)

                img1, img2 = aug.hsv_color_jitter(
                    img1,
                    img2,
                    brightness_jitter_ratio=cfg.AUG.RICH_CROP.
                    BRIGHTNESS_JITTER_RATIO,
                    saturation_jitter_ratio=cfg.AUG.RICH_CROP.
                    SATURATION_JITTER_RATIO,
                    contrast_jitter_ratio=cfg.AUG.RICH_CROP.
                    CONTRAST_JITTER_RATIO)

            if cfg.AUG.RANDOM_ROTATION90:
                rot_k = np.random.randint(0, 4)
                img1 = np.rot90(img1, k=rot_k)
                img2 = np.rot90(img2, k=rot_k) if img2 is not None else None
                grt1 = np.rot90(grt1, k=rot_k)
                grt2 = np.rot90(grt2, k=rot_k) if grt2 is not None else None

            if cfg.AUG.FLIP:
                if cfg.AUG.FLIP_RATIO <= 0:
                    n = 0
                elif cfg.AUG.FLIP_RATIO >= 1:
                    n = 1
                else:
                    n = int(1.0 / cfg.AUG.FLIP_RATIO)
                if n > 0:
                    if np.random.randint(0, n) == 0:
                        img1 = img1[::-1, :, :]
                        img2 = img2[::-1, :, :] if img2 is not None else None
                        grt1 = grt1[::-1, :]
                        grt2 = grt2[::-1, :] if grt2 is not None else None

            if cfg.AUG.MIRROR:
                if np.random.randint(0, 2) == 1:
                    img1 = img1[:, ::-1, :]
                    img2 = img2[:, ::-1, :] if img2 is not None else None
                    grt1 = grt1[:, ::-1]
                    grt2 = grt2[:, ::-1] if grt2 is not None else None

        elif ModelPhase.is_eval(mode):
            img1, img2, grt1, grt2 = aug.resize(
                img1, img2, grt1, grt2, mode=mode)
            img1, img2, grt1, grt2 = aug.rand_crop(
                img1, img2, grt1, grt2, mode=mode)
            if cfg.TEST.TEST_AUG:
                img1 = self.test_aug(img1)
                img2 = self.test_aug(img2) if img2 is not None else None

        elif ModelPhase.is_visual(mode):
            org_shape = [img1.shape[0], img1.shape[1]]
            img1, img2, grt1, grt2 = aug.resize(
                img1, img2, grt1, grt2, mode=mode)
            valid_shape = [img1.shape[0], img1.shape[1]]
            img1, img2, grt1, grt2 = aug.rand_crop(
                img1, img2, grt1, grt2, mode=mode)
        else:
            raise ValueError("Dataset mode={} Error!".format(mode))

        # Normalize image
        img1 = self.normalize_image(img1)
        img2 = self.normalize_image(img2) if img2 is not None else None

        if grt2 is not None:
            grt = grt1 * cfg.DATASET.NUM_CLASSES + grt2

            unchange_idx = np.where((grt1 - grt2) == 0)
            grt[unchange_idx] = 0
            if cfg.DATASET.NUM_CLASSES == 2:
                grt[np.where(grt != 0)] = 1

            ignore_idx = np.where((grt1 == cfg.DATASET.IGNORE_INDEX)
                                  | (grt2 == cfg.DATASET.IGNORE_INDEX))
            grt[ignore_idx] = cfg.DATASET.IGNORE_INDEX
        else:
            grt = grt1

        if ModelPhase.is_train(mode) or ModelPhase.is_eval(mode):
            grt = np.expand_dims(np.array(grt).astype('int32'), axis=0)
            ignore = (grt != cfg.DATASET.IGNORE_INDEX).astype('int32')

        if cfg.DATASET.INPUT_IMAGE_NUM == 1:
            if ModelPhase.is_train(mode):
                return (img1, grt, ignore)
            elif ModelPhase.is_eval(mode):
                return (img1, grt, ignore)
            elif ModelPhase.is_visual(mode):
                return (img1, grt, img1_name, valid_shape, org_shape)
        else:
            if ModelPhase.is_train(mode):
                return (img1, img2, grt, ignore)
            elif ModelPhase.is_eval(mode):
                return (img1, img2, grt, ignore)
            elif ModelPhase.is_visual(mode):
                return (img1, img2, grt, img1_name, img2_name, valid_shape,
                        org_shape)
Exemplo n.º 2
0
    def process_image(self, line, data_dir, mode):
        """ process_image """
        img, grt, img_name, grt_name = self.load_image(
            line, data_dir, mode=mode)
        if mode == ModelPhase.TRAIN:
            img, grt = aug.resize(img, grt, mode)
            if cfg.AUG.RICH_CROP.ENABLE:
                if cfg.AUG.RICH_CROP.BLUR:
                    if cfg.AUG.RICH_CROP.BLUR_RATIO <= 0:
                        n = 0
                    elif cfg.AUG.RICH_CROP.BLUR_RATIO >= 1:
                        n = 1
                    else:
                        n = int(1.0 / cfg.AUG.RICH_CROP.BLUR_RATIO)
                    if n > 0:
                        if np.random.randint(0, n) == 0:
                            radius = np.random.randint(3, 10)
                            if radius % 2 != 1:
                                radius = radius + 1
                            if radius > 9:
                                radius = 9
                            img = cv2.GaussianBlur(img, (radius, radius), 0, 0)

                img, grt = aug.random_rotation(
                    img,
                    grt,
                    rich_crop_max_rotation=cfg.AUG.RICH_CROP.MAX_ROTATION,
                    mean_value=cfg.DATASET.PADDING_VALUE)

                img, grt = aug.rand_scale_aspect(
                    img,
                    grt,
                    rich_crop_min_scale=cfg.AUG.RICH_CROP.MIN_AREA_RATIO,
                    rich_crop_aspect_ratio=cfg.AUG.RICH_CROP.ASPECT_RATIO)
                img = aug.hsv_color_jitter(
                    img,
                    brightness_jitter_ratio=cfg.AUG.RICH_CROP.
                    BRIGHTNESS_JITTER_RATIO,
                    saturation_jitter_ratio=cfg.AUG.RICH_CROP.
                    SATURATION_JITTER_RATIO,
                    contrast_jitter_ratio=cfg.AUG.RICH_CROP.
                    CONTRAST_JITTER_RATIO)

            if cfg.AUG.FLIP:
                if cfg.AUG.FLIP_RATIO <= 0:
                    n = 0
                elif cfg.AUG.FLIP_RATIO >= 1:
                    n = 1
                else:
                    n = int(1.0 / cfg.AUG.FLIP_RATIO)
                if n > 0:
                    if np.random.randint(0, n) == 0:
                        img = img[::-1, :, :]
                        grt = grt[::-1, :]

            if cfg.AUG.MIRROR:
                if np.random.randint(0, 2) == 1:
                    img = img[:, ::-1, :]
                    grt = grt[:, ::-1]

            img, grt = aug.rand_crop(img, grt, mode=mode)
        elif ModelPhase.is_eval(mode):
            img, grt = aug.resize(img, grt, mode=mode)
            img, grt = aug.rand_crop(img, grt, mode=mode)
        elif ModelPhase.is_visual(mode):
            org_shape = [img.shape[0], img.shape[1]]
            img, grt = aug.resize(img, grt, mode=mode)
            valid_shape = [img.shape[0], img.shape[1]]
            img, grt = aug.rand_crop(img, grt, mode=mode)
        else:
            raise ValueError("Dataset mode={} Error!".format(mode))

        # Normalize image
        img = self.normalize_image(img)

        if ModelPhase.is_train(mode) or ModelPhase.is_eval(mode):
            grt = np.expand_dims(np.array(grt).astype('int32'), axis=0)
            ignore = (grt != cfg.DATASET.IGNORE_INDEX).astype('int32')

        if ModelPhase.is_train(mode):
            return (img, grt, ignore)
        elif ModelPhase.is_eval(mode):
            return (img, grt, ignore)
        elif ModelPhase.is_visual(mode):
            return (img, grt, img_name, valid_shape, org_shape)