def __call__(self, img, scale, flip=False, keep_ratio=True): """Call function of SegMapTransform.""" if keep_ratio: img = mmcv.imrescale(img, scale, interpolation='nearest') else: img = mmcv.imresize(img, scale, interpolation='nearest') if flip: img = mmcv.imflip(img) if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) return img
def _pad_img(self, results): for key in results.get('img_fields', ['img']): if self.size is not None: padded_img = mmcv.impad(results[key], self.size, self.pad_val) elif self.size_divisor is not None: padded_img = mmcv.impad_to_multiple( results[key], self.size_divisor, pad_val=self.pad_val) results[key] = padded_img results['pad_shape'] = padded_img.shape results['pad_fixed_size'] = self.size results['pad_size_divisor'] = self.size_divisor
def _pad_img(self, results): if self.size is not None: padded_img = mmcv.impad(results['img'], self.size) elif self.size_divisor is not None: padded_img = mmcv.impad_to_multiple(results['img'], self.size_divisor, pad_val=self.pad_val) results['img'] = padded_img results['pad_shape'] = padded_img.shape results['pad_fixed_size'] = self.size results['pad_size_divisor'] = self.size_divisor
def _pad_img(self, results): """Pad images according to ``self.size``.""" if self.size is not None: padded_img = mmcv.impad( results['img'], shape=self.size, pad_val=self.pad_val) elif self.size_divisor is not None: padded_img = mmcv.impad_to_multiple( results['img'], self.size_divisor, pad_val=self.pad_val) results['img'] = padded_img results['pad_shape'] = padded_img.shape results['pad_fixed_size'] = self.size results['pad_size_divisor'] = self.size_divisor
def __call__(self, img, scale, flip=False, v_flip=False, keep_ratio=True): if keep_ratio: img = mmcv.imrescale(img, scale, interpolation='nearest') else: img = mmcv.imresize(img, scale, interpolation='nearest') if flip: img = mmcv.imflip(img) if v_flip: img = mmcv.imflip(img, direction='vertical') if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) return img
def __call__(self, img, scale, flip=False): img, scale_factor = mmcv.imrescale(img, scale, return_scale=True) img_shape = img.shape img = mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) if flip: img = mmcv.imflip(img) if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) pad_shape = img.shape else: pad_shape = img_shape img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, scale_factor
def _pad_img(self, results): """Pad images according to ``self.size``.""" img = results["img"] if self.size is not None: padded_img = mmcv.impad(img, shape=self.size, pad_val=self.pad_val) elif self.size_divisor is not None: padded_img = mmcv.impad_to_multiple( img, self.size_divisor, pad_val=self.pad_val ) results["img"] = padded_img results["pad_shape"] = padded_img.shape results["pad_fixed_size"] = self.size results["pad_size_divisor"] = self.size_divisor
def _pad_img(self, results): els = ['ref_img', 'img'] if 'ref_img' in results else ['img'] for el in els: if self.size is not None: padded_img = mmcv.impad(results['img'], self.size) elif self.size_divisor is not None: padded_img = mmcv.impad_to_multiple(results[el], self.size_divisor, pad_val=self.pad_val) results[el] = padded_img results['pad_shape'] = padded_img.shape results['pad_fixed_size'] = self.size results['pad_size_divisor'] = self.size_divisor
def _pad_sequence(self, results): """Pad images according to ``self.size``.""" if self.size is not None: padded_sequence_imgs = [ mmcv.impad(img, shape=self.size, pad_val=self.pad_val) for img in results['sequence_imgs'] ] elif self.size_divisor is not None: padded_sequence_imgs = [ mmcv.impad_to_multiple(img, self.size_divisor, pad_val=self.pad_val) for img in results['sequence_imgs'] ] results['sequence_imgs'] = padded_sequence_imgs
def __call__(self, img, scale, flip=False, keep_ratio=True, hsv_h=0, hsv_s=0, hsv_v=0, noisy_mode=None, blur_mode=None): # Augment colorspace if hsv_h+hsv_s+hsv_v > 5: # SV augmentation by 50% img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # hue, sat, val H = img_hsv[:, :, 0].astype(np.float32) # hue S = img_hsv[:, :, 1].astype(np.float32) # saturation V = img_hsv[:, :, 2].astype(np.float32) # value a = random.uniform(-1, 1) * hsv_h + 1 b = random.uniform(-1, 1) * hsv_s + 1 c = random.uniform(-1, 1) * hsv_v + 1 H *= a S *= b V *= c img_hsv[:, :, 0] = H if a < 1 else H.clip(None, 255) img_hsv[:, :, 1] = S if b < 1 else S.clip(None, 255) img_hsv[:, :, 2] = V if c < 1 else V.clip(None, 255) cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # Add noise if noisy_mode is not None: img = self.add_noise(img, noisy_mode) # Blur if blur_mode is not None: img = self.opencv_blur(img, blur_mode) if keep_ratio: img, scale_factor = mmcv.imrescale(img, scale, return_scale=True) else: img, w_scale, h_scale = mmcv.imresize( img, scale, return_scale=True) scale_factor = np.array([w_scale, h_scale, w_scale, h_scale], dtype=np.float32) img_shape = img.shape img = mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) if flip: img = mmcv.imflip(img) if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) pad_shape = img.shape else: pad_shape = img_shape img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, scale_factor
def evaluate_single(net: STMask, im_path=None, save_path=None, idx=None): im = mmcv.imread(im_path) ori_shape = im.shape im, w_scale, h_scale = mmcv.imresize(im, (640, 360), return_scale=True) img_shape = im.shape if cfg.backbone.transform.normalize: im = (im - MEANS) / STD elif cfg.backbone.transform.subtract_means: im = (im - MEANS) elif cfg.backbone.transform.to_float: im = im / 255. im = mmcv.impad_to_multiple(im, 32) pad_shape = im.shape im = torch.tensor(im).permute(2, 0, 1).contiguous().unsqueeze(0).float().cuda() pad_h, pad_w = im.size()[2:4] img_meta = { 'ori_shape': ori_shape, 'img_shape': img_shape, 'pad_shape': pad_shape } if idx is not None: img_meta['frame_id'] = idx if idx is None or idx == 0: img_meta['is_first'] = True else: img_meta['is_first'] = False preds = net(im, img_meta=[img_meta]) preds[0]['detection']['box_ids'] = torch.arange( preds[0]['detection']['box'].size(0)) cfg.preserve_aspect_ratio = True img_numpy = prep_display(preds[0], im[0], pad_h, pad_w, img_meta=img_meta, img_ids=(0, idx)) if save_path is None: plt.imshow(img_numpy) plt.axis('off') plt.show() else: cv2.imwrite(save_path, img_numpy)
def __call__(self, img, scale, flip=False, keep_ratio=True): if keep_ratio: img, scale_factor = mmcv.imrescale(img, scale, return_scale=True, interpolation='nearest') else: img, w_scale, h_scale = mmcv.imresize( img, scale, return_scale=True) scale_factor = np.array([w_scale, h_scale, w_scale, h_scale], dtype=np.float32) img_shape = img.shape if flip: img = mmcv.imflip(img) if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) pad_shape = img.shape else: pad_shape = img_shape img = img[np.newaxis,:,:] return img
def __call__(self, img, scale, flip=False, crop_info=None, keep_ratio=True): # image jittering try: img = Image.fromarray(img) except: print(img) if hasattr(self, 'random_color'): img = self.random_color(img) if hasattr(self, 'random_contrast'): img = self.random_contrast(img) if hasattr(self, 'random_sharpness'): img = self.random_sharpness(img) img = np.array(img) if keep_ratio: img, scale_factor = mmcv.imrescale(img, scale, return_scale=True) else: img, w_scale, h_scale = mmcv.imresize(img, scale, return_scale=True) scale_factor = np.array([w_scale, h_scale, w_scale, h_scale], dtype=np.float32) img_shape = img.shape img = mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) if flip: img = mmcv.imflip(img) if crop_info is not None: # if crop, no need to pad cx1, cy1, cx2, cy2 = crop_info img = img[cy1:cy2, cx1:cx2] pad_shape = img.shape # pad and set pad_shape if crop_info is None and self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) pad_shape = img.shape else: pad_shape = img_shape img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, scale_factor
def __call__(self, img, scale, flip=False, keep_ratio=True): # 1. rescale/resize the image to expected size if keep_ratio: # Resize image while keeping the aspect ratio. # The image will be rescaled as large as possible within the scale. img, scale_factor = mmcv.imrescale( img=img, scale=scale, return_scale=True, interpolation='bilinear', ) else: # Resize image to a given size ignoring the aspect ratio. img, w_scale, h_scale = mmcv.imresize( img=img, size=scale, return_scale=True, interpolation='bilinear', ) scale_factor = np.array( [w_scale, h_scale, w_scale, h_scale], dtype=np.float32 ) # 2. normalize the image img_shape = img.shape img = mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) # 3. flip the image (if needed) if flip: img = mmcv.imflip(img) # 4. pad the image if size_divisor is not None. # size_divisor=32 means sizes are multiplier of 32. if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) pad_shape = img.shape else: pad_shape = img_shape # 5. transpose to (c, h, w) img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, scale_factor
def __call__(self, img, scale, flip=False, keep_ratio=True): if keep_ratio: img, scale_factor = mmcv.imrescale(img, scale, return_scale=True)# scale 包括一个长边一个短边,顺序无所谓 else:# 做识别 不需要保持ratio不变,resize到 224 img, w_scale, h_scale = mmcv.imresize( img, scale, return_scale=True) scale_factor = np.array([w_scale, h_scale, w_scale, h_scale], dtype=np.float32) img_shape = img.shape img = mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) if flip: img = mmcv.imflip(img) if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor)# pad 边,为了每条边都能被divisor这个数字整除,比如 32 pad_shape = img.shape # pad后的图片形状 else: pad_shape = img_shape img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, scale_factor
def __call__(self, img, scale, flip=False, keep_ratio=True): if keep_ratio: img, scale_factor = mmcv.imrescale(img, scale, return_scale=True) else: img, w_scale, h_scale = mmcv.imresize( img, scale, return_scale=True) scale_factor = np.array( [w_scale, h_scale, w_scale, h_scale], dtype=np.float32) img_shape = img.shape img = mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) # (x - mean) / std , brg2rgb if flip: img = mmcv.imflip(img) # flip images if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) # padding image to make sure divided by divisor. pad_shape = img.shape else: pad_shape = img_shape img = img.transpose(2, 0, 1) # HWC to CHW return img, img_shape, pad_shape, scale_factor
def __call__(self, img, scale, flip=False, keep_ratio=True): if keep_ratio: img, scale_factor = mmcv.imrescale(img, scale, return_scale=True) else: img, w_scale, h_scale = mmcv.imresize( img, scale, return_scale=True) scale_factor = np.array([w_scale, h_scale, w_scale, h_scale], dtype=np.float32) img_shape = img.shape img = mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) if flip: img = mmcv.imflip(img) if self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor) pad_shape = img.shape else: pad_shape = img_shape img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, scale_factor
def _pad_img(self, results): for key in results.get('img_fields', ['img']): images, img_num = [], results[key].shape[-1] if self.size is not None: for index in range(img_num): images.append( mmcv.impad(results[key][:, :, :, index], self.size, self.pad_val)) elif self.size_divisor is not None: for index in range(img_num): images.append( mmcv.impad_to_multiple(results[key][:, :, :, index], self.size_divisor, pad_val=self.pad_val)) images = np.stack(images, axis=-1) results[key] = images results['pad_shape'] = images.shape results['pad_fixed_size'] = self.size results['pad_size_divisor'] = self.size_divisor
def stretch(img, img_shape, gt_bboxes, gt_bboxes_8_coo, stretch_ratio, size_divisor): img = img.transpose(1, 2, 0) img = img[:img_shape[0], :img_shape[1], :] # crop from padded image height = int(img_shape[0] * stretch_ratio[1]) width = int(img_shape[1] * stretch_ratio[0]) img = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC) gt_bboxes[:, [0, 2]] = gt_bboxes[:, [0, 2]] * (width / img_shape[1]) gt_bboxes[:, [1, 3]] = gt_bboxes[:, [1, 3]] * (height / img_shape[0]) gt_bboxes_8_coo[:, 0::2] = gt_bboxes_8_coo[:, 0::2] * (width / img_shape[1]) gt_bboxes_8_coo[:, 1::2] = gt_bboxes_8_coo[:, 1::2] * (width / img_shape[1]) img_shape = (height, width, 3) img = mmcv.impad_to_multiple(img, size_divisor) pad_shape = img.shape img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, gt_bboxes, gt_bboxes_8_coo
def get_img(img_meta): img = cv2.imread(img_meta[0]['filename']) # Resize sf = img_meta[0]['scale_factor'] img, scale_factor = mmcv.imrescale(img, sf, True) # Normalize m = img_meta[0]['img_norm_cfg']['mean'] s = img_meta[0]['img_norm_cfg']['std'] t = img_meta[0]['img_norm_cfg']['to_rgb'] img = mmcv.imnormalize(img, m, s, t) # Pad sd = 32 # size_divisor img = mmcv.impad_to_multiple(img, 32, 0) # H x W x C -> C x H x W and expand an dim img = torch.from_numpy(np.transpose(img, (2, 0, 1))).expand(1, -1, -1, -1) return img
def crop(img, img_shape, crop_ratio, gt_bboxes, gt_labels, size_divisor): img = copy.deepcopy(img) img_shape = copy.deepcopy(img_shape) gt_bboxes = copy.deepcopy(gt_bboxes) gt_labels = copy.deepcopy(gt_labels) img = img.transpose(1, 2, 0) height, width, _ = img_shape left = np.random.randint(width - int(width * crop_ratio)) top = np.random.randint(height - int(height * crop_ratio)) img = img[top:top + int(height * crop_ratio), left:left + int(width * crop_ratio), :] # crop image img_shape = img.shape # adjust gt bboxes to fit croped image. i = 0 while (i < gt_bboxes.shape[0]): if (gt_bboxes[i, 0] > left + width) or (gt_bboxes[i, 2] < left) or ( gt_bboxes[i, 1] > top + height) or (gt_bboxes[i, 3] < top): # gt bboxes out of range of croped image gt_bboxes = np.delete(gt_bboxes, i, axis=0) gt_labels = np.delete(gt_labels, i) else: # gt bboxes in croped image or over range of some parts if gt_bboxes[i, 0] < left: gt_bboxes[i, 0] = left if gt_bboxes[i, 2] > left + width - 1: gt_bboxes[i, 2] = left + width - 1 if gt_bboxes[i, 1] < top: gt_bboxes[i, 1] = top if gt_bboxes[i, 3] > top + height - 1: gt_bboxes[i, 3] = top + height - 1 i = i + 1 gt_bboxes[:, [0, 2]] = gt_bboxes[:, [0, 2]] - left gt_bboxes[:, [1, 3]] = gt_bboxes[:, [1, 3]] - top img = mmcv.impad_to_multiple(img, size_divisor) pad_shape = img.shape img = img.transpose(2, 0, 1) return img, img_shape, pad_shape, gt_bboxes, gt_labels
def __call__(self, img, scale, flip=False, crop_info=None, keep_ratio=True, pad_val=0): if keep_ratio: img = mmcv.imrescale(img, scale, interpolation='nearest') else: img = mmcv.imresize(img, scale, interpolation='nearest') if flip: img = mmcv.imflip(img) if crop_info is not None: # if crop, no need to pad cx1, cy1, cx2, cy2 = crop_info img = img[cy1:cy2, cx1:cx2] elif self.size_divisor is not None: img = mmcv.impad_to_multiple(img, self.size_divisor, pad_val=pad_val) return img
def processing_one_image(file_path): img_meta = {} img_meta['filename'] = file_path img_meta['ori_filename'] = file_path img_meta['flip'] = False # 1. Read image file_client = mmcv.FileClient(backend='disk') img_bytes = file_client.get(file_path) orig_img = mmcv.imfrombytes(img_bytes, flag='color') # BGR order img_meta['ori_shape'] = orig_img.shape # 2. Resize test_scale = (1333, 800) img, scale_factor = mmcv.imrescale(orig_img, test_scale, return_scale=True) # the w_scale and h_scale has minor difference # a real fix should be done in the mmcv.imrescale in the future new_h, new_w = img.shape[:2] h, w = orig_img.shape[:2] w_scale = new_w / w h_scale = new_h / h scale_factor = np.array([w_scale, h_scale, w_scale, h_scale], dtype=np.float32) img_meta['scale_factor'] = scale_factor img_meta['img_shape'] = img.shape # 3. Normalize # mean = np.array([102.9801, 115.9465, 122.7717], dtype=np.float32) # std = np.array([1.0, 1.0, 1.0], dtype=np.float32) mean = np.array([103.53, 116.28, 123.675], dtype=np.float32) std = np.array([1.0, 1.0, 1.0], dtype=np.float32) to_rgb = False img = mmcv.imnormalize(img, mean, std, to_rgb) img_meta['img_norm_cfg'] = dict(mean=mean, std=std, to_rgb=to_rgb) # 4. Pad img = mmcv.impad_to_multiple(img, divisor=32, pad_val=0) img_meta["pad_shape"] = img.shape # 5. ToTensor img = torch.from_numpy(img.transpose(2, 0, 1)) return img, img_meta
def __call__(self, img_group, scale, crop_history=None, flip=False, rotate=None, keep_ratio=True, dropout_prob=None, div_255=False, transpose=True, stack=True): # 1. rescale if keep_ratio: tuple_list = [ mmcv.imrescale(img, scale, return_scale=True) for img in img_group ] img_group, scale_factors = list(zip(*tuple_list)) scale_factor = scale_factors[0] else: tuple_list = [ mmcv.imresize(img, scale, return_scale=True) for img in img_group ] img_group, w_scales, h_scales = list(zip(*tuple_list)) scale_factor = np.array( [w_scales[0], h_scales[0], w_scales[0], h_scales[0]], dtype=np.float32) # 2. rotate if rotate is not None: img_group = [mmcv.imrotate(img, rotate) for img in img_group] # 3. crop (if necessary) if crop_history is not None: self.op_crop = GroupCrop(crop_history) if self.op_crop is not None: img_group, crop_quadruple = self.op_crop(img_group) else: crop_quadruple = None img_shape = img_group[0].shape # 4. flip if flip: img_group = [mmcv.imflip(img) for img in img_group] # 5a. extra augmentation if self.extra_augm is not None: img_group = self.extra_augm(img_group) # 5b. coarse dropout if self.dropout_scale is not None and dropout_prob is not None and dropout_prob > 0.0: dropout_mask = self._coarse_dropout_mask(img_group[0].shape, dropout_prob, self.dropout_scale) img_group = [img * dropout_mask for img in img_group] # 6a. div_255 if div_255: img_group = [ mmcv.imnormalize(img, 0, 255, False) for img in img_group ] # 6b. normalize if self.mean is not None and self.std is not None: img_group = [ mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) for img in img_group ] elif self.to_rgb: img_group = [mmcv.bgr2rgb(img) for img in img_group] # 7. pad if self.size_divisor is not None: img_group = [ mmcv.impad_to_multiple(img, self.size_divisor) for img in img_group ] pad_shape = img_group[0].shape else: pad_shape = img_shape # 8. transpose if transpose: img_group = [img.transpose((2, 0, 1)) for img in img_group] # 9. stack into numpy.array if stack: img_group = np.stack(img_group, axis=0) return img_group, img_shape, pad_shape, scale_factor, crop_quadruple
def __call__(self, results): # if self.resize_crop or self.rescale_crop: # img_group, crop_quadruple = self.op_crop(img_group) # img_shape = img_group[0].shape # scale_factor = None # else: # 1. rescale # if keep_ratio: img_group = results['img_group'] tuple_list = [ mmcv.imrescale(img, self.scale, return_scale=True) for img in img_group ] img_group, scale_factors = list(zip(*tuple_list)) scale_factor = scale_factors[0] # else: # tuple_list = [mmcv.imresize( # img, scale, return_scale=True) for img in img_group] # img_group, w_scales, h_scales = list(zip(*tuple_list)) # scale_factor = np.array([w_scales[0], h_scales[0], # w_scales[0], h_scales[0]], # dtype=np.float32) # 2. crop (if necessary) # if crop_history is not None: # self.op_crop = GroupCrop(crop_history) # if self.op_crop is not None: # img_group, crop_quadruple = self.op_crop( # img_group, is_flow=is_flow) # else: crop_quadruple = None img_shape = img_group[0].shape # # 3. flip # if flip: # img_group = [mmcv.imflip(img) for img in img_group] # if is_flow: # for i in range(0, len(img_group), 2): # img_group[i] = mmcv.iminvert(img_group[i]) # # 4a. div_255 # if div_255: # img_group = [mmcv.imnormalize(img, 0, 255, False) # for img in img_group] # 4. normalize img_group = [ mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) for img in img_group ] # 5. pad if self.size_divisor is not None: img_group = [ mmcv.impad_to_multiple(img, self.size_divisor) for img in img_group ] pad_shape = img_group[0].shape else: pad_shape = img_shape # if is_flow: # assert len(img_group[0].shape) == 2 # img_group = [np.stack((flow_x, flow_y), axis=2) # for flow_x, flow_y in zip( # img_group[0::2], img_group[1::2])] # 6. transpose img_group = [img.transpose(2, 0, 1) for img in img_group] # Stack into numpy.array img_group = np.stack(img_group, axis=0) img_group = to_tensor(img_group) img_group = np.transpose(img_group, (1, 0, 2, 3)) results['img_group_0'] = [img_group] #img_group results['img_shape'] = img_shape results['pad_shape'] = pad_shape results['scale_factor'] = scale_factor results['crop_quadruple'] = crop_quadruple results['flip'] = False return results
def __call__(self, img_group, scale, crop_history=None, flip=False, keep_ratio=True, div_255=False, is_flow=False, interpolation='bilinear', normalize=True, more_aug=False): # 1. rescale if keep_ratio: tuple_list = [ mmcv.imrescale(img, scale, return_scale=True, interpolation=interpolation) for img in img_group ] img_group, scale_factors = list(zip(*tuple_list)) scale_factor = scale_factors[0] else: tuple_list = [ mmcv.imresize(img, scale, return_scale=True, interpolation=interpolation) for img in img_group ] img_group, w_scales, h_scales = list(zip(*tuple_list)) scale_factor = np.array( [w_scales[0], h_scales[0], w_scales[0], h_scales[0]], dtype=np.float32) # 2. crop (if necessary) if crop_history is not None: self.op_crop = GroupCrop(crop_history, input_size=self.crop_size, resize=True) if self.op_crop is not None and isinstance( self.op_crop, (GroupCrop, GroupMultiScaleCrop)): img_group, crop_quadruple = self.op_crop( img_group, is_flow=is_flow, interpolation=interpolation) elif self.op_crop is not None: img_group, crop_quadruple = self.op_crop(img_group, is_flow=is_flow) else: crop_quadruple = None img_shape = img_group[0].shape if more_aug: seq = iaa.Sequential([ iaa.GaussianBlur(sigma=np.random.uniform(0, 5)), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05 * 255), per_channel=0.5), ]) img_group = seq(images=np.array(img_group)) # 3. flip if flip: img_group = [mmcv.imflip(img) for img in img_group] if is_flow: for i in range(0, len(img_group), 2): img_group[i] = mmcv.iminvert(img_group[i]) # 4a. div_255 if div_255: img_group = [ mmcv.imnormalize(img, 0, 255, False) for img in img_group ] # 4. normalize if normalize: img_group = [ mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) for img in img_group ] # 5. pad if self.size_divisor is not None: img_group = [ mmcv.impad_to_multiple(img, self.size_divisor) for img in img_group ] pad_shape = img_group[0].shape else: pad_shape = img_shape if is_flow: assert len(img_group[0].shape) == 2 img_group = [ np.stack((flow_x, flow_y), axis=2) for flow_x, flow_y in zip(img_group[0::2], img_group[1::2]) ] # 6. transpose if len(img_shape) == 2: img_group = [img[:, :, np.newaxis] for img in img_group] img_group = [img.transpose(2, 0, 1) for img in img_group] # Stack into numpy.array img_group = np.stack(img_group, axis=0) return img_group, img_shape, pad_shape, scale_factor, crop_quadruple
def __call__(self, img_group, scale, crop_history=None, flip=False, keep_ratio=True, div_255=False, is_flow=False): if self.resize_crop or self.rescale_crop: img_group, crop_quadruple = self.op_crop(img_group) img_shape = img_group[0].shape scale_factor = None else: # 1. rescale if keep_ratio: tuple_list = [ mmcv.imrescale(img, scale, return_scale=True) for img in img_group ] img_group, scale_factors = list(zip(*tuple_list)) scale_factor = scale_factors[0] else: tuple_list = [ mmcv.imresize(img, scale, return_scale=True) for img in img_group ] img_group, w_scales, h_scales = list(zip(*tuple_list)) scale_factor = np.array( [w_scales[0], h_scales[0], w_scales[0], h_scales[0]], dtype=np.float32) # 2. crop (if necessary) if crop_history is not None: self.op_crop = GroupCrop(crop_history) if self.op_crop is not None: img_group, crop_quadruple = self.op_crop(img_group, is_flow=is_flow) else: crop_quadruple = None img_shape = img_group[0].shape # 3. flip if flip: img_group = [mmcv.imflip(img) for img in img_group] if is_flow: for i in range(0, len(img_group), 2): img_group[i] = mmcv.iminvert(img_group[i]) # 4a. div_255 if div_255: img_group = [ mmcv.imnormalize(img, 0, 255, False) for img in img_group ] # 4. normalize img_group = [ mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) for img in img_group ] # 5. pad if self.size_divisor is not None: img_group = [ mmcv.impad_to_multiple(img, self.size_divisor) for img in img_group ] pad_shape = img_group[0].shape else: pad_shape = img_shape if is_flow: assert len(img_group[0].shape) == 2 img_group = [ np.stack((flow_x, flow_y), axis=2) for flow_x, flow_y in zip(img_group[0::2], img_group[1::2]) ] # 6. transpose img_group = [img.transpose(2, 0, 1) for img in img_group] # Stack into numpy.array img_group = np.stack(img_group, axis=0) return img_group, img_shape, pad_shape, scale_factor, crop_quadruple
# flip the image vertically mmcv.imflip(img, direction='vertical') # ============Crop============= # crop the region (10, 10, 100, 120) bboxes = np.array([10, 10, 100, 120]) patch = mmcv.imcrop(img, bboxes) # crop two regions (10, 10, 100, 120) and (0, 0, 50, 50) bboxes = np.array([[10, 10, 100, 120], [0, 0, 50, 50]]) patches = mmcv.imcrop(img, bboxes) # crop two regions, and rescale the patches by 1.2x patches = mmcv.imcrop(img, bboxes, scale_ratio=1.2) # =============Padding============= # pad the image to (1000, 1200) with all zeros img_ = mmcv.impad(img, (1000, 1200), pad_val=0) # pad the image to (1000, 1200) with different values for three channels. img_ = mmcv.impad(img, (1000, 1200), pad_val=[100, 50, 200]) # pad an image so that each edge is a multiple of some value. img_ = mmcv.impad_to_multiple(img, 32)
def __call__(self, img_group, scale, crop_history=None, flip=False, keep_ratio=True, div_255=False, is_flow=False, image_name=None): if self.resize_crop or self.rescale_crop: if self.afterdetect_resize: img_group = [ cv2.resize(img, self.afterdetect_resize) for img in img_group ] img_group, crop_quadruple = self.op_crop(img_group) img_shape = img_group[0].shape scale_factor = None else: # 1. rescale if keep_ratio: tuple_list = [ mmcv.imrescale(img, scale, return_scale=True) for img in img_group ] img_group, scale_factors = list(zip(*tuple_list)) scale_factor = scale_factors[0] else: tuple_list = [ mmcv.imresize(img, scale, return_scale=True) for img in img_group ] img_group, w_scales, h_scales = list(zip(*tuple_list)) scale_factor = np.array( [w_scales[0], h_scales[0], w_scales[0], h_scales[0]], dtype=np.float32) if self.pre_mean_volume is not None: volume_len = self.pre_mean_volume.shape[0] img_group = [ img - self.pre_mean_volume[i % volume_len, ...] for i, img in enumerate(img_group) ] # 2. crop (if necessary) if crop_history is not None: self.op_crop = GroupCrop(crop_history) if self.op_crop is not None: img_group, crop_quadruple = self.op_crop(img_group, is_flow=is_flow) else: crop_quadruple = None img_shape = img_group[0].shape # import matplotlib.pyplot as plt # import os # for i in range(len(img_group)): # plt.imshow(img_group[i]) # save_dir = os.path.join("show_fig",image_name.split("/")[-1]) # if not os.path.exists(save_dir): # os.mkdir(save_dir) # plt.savefig(os.path.join(save_dir,"{}".format(i))) # 3. flip if flip: img_group = [mmcv.imflip(img) for img in img_group] if is_flow: for i in range(0, len(img_group), 2): img_group[i] = mmcv.iminvert(img_group[i]) # 4a. div_255 if div_255: img_group = [ mmcv.imnormalize(img, 0, 255, False) for img in img_group ] # 4. normalize img_group = [ mmcv.imnormalize(img, self.mean, self.std, self.to_rgb) for img in img_group ] # 5. pad if self.size_divisor is not None: img_group = [ mmcv.impad_to_multiple(img, self.size_divisor) for img in img_group ] pad_shape = img_group[0].shape else: pad_shape = img_shape if is_flow: assert len(img_group[0].shape) == 2 img_group = [ np.stack((flow_x, flow_y), axis=2) for flow_x, flow_y in zip(img_group[0::2], img_group[1::2]) ] # 6. transpose img_group = [img.transpose(2, 0, 1) for img in img_group] # Stack into numpy.array img_group = np.stack(img_group, axis=0) return img_group, img_shape, pad_shape, scale_factor, crop_quadruple