def apply_to_mask(self, mask, selected_object, bbox, **params): if selected_object is None: if self.always_resize: mask = F.resize(mask, height=self.height, width=self.width, interpolation=cv2.INTER_NEAREST) return mask rmin, rmax, cmin, cmax = bbox mask = mask[rmin:rmax + 1, cmin:cmax + 1] if isinstance(selected_object, tuple): layer_indx, mask_id = selected_object obj_mask = mask[:, :, layer_indx] == mask_id new_mask = np.zeros_like(mask) new_mask[:, :, layer_indx][obj_mask] = mask_id else: obj_mask = mask == selected_object new_mask = mask.copy() new_mask[np.logical_not(obj_mask)] = 0 new_mask = F.resize(new_mask, height=self.height, width=self.width, interpolation=cv2.INTER_NEAREST) return new_mask
def apply(self, image, **params): height, width, _ = image.shape if width > 320 or height > 320: scale = random.random() * 0.4 + 0.2 image = functional.resize(image, height=int(height * scale), width=int(width * scale), interpolation=self.interpolation) return functional.resize(image, height=height, width=width, interpolation=self.interpolation) else: return image
def apply(self, img, selected_object, bbox, **params): if selected_object is None: if self.always_resize: img = F.resize(img, height=self.height, width=self.width) return img rmin, rmax, cmin, cmax = bbox img = img[rmin:rmax + 1, cmin:cmax + 1] img = F.resize(img, height=self.height, width=self.width) return img
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): self.img_width = img.shape[1] self.img_height = img.shape[0] return af.resize(img, height=self.height, width=self.width, interpolation=interpolation)
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): new_height, new_width = _compute_new_shape(self.length, params["rows"], params["cols"]) return F.resize(img, height=new_height, width=new_width, interpolation=interpolation)
def predict(model, from_file_names, batch_size, to_path, img_transform): loader = DataLoader(dataset=SaltDataset(from_file_names, transform=img_transform, mode='predict'), shuffle=False, batch_size=batch_size, num_workers=args.workers, pin_memory=torch.cuda.is_available()) with torch.no_grad(): for _, (inputs, paths) in enumerate(tqdm(loader, desc='Predict')): inputs = utils.cuda(inputs) outputs = model(inputs) for i, _ in enumerate(paths): factor = prepare_data.binary_factor t_mask = (F.sigmoid(outputs[i, 0]).data.cpu().numpy() * factor).astype(np.uint8) final_mask = center_crop(t_mask, 202, 202) final_mask = resize(final_mask, original_height, original_width, interpolation=cv2.INTER_AREA) to_path.mkdir(exist_ok=True, parents=True) cv2.imwrite(str(to_path / (Path(paths[i]).stem + '.png')), final_mask)
def albumentations(self, img): img = albumentations.random_crop(img, crop_height=64, crop_width=64, h_start=0, w_start=0) return albumentations.resize(img, height=512, width=512)
def fixed_size_rotate_crop( img, angle, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_REFLECT_101, value=None, ): original_height, original_width = img.shape[:2] rot_img = rotate_no_crop(img, angle, interpolation, border_mode, value) new_height, new_width = rotatedRectWithMaxArea(original_height, original_width, angle) cropped_img = F.center_crop(rot_img, new_height, new_width) resize_height, resize_width = get_resized_max_ratio( original_height, original_width, new_height, new_width) resized_img = F.resize( cropped_img, height=resize_height, width=resize_width, interpolation=interpolation, ) return F.center_crop(resized_img, original_height, original_width)
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): a, b, *_ = img.shape alpha = float(self.max_size) / max(a, b) n_height, n_width = int(a * alpha), int(b * alpha) return F.resize(img, height=n_height, width=n_width, interpolation=interpolation)
def test_resize_nearest_interpolation(target): img = np.array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]], dtype=np.uint8) expected = np.array([[1, 1], [3, 3]], dtype=np.uint8) img, expected = convert_2d_to_target_format([img, expected], target=target) resized_img = F.resize(img, 2, 2, interpolation=cv2.INTER_NEAREST) height, width = resized_img.shape[:2] assert height == 2 assert width == 2 assert np.array_equal(resized_img, expected)
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): height, width = img.shape[:2] new_height, new_width = self._compute_new_hw(height, width) return F.resize(img, height=new_height, width=new_width, interpolation=interpolation)
def test_resize_different_height_and_width(target): img = np.ones((100, 100), dtype=np.uint8) img = convert_2d_to_target_format([img], target=target) resized_img = F.resize(img, height=20, width=30) height, width = resized_img.shape[:2] assert height == 20 assert width == 30 if target == 'image': num_channels = resized_img.shape[2] assert num_channels == 3
def apply(self, img, crop_height=0, crop_width=0, h_start=0, w_start=0, interpolation=cv2.INTER_LINEAR, **params): img = img[h_start:h_start + crop_height, w_start:w_start + crop_width] return F.resize(img, self.height, self.width, interpolation)
def apply(self, img, new_height=0, new_width=0, interpolation=cv2.INTER_LINEAR, **params): return F.resize(img, height=new_height, width=new_width, interpolation=interpolation)
def test_resize_default_interpolation_float(target): img = np.array([[0.1, 0.1, 0.1, 0.1], [0.2, 0.2, 0.2, 0.2], [0.3, 0.3, 0.3, 0.3], [0.4, 0.4, 0.4, 0.4]], dtype=np.float32) expected = np.array([[0.15, 0.15], [0.35, 0.35]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) resized_img = F.resize(img, 2, 2) height, width = resized_img.shape[:2] assert height == 2 assert width == 2 assert_array_almost_equal_nulp(resized_img, expected)
def test_resize_nearest_interpolation_float(target): img = np.array([[0.1, 0.1, 0.1, 0.1], [0.2, 0.2, 0.2, 0.2], [0.3, 0.3, 0.3, 0.3], [0.4, 0.4, 0.4, 0.4]], dtype=np.float32) expected = np.array([[0.1, 0.1], [0.3, 0.3]], dtype=np.float32) img, expected = convert_2d_to_target_format([img, expected], target=target) resized_img = F.resize(img, 2, 2, interpolation=cv2.INTER_NEAREST) height, width = resized_img.shape[:2] assert height == 2 assert width == 2 assert np.array_equal(resized_img, expected)
def random_resize_and_stretch(self, img, new_width_range, stretch_limit = 0): new_width_range = T.to_tuple(new_width_range) stretch_limit = T.to_tuple(stretch_limit, bias=1) new_sz = int(random.uniform(new_width_range[0], new_width_range[1])) stretch = random.uniform(stretch_limit[0], stretch_limit[1]) img_max_sz = img.shape[1] #max(img.shape[0]*stretch, img.shape[1]) #img.shape[1] # GVNC - now it is resizing to max new_width = int(img.shape[1]*new_sz/img_max_sz) new_width = ((new_width+31)//32)*32 new_height = int(img.shape[0]*stretch*new_sz/img_max_sz) new_height = ((new_height+31)//32)*32 return albu_f.resize(img, height=new_height, width=new_width, interpolation=cv2.INTER_LINEAR)
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): if self.output_size == -1: return img input_shape = img.shape # We want X/Y = x/y and we have size = x*y so : ratio = input_shape[1] / input_shape[0] new_height = int(np.sqrt(self.output_size / ratio)) new_width = int(self.output_size / new_height) return F.resize( img, height=new_height, width=new_width, interpolation=interpolation )
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): height, width, _ = img.shape target_width = width * self.height // height if self.min_width: target_width = max(target_width, self.min_width) if self.max_width: target_width = min(target_width, self.max_width) return F.resize(img, height=self.height, width=target_width, interpolation=interpolation)
def apply(self, img, height_scale=1.0, width_scale=1.0, h_start=0, w_start=0, interpolation=cv2.INTER_LINEAR, pad_value=0, pad_loc_seed=None, **params): img_height, img_width = img.shape[:2] crop_height, crop_width = int(img_height * height_scale), int( img_width * width_scale) crop = self.random_crop(img, crop_height, crop_width, h_start, w_start, pad_value, pad_loc_seed) return F.resize(crop, img_height, img_width, interpolation)
def __call__(self, force_apply=True, **data) -> Tuple[torch.Tensor, torch.Tensor]: image, mask = data["image"], data["mask"] new_size = np.round( np.array(image.shape[:2]) / self.downsampling_factor).astype( np.int64) image = F.resize( image, height=new_size[0], width=new_size[1], interpolation=self.interpolation, ) # create output dictionary data["image"], data["mask"] = image, mask return data
def apply(self, img, **params): h, w = img.shape[:2] nh = int(h / self.rf) * self.rf nw = int(w / self.rf) * self.rf interpolation = cv2.INTER_LINEAR #cv2.INTER_NEAREST return AF.resize(img, nh, nw, interpolation)
def albumentations(self, img): return albumentations.resize(img, height=512, width=512)
def __call__(self, image: np.ndarray) -> np.ndarray: image = F.resize(image, height=self.height, width=self.width) color_features = self.color_spaces(image) features = self.extract_global_features(color_features) return features
def random_stretch_image(img, **params): # return functional.resize(img, (img.height, int(img.width * random.uniform(0.95, 1.05)))) height, width, _ = img.shape return AF.resize(img, height, int(width * random.uniform(0.95, 1.05)))
def rescale(img, scale: float, interpolation: int = cv2.INTER_AREA): h, w = img.shape[:2] h2, w2 = int(np.round(h * scale)), int(np.round(w * scale)) return AF.resize(img, h2, w2, interpolation)
def apply(self, img, interpolation=cv2.INTER_LINEAR, **params): # print('apply, ', params) return F.resize(img, height=params['height'], width=params['width'], interpolation=interpolation)
def resize_from_any(image: np.ndarray) -> np.ndarray: return resize(image, 101, 101)
def crop_resize_from_256(image: np.ndarray) -> np.ndarray: cropped = center_crop(image, 202, 202) return resize(cropped, 101, 101)
def preprocessing(image: np.array) -> torch.Tensor: height, width = 640, 640 image = resize(image, height, width) image = torch_tools.imagenet_normalize(image) return to_tensor(image).squeeze().unsqueeze(0)