Пример #1
0
    def _map_func(self, dp, is_training):
        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)

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

        if is_training:
            # if random.uniform(0, 1)>0.7:
            image, boxes = Random_scale_withbbox(
                image,
                boxes,
                target_shape=[cfg.MODEL.hin, cfg.MODEL.win],
                jitter=0.3)
            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)

            if random.uniform(0, 1) > 0.5:
                image = Pixel_jitter(image, 15)
            if random.uniform(0, 1) > 0.8:
                image = Gray_aug(image)

        image, shift_x, shift_y = Fill_img(image,
                                           target_width=cfg.MODEL.win,
                                           target_height=cfg.MODEL.hin)
        # boxes[:, 0:4] = boxes[:, 0:4] + np.array([shift_x, shift_y, shift_x, shift_y], dtype='float32')
        boxes[:, 0] += shift_x
        boxes[:, 1] += shift_y
        boxes[:, 2] += shift_x
        boxes[:, 3] += shift_y
        boxes[boxes[:, 4] >= 0, 4] += shift_x
        boxes[boxes[:, 5] >= 0, 5] += shift_y
        boxes[boxes[:, 6] >= 0, 6] += shift_x
        boxes[boxes[:, 7] >= 0, 7] += shift_y
        boxes[boxes[:, 8] >= 0, 8] += shift_x
        boxes[boxes[:, 9] >= 0, 9] += shift_y
        boxes[boxes[:, 10] >= 0, 10] += shift_x
        boxes[boxes[:, 11] >= 0, 11] += shift_y
        boxes[boxes[:, 12] >= 0, 12] += shift_x
        boxes[boxes[:, 13] >= 0, 13] += shift_y

        h, w, _ = image.shape
        boxes[:, 0] /= w
        boxes[:, 1] /= h
        boxes[:, 2] /= w
        boxes[:, 3] /= h
        boxes[boxes[:, 4] >= 0, 4] /= w
        boxes[boxes[:, 5] >= 0, 5] /= h
        boxes[boxes[:, 6] >= 0, 6] /= w
        boxes[boxes[:, 7] >= 0, 7] /= h
        boxes[boxes[:, 8] >= 0, 8] /= w
        boxes[boxes[:, 9] >= 0, 9] /= h
        boxes[boxes[:, 10] >= 0, 10] /= w
        boxes[boxes[:, 11] >= 0, 11] /= h
        boxes[boxes[:, 12] >= 0, 12] /= w
        boxes[boxes[:, 13] >= 0, 13] /= h

        image = cv2.resize(image, (cfg.MODEL.win, cfg.MODEL.hin))
        image = image.astype(np.uint8)

        ### cover the small faces with invisible landmarks
        boxes_clean = []
        for i in range(boxes.shape[0]):
            box = boxes[i]
            if box[4] < 0:
                image[int(box[1] * cfg.MODEL.hin):int(box[3] * cfg.MODEL.hin),
                      int(box[0] *
                          cfg.MODEL.hin):int(box[2] * cfg.MODEL.hin
                                             ), :] = cfg.DATA.PIXEL_MEAN
            else:
                boxes_clean.append([
                    box[1], box[0], box[3], box[2], box[5], box[4], box[7],
                    box[6], box[9], box[8], box[11], box[10], box[13], box[12]
                ])

        boxes = np.array(boxes_clean)
        # tmp = boxes[:, ::2]
        # boxes[:, ::2] = boxes[:, 1::2]
        # boxes[:, 1::2] = tmp

        if cfg.TRAIN.vis:
            for i in range(boxes.shape[0]):
                box = boxes[i]
                color = (255, 0, 0)
                thickness = 2
                radius = 2
                cv2.rectangle(
                    image,
                    (int(box[1] * cfg.MODEL.win), int(box[0] * cfg.MODEL.hin)),
                    (int(box[3] * cfg.MODEL.win), int(box[2] * cfg.MODEL.hin)),
                    color, thickness)
                for point_x, point_y in zip(box[5::2], box[4::2]):
                    if point_x > 0 and point_y > 0:
                        cv2.circle(image, (int(point_x * cfg.MODEL.win),
                                           int(point_y * cfg.MODEL.hin)),
                                   radius, color, thickness)

        reg_targets, matches = self.produce_target(boxes)
        image = image.astype(np.float32)

        # if reg_targets.shape[0] > 0:
        #     reg_targets = reg_targets[:, :4]

        return image, reg_targets, matches
    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
Пример #3
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):
        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], 1])

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

        #########random scale
        ############## becareful with this func because there is a Infinite loop in its body
        if is_training:

            random_index = random.uniform(0, 1)
            if random_index > 0.7:
                image, boxes = Random_scale_withbbox(
                    image,
                    boxes,
                    target_shape=[cfg.MODEL.hin, cfg.MODEL.win],
                    jitter=0.3)
            elif random_index < 0.3 and random_index <= 0.7:
                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)
            else:
                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)

            boxes_ = boxes[:, 0:4]
            klass_ = boxes[:, 4:]
            if random.uniform(0, 1) > 0.5:
                image, shift_x, shift_y = Fill_img(image,
                                                   target_width=cfg.MODEL.win,
                                                   target_height=cfg.MODEL.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.MODEL.win, cfg.MODEL.hin),
                               interpolation=interp_method)

            boxes_[:, 0] *= cfg.MODEL.win
            boxes_[:, 1] *= cfg.MODEL.hin
            boxes_[:, 2] *= cfg.MODEL.win
            boxes_[:, 3] *= cfg.MODEL.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 = self.color_augmentor(image)

            if random.uniform(0, 1) > 0.5:
                image = Pixel_jitter(image, 15)
            if random.uniform(0, 1) > 0.8:
                image = Gray_aug(image)

        else:
            boxes_ = boxes[:, 0:4]
            klass_ = boxes[:, 4:]
            image, shift_x, shift_y = Fill_img(image,
                                               target_width=cfg.MODEL.win,
                                               target_height=cfg.MODEL.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 = cv2.resize(image, (cfg.MODEL.win, cfg.MODEL.hin))

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

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

            if (box[3] - box[1]) * (box[2] -
                                    box[0]) < cfg.DATA.cover_small_face:
                image[int(box[1]):int(box[3]),
                      int(box[0]):int(box[2]), :] = cfg.DATA.PIXEL_MEAN
            else:
                boxes_clean.append([box[1], box[0], box[3], box[2]])
        boxes = np.array(boxes_clean)
        boxes = boxes / cfg.MODEL.hin

        if cfg.TRAIN.vis:
            for i in range(boxes.shape[0]):
                box = boxes[i]
                cv2.rectangle(
                    image,
                    (int(box[1] * cfg.MODEL.hin), int(box[0] * cfg.MODEL.hin)),
                    (int(box[3] * cfg.MODEL.hin), int(box[2] * cfg.MODEL.hin)),
                    (255, 0, 0), 7)

        reg_targets, matches = self.produce_target(boxes)

        image = image.astype(np.float32)

        return image, reg_targets, matches