Example #1
0
    def __init__(self,
                 img_height=300,
                 img_width=300,
                 background=(123, 117, 104),
                 labels_format={
                     'class_id': 0,
                     'xmin': 1,
                     'ymin': 2,
                     'xmax': 3,
                     'ymax': 4
                 }):
        '''
        Arguments:
            height (int): The desired height of the output images in pixels.
            width (int): The desired width of the output images in pixels.
            background (list/tuple, optional): A 3-tuple specifying the RGB color value of the
                background pixels of the translated images.
            labels_format (dict, optional): A dictionary that defines which index in the last axis of the labels
                of an image contains which bounding box coordinate. The dictionary maps at least the keywords
                'xmin', 'ymin', 'xmax', and 'ymax' to their respective indices within last axis of the labels array.
        '''

        self.labels_format = labels_format

        self.photometric_distortions = SSDPhotometricDistortions()
        self.expand = SSDExpand(background=background,
                                labels_format=self.labels_format)
        self.random_crop = SSDRandomCrop(labels_format=self.labels_format)
        self.random_flip = RandomFlip(dim='horizontal',
                                      prob=0.5,
                                      labels_format=self.labels_format)

        # This box filter makes sure that the resized images don't contain any degenerate boxes.
        # Resizing the images could lead the boxes to becomes smaller. For boxes that are already
        # pretty small, that might result in boxes with height and/or width zero, which we obviously
        # cannot allow.
        self.box_filter = BoxFilter(check_overlap=False,
                                    check_min_area=False,
                                    check_degenerate=True,
                                    labels_format=self.labels_format)

        self.resize = ResizeRandomInterp(height=img_height,
                                         width=img_width,
                                         interpolation_modes=[
                                             cv2.INTER_NEAREST,
                                             cv2.INTER_LINEAR, cv2.INTER_CUBIC,
                                             cv2.INTER_AREA, cv2.INTER_LANCZOS4
                                         ],
                                         box_filter=self.box_filter,
                                         labels_format=self.labels_format)

        self.sequence = [
            self.photometric_distortions, self.expand, self.random_crop,
            self.random_flip, self.resize
        ]
Example #2
0
    def __init__(self,
                 img_height=300,
                 img_width=300,
                 background=(123, 117, 104),
                 labels_format={
                     'class_id': 0,
                     'xmin': 1,
                     'ymin': 2,
                     'xmax': 3,
                     'ymax': 4
                 }):

        self.labels_format = labels_format

        self.photometric_distortions = SSDPhotometricDistortions()
        self.expand = SSDExpand(background=background,
                                labels_format=self.labels_format)
        self.random_crop = SSDRandomCrop(labels_format=self.labels_format)
        self.random_flip = RandomFlip(dim='horizontal',
                                      prob=0.5,
                                      labels_format=self.labels_format)

        # This box filter makes sure that the resized images don't contain any degenerate boxes.
        # Resizing the images could lead the boxes to becomes smaller. For boxes that are already
        # pretty small, that might result in boxes with height and/or width zero, which we obviously
        # cannot allow.
        self.box_filter = BoxFilter(check_overlap=False,
                                    check_min_area=False,
                                    check_degenerate=True,
                                    labels_format=self.labels_format)

        self.resize = ResizeRandomInterp(height=img_height,
                                         width=img_width,
                                         interpolation_modes=[
                                             cv2.INTER_NEAREST,
                                             cv2.INTER_LINEAR, cv2.INTER_CUBIC,
                                             cv2.INTER_AREA, cv2.INTER_LANCZOS4
                                         ],
                                         box_filter=self.box_filter,
                                         labels_format=self.labels_format)

        self.sequence = [
            self.photometric_distortions, self.expand, self.random_crop,
            self.random_flip, self.resize
        ]
    def __init__(self,
                 img_height=300,
                 img_width=300,
                 background=(123, 117, 104),
                 labels_format={
                     'class_id': 0,
                     'xmin': 1,
                     'ymin': 2,
                     'xmax': 3,
                     'ymax': 4
                 }):
        '''
        Arguments:
            height (int): The desired height of the output images in pixels.
            width (int): The desired width of the output images in pixels.
            background (list/tuple, optional): A 3-tuple specifying the RGB color value of the
                background pixels of the translated images.
            labels_format (dict, optional): A dictionary that defines which index in the last axis of the labels
                of an image contains which bounding box coordinate. The dictionary maps at least the keywords
                'xmin', 'ymin', 'xmax', and 'ymax' to their respective indices within last axis of the labels array.
        '''

        self.labels_format = labels_format

        self.photometric_distortions = SSDPhotometricDistortions()
        self.expand = SSDExpand(background=background,
                                labels_format=self.labels_format)
        self.random_crop = SSDRandomCrop(labels_format=self.labels_format)
        self.random_flip = RandomFlip(dim='horizontal',
                                      prob=0.5,
                                      labels_format=self.labels_format)
        self.resize = ResizeRandomInterp(height=img_height,
                                         width=img_width,
                                         interpolation_modes=[
                                             cv2.INTER_NEAREST,
                                             cv2.INTER_LINEAR, cv2.INTER_CUBIC,
                                             cv2.INTER_AREA, cv2.INTER_LANCZOS4
                                         ],
                                         labels_format=self.labels_format)

        self.sequence = [
            self.photometric_distortions, self.expand, self.random_crop,
            self.random_flip, self.resize
        ]
Example #4
0
    def __init__(self,
                 random_brightness=(-48, 48, 0.5),
                 random_contrast=(0.5, 1.8, 0.5),
                 random_saturation=(0.5, 1.8, 0.5),
                 random_hue=(18, 0.5),
                 random_flip=0.5,
                 random_translate=((0.03, 0.5), (0.03, 0.5), 0.5),
                 random_scale=(0.5, 2.0, 0.5),
                 n_trials_max=3,
                 clip_boxes=True,
                 overlap_criterion='area',
                 bounds_box_filter=(0.3, 1.0),
                 bounds_validator=(0.5, 1.0),
                 n_boxes_min=1,
                 background=(0, 0, 0),
                 labels_format={
                     'class_id': 0,
                     'xmin': 1,
                     'ymin': 2,
                     'xmax': 3,
                     'ymax': 4
                 }):

        if (random_scale[0] >= 1) or (random_scale[1] <= 1):
            raise ValueError(
                "This sequence of transformations only makes sense if the minimum scaling factor is <1 and the maximum scaling factor is >1."
            )

        self.n_trials_max = n_trials_max
        self.clip_boxes = clip_boxes
        self.overlap_criterion = overlap_criterion
        self.bounds_box_filter = bounds_box_filter
        self.bounds_validator = bounds_validator
        self.n_boxes_min = n_boxes_min
        self.background = background
        self.labels_format = labels_format

        # 图像变换之后保留哪些boxes
        self.box_filter = BoxFilter(check_overlap=True,
                                    check_min_area=True,
                                    check_degenerate=True,
                                    overlap_criterion=self.overlap_criterion,
                                    overlap_bounds=self.bounds_box_filter,
                                    min_area=16,
                                    labels_format=self.labels_format)

        # 训练图像是否有效
        self.image_validator = ImageValidator(
            overlap_criterion=self.overlap_criterion,
            bounds=self.bounds_validator,
            n_boxes_min=self.n_boxes_min,
            labels_format=self.labels_format)

        # Utility distortions
        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        self.convert_to_3_channels = ConvertTo3Channels()  # 确保所有图像3通道

        # Photometric transformations
        self.random_brightness = RandomBrightness(lower=random_brightness[0],
                                                  upper=random_brightness[1],
                                                  prob=random_brightness[2])
        self.random_contrast = RandomContrast(lower=random_contrast[0],
                                              upper=random_contrast[1],
                                              prob=random_contrast[2])
        self.random_saturation = RandomSaturation(lower=random_saturation[0],
                                                  upper=random_saturation[1],
                                                  prob=random_saturation[2])
        self.random_hue = RandomHue(max_delta=random_hue[0],
                                    prob=random_hue[1])

        # Geometric transformations
        self.random_flip = RandomFlip(dim='horizontal',
                                      prob=random_flip,
                                      labels_format=self.labels_format)
        self.random_translate = RandomTranslate(
            dy_minmax=random_translate[0],
            dx_minmax=random_translate[1],
            prob=random_translate[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)
        self.random_zoom_in = RandomScale(min_factor=1.0,
                                          max_factor=random_scale[1],
                                          prob=random_scale[2],
                                          clip_boxes=self.clip_boxes,
                                          box_filter=self.box_filter,
                                          image_validator=self.image_validator,
                                          n_trials_max=self.n_trials_max,
                                          background=self.background,
                                          labels_format=self.labels_format)
        self.random_zoom_out = RandomScale(
            min_factor=random_scale[0],
            max_factor=1.0,
            prob=random_scale[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)

        # 放大
        self.sequence1 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.random_translate, self.random_zoom_in, self.random_flip
        ]

        # 缩小
        self.sequence2 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.convert_to_uint8,
            self.convert_RGB_to_HSV, self.convert_to_float32,
            self.random_saturation, self.random_hue, self.convert_to_uint8,
            self.convert_HSV_to_RGB, self.convert_to_float32,
            self.random_contrast, self.convert_to_uint8, self.random_zoom_out,
            self.random_translate, self.random_flip
        ]
    def __init__(self,
                 resize_height,
                 resize_width,
                 random_brightness=(-48, 48, 0.5),
                 random_contrast=(0.5, 1.8, 0.5),
                 random_saturation=(0.5, 1.8, 0.5),
                 random_hue=(18, 0.5),
                 random_flip=0.5,
                 min_scale=0.3,
                 max_scale=2.0,
                 min_aspect_ratio=0.5,
                 max_aspect_ratio=2.0,
                 n_trials_max=3,
                 clip_boxes=True,
                 overlap_criterion='area',
                 bounds_box_filter=(0.3, 1.0),
                 bounds_validator=(0.5, 1.0),
                 n_boxes_min=1,
                 background=(0, 0, 0),
                 labels_format={'class_id': 0, 'xmin': 1, 'ymin': 2, 'xmax': 3, 'ymax': 4}):

        self.n_trials_max = n_trials_max
        self.clip_boxes = clip_boxes
        self.overlap_criterion = overlap_criterion
        self.bounds_box_filter = bounds_box_filter
        self.bounds_validator = bounds_validator
        self.n_boxes_min = n_boxes_min
        self.background = background
        self.labels_format = labels_format

        # Determines which boxes are kept in an image after the transformations have been applied.
        self.box_filter_patch = BoxFilter(check_overlap=True,
                                          check_min_area=False,
                                          check_degenerate=False,
                                          overlap_criterion=self.overlap_criterion,
                                          overlap_bounds=self.bounds_box_filter,
                                          labels_format=self.labels_format)

        self.box_filter_resize = BoxFilter(check_overlap=False,
                                           check_min_area=True,
                                           check_degenerate=True,
                                           min_area=16,
                                           labels_format=self.labels_format)

        # Determines whether the result of the transformations is a valid training image.
        self.image_validator = ImageValidator(overlap_criterion=self.overlap_criterion,
                                              bounds=self.bounds_validator,
                                              n_boxes_min=self.n_boxes_min,
                                              labels_format=self.labels_format)

        # Utility transformations
        self.convert_to_3_channels = ConvertTo3Channels()  # Make sure all images end up having 3 channels.
        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        self.resize = Resize(height=resize_height,
                             width=resize_width,
                             box_filter=self.box_filter_resize,
                             labels_format=self.labels_format)

        # Photometric transformations
        self.random_brightness = RandomBrightness(lower=random_brightness[0], upper=random_brightness[1],
                                                  prob=random_brightness[2])
        self.random_contrast = RandomContrast(lower=random_contrast[0], upper=random_contrast[1],
                                              prob=random_contrast[2])
        self.random_saturation = RandomSaturation(lower=random_saturation[0], upper=random_saturation[1],
                                                  prob=random_saturation[2])
        self.random_hue = RandomHue(max_delta=random_hue[0], prob=random_hue[1])

        # Geometric transformations
        self.random_flip = RandomFlip(dim='horizontal', prob=random_flip, labels_format=self.labels_format)
        self.patch_coord_generator = PatchCoordinateGenerator(must_match='w_ar',
                                                              min_scale=min_scale,
                                                              max_scale=max_scale,
                                                              scale_uniformly=False,
                                                              min_aspect_ratio=min_aspect_ratio,
                                                              max_aspect_ratio=max_aspect_ratio)
        self.random_patch = RandomPatch(patch_coord_generator=self.patch_coord_generator,
                                        box_filter=self.box_filter_patch,
                                        image_validator=self.image_validator,
                                        n_trials_max=self.n_trials_max,
                                        clip_boxes=self.clip_boxes,
                                        prob=1.0,
                                        can_fail=False,
                                        labels_format=self.labels_format)

        # Define the processing chain
        self.transformations = [self.convert_to_3_channels,
                                self.convert_to_float32,
                                self.random_brightness,
                                self.random_contrast,
                                self.convert_to_uint8,
                                self.convert_RGB_to_HSV,
                                self.convert_to_float32,
                                self.random_saturation,
                                self.random_hue,
                                self.convert_to_uint8,
                                self.convert_HSV_to_RGB,
                                self.random_patch,
                                self.random_flip,
                                self.resize]
Example #6
0
    def __init__(self,
                 resize_height,
                 resize_width,
                 random_brightness=(-20, 20, 0.5),
                 random_contrast=(0.8, 1.0, 0.5),
                 random_saturation=(0.8, 1.8, 0.5),
                 random_hue=(10, 0.5),
                 random_flip=0.5,
                 random_rotate_small=([np.pi / 40, np.pi / 30], 0.5),
                 random_rotate_big=([np.pi / 2, np.pi, 3 * np.pi / 2], 0.5),
                 min_scale=0.8,
                 max_scale=1.05,
                 min_aspect_ratio=0.8,
                 max_aspect_ratio=1.2,
                 n_trials_max=3,
                 overlap_criterion='center_point',
                 bounds_box_filter=(0.3, 1.0),
                 bounds_validator=(0.5, 1.0),
                 n_boxes_min=1,
                 random_translate=((0.03, 0.05), (0.03, 0.05), 0.5),
                 random_scale=(0.9, 1.1, 0.5),
                 proba_no_aug=1 / 3):

        self.n_trials_max = n_trials_max
        self.overlap_criterion = overlap_criterion
        self.bounds_box_filter = bounds_box_filter
        self.bounds_validator = bounds_validator
        self.n_boxes_min = n_boxes_min

        self.proba_no_aug = proba_no_aug  # the probability of not performing any transformations

        # Determines which boxes are kept in an image after the transformations have been applied.
        self.box_filter = BoxFilter(check_overlap=True,
                                    check_min_area=False,
                                    check_degenerate=False,
                                    overlap_criterion=self.overlap_criterion,
                                    overlap_bounds=self.bounds_box_filter)

        self.box_filter_resize = BoxFilter(check_overlap=False,
                                           check_min_area=True,
                                           check_degenerate=True,
                                           min_area=16)

        # Determines whether the result of the transformations is a valid training image.
        self.image_validator = ImageValidator(
            overlap_criterion=self.overlap_criterion,
            bounds=self.bounds_validator,
            n_boxes_min=self.n_boxes_min)

        # Utility transformations
        self.convert_to_3_channels = ConvertTo3Channels(
        )  # Make sure all images end up having 3 channels.
        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        self.resize = Resize(height=resize_height,
                             width=resize_width,
                             box_filter=self.box_filter_resize)

        # Photometric transformations
        self.random_brightness = RandomBrightness(lower=random_brightness[0],
                                                  upper=random_brightness[1],
                                                  prob=random_brightness[2])
        self.random_contrast = RandomContrast(lower=random_contrast[0],
                                              upper=random_contrast[1],
                                              prob=random_contrast[2])
        self.random_saturation = RandomSaturation(lower=random_saturation[0],
                                                  upper=random_saturation[1],
                                                  prob=random_saturation[2])
        self.random_hue = RandomHue(max_delta=random_hue[0],
                                    prob=random_hue[1])

        # Geometric transformations
        self.random_horizontal_flip = RandomFlip(dim='horizontal',
                                                 prob=random_flip)
        self.random_vertical_flip = RandomFlip(dim='vertical',
                                               prob=random_flip)
        self.random_translate = RandomTranslate(
            dy_minmax=random_translate[0],
            dx_minmax=random_translate[1],
            prob=random_translate[2],
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max)

        self.random_rotate_small = RandomRotate(
            angles=random_rotate_small[0],
            prob=random_rotate_small[1],
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max)

        self.random_rotate_big = RandomRotate(
            angles=random_rotate_big[0],
            prob=random_rotate_big[1],
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max)

        self.random_zoom_in = RandomScale(min_factor=1.0,
                                          max_factor=random_scale[1],
                                          prob=random_scale[2],
                                          box_filter=self.box_filter,
                                          image_validator=self.image_validator,
                                          n_trials_max=self.n_trials_max)

        self.random_zoom_out = RandomScale(
            min_factor=random_scale[0],
            max_factor=random_scale[0],
            prob=random_scale[2],
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max)

        # random patch generator is not used for the moment but it could be useful in your project
        self.patch_coord_generator = PatchCoordinateGenerator(
            must_match='h_w',
            min_scale=min_scale,
            max_scale=max_scale,
            scale_uniformly=False,
            min_aspect_ratio=min_aspect_ratio,
            max_aspect_ratio=max_aspect_ratio)

        self.random_patch = RandomPatch(
            patch_coord_generator=self.patch_coord_generator,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            prob=0.5,
            can_fail=False)

        # If we zoom in, do translation before scaling.
        self.sequence1 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.random_horizontal_flip, self.random_vertical_flip,
            self.random_translate, self.random_rotate_big,
            self.random_rotate_small, self.random_zoom_in, self.random_patch,
            self.resize
        ]

        # If we zoom out, do translation after scaling.
        self.sequence2 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.random_horizontal_flip, self.random_vertical_flip,
            self.random_zoom_out, self.random_translate,
            self.random_rotate_big, self.random_rotate_small,
            self.random_patch, self.resize
        ]

        self.sequence3 = [
            self.convert_to_3_channels, self.convert_to_uint8,
            self.random_horizontal_flip, self.random_vertical_flip,
            self.random_translate, self.random_rotate_big,
            self.random_rotate_small, self.resize
        ]
    def __init__(
        self,
        random_brightness=(-48, 48, 0.5),
        random_contrast=(0.5, 1.8, 0.5),
        random_saturation=(0.5, 1.8, 0.5),
        random_hue=(18, 0.5),
        random_flip=0.5,
        # 最后一个元素表示 prob
        random_translate=((0.03, 0.5), (0.03, 0.5), 0.5),
        # 最后一个元素表示 prob
        random_scale=(0.5, 2.0, 0.5),
        # translate or scale 后的 image 如果不合格可以重复进行的最大次数
        n_trials_max=3,
        clip_boxes=True,
        overlap_criterion_box_filter='area',
        overlap_criterion_validator='area',
        bounds_box_filter=(0.3, 1.0),
        bounds_validator=(0.5, 1.0),
        n_boxes_min=1,
        background=(0, 0, 0),
        labels_format=('class_id', 'xmin', 'ymin', 'xmax', 'ymax')):

        if (random_scale[0] >= 1) or (random_scale[1] <= 1):
            raise ValueError(
                "This sequence of transformations only makes sense"
                "if the minimum scaling factor is <1 and the maximum scaling factor is >1."
            )
        self.n_trials_max = n_trials_max
        self.clip_boxes = clip_boxes
        self.overlap_criterion_box_filter = overlap_criterion_box_filter
        self.overlap_criterion_validator = overlap_criterion_validator
        self.bounds_box_filter = bounds_box_filter
        self.bounds_validator = bounds_validator
        self.n_boxes_min = n_boxes_min
        self.background = background
        self.labels_format = labels_format

        # Determines which boxes are kept in an image after the transformations have been applied.
        self.box_filter = BoxFilter(
            check_overlap=True,
            check_min_area=True,
            check_degenerate=True,
            overlap_criterion=self.overlap_criterion_box_filter,
            overlap_bounds=self.bounds_box_filter,
            min_area=16,
            labels_format=self.labels_format)

        # Determines whether the result of the transformations is a valid training image.
        self.image_validator = ImageValidator(
            overlap_criterion=self.overlap_criterion_validator,
            overlap_bounds=self.bounds_validator,
            n_boxes_min=self.n_boxes_min,
            labels_format=self.labels_format)

        # Utility distortions
        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        # Make sure all images end up having 3 channels.
        self.convert_to_3_channels = ConvertTo3Channels()

        # Photometric transformations
        self.random_brightness = RandomBrightness(lower=random_brightness[0],
                                                  upper=random_brightness[1],
                                                  prob=random_brightness[2])
        self.random_contrast = RandomContrast(lower=random_contrast[0],
                                              upper=random_contrast[1],
                                              prob=random_contrast[2])
        self.random_saturation = RandomSaturation(lower=random_saturation[0],
                                                  upper=random_saturation[1],
                                                  prob=random_saturation[2])
        self.random_hue = RandomHue(max_delta=random_hue[0],
                                    prob=random_hue[1])

        # Geometric transformations
        self.random_flip = RandomFlip(dim='horizontal',
                                      prob=random_flip,
                                      labels_format=self.labels_format)
        self.random_translate = RandomTranslate(
            dy_minmax=random_translate[0],
            dx_minmax=random_translate[1],
            prob=random_translate[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)
        self.random_zoom_in = RandomScale(min_factor=1.0,
                                          max_factor=random_scale[1],
                                          prob=random_scale[2],
                                          clip_boxes=self.clip_boxes,
                                          box_filter=self.box_filter,
                                          image_validator=self.image_validator,
                                          n_trials_max=self.n_trials_max,
                                          background=self.background,
                                          labels_format=self.labels_format)
        self.random_zoom_out = RandomScale(
            min_factor=random_scale[0],
            max_factor=1.0,
            prob=random_scale[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)

        # If we zoom in, do translation before scaling.
        self.sequence1 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.random_translate, self.random_zoom_in, self.random_flip
        ]

        # If we zoom out, do scaling before translation.
        self.sequence2 = [
            self.convert_to_3_channels, self.convert_to_float32,
            self.random_brightness, self.random_contrast,
            self.convert_to_uint8, self.convert_RGB_to_HSV,
            self.convert_to_float32, self.random_saturation, self.random_hue,
            self.convert_to_uint8, self.convert_HSV_to_RGB,
            self.convert_to_float32, self.random_zoom_out,
            self.random_translate, self.random_flip
        ]
Example #8
0
    def __init__(
        self,
        random_brightness=(-48, 48, 0.5),
        random_contrast=(0.5, 1.8, 0.5),
        random_saturation=(0.5, 1.8, 0.5),
        random_hue=(18, 0.5),
        random_flip=0.5,
        random_translate=((0.03, 0.5), (0.03, 0.5), 0.5),
        random_scale=(0.5, 2.0, 0.5),
        random_gaussian_noise=(0.5, 0., 10),  # gaussine noise
        random_poisson_noise=(0.5, 60),  # poisson noise
        random_salt_pepper_noise=(0.5, 0.5,
                                  0.005),  # salt&pepper or impalse noise 
        random_row_defect=(0.5, 1),  # row defect
        random_col_defect=(0.5, 1),  # col defect
        n_trials_max=3,
        clip_boxes=True,
        overlap_criterion='area',
        bounds_box_filter=(0.3, 1.0),
        bounds_validator=(0.5, 1.0),
        n_boxes_min=1,
        background=(0, 0, 0),
        labels_format={
            'class_id': 0,
            'xmin': 1,
            'ymin': 2,
            'xmax': 3,
            'ymax': 4
        }):

        if (random_scale[0] >= 1) or (random_scale[1] <= 1):
            raise ValueError(
                "This sequence of transformations only makes sense if the minimum scaling factor is <1 and the maximum scaling factor is >1."
            )

        self.n_trials_max = n_trials_max
        self.clip_boxes = clip_boxes
        self.overlap_criterion = overlap_criterion
        self.bounds_box_filter = bounds_box_filter
        self.bounds_validator = bounds_validator
        self.n_boxes_min = n_boxes_min
        self.background = background
        self.labels_format = labels_format

        # Determines which boxes are kept in an image after the transformations have been applied.
        self.box_filter = BoxFilter(check_overlap=True,
                                    check_min_area=True,
                                    check_degenerate=True,
                                    overlap_criterion=self.overlap_criterion,
                                    overlap_bounds=self.bounds_box_filter,
                                    min_area=16,
                                    labels_format=self.labels_format)

        # Determines whether the result of the transformations is a valid training image.
        self.image_validator = ImageValidator(
            overlap_criterion=self.overlap_criterion,
            bounds=self.bounds_validator,
            n_boxes_min=self.n_boxes_min,
            labels_format=self.labels_format)

        # Utility distortions
        self.convert_RGB_to_HSV = ConvertColor(current='RGB', to='HSV')
        self.convert_HSV_to_RGB = ConvertColor(current='HSV', to='RGB')
        self.convert_to_float32 = ConvertDataType(to='float32')
        self.convert_to_uint8 = ConvertDataType(to='uint8')
        self.convert_to_3_channels = ConvertTo3Channels(
        )  # Make sure all images end up having 3 channels.
        self.convert_to_1_channel = ConvertTo1Channel(
        )  # Make sure all images end up having 3 channels.

        # Photometric transformations
        self.random_brightness = RandomBrightness(lower=random_brightness[0],
                                                  upper=random_brightness[1],
                                                  prob=random_brightness[2])
        self.random_contrast = RandomContrast(lower=random_contrast[0],
                                              upper=random_contrast[1],
                                              prob=random_contrast[2])
        self.random_saturation = RandomSaturation(lower=random_saturation[0],
                                                  upper=random_saturation[1],
                                                  prob=random_saturation[2])
        self.random_hue = RandomHue(max_delta=random_hue[0],
                                    prob=random_hue[1])

        # Geometric transformations
        self.random_flip = RandomFlip(dim='horizontal',
                                      prob=random_flip,
                                      labels_format=self.labels_format)
        self.random_translate = RandomTranslate(
            dy_minmax=random_translate[0],
            dx_minmax=random_translate[1],
            prob=random_translate[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)
        self.random_zoom_in = RandomScale(min_factor=1.0,
                                          max_factor=random_scale[1],
                                          prob=random_scale[2],
                                          clip_boxes=self.clip_boxes,
                                          box_filter=self.box_filter,
                                          image_validator=self.image_validator,
                                          n_trials_max=self.n_trials_max,
                                          background=self.background,
                                          labels_format=self.labels_format)
        self.random_zoom_out = RandomScale(
            min_factor=random_scale[0],
            max_factor=1.0,
            prob=random_scale[2],
            clip_boxes=self.clip_boxes,
            box_filter=self.box_filter,
            image_validator=self.image_validator,
            n_trials_max=self.n_trials_max,
            background=self.background,
            labels_format=self.labels_format)

        # noises and sensor defects
        self.random_RowDefect = RandomRowDefect(prob=random_row_defect[0],
                                                thikness=random_row_defect[1])
        self.random_col_defect = RandomColDefect(prob=random_col_defect[0],
                                                 thikness=random_col_defect[1])
        self.random_salt_pepper = RandomSaltPepperNoise(
            prob=random_salt_pepper_noise[0],
            salt_vs_pepper_ratio=random_salt_pepper_noise[1],
            percentage=random_salt_pepper_noise[2])
        self.random_poisson = RandomPoissonNoise(
            prob=random_poisson_noise[0], Lambda=random_poisson_noise[1])
        self.random_gaussian = RandomGaussianNoise(
            prob=random_gaussian_noise[0],
            mean=random_gaussian_noise[1],
            sigma=random_gaussian_noise[2])

        # If we zoom in, do translation before scaling.
        self.sequence1 = [
            self.convert_to_1_channel,
            self.convert_to_float32,
            self.random_brightness,
            self.random_contrast,
            #                          self.convert_to_uint8,
            #                          self.convert_RGB_to_HSV,
            #                          self.convert_to_float32,
            #                          self.random_saturation,
            #                          self.random_hue,
            self.convert_to_uint8,
            #                          self.convert_HSV_to_RGB,
            self.random_translate,
            self.random_zoom_in,
            self.random_flip,
            self.random_salt_pepper,
            self.random_poisson,
            self.random_gaussian,
            self.random_col_defect,
            self.convert_to_1_channel
        ]

        # If we zoom out, do scaling before translation.
        self.sequence2 = [
            self.convert_to_1_channel,
            self.convert_to_float32,
            self.random_brightness,
            #                          self.convert_to_uint8,
            #                          self.convert_RGB_to_HSV,
            #                          self.convert_to_float32,
            #                          self.random_saturation,
            #                          self.random_hue, #
            #                          self.convert_to_uint8,
            #                          self.convert_HSV_to_RGB,
            self.convert_to_float32,
            self.random_contrast,
            self.convert_to_uint8,
            self.random_zoom_out,
            self.random_translate,
            self.random_flip,
            self.random_salt_pepper,
            self.random_poisson,
            self.random_gaussian,
            self.random_col_defect,
            self.convert_to_1_channel
        ]
                                    aspect_ratios_per_layer=aspect_ratios,
                                    two_boxes_for_ar1=two_boxes_for_ar1,
                                    steps=steps,
                                    offsets=offsets,
                                    clip_boxes=clip_boxes,
                                    variances=variances,
                                    matching_type='multi',
                                    pos_iou_threshold=0.5,
                                    neg_iou_limit=0.5,
                                    normalize_coords=normalize_coords)

# Create Transformations
convert_to_3_channels = ConvertTo3Channels()
convert_to_uint8 = ConvertDataType(to='uint8')
resize = Resize(height=img_height, width=img_width)
random_flip_hor = RandomFlip(dim='horizontal', prob=0.5)
random_flip_ver = RandomFlip(dim='vertical', prob=0.5)

ssd_expand = SSDExpand()

box_filter = BoxFilter(overlap_criterion='area', overlap_bounds=(0.4, 1.0))
image_validator = ImageValidator(overlap_criterion='area',
                                 bounds=(0.3, 1.0),
                                 n_boxes_min=1)
random_translate = RandomTranslate(dy_minmax=(0.03, 0.3),
                                   dx_minmax=(0.03, 0.3),
                                   prob=0.5,
                                   clip_boxes=False,
                                   box_filter=None,
                                   image_validator=image_validator,
                                   n_trials_max=3)