def __getitem__(self, index: int) -> Dict[str, Any]: labels = self.labels[index] file_name = labels["file_name"] if self.image_path is None: image = load_rgb(labels["file_path"]) else: image = load_rgb(self.image_path / file_name) # annotations will have the format # 4: box, 10 landmarks, 1: landmarks / no landmarks num_annotations = 4 + 10 + 1 annotations = np.zeros((0, num_annotations)) image_height, image_width = image.shape[:2] for label in labels["annotations"]: annotation = np.zeros((1, num_annotations)) x_min, y_min, x_max, y_max = label["bbox"] annotation[0, 0] = np.clip(x_min, 0, image_width - 1) annotation[0, 1] = np.clip(y_min, 0, image_height - 1) annotation[0, 2] = np.clip(x_max, x_min + 1, image_width - 1) annotation[0, 3] = np.clip(y_max, y_min + 1, image_height - 1) if "landmarks" in label and label["landmarks"]: landmarks = np.array(label["landmarks"]) # landmarks annotation[0, 4:14] = landmarks.reshape(-1, 10) if annotation[0, 4] < 0: annotation[0, 14] = -1 else: annotation[0, 14] = 1 annotations = np.append(annotations, annotation, axis=0) if self.rotate90: image, annotations = random_rotate_90(image, annotations.astype(int)) image, annotations = self.preproc(image, annotations) image = self.transform(image=image)["image"] return { "image": tensor_from_rgb_image(image), "annotation": annotations.astype(np.float32), "file_name": file_name, }
def __getitem__(self, idx): image_path = self.file_paths[idx] raw_image = load_rgb(image_path, lib="cv2") image = raw_image.astype(np.float32) if self.origin_size: resize = 1 else: # testing scale im_shape = image.shape image_size_min = np.min(im_shape[:2]) image_size_max = np.max(im_shape[:2]) resize = float(self.target_size) / float(image_size_min) # prevent bigger axis from being more than max_size: if np.round(resize * image_size_max) > self.max_size: resize = float(self.max_size) / float(image_size_max) image = cv2.resize(image, None, None, fx=resize, fy=resize, interpolation=cv2.INTER_LINEAR) image = self.transform(image=image)["image"] return { "torched_image": tensor_from_rgb_image(image), "resize": resize, "raw_image": raw_image, "image_path": str(image_path), }
def __getitem__(self, idx: int) -> Optional[Dict[str, Any]]: image_path = self.file_paths[idx] image = load_rgb(image_path) image = self.transform(image=image)["image"] return {"torched_image": tensor_from_rgb_image(image), "image_path": str(image_path)}
def __getitem__(self, index: int) -> Dict[str, torch.Tensor]: image = load_rgb(self.image_paths[index]) image = self.transform(image=image)["image"] mask = generate_stroke_mask((image.shape[1], image.shape[0])) return { "image": tensor_from_rgb_image(image), "mask": torch.unsqueeze(torch.from_numpy(mask), 0) }
def process_image(file_path: Path, max_severity: int, output_dir: Path) -> None: image = load_rgb(file_path) for corruption in get_corruption_names(): for severity in range(max_severity): corrupted = corrupt(image, corruption_name=corruption, severity=severity + 1) corrupted = bgr2rgb(corrupted) cv2.imwrite( str(output_dir.joinpath(f"{file_path.stem}_{corruption}_{severity + 1}{file_path.suffix}")), corrupted )
def __getitem__(self, index: int) -> Dict[str, Any]: labels = self.labels[index] file_name = labels["file_name"] image = load_rgb(self.image_path / file_name) # annotations will have the format # 4: box, 10 landmarks, 1: landmarks / no landmarks, 1: mask / no_mask annotations = np.zeros((0, 16)) for label in labels["annotations"]: annotation = np.zeros((1, 16)) # bbox annotation[0, 0] = label["x_min"] annotation[0, 1] = label["y_min"] annotation[0, 2] = label["x_min"] + label["width"] annotation[0, 3] = label["y_min"] + label["height"] if label["landmarks"]: landmarks = np.array(label["landmarks"]) # landmarks annotation[0, 4:14] = landmarks[self.valid_annotation_indices] if annotation[0, 4] < 0: annotation[0, 14] = -1 else: annotation[0, 14] = 1 if "dlib_landmarks" in label and self.add_mask_prob is not None and random.random() < self.add_mask_prob: points = label["dlib_landmarks"] target_points, _, _ = extract_target_points_and_characteristic(np.array(points).astype(np.int32)) image = cv2.fillPoly(image, [target_points], color=random_color()) annotation[0, 15] = 1 else: annotation[0, 15] = 0 annotations = np.append(annotations, annotation, axis=0) target = np.array(annotations) image, target = self.preproc(image, target) image = albu.Compose( [ albu.RandomBrightnessContrast(brightness_limit=0.125, contrast_limit=(0.5, 1.5), p=0.5), albu.HueSaturationValue(hue_shift_limit=18, val_shift_limit=0, p=0.5), albu.Resize(height=self.image_size, width=self.image_size, p=1), albu.Normalize(p=1), ] )(image=image)["image"] return { "image": tensor_from_rgb_image(image), "annotation": target.astype(np.float32), "file_name": file_name, }
def __getitem__(self, idx: int) -> Dict[str, Any]: image_path = self.samples[idx] image = load_rgb(image_path, lib="cv2") # apply augmentations image = self.transform(image=image)["image"] return { "image_id": image_path.stem, "image": tensor_from_rgb_image(image), "image_path": str(image_path) }
def load_item(self, index): img = load_rgb(self.data[index]) if self.training: img = self.resize(img) else: img = self.resize(img, True, True, True) # load mask mask = self.load_mask(img, index) # augment data if self.augment and np.random.binomial(1, 0.5) > 0: img = img[:, ::-1, ...] mask = mask[:, ::-1, ...] return self.to_tensor(img), self.to_tensor(mask)
def __getitem__(self, idx: int) -> Dict[str, Any]: idx = idx % len(self.image_paths) image_path = self.image_paths[idx] image = load_rgb(image_path, lib="cv2") # apply augmentations image = self.transform(image=image)["image"] orientation = random.randint(0, 3) image = np.rot90(image, orientation) return {"image_id": image_path.stem, "features": tensor_from_rgb_image(image), "targets": orientation}
def __getitem__(self, idx: int) -> Dict[str, Any]: idx = idx % len(self.samples) image_path, class_id = self.samples[idx] image = load_rgb(image_path, lib="cv2") # apply augmentations image = self.transform(image=image)["image"] return { "image_id": image_path.stem, "features": tensor_from_rgb_image(image), "targets": class_id }
def __getitem__(self, index: int) -> Dict[str, Any]: labels = self.labels[index] file_name = labels["file_name"] image = load_rgb(self.image_path / file_name) # annotations will have the format # 4: box, 10 landmarks, 1: landmarks / no landmarks num_annotations = 4 + 10 + 1 annotations = np.zeros((0, num_annotations)) image_height, image_width = image.shape[:2] for label in labels["annotations"]: annotation = np.zeros((1, num_annotations)) # bbox annotation[0, 0] = np.clip(label["x_min"], 0, image_width - 1) annotation[0, 1] = np.clip(label["y_min"], 0, image_height - 1) annotation[0, 2] = np.clip(label["x_min"] + label["width"], 1, image_width - 1) annotation[0, 3] = np.clip(label["y_min"] + label["height"], 1, image_height - 1) if not 0 <= annotation[0, 0] < annotation[0, 2] < image_width: continue if not 0 <= annotation[0, 1] < annotation[0, 3] < image_height: continue if "landmarks" in label and label["landmarks"]: landmarks = np.array(label["landmarks"]) # landmarks annotation[0, 4:14] = landmarks[self.valid_annotation_indices] if annotation[0, 4] < 0: annotation[0, 14] = -1 else: annotation[0, 14] = 1 annotations = np.append(annotations, annotation, axis=0) image, target = self.preproc(image, annotations) image = self.transform(image=image)["image"] return { "image": tensor_from_rgb_image(image), "annotation": target.astype(np.float32), "file_name": file_name, }
def __getitem__(self, idx: int) -> Optional[Dict[str, Any]]: image_path = self.file_paths[idx] image = load_rgb(image_path) height, width = image.shape[:2] image = self.transform(image=image)["image"] pad_dict = pad_to_size((max(image.shape[:2]), max(image.shape[:2])), image) return { "torched_image": tensor_from_rgb_image(pad_dict["image"]), "image_path": str(image_path), "pads": pad_dict["pads"], "original_width": width, "original_height": height, }
def __getitem__(self, idx: int) -> Dict[str, Any]: image_path, mask_path = self.samples[idx] image = load_rgb(image_path) mask = load_grayscale(mask_path) # apply augmentations sample = self.transform(image=image, mask=mask) image, mask = sample["image"], sample["mask"] mask = (mask > 0).astype(np.uint8) mask = torch.from_numpy(mask) return { "image_id": image_path.stem, "features": tensor_from_rgb_image(image), "masks": torch.unsqueeze(mask, 0).float(), }
def main(): args = get_args() label_files = args.label_folder.rglob("*.png") output_image_folder = args.output_folder / "images" output_image_folder.mkdir(exist_ok=True, parents=True) output_label_folder = args.output_folder / "labels" output_label_folder.mkdir(exist_ok=True, parents=True) for label_file_name in tqdm(sorted(label_files)): label = load_rgb(image_path=label_file_name, lib="cv2") mask = (label == PERSON_PIXELS).all(axis=-1).astype(np.uint8) if mask.sum() == 0: continue shutil.copy( str(args.image_folder / f"{label_file_name.stem}.jpg"), str(output_image_folder / f"{label_file_name.stem}.jpg"), ) cv2.imwrite(str(output_label_folder / label_file_name.name), mask * 255)
def __getitem__(self, idx: int) -> Dict[str, Any]: image_path = self.file_names[idx] image = load_rgb(image_path) height, width = image.shape[:2] # Resize resizer = albu.Compose([albu.LongestMaxSize(max_size=768, p=1)], p=1) image = resizer(image=image)["image"] # pad image, pads = pad(image, factor=768) # apply augmentations image = self.transform(image=image)["image"] return { "image_id": image_path.stem, "features": tensor_from_rgb_image(image), "pads": np.array(pads).T, "height": height, "width": width, }
from PIL import Image import numpy as np import cv2 import torch import albumentations as albu from iglovikov_helper_functions.utils.image_utils import load_rgb, pad, unpad from iglovikov_helper_functions.dl.pytorch.utils import tensor_from_rgb_image from midv500models.pre_trained_models import create_model start = timeit.default_timer() model = create_model('Unet_resnet34_2020-05-19') model.eval() image = load_rgb(sys.argv[1]) # im = Image.fromarray(image) # im.save() transform = albu.Compose([albu.Normalize(p=1)], p=1) padded_image, pads = pad(image, factor=32, border=cv2.BORDER_CONSTANT) x = transform(image=padded_image)['image'] x = torch.unsqueeze(tensor_from_rgb_image(x), 0) with torch.no_grad(): prediction = model(x)[0][0] mask = (prediction > 0).cpu().numpy().astype(np.uint8) mask = unpad(mask, pads) # im = Image.fromarray(mask) # im.save()