def main(args): img = ecvl.ImRead(args.in_img) augs = ecvl.SequentialAugmentationContainer([ ecvl.AugRotate([-5, 5]), ecvl.AugMirror(.5), ecvl.AugFlip(.5), ecvl.AugGammaContrast([3, 5]), ecvl.AugAdditiveLaplaceNoise([0, 0.2 * 255]), ecvl.AugCoarseDropout([0, 0.55], [0.02, 0.1], 0.5), ecvl.AugAdditivePoissonNoise([0, 40]), ecvl.AugResizeDim([500, 500]), ]) ecvl.AugmentationParam.SetSeed(0) augs.Apply(img) print("Executing ImageToTensor") t = ecvl.ImageToTensor(img) t.div_(128) t.mult_(128) print("Executing TensorToImage") img = ecvl.TensorToImage(t) print("Executing TensorToView") ecvl.TensorToView(t) _ = ecvl.AugmentationFactory.create(AUG_TXT) training_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugRotate([-5, 5]), ecvl.AugAdditiveLaplaceNoise([0, 0.2 * 255]), ecvl.AugCoarseDropout([0, 0.55], [0.02, 0.1], 0), ecvl.AugAdditivePoissonNoise([0, 40]), ecvl.AugResizeDim([30, 30]), ]) test_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim([30, 30]), ]) ds_augs = ecvl.DatasetAugmentations([training_augs, None, test_augs]) batch_size = 64 print("Creating a DLDataset") d = ecvl.DLDataset(args.in_ds, batch_size, ds_augs, ecvl.ColorType.GRAY) print("Create x and y") x = Tensor( [batch_size, d.n_channels_, d.resize_dims_[0], d.resize_dims_[1]]) y = Tensor([batch_size, len(d.classes_)]) # Load a batch of d.batch_size_ images into x and corresponding labels # into y. Images are resized to the dimensions specified in the # augmentations chain print("Executing LoadBatch on training set") d.LoadBatch(x, y) # Change colortype and channels img = ecvl.TensorToImage(x) img.colortype_ = ecvl.ColorType.GRAY img.channels_ = "xyc" # Switch to Test split and load a batch of images print("Executing LoadBatch on test set") d.SetSplit(ecvl.SplitType.test) d.LoadBatch(x, y)
def read_slide(slide_fn, level=4): levels = ecvl.OpenSlideGetLevels(slide_fn) dims = [0, 0] + levels[level] img = ecvl.OpenSlideRead(slide_fn, level, dims) t = ecvl.ImageToTensor(img) t_np = t.getdata() s = t_np.shape t_np = t_np.transpose((1, 2, 0)).reshape( (s[1] * s[2], 3)) # Channel last and reshape t_eval = Tensor.fromarray(t_np) print(t_eval.getShape()) return t_eval, s
def get_mask_tissue_from_slide(self, slide_fn, level=2, use_openslide=False): if use_openslide and not no_openslide: # Using open slide slide = openslide.open_slide(slide_fn) dims = slide.level_dimensions ds = [int(i) for i in slide.level_downsamples] x0 = 0 y0 = 0 x1 = dims[0][0] y1 = dims[0][1] delta_x = x1 - x0 delta_y = y1 - y0 pil_img = slide.read_region(location=(x0, y0), level=level, size=(delta_x // ds[level], delta_y // ds[level])) np_img = np.array(pil_img) msk_pred = self.get_tissue_mask(np_img, channel_first=False, BGR=False) else: ## Using ECVL levels = ecvl.OpenSlideGetLevels(slide_fn) dims = [0, 0] + levels[level] img = ecvl.OpenSlideRead(slide_fn, level, dims) t = ecvl.ImageToTensor(img) np_img = t.getdata( ) # Tensor to numpy array (ecvl read images with channel first) msk_pred = self.get_tissue_mask(np_img, channel_first=True, BGR=True) return msk_pred
def main(args): if not ecvl.ECVL_EDDL: print("No EDDL support - quitting") sys.exit(0) img = ecvl.ImRead(args.in_img) augs = ecvl.SequentialAugmentationContainer([ ecvl.AugCenterCrop(), # Make image square ecvl.AugRotate([-5, 5]), ecvl.AugMirror(.5), ecvl.AugFlip(.5), ecvl.AugGammaContrast([3, 5]), ecvl.AugAdditiveLaplaceNoise([0, 0.2 * 255]), ecvl.AugCoarseDropout([0, 0.55], [0.02, 0.1], 0.5), ecvl.AugAdditivePoissonNoise([0, 40]), ecvl.AugResizeDim([500, 500]), ecvl.AugCenterCrop([224, 224]), ecvl.AugToFloat32(255), ecvl.AugNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]), ]) ecvl.AugmentationParam.SetSeed(0) print("Applying augmentations") augs.Apply(img) print("Executing ImageToTensor") t = ecvl.ImageToTensor(img) t.div_(128) t.mult_(128) print("Executing TensorToImage") img = ecvl.TensorToImage(t) print("Executing TensorToView") ecvl.TensorToView(t) print("Applying augmentations (from text)") newdeal_augs = ecvl.AugmentationFactory.create(AUG_TXT) newdeal_augs.Apply(img) training_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugRotate([-5, 5]), ecvl.AugAdditiveLaplaceNoise([0, 0.2 * 255]), ecvl.AugCoarseDropout([0, 0.55], [0.02, 0.1], 0), ecvl.AugAdditivePoissonNoise([0, 40]), ecvl.AugResizeDim([30, 30]), ecvl.AugToFloat32(255), ecvl.AugNormalize(0.449, 0.226), # mean of imagenet stats ]) test_augs = ecvl.SequentialAugmentationContainer([ ecvl.AugResizeDim([30, 30]), ecvl.AugToFloat32(255), ecvl.AugNormalize(0.449, 0.226), # mean of imagenet stats ]) # number of augmentation containers must match number of dataset splits ds_augs = ecvl.DatasetAugmentations([training_augs, test_augs]) batch_size = 64 print("Creating a DLDataset") d = ecvl.DLDataset(args.in_ds, batch_size, ds_augs, ecvl.ColorType.GRAY) print("Create x and y") x = Tensor( [batch_size, d.n_channels_, d.resize_dims_[0], d.resize_dims_[1]]) y = Tensor([batch_size, len(d.classes_)]) # Load a batch of d.batch_size_ images into x and corresponding labels # into y. Images are resized to the dimensions specified in the # augmentations chain print("Executing LoadBatch on training set") d.LoadBatch(x, y) # Change colortype and channels img = ecvl.TensorToImage(x) img.colortype_ = ecvl.ColorType.GRAY img.channels_ = "xyc" # Switch to Test split and load a batch of images print("Executing LoadBatch on test set") d.SetSplit(ecvl.SplitType.test) d.LoadBatch(x, y) # Save some input images ecvl.ImWrite("mnist_batch.png", ecvl.MakeGrid(x, 8, False)) ecvl.ImWrite("mnist_batch_normalized.png", ecvl.MakeGrid(x, 8, True))
def PneumothoraxLoadBatch(d, black_indices, m_i, b_i): mask_indices = d.GetSplit() len_mask_indices = len(mask_indices) bs = d.batch_size_ names = [] images = [] labels = [] if d.current_split_ == ecvl.SplitType.training: index = 0 elif d.current_split_ == ecvl.SplitType.validation: index = 1 else: index = 2 start = d.current_batch_[index] * bs d.current_batch_ = [ elem + 1 if i == index else elem for i, elem in enumerate(d.current_batch_) ] expr = True for i in range(start, start + bs): if d.current_split_ == ecvl.SplitType.training: # in training, check if you can take # other black ground truth images.. if len_mask_indices * 1.25 - i > len_mask_indices - m_i: expr = random.random() >= 0.2 and m_i < len_mask_indices # ..otherwise, you have to take a ground truth with mask else: # in validation, first take all the samples with the ground # truth with mask and then all those with the black ground truth. expr = m_i < len_mask_indices if expr: index = mask_indices[m_i] m_i += 1 else: index = black_indices[b_i] b_i += 1 # insert the original name of images and ground truth in case # you want to save predictions during validation elem = d.samples_[index] names.append(elem.location_[0]) names.append(elem.label_path_) # Read the image img = elem.LoadImage(d.ctype_, False) # Read the ground truth gt = elem.LoadImage(d.ctype_gt_, True) # Apply chain of augmentations to sample image # and corresponding ground truth d.augs_.Apply(d.current_split_, img, gt) # Copy image into tensor (images) images.append(ecvl.ImageToTensor(img)) # Copy label into tensor (labels) labels.append(ecvl.ImageToTensor(gt)) return d, images, labels, names, m_i, b_i