def _map_func(self,dp,is_training): """Data augmentation function.""" ####customed here try: fname, ann = dp image = cv2.imread(fname, cv2.IMREAD_COLOR) if cfg.DATA.rgb: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) label = np.array(ann) if is_training: image=self.random_crop_resize(image) if random.uniform(0, 1) > 0.5: image, _ = Mirror(image, label=None, symmetry=None) if random.uniform(0, 1) > 0.5: angle = random.uniform(-45, 45) image, _ = Rotate_aug(image, label=None, angle=angle) if random.uniform(0, 1) > 1.: strength = random.uniform(0, 50) image, _ = Affine_aug(image, strength=strength, label=None) if random.uniform(0, 1) > 0.5: image=self.color_augmentor(image) if random.uniform(0, 1) > 1.0: image=pixel_jitter(image,15) if random.uniform(0, 1) > 0.5: image = Img_dropout(image, 0.2) else: ###centercrop image = self.center_crop(image) label = label.astype(np.int64) image= image.astype(np.uint8) except: logger.info('some err happended with %s'%fname, ' but handled with -1') image=np.zeros(shape=[cfg.MODEL.hin,cfg.MODEL.win,3],dtype=np.uint8) label = np.array(-1,dtype=np.int64) return image, label
def _map_func(self,dp,is_training): """Data augmentation function.""" ####customed here fname= dp['image_path'] keypoints=dp['keypoints'] bbox =dp['bbox'] # attr =dp['attr'] #### 300W if keypoints is not None: image = cv2.imread(fname, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) label = np.array(keypoints, dtype=np.float).reshape((-1, 2)) bbox = np.array(bbox) ### random crop and resize crop_image, label = self.augmentationCropImage(image, bbox, label, is_training) if is_training: if random.uniform(0, 1) > 0.5: crop_image, label = Mirror(crop_image, label=label, symmetry=cfg.DATA.symmetry) if random.uniform(0, 1) > 0.0: angle = random.uniform(-45, 45) crop_image, label = Rotate_aug(crop_image, label=label, angle=angle) if random.uniform(0, 1) > 0.5: strength = random.uniform(0, 50) crop_image, label = Affine_aug(crop_image, strength=strength, label=label) if random.uniform(0, 1) > 0.5: crop_image=self.color_augmentor(crop_image) if random.uniform(0, 1) > 0.5: crop_image=pixel_jitter(crop_image,15) if random.uniform(0, 1) > 0.5: crop_image = Img_dropout(crop_image, 0.2) if random.uniform(0, 1) > 0.5: crop_image = Padding_aug(crop_image, 0.3) #######head pose reprojectdst, euler_angle = get_head_pose(label, crop_image) PRY = euler_angle.reshape([-1]).astype(np.float32) / 90. ######cla_label cla_label = np.zeros([4]) if np.sqrt(np.square(label[37, 0] - label[41, 0]) + np.square(label[37, 1] - label[41, 1])) / cfg.MODEL.hin < self.eye_close_thres \ or np.sqrt(np.square(label[38, 0] - label[40, 0]) + np.square(label[38, 1] - label[40, 1])) / cfg.MODEL.hin < self.eye_close_thres: cla_label[0] = 1 if np.sqrt(np.square(label[43, 0] - label[47, 0]) + np.square(label[43, 1] - label[47, 1])) / cfg.MODEL.hin < self.eye_close_thres \ or np.sqrt(np.square(label[44, 0] - label[46, 0]) + np.square(label[44, 1] - label[46, 1])) / cfg.MODEL.hin < self.eye_close_thres: cla_label[1] = 1 if np.sqrt(np.square(label[61, 0] - label[67, 0]) + np.square(label[61, 1] - label[67, 1])) / cfg.MODEL.hin < self.mouth_close_thres \ or np.sqrt(np.square(label[62, 0] - label[66, 0]) + np.square(label[62, 1] - label[66, 1])) / cfg.MODEL.hin < self.mouth_close_thres \ or np.sqrt(np.square(label[63, 0] - label[65, 0]) + np.square(label[63, 1] - label[65, 1])) / cfg.MODEL.hin < self.mouth_close_thres: cla_label[2] = 1 ### mouth open big 1 mean true if np.sqrt(np.square(label[62, 0] - label[66, 0]) + np.square(label[62, 1] - label[66, 1])) / cfg.MODEL.hin > self.big_mouth_open_thres: cla_label[3] = 1 crop_image_height, crop_image_width, _ = crop_image.shape label = label.astype(np.float32) label[:, 0] = label[:, 0] / crop_image_width label[:, 1] = label[:, 1] / crop_image_height crop_image = crop_image.astype(np.float32) label = label.reshape([-1]).astype(np.float32) cla_label = cla_label.astype(np.float32) label = np.concatenate([label, PRY, cla_label], axis=0) # else: # image = cv2.imread(fname, cv2.IMREAD_COLOR) # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # # bbox = np.array(bbox) # # ### random crop and resize # crop_image, _ = self.augmentationCropImage(image, bbox, np.array([[0,0]], dtype=np.float).reshape((-1, 2)), is_training) # # if is_training: # # if random.uniform(0, 1) > 0.5: # crop_image, _ = Mirror(crop_image, label=None, symmetry=None) # if random.uniform(0, 1) > 0.0: # angle = random.uniform(-45, 45) # crop_image, _ = Rotate_aug(crop_image, label=None, angle=angle) # # if random.uniform(0, 1) > 0.5: # strength = random.uniform(0, 50) # crop_image, _ = Affine_aug(crop_image, strength=strength, label=None) # # if random.uniform(0, 1) > 0.5: # crop_image = self.color_augmentor(crop_image) # if random.uniform(0, 1) > 0.5: # crop_image = pixel_jitter(crop_image, 15) # if random.uniform(0, 1) > 0.5: # crop_image = Img_dropout(crop_image, 0.2) # # if random.uniform(0, 1) > 0.5: # crop_image = Padding_aug(crop_image, 0.3) # # #######head pose # # crop_image = crop_image.astype(np.float32) # # label=np.zeros(shape=cfg.MODEL.out_channel-2)-1 # label[-4:]=np.array(attr) # # ### index 0 means things come from keypoints, 1 is from celeba # weights = np.array([0, 1]) # # label = np.concatenate([label, weights], axis=0) return crop_image, label
def _map_func(self, dp, is_training): """Data augmentation function.""" ####customed here fname, ann = dp image = cv2.imread(fname, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) label = np.array([ann.split(' ')], dtype=np.float).reshape((-1, 2)) bbox = np.array([ np.min(label[:, 0]), np.min(label[:, 1]), np.max(label[:, 0]), np.max(label[:, 1]) ]) ### random crop and resize crop_image, label = self.augmentationCropImage(image, bbox, label, is_training) if is_training: if random.uniform(0, 1) > 0.5: crop_image, label = Mirror(crop_image, label=label, symmetry=cfg.DATA.symmetry) if random.uniform(0, 1) > 0.0: angle = random.uniform(-45, 45) crop_image, label = Rotate_aug(crop_image, label=label, angle=angle) if random.uniform(0, 1) > 0.5: strength = random.uniform(0, 50) crop_image, label = Affine_aug(crop_image, strength=strength, label=label) if random.uniform(0, 1) > 0.5: crop_image = self.color_augmentor(crop_image) if random.uniform(0, 1) > 0.5: crop_image = pixel_jitter(crop_image, 15) if random.uniform(0, 1) > 0.5: crop_image = Img_dropout(crop_image, 0.2) if random.uniform(0, 1) > 0.5: crop_image = Padding_aug(crop_image, 0.3) #######head pose reprojectdst, euler_angle = get_head_pose(label, crop_image) PRY = euler_angle.reshape([-1]).astype(np.float32) / 90. ######cla_label cla_label = np.zeros([4]) if np.sqrt(np.square(label[37, 0] - label[41, 0]) + np.square(label[37, 1] - label[41, 1])) / cfg.MODEL.hin < self.eye_close_thres \ or np.sqrt(np.square(label[38, 0] - label[40, 0]) + np.square(label[38, 1] - label[40, 1])) / cfg.MODEL.hin < self.eye_close_thres: cla_label[0] = 1 if np.sqrt(np.square(label[43, 0] - label[47, 0]) + np.square(label[43, 1] - label[47, 1])) / cfg.MODEL.hin < self.eye_close_thres \ or np.sqrt(np.square(label[44, 0] - label[46, 0]) + np.square(label[44, 1] - label[46, 1])) / cfg.MODEL.hin < self.eye_close_thres: cla_label[1] = 1 if np.sqrt(np.square(label[61, 0] - label[67, 0]) + np.square(label[61, 1] - label[67, 1])) / cfg.MODEL.hin < self.mouth_close_thres \ or np.sqrt(np.square(label[62, 0] - label[66, 0]) + np.square(label[62, 1] - label[66, 1])) / cfg.MODEL.hin < self.mouth_close_thres \ or np.sqrt(np.square(label[63, 0] - label[65, 0]) + np.square(label[63, 1] - label[65, 1])) / cfg.MODEL.hin < self.mouth_close_thres: cla_label[2] = 1 ### mouth open big 1 mean true if np.sqrt( np.square(label[62, 0] - label[66, 0]) + np.square(label[62, 1] - label[66, 1]) ) / cfg.MODEL.hin > self.big_mouth_open_thres: cla_label[3] = 1 crop_image_height, crop_image_width, _ = crop_image.shape label = label.astype(np.float32) label[:, 0] = label[:, 0] / crop_image_width label[:, 1] = label[:, 1] / crop_image_height crop_image = crop_image.astype(np.float32) label = label.reshape([-1]).astype(np.float32) cla_label = cla_label.astype(np.float32) label = np.concatenate([label, PRY, cla_label], axis=0) return crop_image, label
def _map_func(self,dp,is_training): """Data augmentation function.""" ####customed here cur_data_info=dp.rstrip().split(' ') fname= cur_data_info[0] label=cur_data_info[1:4] label=[int(x) for x in label] # attr =dp['attr'] #### 300W image = cv2.imread(fname, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) label = np.array(label, dtype=np.float) if is_training: if random.uniform(0, 1) > 0.5: image=Random_crop(image,shrink=0.2) # if random.uniform(0, 1) > 0.5.: # image, _ = Mirror(image, label=None, symmetry=None) if random.uniform(0, 1) > 0.0: angle = random.uniform(-30, 30) image, _ = Rotate_aug(image, label=None, angle=angle) if random.uniform(0, 1) > 0.5: strength = random.uniform(0, 50) image, _ = Affine_aug(image, strength=strength, label=None) 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.5: image = Img_dropout(image, 0.2) if random.uniform(0, 1) > 0.5: image = Padding_aug(image, 0.3) 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) #######head pose if cfg.MODEL.channel==1: image = image.astype(np.uint8) image=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) image=np.expand_dims(image,-1) image = np.transpose(image,axes=[2,0,1]) label = label.reshape([-1]).astype(np.float32) image = image.astype(np.float32) if not cfg.TRAIN.vis : image=image/255. return image, label
def _map_func(self, dp, is_training): """Data augmentation function.""" ####customed here fname = dp['image_path'] keypoints = dp['keypoints'] # attr =dp['attr'] #### 300W if keypoints is not None: image = cv2.imread(fname, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) label = np.array(keypoints, dtype=np.float).reshape((-1, 2)) #bbox = np.array(bbox) ### random crop and resize #crop_image, label = self.augmentationCropImage(image, bbox, label, is_training) crop_image, label = self.preprocess(image, cfg.MODEL.hin, cfg.MODEL.win, label) if is_training: if random.uniform(0, 1) > 0.5: crop_image, label = Mirror(crop_image, label=label, symmetry=cfg.DATA.symmetry) if random.uniform(0, 1) > 0.0: angle = random.uniform(-45, 45) crop_image, label = Rotate_aug(crop_image, label=label, angle=angle) if random.uniform(0, 1) > 0.5: strength = random.uniform(0, 50) crop_image, label = Affine_aug(crop_image, strength=strength, label=label) if random.uniform(0, 1) > 0.5: crop_image = self.color_augmentor(crop_image) if random.uniform(0, 1) > 0.5: crop_image = pixel_jitter(crop_image, 15) if random.uniform(0, 1) > 0.5: crop_image = Img_dropout(crop_image, 0.2) if random.uniform(0, 1) > 0.5: crop_image = Padding_aug(crop_image, 0.3) look = dp['look_vec'] crop_image_height, crop_image_width, _ = crop_image.shape label = label.astype(np.float32) label[:, 0] = label[:, 0] / crop_image_width label[:, 1] = label[:, 1] / crop_image_height crop_image = crop_image.astype(np.float32) label = label.reshape([-1]).astype(np.float32) ##cla_label = cla_label.astype(np.float32) look = np.array(look, dtype=np.float32) label = np.concatenate([label, look], axis=0) # else: # image = cv2.imread(fname, cv2.IMREAD_COLOR) # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # # bbox = np.array(bbox) # # ### random crop and resize # crop_image, _ = self.augmentationCropImage(image, bbox, np.array([[0,0]], dtype=np.float).reshape((-1, 2)), is_training) # # if is_training: # # if random.uniform(0, 1) > 0.5: # crop_image, _ = Mirror(crop_image, label=None, symmetry=None) # if random.uniform(0, 1) > 0.0: # angle = random.uniform(-45, 45) # crop_image, _ = Rotate_aug(crop_image, label=None, angle=angle) # # if random.uniform(0, 1) > 0.5: # strength = random.uniform(0, 50) # crop_image, _ = Affine_aug(crop_image, strength=strength, label=None) # # if random.uniform(0, 1) > 0.5: # crop_image = self.color_augmentor(crop_image) # if random.uniform(0, 1) > 0.5: # crop_image = pixel_jitter(crop_image, 15) # if random.uniform(0, 1) > 0.5: # crop_image = Img_dropout(crop_image, 0.2) # # if random.uniform(0, 1) > 0.5: # crop_image = Padding_aug(crop_image, 0.3) # # #######head pose # # crop_image = crop_image.astype(np.float32) # # label=np.zeros(shape=cfg.MODEL.out_channel-2)-1 # label[-4:]=np.array(attr) # # ### index 0 means things come from keypoints, 1 is from celeba # weights = np.array([0, 1]) # # label = np.concatenate([label, weights], axis=0) return crop_image, label
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.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.5: boxes_ = boxes[:, 0:4] klass_ = boxes[:, 4:] angel = random.uniform(-30, 30) 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) 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_