def __getitem__(self, idx: int) -> dict: #### result = {} image_path = self.images[idx] image = utils.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if np.shape(image)[1] != 512: image = cv2.resize(image, (512, 512)) if self.masks is not None: mask = utils.imread(self.masks[idx]) if np.shape(mask)[1] != 512: mask = cv2.resize(mask, (512, 512)) # extract certain classes from mask (e.g. cars) mask_kidney = mask[:, :, 0] == 255 mask_tumor = mask[:, :, 1] == 255 masks = [mask_kidney, mask_tumor] mask = np.stack(masks, axis=-1).astype('float') result["mask"] = mask result["image"] = image if self.transforms is not None: result = self.transforms(**result) result["filename"] = image_path.name return result
def preprocess(self, image_path: Path): try: if self.extension in ("jpg", "JPG", "jpeg", "JPEG"): image = np.array( imread(uri=image_path, grayscale=self.grayscale, expand_dims=self.expand_dims, exifrotate=not self.clear_exif)) else: # imread does not have exifrotate for non-jpeg type image = np.array( imread(uri=image_path, grayscale=self.grayscale, expand_dims=self.expand_dims)) except Exception as e: print(f"Cannot read file {image_path}, exception: {e}") return if self.max_size is not None: image = longest_max_size(image, self.max_size, self.interpolation) target_path = self.out_dir / image_path.relative_to(self.in_dir) target_path.parent.mkdir(parents=True, exist_ok=True) image = image.clip(0, 255).round().astype(np.uint8) imwrite(target_path, image)
def show(index: int, images: List[Path], masks: List[Path], transforms=None) -> None: image_path = images[index] name = image_path.name image = utils.imread(image_path) mask = utils.imread(masks[index]) if transforms is not None: temp = transforms(image=image, mask=mask) image = temp["image"] mask = temp["mask"] show_examples(name, image, mask)
def preprocess(self, filepath: Path) -> None: """Process one file.""" try: image = np.array(utils.imread(filepath)) except Exception as e: logger.warning(f"Cannot read file {filepath}, exception: {e}") return filename, extention = filepath.stem, filepath.suffix out_dir = (self.out_dir / filepath.relative_to(self.in_dir)).parent out_dir.mkdir(parents=True, exist_ok=True) patches = cut_with_overlap( image=image.clip(0, 255).round().astype(np.uint8), patch_height=self.patch_height, patch_width=self.patch_width, height_overlap=self.height_overlap, width_overlap=self.width_overlap, min_height=self.min_height, min_width=self.min_width, ) for index, patch in enumerate(patches): out_path = out_dir / f"{filename}_{index}{extention}" utils.imwrite(uri=out_path, im=patch)
def __getitem__(self, idx: int) -> dict: image_path = self.images[idx] name = image_path.name image = utils.imread(image_path) mask = None if self.transforms is not None: if self.masks is not None: mask = imread(self.mask_path / f"{os.path.splitext(name)[0]}.png") transformed = self.transforms(image=image, mask=mask) image_tf = transformed["image"] mask_tf = transformed["mask"] result = {"image": image_tf, "mask": mask_tf} else: transformed = self.transforms( image=image, ) image_tf = transformed["image"] result = {"image": image_tf} else: result = {"image": image, "mask": mask} result["filename_img"] = image_path.name result["filename_mask"] = f"{os.path.splitext(name)[0]}.png" return result
def preprocess(self, image_path: Path): """@TODO: Docs. Contribution is welcome.""" try: _, extension = os.path.splitext(image_path) kwargs = { "grayscale": self.grayscale, "expand_dims": self.expand_dims, } if extension.lower() in {"jpg", "jpeg"}: # imread does not have exifrotate for non-jpeg type kwargs["exifrotate"] = not self.clear_exif image = np.array(imread(uri=image_path, **kwargs)) except Exception as e: print(f"Cannot read file {image_path}, exception: {e}") return if self.max_size is not None: image = longest_max_size(image, self.max_size, self.interpolation) target_path = self.out_dir / image_path.relative_to(self.in_dir) target_path.parent.mkdir(parents=True, exist_ok=True) image = image.clip(0, 255).round().astype(np.uint8) imwrite(target_path, image)
def save_image(image, mask, path, orig_shape): mask_path = f"{PurePath(path).parent}/{PurePath(path).stem}_mask.png" colorize_mask(mask, palete).save(mask_path) image = albu.Resize(512, 512)(image=image)['image'] mask = imread(mask_path) out_im = image // 2 + mask // 2 io.imsave(path, albu.Resize(*orig_shape)(image=out_im)['image']) return mask_path
def read_random_images(data, transforms=None) -> List[Tuple[str, np.ndarray]]: data_ = np.random.choice(data, size=4) result = [] for d in data_: image = imread(d['filepath']) if transforms is not None: image = transforms(image=image)['image'] result.append((d['class'], image)) return result
def test_imread(): """Tests ``imread`` functionality.""" jpg_rgb_uri = ( "https://raw.githubusercontent.com/catalyst-team/catalyst-pics/master" "/test_images/catalyst_icon.jpg") jpg_grs_uri = ( "https://raw.githubusercontent.com/catalyst-team/catalyst-pics/master" "/test_images/catalyst_icon_grayscale.jpg") png_rgb_uri = ( "https://raw.githubusercontent.com/catalyst-team/catalyst-pics/master" "/test_images/catalyst_icon.png") png_grs_uri = ( "https://raw.githubusercontent.com/catalyst-team/catalyst-pics/master" "/test_images/catalyst_icon_grayscale.png") for uri in [jpg_rgb_uri, jpg_grs_uri, png_rgb_uri, png_grs_uri]: img = utils.imread(uri) assert img.shape == (400, 400, 3) img = utils.imread(uri, grayscale=True) assert img.shape == (400, 400, 1)
def show_random_transform(transforms): # transforms = compose([resize_transforms(), hard_transforms(), post_transforms()]) length = len(ALL_IMAGES) index = random.randint(0, length - 1) IMG = ALL_IMAGES[index] plt.figure(figsize=(20, 28)) plt.subplot(1, 2, 1) plt.imshow(utils.imread(IMG)) plt.title(f"Image: {IMG}") transformed = transforms(image=utils.imread(IMG)) transformed_image = transformed["image"] TRANSFORMED = np.moveaxis((np.array(transformed_image) * 255).astype("uint8"), 0, 2) plt.subplot(1, 2, 2) plt.imshow(Image.fromarray(TRANSFORMED)) plt.title(f"Transformed") Image.fromarray(TRANSFORMED).save("prova.png")
def show(index: int, images: List[Path], masks: List[Path], transforms=None) -> None: image_path = images[index] name = image_path.name image = utils.imread(image_path) mask = imread(train_mask_path / f"{os.path.splitext(name)[0]}_mask.png") # mask = utils.imread(masks[index]) if transforms is not None: temp = transforms(image=image, mask=mask) image = temp["image"] mask = temp["mask"] show_examples(name, image, mask)
def preprocess(self, image_path: Path): image = imread(image_path, rootpath=str(self.in_dir)) heigth, width = image.shape[:2] mask = np.zeros((heigth, width, len(self.index2color)), dtype=np.uint8) for index in range(len(self.index2color)): mask[np.all((image == self.index2color[index]), axis=-1), index] = 255 target_path = self.out_dir / f"{id_from_fname(image_path)}.tiff" target_path.parent.mkdir(parents=True, exist_ok=True) mimwrite_with_meta(target_path, np.dsplit(mask, mask.shape[2]), {"compress": 9})
def compute_segmenation_weight(img_path, img_width, img_height, contrast=1): """ Function calculate weight matrix for content loss using fully convolutional neural network Parameters: ---------- img_path : str Path to input picture img_width : int Width of image img_height :int Height of image contrast: int Contrast coefficient. Default value equal to one. Bigger value means, that front object will have bigger values in weight matrix. """ default_image_size = 320 def pre_transforms(image_size=default_image_size): return [albu.Resize(image_size, image_size, p=1)] def post_transforms(): # we use ImageNet image normalization # and convert it to torch.Tensor return [albu.Normalize(), ToTensor()] def compose(transforms_to_compose): # combine all augmentations into one single pipeline result = albu.Compose( [item for sublist in transforms_to_compose for item in sublist]) return result valid_transforms = compose([pre_transforms(), post_transforms()]) model = smp.FPN(encoder_name="resnext101_32x8d", classes=1) model.load_state_dict(torch.load(f'./models/model.pth')) model.eval() image = {'image': utils.imread(img_path)} x = valid_transforms(**image)['image'] y = model.predict(x.view(1, 3, 320, 320))[0, 0].sigmoid().numpy() w = resize(y, (img_height, img_width), preserve_range=True) w = w / w.sum() * img_width * img_height wn = np.clip(contrast * (w - w.mean()) + w.mean(), 1e-1, w.sum())[np.newaxis, np.newaxis, :, :] wn = torch.from_numpy(wn).cuda().float() del model, x, y, w return wn
def prepare(self): image_path = self.image scale = 1 / self.downscale_factor interpolation = self.interpolation img_input_target = imread(image_path) img_input = cv2.resize( img_input_target, None, None, fx=scale, fy=scale, interpolation=default_interpolations[interpolation]) return img_input, img_input_target
def __getitem__(self, idx: int) -> dict: image_path = self.images[idx] name = image_path.name image = utils.imread(image_path) result = {"image": image} if self.masks is not None: mask = imread(train_mask_path / f"{os.path.splitext(name)[0]}_mask.png") result["mask"] = mask if self.transforms is not None: result = self.transforms(**result) result["filename"] = image_path.name return result
def __call__(self, element): """Reads a row from your annotations dict with filename and transfer it to an image Args: element: elem in your dataset Returns: np.ndarray: Image """ image_name = str(element[self.input_key]) img = utils.imread( image_name, rootpath=self.rootpath, grayscale=self.grayscale ) output = {self.output_key: img} return output
def __getitem__(self, idx: int) -> dict: """Main method""" image_path = self.images[idx] image = utils.imread(image_path) result = {"image": image} if self.masks is not None: mask = gif_imread(self.masks[idx]) result["mask"] = mask if self.transforms is not None: result = self.transforms(result) result["filename"] = image_path.name return result
def __call__(self, row): """Reads a row from your annotations dict with filename and transfer it to an image Args: row: elem in your dataset. Returns: np.ndarray: Image """ image_name = str(row[self.input_key]) img = imread(image_name, rootpath=self.datapath, grayscale=self.grayscale) result = {self.output_key: img} return result
def preprocess(self, image_path: Path): try: image = np.array( imread(uri=image_path, grayscale=self.grayscale, expand_dims=self.expand_dims, exifrotate=not self.clear_exif)) except Exception: return if self.max_size is not None: image = longest_max_size(image, self.max_size, self.interpolation) target_path = self.out_dir / image_path.relative_to(self.in_dir) target_path.parent.mkdir(parents=True, exist_ok=True) image = image.clip(0, 255).round().astype(np.uint8) imwrite(target_path, image)
def main(): args = parse_arguments() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = DeepLab(num_classes=18, pretrained=False) model.load_state_dict(torch.load(args.model_path, map_location=device)['model_state_dict']) model.to(device) model.eval() image = imread(args.image_path) valid_transformation = albu.Compose( [albu.Resize(512, 512), albu.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), ToTensor()] ) im = valid_transformation(image=image)["image"].unsqueeze(0) prediction = model(im.to(device)) prediction = prediction.squeeze(0).detach().cpu().numpy() prediction = F.softmax(torch.from_numpy(prediction), dim=0).argmax(0).cpu().numpy() save_image(image, prediction, args.out_path, image.shape[:2])
def __getitem__(self, idx: int) -> dict: image_path = self.df['image'][idx] mask_path = self.df['mask'][idx] image = imread(image_path) if mask_path is np.nan: mask = self._create_empty_mask(image.shape[:2]) else: mat_file = sio.loadmat(self.df['mask'][idx]) mask = self._create_mask(mat_file) result = {"image": image} if self.masks is not None: result["mask"] = mask if self.transforms is not None: result = self.transforms(**result) result['mask'] = torch.squeeze(result['mask']).permute(2, 0, 1) return result
def __getitem__(self, idx: int) -> dict: """Fetch a data sample for a given index. Args: index: index of the element in the dataset Returns: Single element by index """ image_path = self.images[idx] image = utils.imread(image_path) result = {"image": image} if self.masks is not None: mask = gif_imread(self.masks[idx]) result["mask"] = mask if self.transforms is not None: result = self.transforms(result) result["filename"] = image_path.name return result
def colors_in_image(uri) -> Set: image = imread(uri, rootpath=args.in_dir) colors = np.unique(image.reshape(-1, image.shape[-1]), axis=0) result = {tuple(row) for row in colors.tolist()} # np.array to hashable return result
def __getitem__(self, idx: int) -> Dict[str, Any]: annotation = self._annotations_dataset[idx] image_name = annotation['image_name'] detections = annotation['detections'] image = utils.imread(image_name) x_scale, y_scale = self.image_size / image.shape[1], self.image_size / image.shape[0] image = cv2.resize(image, (self.image_size, self.image_size), cv2.INTER_LINEAR) detections = [ { 'category_id': detection['category_id'], 'category_name': detection['category_name'], 'bbox': detection['bbox'].copy() } for detection in detections ] for detection in detections: detection['bbox'][0::2] *= x_scale detection['bbox'][1::2] *= y_scale bboxes = [] labels = [] for detection in detections: median_x = (detection['bbox'][0] + detection['bbox'][2]) // 2 median_y = (detection['bbox'][1] + detection['bbox'][3]) // 2 # CenterNet are VERY bad when center of detected objects not in the images # Let's delete this bboxes if not (0 <= median_x <= image.shape[1]) or not (0 <= median_y <= image.shape[0]): continue detection['bbox'][0::2] = np.clip(detection['bbox'][0::2], 0, image.shape[1]) detection['bbox'][1::2] = np.clip(detection['bbox'][1::2], 0, image.shape[0]) bboxes.append(detection['bbox']) labels.append(detection['category_id']) bboxes = np.array(bboxes) labels = np.array(labels) if self.transform is not None: result = self.transform( image=image, bboxes=bboxes, labels=labels, ) else: result = dict( image=image, bboxes=bboxes, labels=labels, ) image = result["image"].astype(np.uint8) bboxes = result["bboxes"] labels = result["labels"] input_height, input_width = image.shape[0], image.shape[1] # Normalization input = (image.astype(np.float32) / 255.) * 2. - 1. input = input.transpose(2, 0, 1) output_height = input_height // self._down_ratio output_width = input_width // self._down_ratio # trans_output = get_affine_transform(center, scale, 0, [output_width, output_height]) heatmap = np.zeros((self._num_classes, output_height, output_width), dtype=np.float32) width_height = np.zeros((self._max_objects, 2), dtype=np.float32) reg = np.zeros((self._max_objects, 2), dtype=np.float32) ind = np.zeros(self._max_objects, dtype=np.int64) reg_mask = np.zeros(self._max_objects, dtype=np.uint8) draw_gaussian = draw_umich_gaussian new_bboxes = [] num_objs = min(len(bboxes), self._max_objects) for i in range(num_objs): bbox = np.array(bboxes[i], dtype=np.float32) / self._down_ratio class_id = labels[i] bbox[[0, 2]] = np.clip(bbox[[0, 2]], 0, output_width - 1) bbox[[1, 3]] = np.clip(bbox[[1, 3]], 0, output_height - 1) h, w = bbox[3] - bbox[1], bbox[2] - bbox[0] new_bboxes.append(bbox) if h > 0 and w > 0: radius = gaussian_radius((math.ceil(h), math.ceil(w))) radius = max(0, int(radius)) _center = np.array( [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2], dtype=np.float32 ) _center_int = _center.astype(np.int32) draw_gaussian(heatmap[class_id], _center_int, radius) width_height[i] = 1. * w, 1. * h ind[i] = _center_int[1] * output_width + _center_int[0] reg[i] = _center - _center_int reg_mask[i] = 1 result = { "filename": image_name, "input": torch.from_numpy(input), "hm": torch.from_numpy(heatmap), "reg_mask": torch.from_numpy(reg_mask), "ind": torch.from_numpy(ind), "wh": torch.from_numpy(width_height), "reg": torch.from_numpy(reg), "bboxes": np.array(bboxes), "labels": np.array(labels), } return result
from catalyst import utils from catalyst.data.cv import BlurMixin, FlareMixin, RotateMixin jpg_rgb_uri = ( "https://raw.githubusercontent.com/catalyst-team/catalyst-pics/master" "/test_images/catalyst_icon.jpg") image = utils.imread(jpg_rgb_uri) def test_blur_mixin(): """@TODO: Docs. Contribution is welcome.""" global image image_dump = image.copy() mixin = BlurMixin() input = {"image": image_dump} # noqa: WPS125 output = mixin(input) assert mixin.input_key in output assert mixin.output_key in output assert output[mixin.input_key].shape == image_dump.shape assert 0 <= output[mixin.output_key] < mixin.blur_max def test_flare_mixin(): """@TODO: Docs. Contribution is welcome.""" global image image_dump = image.copy()
def __call__(self, row): file_dir = str(row[self.input_key]) if self.datapath is not None: file_dir = (file_dir if file_dir.startswith(self.datapath) else os.path.join(self.datapath, file_dir)) frames = [os.path.join(file_dir, img) for img in os.listdir(file_dir)] fps = int(frames[0].split("/")[-1].split("_")[1].split(".")[0]) # Step 1: sorting frames = sorted(frames) # Step 2: sampling by time if self.time_window: frame_window = self.time_window * fps if self.with_offset: start_frame = row["offset"] * fps elif len(frames) > frame_window: start_frame = np.random.randint(0, len(frames) - frame_window) else: start_frame = 0 # mb less than frame_window frames = frames[start_frame:start_frame + frame_window] else: frame_window = len(frames) # Step 3: choosing frames from time range # Option a: uniform time sampling (with constant time step) if self.uniform_time_sample: frames_indexes = [ int(frame_window / self.num_frames * i) for i in range(self.num_frames) ] # If frames less than needed - duplicate last. # Important to keep constant time step frames = [frames[min(i, len(frames) - 1)] for i in frames_indexes] # Option b: (self.num_frames is None) use all frames (bad idea) elif self.num_frames is not None: tmp_frames = [] if self.num_segments is not None: # Option c: random n sample from each successive interval frames = np.array_split(frames, self.num_segments) else: # Option d: random n sample (at all) from all frames frames = [frames] for frames_ in frames: replace_ = self.num_frames > len(frames_) frames_ = np.random.choice(frames_, self.num_frames, replace=replace_).tolist() tmp_frames.extend(frames_) tmp_frames = sorted(tmp_frames) # because of random.choice frames = tmp_frames frames = [ imread(x, rootpath=self.datapath, grayscale=self.grayscale) for x in frames ] result = {self.output_key: frames} return result