예제 #1
0
    def produce_target(self, holder):
        alig_data = []

        if self.scale_range is not None:
            max_shape = [
                random.randint(*self.scale_range),
                random.randint(*self.scale_range)
            ]

            max_shape[0] = int(
                np.ceil(max_shape[0] / self.divide_size) * self.divide_size)
            max_shape[1] = int(
                np.ceil(max_shape[1] / self.divide_size) * self.divide_size)

            anchor_tools.reset_anchors((max_shape[1], max_shape[0]))
        else:
            max_shape = self.input_size

        # copy images to the upper left part of the image batch object
        for [image, boxes_, klass_] in holder:

            try:
                # construct an image batch object
                image, shift_x, shift_y = Fill_img(image,
                                                   target_width=max_shape[0],
                                                   target_height=max_shape[1])
                boxes_[:, 0:4] = boxes_[:, 0:4] + np.array(
                    [shift_x, shift_y, shift_x, shift_y], dtype='float32')
                h, w, _ = image.shape
                boxes_[:, 0] /= w
                boxes_[:, 1] /= h
                boxes_[:, 2] /= w
                boxes_[:, 3] /= h
                image = image.astype(np.uint8)

                image = cv2.resize(image, (max_shape[0], max_shape[1]))
                boxes_[:, 0] *= max_shape[0]
                boxes_[:, 1] *= max_shape[1]
                boxes_[:, 2] *= max_shape[0]
                boxes_[:, 3] *= max_shape[1]
            except:
                logger.warn('there is an err with but it is was handled')
                traceback.print_exc()
                image = np.zeros(shape=(max_shape[1], max_shape[0], 3),
                                 dtype=np.float32)
                boxes_ = np.array([[0, 0, 100, 100]])
                klass_ = np.array([0])

            if cfg.TRAIN.vis:
                for __box in boxes_:
                    cv2.rectangle(image, (int(__box[0]), int(__box[1])),
                                  (int(__box[2]), int(__box[3])), (255, 0, 0),
                                  4)

            all_boxes, all_labels = anchor_tools.produce_target(boxes_, klass_)

            alig_data.append([image, all_boxes, all_labels])

        return alig_data
예제 #2
0
    def _map_func(self, dp, is_training):
        """Data augmentation function."""
        ####customed here
        try:
            fname, annos = dp
            image = cv2.imread(fname, cv2.IMREAD_COLOR)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            labels = annos.split(' ')
            boxes = []

            for label in labels:
                bbox = np.array(label.split(','), dtype=np.float)
                boxes.append([bbox[0], bbox[1], bbox[2], bbox[3], bbox[4]])

            boxes = np.array(boxes, dtype=np.float)

            if is_training:

                sample_dice = random.uniform(0, 1)
                if sample_dice > 0.8 and sample_dice <= 1:
                    image, boxes = Random_scale_withbbox(
                        image,
                        boxes,
                        target_shape=[cfg.DATA.hin, cfg.DATA.win],
                        jitter=0.3)
                elif sample_dice > 0.4 and sample_dice <= 0.8:
                    boxes_ = boxes[:, 0:4]
                    klass_ = boxes[:, 4:]

                    image, boxes_, klass_ = dsfd_aug(image, boxes_, klass_)

                    image = image.astype(np.uint8)
                    boxes = np.concatenate([boxes_, klass_], axis=1)
                else:
                    boxes_ = boxes[:, 0:4]
                    klass_ = boxes[:, 4:]
                    image, boxes_, klass_ = baidu_aug(image, boxes_, klass_)

                    image = image.astype(np.uint8)
                    boxes = np.concatenate([boxes_, klass_], axis=1)

                if random.uniform(0, 1) > 0.5:
                    image, boxes = Random_flip(image, boxes)

                if random.uniform(0, 1) > 0.5:
                    image = self.color_augmentor(image)

            else:
                boxes_ = boxes[:, 0:4]
                klass_ = boxes[:, 4:]
                image, shift_x, shift_y = Fill_img(image,
                                                   target_width=cfg.DATA.win,
                                                   target_height=cfg.DATA.hin)
                boxes_[:, 0:4] = boxes_[:, 0:4] + np.array(
                    [shift_x, shift_y, shift_x, shift_y], dtype='float32')
                h, w, _ = image.shape
                boxes_[:, 0] /= w
                boxes_[:, 1] /= h
                boxes_[:, 2] /= w
                boxes_[:, 3] /= h
                image = image.astype(np.uint8)
                image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin))

                boxes_[:, 0] *= cfg.DATA.win
                boxes_[:, 1] *= cfg.DATA.hin
                boxes_[:, 2] *= cfg.DATA.win
                boxes_[:, 3] *= cfg.DATA.hin
                image = image.astype(np.uint8)
                boxes = np.concatenate([boxes_, klass_], axis=1)

            if boxes.shape[0] == 0 or np.sum(image) == 0:
                boxes_ = np.array([[0, 0, 100, 100]])
                klass_ = np.array([0])
            else:
                boxes_ = np.array(boxes[:, 0:4], dtype=np.float32)
                klass_ = np.array(boxes[:, 4], dtype=np.int64)

        except:
            logger.warn('there is an err with %s' % fname)
            traceback.print_exc()
            image = np.zeros(shape=(cfg.DATA.hin, cfg.DATA.win, 3),
                             dtype=np.float32)
            boxes_ = np.array([[0, 0, 100, 100]])
            klass_ = np.array([0])

        return image, boxes_, klass_
    def _map_func(self, dp, is_training):
        """Data augmentation function."""
        ####customed here
        try:
            fname, annos = dp
            image = cv2.imread(fname, cv2.IMREAD_COLOR)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            labels = annos.split(' ')
            boxes = []

            for label in labels:
                bbox = np.array(label.split(','), dtype=np.float)
                boxes.append([bbox[0], bbox[1], bbox[2], bbox[3], bbox[4]])

            boxes = np.array(boxes, dtype=np.float)

            img = image

            # if is_training:
            #
            #     height, width = img.shape[0], img.shape[1]
            #     c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
            #     if 0:
            #         input_h = (height | self.opt.pad) + 1
            #         input_w = (width | self.opt.pad) + 1
            #         s = np.array([input_w, input_h], dtype=np.float32)
            #     else:
            #         s = max(img.shape[0], img.shape[1]) * 1.0
            #         input_h, input_w = cfg.DATA.hin, cfg.DATA.win
            #
            #
            #
            #     flipped=False
            #     if 1:
            #         if 1:
            #             s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
            #             w_border = self._get_border(128, img.shape[1])
            #             h_border = self._get_border(128, img.shape[0])
            #             c[0] = np.random.randint(low=w_border, high=img.shape[1] - w_border)
            #             c[1] = np.random.randint(low=h_border, high=img.shape[0] - h_border)
            #
            #         if np.random.random() < 0.5:
            #             flipped=True
            #             img = img[:, ::-1, :]
            #             c[0] = width - c[0] - 1
            #
            #
            #     trans_output = get_affine_transform(c, s, 0, [input_w, input_h])
            #
            #
            #     inp = cv2.warpAffine(img, trans_output,
            #                          (input_w, input_h),
            #                          flags=cv2.INTER_LINEAR)
            #
            #     boxes_ = boxes[:,:4]
            #     klass_ = boxes[:,4:5]
            #
            #
            #     boxes_refine=[]
            #     for k in range(boxes_.shape[0]):
            #         bbox = boxes_[k]
            #
            #         cls_id = klass_[k]
            #         if flipped:
            #             bbox[[0, 2]] = width - bbox[[2, 0]] - 1
            #         bbox[:2] = affine_transform(bbox[:2], trans_output)
            #         bbox[2:] = affine_transform(bbox[2:], trans_output)
            #         bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, input_w - 1)
            #         bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, input_h - 1)
            #
            #         boxes_refine.append(bbox)
            #
            #     boxes_refine=np.array(boxes_refine)
            #
            #     image = inp.astype(np.uint8)
            #
            #     if random.uniform(0, 1) > 0.5:
            #         image =self.color_augmentor(image)
            #     # if random.uniform(0, 1) > 0.5:
            #     #     image =pixel_jitter(image,15)
            #     image = image.astype(np.uint8)
            #
            #     boxes = np.concatenate([boxes_refine, klass_], axis=1)
            if is_training:
                if 1:
                    height, width = img.shape[0], img.shape[1]
                    c = np.array([img.shape[1] / 2., img.shape[0] / 2.],
                                 dtype=np.float32)
                    if 0:
                        input_h = (height | self.opt.pad) + 1
                        input_w = (width | self.opt.pad) + 1
                        s = np.array([input_w, input_h], dtype=np.float32)
                    else:
                        s = max(img.shape[0], img.shape[1]) * 1.0
                        input_h, input_w = cfg.DATA.hin, cfg.DATA.win

                    flipped = False
                    if 1:
                        if 1:
                            s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
                            w_border = self._get_border(128, img.shape[1])
                            h_border = self._get_border(128, img.shape[0])
                            c[0] = np.random.randint(low=w_border,
                                                     high=img.shape[1] -
                                                     w_border)
                            c[1] = np.random.randint(low=h_border,
                                                     high=img.shape[0] -
                                                     h_border)

                        if np.random.random() < 0.5:
                            flipped = True
                            img = img[:, ::-1, :]
                            c[0] = width - c[0] - 1

                    trans_output = get_affine_transform(
                        c, s, 0, [input_w, input_h])

                    inp = cv2.warpAffine(img,
                                         trans_output, (input_w, input_h),
                                         flags=cv2.INTER_LINEAR)

                    boxes_ = boxes[:, :4]
                    klass_ = boxes[:, 4:5]

                    boxes_refine = []
                    for k in range(boxes_.shape[0]):
                        bbox = boxes_[k]

                        cls_id = klass_[k]
                        if flipped:
                            bbox[[0, 2]] = width - bbox[[2, 0]] - 1
                        bbox[:2] = affine_transform(bbox[:2], trans_output)
                        bbox[2:] = affine_transform(bbox[2:], trans_output)
                        bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, input_w - 1)
                        bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, input_h - 1)

                        boxes_refine.append(bbox)

                    boxes_refine = np.array(boxes_refine)

                    image = inp.astype(np.uint8)

                    if random.uniform(0, 1) > 0.5:
                        image = self.color_augmentor(image)
                    # if random.uniform(0, 1) > 0.5:
                    #     image =pixel_jitter(image,15)
                    image = image.astype(np.uint8)

                    boxes = np.concatenate([boxes_refine, klass_], axis=1)

                # else:
                #
                #     ##### different ratios
                #     sample_dice = random.uniform(0, 1)
                #     if sample_dice > 0.8 and sample_dice <= 1:
                #         image, boxes = Random_scale_withbbox(image, boxes, target_shape=[cfg.DATA.hin, cfg.DATA.win],
                #                                              jitter=0.5)
                #
                #     elif sample_dice > 0.4 and sample_dice <= 0.8:
                #         boxes_ = boxes[:, 0:4]
                #         klass_ = boxes[:, 4:]
                #
                #         image, boxes_, klass_ = dsfd_aug(image, boxes_, klass_)
                #
                #         image = image.astype(np.uint8)
                #         boxes = np.concatenate([boxes_, klass_], axis=1)
                #     else:
                #         boxes_ = boxes[:, 0:4]
                #         klass_ = boxes[:, 4:]
                #         image, boxes_, klass_ = baidu_aug(image, boxes_, klass_)
                #
                #         image = image.astype(np.uint8)
                #         boxes = np.concatenate([boxes_, klass_], axis=1)
                #
                #     if random.uniform(0, 1) > 0.5:
                #         image, boxes = Random_flip(image, boxes)
                #
                #     # if random.uniform(0, 1) > 0.3:
                #     #     boxes_ = boxes[:, 0:4]
                #     #     klass_ = boxes[:, 4:]
                #     #     angel = random.uniform(-45, 45)
                #     #     image, boxes_ = Rotate_with_box(image, angel, boxes_)
                #     #     boxes = np.concatenate([boxes_, klass_], axis=1)
                #
                #     if random.uniform(0, 1) > 0.5:
                #         image = self.color_augmentor(image)
                #     # if random.uniform(0, 1) > 0.5:
                #     #     image = pixel_jitter(image, 15)
                #
                #     boxes_ = boxes
                #
                #     image, shift_x, shift_y = Fill_img(image, target_width=cfg.DATA.win, target_height=cfg.DATA.hin)
                #     boxes_[:, 0:4] = boxes_[:, 0:4] + np.array([shift_x, shift_y, shift_x, shift_y], dtype='float32')
                #     h, w, _ = image.shape
                #     boxes_[:, 0] /= w
                #     boxes_[:, 1] /= h
                #     boxes_[:, 2] /= w
                #     boxes_[:, 3] /= h
                #     image = image.astype(np.uint8)
                #     image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin))
                #
                #     boxes_[:, 0] *= cfg.DATA.win
                #     boxes_[:, 1] *= cfg.DATA.hin
                #     boxes_[:, 2] *= cfg.DATA.win
                #     boxes_[:, 3] *= cfg.DATA.hin
                #     image = image.astype(np.uint8)
                #     boxes = boxes_
            else:
                boxes_ = boxes[:, 0:4]
                klass_ = boxes[:, 4:]
                image, shift_x, shift_y = Fill_img(image,
                                                   target_width=cfg.DATA.win,
                                                   target_height=cfg.DATA.hin)
                boxes_[:, 0:4] = boxes_[:, 0:4] + np.array(
                    [shift_x, shift_y, shift_x, shift_y], dtype='float32')
                h, w, _ = image.shape
                boxes_[:, 0] /= w
                boxes_[:, 1] /= h
                boxes_[:, 2] /= w
                boxes_[:, 3] /= h
                image = image.astype(np.uint8)
                image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin))

                boxes_[:, 0] *= cfg.DATA.win
                boxes_[:, 1] *= cfg.DATA.hin
                boxes_[:, 2] *= cfg.DATA.win
                boxes_[:, 3] *= cfg.DATA.hin
                image = image.astype(np.uint8)
                boxes = np.concatenate([boxes_, klass_], axis=1)

            if boxes.shape[0] == 0 or np.sum(image) == 0:
                boxes_ = np.array([[0, 0, -1, -1]])
                klass_ = np.array([0])
            else:
                boxes_ = np.array(boxes[:, 0:4], dtype=np.float32)
                klass_ = np.array(boxes[:, 4], dtype=np.int64)

        except:
            logger.warn('there is an err with %s' % fname)
            traceback.print_exc()
            image = np.zeros(shape=(cfg.DATA.hin, cfg.DATA.win, 3),
                             dtype=np.uint8)
            boxes_ = np.array([[0, 0, -1, -1]])
            klass_ = np.array([0])

        return image, boxes_, klass_
예제 #4
0
    def _map_func(self,dp,is_training):
        """Data augmentation function."""
        ####customed here
        try:
            fname, annos = dp
            image = cv2.imread(fname, cv2.IMREAD_COLOR)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            labels = annos.split(' ')
            boxes = []


            for label in labels:
                bbox = np.array(label.split(','), dtype=np.float)
                boxes.append([bbox[0], bbox[1], bbox[2], bbox[3], bbox[4]])

            boxes = np.array(boxes, dtype=np.float)

            img=image

            if is_training:





                ###random crop and flip
                height, width = img.shape[0], img.shape[1]
                c = np.array([img.shape[1] / 2., img.shape[0] / 2.], dtype=np.float32)
                if 0:
                    input_h = (height | self.opt.pad) + 1
                    input_w = (width | self.opt.pad) + 1
                    s = np.array([input_w, input_h], dtype=np.float32)
                else:
                    s = max(img.shape[0], img.shape[1]) * 1.0
                    input_h, input_w = cfg.DATA.hin, cfg.DATA.win

                flipped = False
                if 1:
                    if 1:
                        s = s * np.random.choice(np.arange(0.6, 1.4, 0.1))
                        w_border = self._get_border(128, img.shape[1])
                        h_border = self._get_border(128, img.shape[0])
                        c[0] = np.random.randint(low=w_border, high=img.shape[1] - w_border)
                        c[1] = np.random.randint(low=h_border, high=img.shape[0] - h_border)

                    if np.random.random() < 0.5:
                        flipped = True
                        img = img[:, ::-1, :]
                        c[0] = width - c[0] - 1

                trans_output = get_affine_transform(c, s, 0, [input_w, input_h])

                inp = cv2.warpAffine(img, trans_output,
                                     (input_w, input_h),
                                     flags=cv2.INTER_LINEAR)

                boxes_ = boxes[:, :4]
                klass_ = boxes[:, 4:5]

                boxes_refine = []
                for k in range(boxes_.shape[0]):
                    bbox = boxes_[k]

                    cls_id = klass_[k]
                    if flipped:
                        bbox[[0, 2]] = width - bbox[[2, 0]] - 1
                    bbox[:2] = affine_transform(bbox[:2], trans_output)
                    bbox[2:] = affine_transform(bbox[2:], trans_output)
                    bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, input_w - 1)
                    bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, input_h - 1)

                    boxes_refine.append(bbox)

                boxes_refine = np.array(boxes_refine)
                image = inp.astype(np.uint8)
                boxes = np.concatenate([boxes_refine, klass_], axis=1)

                ####random crop and flip
                #### pixel level aug

                image=self.train_trans(image=image)['image']

                ####

            else:
                boxes_ = boxes[:, 0:4]
                klass_ = boxes[:, 4:]
                image, shift_x, shift_y = Fill_img(image, target_width=cfg.DATA.win, target_height=cfg.DATA.hin)
                boxes_[:, 0:4] = boxes_[:, 0:4] + np.array([shift_x, shift_y, shift_x, shift_y], dtype='float32')
                h, w, _ = image.shape
                boxes_[:, 0] /= w
                boxes_[:, 1] /= h
                boxes_[:, 2] /= w
                boxes_[:, 3] /= h
                image = image.astype(np.uint8)
                image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin))

                boxes_[:, 0] *= cfg.DATA.win
                boxes_[:, 1] *= cfg.DATA.hin
                boxes_[:, 2] *= cfg.DATA.win
                boxes_[:, 3] *= cfg.DATA.hin
                image = image.astype(np.uint8)
                boxes = np.concatenate([boxes_, klass_], axis=1)

            if boxes.shape[0] == 0 or np.sum(image) == 0:
                boxes_ = np.array([[0, 0, -1, -1]])
                klass_ = np.array([0])
            else:
                boxes_ = np.array(boxes[:, 0:4], dtype=np.float32)
                klass_ = np.array(boxes[:, 4], dtype=np.int64)


        except:
            logger.warn('there is an err with %s' % fname)
            traceback.print_exc()
            image = np.zeros(shape=(cfg.DATA.hin, cfg.DATA.win, 3), dtype=np.uint8)
            boxes_ = np.array([[0, 0, -1, -1]])
            klass_ = np.array([0])

        return image, boxes_, klass_
    def _map_func(self,dp,is_training):
        """Data augmentation function."""
        ####customed here
        try:
            fname, annos = dp
            image = cv2.imread(fname, cv2.IMREAD_COLOR)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            labels = annos.split(' ')
            boxes = []
            for label in labels:
                bbox = np.array(label.split(','), dtype=np.float)
                ##the augmentor need ymin,xmin,ymax,xmax
                boxes.append([bbox[0], bbox[1], bbox[2], bbox[3], bbox[4]])

            boxes = np.array(boxes, dtype=np.float)

            ###clip the bbox for the reason that some bboxs are beyond the image
            # h_raw_limit, w_raw_limit, _ = image.shape
            # boxes[:, 2] = np.clip(boxes[:, 2], 0, w_raw_limit)
            # boxes[:, 3] = np.clip(boxes[:, 3], 0, h_raw_limit)
            # boxes[boxes < 0] = 0
            #########random scale
            ############## becareful with this func because there is a Infinite loop in its body
            if is_training:

                sample_dice = random.uniform(0, 1)
                if sample_dice > 0.8 and sample_dice <= 1:
                    image, boxes = Random_scale_withbbox(image, boxes, target_shape=[cfg.DATA.hin, cfg.DATA.win],
                                                         jitter=0.3)
                elif sample_dice > 0.4 and sample_dice <= 0.8:
                    boxes_ = boxes[:, 0:4]
                    klass_ = boxes[:, 4:]

                    image, boxes_, klass_ = dsfd_aug(image, boxes_, klass_)
                    if random.randint(0,1):
                        image, shift_x, shift_y = Fill_img(image, target_width=cfg.DATA.win, target_height=cfg.DATA.hin)
                        boxes_[:, 0:4] = boxes_[:, 0:4] + np.array([shift_x, shift_y, shift_x, shift_y], dtype='float32')
                    h, w, _ = image.shape
                    boxes_[:, 0] /= w
                    boxes_[:, 1] /= h
                    boxes_[:, 2] /= w
                    boxes_[:, 3] /= h
                    interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST,
                                      cv2.INTER_LANCZOS4]
                    interp_method = random.choice(interp_methods)
                    image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin), interpolation=interp_method)

                    boxes_[:, 0] *= cfg.DATA.win
                    boxes_[:, 1] *= cfg.DATA.hin
                    boxes_[:, 2] *= cfg.DATA.win
                    boxes_[:, 3] *= cfg.DATA.hin
                    image = image.astype(np.uint8)
                    boxes = np.concatenate([boxes_, klass_], axis=1)
                else:
                    boxes_ = boxes[:, 0:4]
                    klass_ = boxes[:, 4:]
                    image, boxes_, klass_ = baidu_aug(image, boxes_, klass_)
                    if random.randint(0, 1):
                        image, shift_x, shift_y = Fill_img(image, target_width=cfg.DATA.win, target_height=cfg.DATA.hin)
                        boxes_[:, 0:4] = boxes_[:, 0:4] + np.array([shift_x, shift_y, shift_x, shift_y], dtype='float32')
                    h, w, _ = image.shape
                    boxes_[:, 0] /= w
                    boxes_[:, 1] /= h
                    boxes_[:, 2] /= w
                    boxes_[:, 3] /= h
                    interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST,
                                      cv2.INTER_LANCZOS4]
                    interp_method = random.choice(interp_methods)
                    image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin), interpolation=interp_method)

                    boxes_[:, 0] *= cfg.DATA.win
                    boxes_[:, 1] *= cfg.DATA.hin
                    boxes_[:, 2] *= cfg.DATA.win
                    boxes_[:, 3] *= cfg.DATA.hin
                    image = image.astype(np.uint8)
                    boxes = np.concatenate([boxes_, klass_], axis=1)

                if random.uniform(0, 1) > 0.5:
                    image, boxes = Random_flip(image, boxes)
                # if random.uniform(0, 1) > 0.5:
                #     image = Pixel_jitter(image, max_=15)
                if random.uniform(0, 1) > 0.5:
                    image = Random_brightness(image, 35)
                if random.uniform(0, 1) > 0.5:
                    image = Random_contrast(image, [0.5, 1.5])
                if random.uniform(0, 1) > 0.5:
                    image = Random_saturation(image, [0.5, 1.5])
                # if random.uniform(0, 1) > 0.5:
                #    a = [3, 5, 7, 9]
                #    k = random.sample(a, 1)[0]
                #    image = Blur_aug(image, ksize=(k, k))
                if random.uniform(0, 1) > 0.7:
                    image = Gray_aug(image)
                # if random.uniform(0, 1) > 0.7:
                #     image = Swap_change_aug(image)
                # if random.uniform(0, 1) > 0.7:
                #     boxes_ = boxes[:, 0:4]
                #     klass_ = boxes[:, 4:]
                #     angle = random.sample([-90, 90], 1)[0]
                #     image, boxes_ = Rotate_with_box(image, boxes=boxes_, angle=angle)
                #     boxes = np.concatenate([boxes_, klass_], axis=1)


            else:
                boxes_ = boxes[:, 0:4]
                klass_ = boxes[:, 4:]
                image, shift_x, shift_y = Fill_img(image, target_width=cfg.DATA.win, target_height=cfg.DATA.hin)
                boxes_[:, 0:4] = boxes_[:, 0:4] + np.array([shift_x, shift_y, shift_x, shift_y], dtype='float32')
                h, w, _ = image.shape
                boxes_[:, 0] /= w
                boxes_[:, 1] /= h
                boxes_[:, 2] /= w
                boxes_[:, 3] /= h
                image = image.astype(np.uint8)
                image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin))

                boxes_[:, 0] *= cfg.DATA.win
                boxes_[:, 1] *= cfg.DATA.hin
                boxes_[:, 2] *= cfg.DATA.win
                boxes_[:, 3] *= cfg.DATA.hin
                image = image.astype(np.uint8)
                boxes = np.concatenate([boxes_, klass_], axis=1)
            if cfg.TRAIN.vis:
                for __box in boxes:
                    cv2.rectangle(image, (int(__box[0]), int(__box[1])),
                                  (int(__box[2]), int(__box[3])), (255, 0, 0), 4)

            ###cove the small faces
            boxes_clean = []
            for i in range(boxes.shape[0]):
                box = boxes[i]

                if (box[3] - box[1]) < cfg.DATA.cover_small_face or (box[2] - box[0]) < cfg.DATA.cover_small_face:
                    # image[int(box[1]):int(box[3]), int(box[0]):int(box[2]), :] = 0
                    continue
                else:
                    boxes_clean.append(box)
            boxes = np.array(boxes_clean)

            if boxes.shape[0] == 0 or np.sum(image) == 0:
                boxes_ = np.array([[0, 0, 100, 100]])
                klass_ = np.array([0])
            else:
                boxes_ = np.array(boxes[:, 0:4], dtype=np.float32)
                klass_ = np.array(boxes[:, 4], dtype=np.int64)




        except:
            logger.warn('there is an err with %s' % fname)
            traceback.print_exc()
            image = np.zeros(shape=(cfg.DATA.hin, cfg.DATA.win, 3), dtype=np.float32)
            boxes_ = np.array([[0, 0, 100, 100]])
            klass_ = np.array([0])

        all_boxes, all_labels = cfg.ANCHOR.achors.produce_target(boxes_, klass_)
        return image, all_boxes, all_labels
예제 #6
0
    def _map_func(self, dp, is_training):
        """Data augmentation function."""
        ####customed here
        try:
            fname, annos = dp
            image = cv2.imread(fname, cv2.IMREAD_COLOR)
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            labels = annos.split(' ')
            boxes = []

            for label in labels:
                sp = label.split(',')
                if len(sp) > 1:
                    bbox = np.array(label.split(','), dtype=np.float)
                    boxes.append([bbox[0], bbox[1], bbox[2], bbox[3], bbox[4]])

            boxes = np.array(boxes, dtype=np.float)

            if is_training:

                annotations = {
                    'image': image,
                    'bboxes': boxes,
                    'category_id': np.ones(len(boxes))
                }
                augmented = self.aug(**annotations)
                image = np.array(augmented['image'])
                boxes = np.array(augmented['bboxes'])
                h, w, _ = image.shape
                # if len(boxes) == 0:
                #     print('o')
                #     exit()
                # print(np.unique(boxes[:,0] < 0))
                # print(np.unique(boxes[:,1] < 0))
                #
                # boxes[:,1][boxes[:,1] > h] = h-1
                # boxes[:,3][boxes[:,3] > w] = w-1
                # boxes[:,3] = min(boxes[:,3],h)
                # print(np.unique(boxes[:, 1] > w))
                # print(np.unique(boxes[:, 3] > h))

                # sample_dice = random.uniform(0, 1)
                # if sample_dice > 0.8 and sample_dice <= 1:
                #     image, boxes = Random_scale_withbbox(image, boxes, target_shape=[cfg.DATA.hin, cfg.DATA.win],
                #                                          jitter=0.3)
                #
                # elif sample_dice > 0.4 and sample_dice <= 0.8:
                #     boxes_ = boxes[:, 0:4]
                #     klass_ = boxes[:, 4:]
                #
                #     image, boxes_, klass_ = dsfd_aug(image, boxes_, klass_)
                #
                #     image = image.astype(np.uint8)
                #     boxes = np.concatenate([boxes_, klass_], axis=1)
                # else:
                #     boxes_ = boxes[:, 0:4]
                #     klass_ = boxes[:, 4:]
                #     image, boxes_, klass_ = baidu_aug(image, boxes_, klass_)
                #
                #     image = image.astype(np.uint8)
                #     boxes = np.concatenate([boxes_, klass_], axis=1)

                # if random.uniform(0, 1) > 0.5:
                #     image, boxes = Random_flip(image, boxes)
                #
                # if random.uniform(0, 1) > 0.5:
                #     image =self.color_augmentor(image)

            else:
                boxes_ = boxes[:, 0:4]
                klass_ = boxes[:, 4:]
                image, shift_x, shift_y = Fill_img(image,
                                                   target_width=cfg.DATA.win,
                                                   target_height=cfg.DATA.hin)
                boxes_[:, 0:4] = boxes_[:, 0:4] + np.array(
                    [shift_x, shift_y, shift_x, shift_y], dtype='float32')
                h, w, _ = image.shape
                boxes_[:, 0] /= w
                boxes_[:, 1] /= h
                boxes_[:, 2] /= w
                boxes_[:, 3] /= h
                image = image.astype(np.uint8)
                image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin))

                boxes_[:, 0] *= cfg.DATA.win
                boxes_[:, 1] *= cfg.DATA.hin
                boxes_[:, 2] *= cfg.DATA.win
                boxes_[:, 3] *= cfg.DATA.hin
                image = image.astype(np.uint8)
                boxes = np.concatenate([boxes_, klass_], axis=1)

            if boxes.shape[0] == 0 or np.sum(image) == 0:
                boxes_ = np.array([[0, 0, 100, 100]]).astype(np.float32)
                klass_ = np.array([0]).astype(np.float32)
            else:
                boxes_ = np.array(boxes[:, 0:4], dtype=np.float32)
                klass_ = np.array(boxes[:, 4], dtype=np.int64)

        except:
            try:
                image, shift_x, shift_y = Fill_img(image,
                                                   target_width=cfg.DATA.win,
                                                   target_height=cfg.DATA.hin)

                image = image.astype(np.uint8)
                image = cv2.resize(image, (cfg.DATA.win, cfg.DATA.hin))
                image = image.astype(np.uint8)
                # image = np.zeros(shape=(cfg.DATA.hin, cfg.DATA.win, 3), dtype=np.float32)

                boxes_ = np.array([[0, 0, 100, 100]]).astype(np.float32)
                klass_ = np.array([0]).astype(np.float32)
            except:
                logger.warn('there is an err with %s' % fname)
                traceback.print_exc()

        return image, boxes_, klass_