def get_ground_truth_data(annotation_line, input_shape, augment=True, max_boxes=20): '''random preprocessing for real-time data augmentation''' line = annotation_line.split() image = Image.open(line[0]) image_size = image.size model_input_size = tuple(reversed(input_shape)) boxes = np.array( [np.array(list(map(int, box.split(',')))) for box in line[1:]]) if not augment: new_image, padding_size, offset = letterbox_resize( image, target_size=model_input_size, return_padding_info=True) image_data = np.array(new_image) / 255. # reshape boxes boxes = reshape_boxes(boxes, src_shape=image_size, target_shape=model_input_size, padding_shape=padding_size, offset=offset) # Get box parameters as (x_center, y_center, box_width, box_height, cls_id) #boxes = transform_box_info(boxes, model_input_size) if len(boxes) > max_boxes: boxes = boxes[:max_boxes] # fill in box data box_data = np.zeros((max_boxes, 5)) if len(boxes) > 0: box_data[:len(boxes)] = boxes return image_data, box_data # random resize image and crop|padding to target size image, padding_size, padding_offset = random_resize_crop_pad( image, target_size=model_input_size) # random horizontal flip image image, horizontal_flip = random_horizontal_flip(image) # random adjust brightness image = random_brightness(image) # random adjust color level image = random_chroma(image) # random adjust contrast image = random_contrast(image) # random adjust sharpness image = random_sharpness(image) # random convert image to grayscale image = random_grayscale(image) # random vertical flip image image, vertical_flip = random_vertical_flip(image) # random distort image in HSV color space image = random_hsv_distort(image) # reshape boxes based on augment boxes = reshape_boxes(boxes, src_shape=image_size, target_shape=model_input_size, padding_shape=padding_size, offset=padding_offset, horizontal_flip=horizontal_flip, vertical_flip=vertical_flip) # Get box parameters as (x_center, y_center, box_width, box_height, cls_id) #boxes = transform_box_info(boxes, model_input_size) if len(boxes) > max_boxes: boxes = boxes[:max_boxes] # prepare image & box data image_data = np.array(image) / 255. box_data = np.zeros((max_boxes, 5)) if len(boxes) > 0: box_data[:len(boxes)] = boxes return image_data, box_data
def get_ground_truth_data(annotation_line, input_shape, augment=True, max_boxes=100): '''random preprocessing for real-time data augmentation''' line = annotation_line.split() image = Image.open(line[0]) image_size = image.size model_input_size = tuple(reversed(input_shape)) boxes = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]]) if not augment: new_image, padding_size, offset = letterbox_resize(image, target_size=model_input_size, return_padding_info=True) image_data = np.array(new_image) image_data = normalize_image(image_data) # reshape boxes boxes = reshape_boxes(boxes, src_shape=image_size, target_shape=model_input_size, padding_shape=padding_size, offset=offset) if len(boxes)>max_boxes: boxes = boxes[:max_boxes] # fill in box data box_data = np.zeros((max_boxes,5)) if len(boxes)>0: box_data[:len(boxes)] = boxes return image_data, box_data # random resize image and crop|padding to target size image, padding_size, padding_offset = random_resize_crop_pad(image, target_size=model_input_size) # random horizontal flip image image, horizontal_flip = random_horizontal_flip(image) # random adjust brightness image = random_brightness(image) # random adjust color level image = random_chroma(image) # random adjust contrast image = random_contrast(image) # random adjust sharpness image = random_sharpness(image) # random convert image to grayscale image = random_grayscale(image) # random do normal blur to image #image = random_blur(image) # random do motion blur to image #image = random_motion_blur(image, prob=0.2) # random vertical flip image image, vertical_flip = random_vertical_flip(image) # random distort image in HSV color space # NOTE: will cost more time for preprocess # and slow down training speed #image = random_hsv_distort(image) # reshape boxes based on augment boxes = reshape_boxes(boxes, src_shape=image_size, target_shape=model_input_size, padding_shape=padding_size, offset=padding_offset, horizontal_flip=horizontal_flip, vertical_flip=vertical_flip) # random rotate image and boxes image, boxes = random_rotate(image, boxes) # random add gridmask augment for image and boxes image, boxes = random_gridmask(image, boxes) if len(boxes)>max_boxes: boxes = boxes[:max_boxes] # prepare image & box data image_data = np.array(image) image_data = normalize_image(image_data) box_data = np.zeros((max_boxes,5)) if len(boxes)>0: box_data[:len(boxes)] = boxes return image_data, box_data
def __getitem__(self, i): for n, (image_path, label_path) in enumerate( zip( self.image_path_list[i * self.batch_size:(i + 1) * self.batch_size], self.label_path_list[i * self.batch_size:(i + 1) * self.batch_size])): # Load image and label array image = cv2.imread( image_path, cv2.IMREAD_COLOR ) # cv2.IMREAD_COLOR/cv2.IMREAD_GRAYSCALE/cv2.IMREAD_UNCHANGED label = np.array(Image.open(label_path)) # we reset all the invalid label value as 0(background) in training, # but as 255(invalid) in eval if self.is_eval: label[label > (self.num_classes - 1)] = 255 else: label[label > (self.num_classes - 1)] = 0 # Do augmentation if self.augment: # random horizontal flip image image, label = random_horizontal_flip(image, label) # random vertical flip image image, label = random_vertical_flip(image, label) # random zoom & rotate image image, label = random_zoom_rotate(image, label) # random adjust brightness image = random_brightness(image) # random adjust color level image = random_chroma(image) # random adjust contrast image = random_contrast(image) # random adjust sharpness image = random_sharpness(image) # random convert image to grayscale image = random_grayscale(image) # random do gaussian blur to image image = random_blur(image) # random crop image & label image, label = random_crop(image, label, self.target_size) # random do histogram equalization using CLAHE image = random_histeq(image) # Resize image & label mask to model input shape image = cv2.resize(image, self.target_size) label = cv2.resize(label, self.target_size, interpolation=cv2.INTER_NEAREST) label = label.astype('int32') y = label.flatten() # we reset all the invalid label value as 0(background) in training, # but as 255(invalid) in eval if self.is_eval: y[y > (self.num_classes - 1)] = 255 else: y[y > (self.num_classes - 1)] = 0 # append input image and label array self.X[n] = image self.Y[n] = np.expand_dims(y, -1) ########################################################################### # # generating adaptive pixels weights array, for unbalanced classes training # ########################################################################### # Create adaptive pixels weights for all classes on one image, # according to pixel number of classes class_list = np.unique(y) if len(class_list): class_weights = class_weight.compute_class_weight( 'balanced', class_list, y) class_weights = { class_id: weight for class_id, weight in zip(class_list, class_weights) } # class_weigts dict would be like: # { # 0: 0.5997304983036035, # 12: 2.842871240958237, # 15: 1.0195474451419193 # } for class_id in class_list: np.putmask(self.PIXEL_WEIGHTS[n], y == class_id, class_weights[class_id]) # A trick of keras data generator: the last item yield # from a generator could be a sample weights array sample_weight_dict = {'pred_mask': self.PIXEL_WEIGHTS} if self.weighted_type == 'adaptive': return self.X, self.Y, sample_weight_dict else: return self.X, self.Y