def build_preds(input_im, tg_size): img = tr.ToTensor()(resize(input_im)).view(1, 3, *tg_size).to(device) with torch.no_grad(): logits = model(img) probas = F.softmax(logits, dim=1) width, height = input_im.size orig_size = (height, width) pred_rgb = np.zeros([*tg_size, 3]) probas_np = probas[0].detach().cpu().numpy() # probas has 4 channels pred_vessels = 1 - probas_np[0] pred_rgb[:, :, 0] = probas_np[1] pred_rgb[:, :, 1] = probas_np[2] pred_rgb[:, :, 2] = probas_np[3] pretty_pred = (pred_rgb + np.stack(3 * [probas_np[0]], axis=2)).clip(0, 1) # recover original resolution pred_vessels = sk_resize(pred_vessels, orig_size, anti_aliasing=True, mode='reflect') pred_rgb = sk_resize(pred_rgb, orig_size, anti_aliasing=True, mode='reflect') pretty_pred = sk_resize(pretty_pred, orig_size, anti_aliasing=True, mode='reflect') return pred_vessels, pred_rgb, pretty_pred
def wrapped(): dataset_path = 'image_data' imgs_list = glob.glob(dataset_path + '/img/*.jpg') random.shuffle(imgs_list) # gather all corresponding masks for each image all_masks_files = glob.glob(dataset_path + '/bool_mask_sep_inst/*.npy') image_to_masks = defaultdict(list) for x in all_masks_files: x = os.path.basename(x) # MaskId := ImageId_MaskNum.npy image_id = x[:x.rindex('_')] image_to_masks[image_id].append(x) for fname in imgs_list: image_id = os.path.basename(fname).rstrip('.jpg') mask_base = random.choice(image_to_masks[image_id]) ref_mask_path = random.choice(all_masks_files) image = skimage.img_as_float( imread(dataset_path + '/img/' + image_id + '.jpg')) mask = np.load(dataset_path + '/bool_mask_sep_inst/' + mask_base) ref_mask = np.load(ref_mask_path) if patch_size is not None: image = sk_resize(image, (patch_size, patch_size)) mask = skimage.img_as_bool( sk_resize(mask * 255., (patch_size, patch_size))) ref_mask = skimage.img_as_bool( sk_resize(ref_mask * 255., (patch_size, patch_size))) if align: ref_mask = align_mask(mask, ref_mask) yield (image, mask, ref_mask)
def detect_multi_scale(model, img, scales, recepive_field): result = [] for scale in scales: scaled_img = cv2.resize( img, (int(img.shape[1] / scale), int(img.shape[0] / scale))) output = detect_img(model, np.float32(scaled_img)) output = sk_resize(output, output_shape=img.shape[:2], preserve_range=True) output = np.float32(output) #cv2.imshow("Scale " + str(scale), output) #cv2.waitKey(10) coordinates = peak_local_max(output, min_distance=30, threshold_abs=-1) for y, x in coordinates: detection = map(int, [ x, y, recepive_field * scale, recepive_field * scale, output[y, x] ]) result.append(detection) # Non maximal surpression: result = sorted(result, key=lambda r: r[-1], reverse=True) after_nms = [] for detection in result: keep_detection = True for other_detection in after_nms: if intersects(detection, other_detection): keep_detection = False break if keep_detection: after_nms.append(detection) return after_nms
def resize_np(im, r, c, rn, cn, crop_size=224, divide255=False): # BBOX: [col,row,col+col_no,row+row_no] # print(im.shape,flush=True) to_resize = im[:, r:r + rn, c:c + cn] # print(to_resize.shape,flush=True) # print(to_resize,flush=True) # print(np.unique(to_resize),flush=True) # print(len(np.unique(to_resize)),flush=True) # print('++++++',flush=True) # print('++++++',flush=True) # print('++++++',flush=True) # print('++++++',flush=True) # print('++++++',flush=True) # to_resize = to_resize.permute(2,0,1) if len(np.unique(to_resize)) > 0: resized_img = torch.from_numpy(sk_resize(to_resize, (3, crop_size, crop_size), order=3, mode='reflect')).float() elif to_resize.shape[1] == 0 or to_resize.shape[2] == 0: return None else: value = to_resize[0, 0, 0] resized_img = torch.ones(3, crop_size, crop_size) * value if divide255: resized_img = resized_img / 255 return resized_img.unsqueeze(0)
def __call__(self, sample): # Decouple sample into id and image stack components # idx, sample = sample # get the scans and the mask label. scans, label224 = sample[:, :, :-1], sample[:, :, -1] # get one hot representation for mask # label_onehot = (np.arange(2) == label[..., None]).astype(np.int64) #make datatypes consistent label28 = sk_resize(label224, (28, 28)).astype(np.float32) scans, label28, label224 = scans.astype( np.float32), label28, label224.astype(np.float32) # swap color axis because # numpy image: H x W x C # torch image: C X H X W scans, label224, label28 = scans.transpose( (2, 0, 1)), label224[np.newaxis, ...], label28[np.newaxis, ...] scans, label28, label224 = torch.from_numpy(scans), torch.from_numpy( label28), torch.from_numpy(label224) return scans, label224, label28
def crop2fullmask(crop_mask, bbox, im=None, im_size=None, zero_pad=False, relax=0, mask_relax=True, interpolation=cv2.INTER_CUBIC, scikit=True): if scikit: from skimage.transform import resize as sk_resize assert (not (im is None and im_size is None) ), 'You have to provide an image or the image size' if im is None: im_si = im_size else: im_si = im.shape # Borers of image bounds = (0, 0, im_si[1] - 1, im_si[0] - 1) # Valid bounding box locations as (x_min, y_min, x_max, y_max) bbox_valid = (max(bbox[0], bounds[0]), max(bbox[1], bounds[1]), min(bbox[2], bounds[2]), min(bbox[3], bounds[3])) # Bounding box of initial mask bbox_init = (bbox[0] + relax, bbox[1] + relax, bbox[2] - relax, bbox[3] - relax) if zero_pad: # Offsets for x and y offsets = (-bbox[0], -bbox[1]) else: assert ((bbox == bbox_valid).all()) offsets = (-bbox_valid[0], -bbox_valid[1]) # Simple per element addition in the tuple inds = tuple(map(sum, zip(bbox_valid, offsets + offsets))) if scikit: crop_mask = sk_resize(crop_mask, (bbox[3] - bbox[1] + 1, bbox[2] - bbox[0] + 1), order=0, mode='constant').astype(crop_mask.dtype) else: crop_mask = cv2.resize(crop_mask, (bbox[2] - bbox[0] + 1, bbox[3] - bbox[1] + 1), interpolation=interpolation) result_ = np.zeros(im_si) result_[bbox_valid[1]:bbox_valid[3] + 1, bbox_valid[0]:bbox_valid[2] + 1] = \ crop_mask[inds[1]:inds[3] + 1, inds[0]:inds[2] + 1] result = np.zeros(im_si) if mask_relax: result[bbox_init[1]:bbox_init[3]+1, bbox_init[0]:bbox_init[2]+1] = \ result_[bbox_init[1]:bbox_init[3]+1, bbox_init[0]:bbox_init[2]+1] else: result = result_ return result
def _fixed_resize(self, sample, resolution, flagval, scikit=False): if isinstance(resolution, int): tmp = [resolution, resolution] tmp[np.argmax(sample.shape[:2])] = int( round( float(resolution) / np.min(sample.shape[:2]) * np.max(sample.shape[:2]))) resolution = tuple(tmp) if sample.ndim == 2 or (sample.ndim == 3 and sample.shape[2] == 3): if scikit: sample = sk_resize(sample, resolution, order=0, mode='constant').astype(sample.dtype) else: sample = cv2.resize(sample, resolution[::-1], interpolation=flagval) else: tmp = sample sample = np.zeros(np.append(resolution, tmp.shape[2]), dtype=np.float32) for ii in range(sample.shape[2]): sample[:, :, ii] = cv2.resize(tmp[:, :, ii], resolution[::-1], interpolation=flagval) return sample
def save_meta_imgs(self, meta_img): # Center crop pad_h = (meta_img.shape[2] - self.target_height) // 2 pad_w = (meta_img.shape[3] - self.target_width) // 2 meta_img = meta_img[:, :, pad_h:pad_h + self.target_height, pad_w:pad_w + self.target_width] # Save the full image and the low-resolution image (for visualization) meta_img = meta_img.clamp(-1, 1).permute(0, 2, 3, 1) meta_img = (meta_img + 1) / 2 meta_img_np = meta_img.numpy() for i in range(self.config.train_params.batch_size): global_id = self.cur_global_id + i save_path = os.path.join(self.save_root, str(global_id).zfill(6) + ".png") plt.imsave(save_path, meta_img_np[i]) if hasattr(self.config.task, "lowres_height") and self.config.task.lowres_height > 0: lr_save_path = os.path.join( self.save_root, str(global_id).zfill(6) + "_lr.png") resize_ratio = self.config.task.lowres_height / self.config.task.height resize_shape = (int(round(self.target_height * resize_ratio)), int(round(self.target_width * resize_ratio))) lr_img = sk_resize((meta_img_np[i] * 255).astype(np.uint8), resize_shape) plt.imsave(lr_save_path, lr_img)
def __call__(self, images): images = images.toarray() resized = np.zeros((len(images), self.height, self.width)) for idx in range(0, len(images)): img = images[idx] resized_img = sk_resize(img, (self.height, self.width)) resized[idx] = resized_img return td.images.fromarray(resized)
def rescale_tmaps(tmaps, new_size): """ Resize all transient maps in tmaps to roughly match the shape specified in new_size :param tmaps: list of tmaps :param new_size: shape = (2,) list or tuple with shape to rescale to :return: list of reshaped tmaps """ tmaps_rescale = [] for tmap in tmaps: tmaps_rescale.append(sk_resize(tmap, new_size, anti_aliasing=True)) return tmaps_rescale
def __getitem__(self, index): fname = self.file_list[index] # "xyz.nii" fpath = os.path.join(self.data_dir, fname) img_nib = nib.load(fpath) img = img_nib.get_fdata() # converts to numpy array # add data augmentation here img = sk_resize(img, (128, 128, 32), order=2) # img = img - img.mean() img = img / img.max() if self.model_name == "VGG" or self.model_name == "ResNet2D": img = sk_resize(img, (224, 224, 32), order=1) image = torch.FloatTensor(np.transpose(img, (2, 0, 1))) if self.model_name == "ResNet3D" or self.model_name == "VGG3D": image3d = torch.FloatTensor(np.transpose(img, (2, 0, 1))) image = image3d.view(1, 32, 128, 128) label = int(self.labels[index]) return image, label
def init_bins(fnames, n_samples=None, isDCM=True): """Initialize bins to equally distribute the histogram of the dataset""" # Select randomly n_samples if n_samples is not None: fnames_sample = fnames.copy() random.shuffle(fnames_sample) fnames_sample = fnames_sample[:n_samples] else: fnames_sample = fnames if isDCM: # Extract the current smallest size try: # Extract DCMs dcms = fnames_sample.map(dcmread) # Get the current smallest size resize = min(dcms.attrgot("scaled_px").map(lambda x: x.size())) except AttributeError: import pydicom from numpy import inf dcms = [] resize = [] # Extract different size and get the samllest one for fname in fnames_sample: dcm = fname.dcmread() resize.append(dcm.scaled_px.size()) dcms.append(dcm) resize = min(resize) # Extract bins from scaled and resized samples samples = [] for dcm in dcms: try: samples.append( torch.from_numpy(sk_resize(dcm_scale(dcm), resize))) except AttributeError: continue else: samples = [] for fn in fnames_sample: image = Image.open(fn) samples.append(TF.to_tensor(image)) # Calculate the frequency bins from these samples samples = torch.stack(tuple(samples)) bins = samples.freqhist_bins() return bins
def process_raw_dataset(raw_ds, target_size=(400, 400)): """ """ ds = [] for raw_img in raw_ds: # Get the aspect ratio of the image, so that when it's resized, the # bounding boxes can be resized correctly yshape, xshape, _ = raw_img.data.shape yratio = target_size[0] / yshape xratio = target_size[1] / xshape data = sk_resize(raw_img.data, target_size, preserve_range=True) data = __process_custom(data) img = obj_pipeline.ObjImage(data=data) img.set_image_id(raw_img.image_id) for label, features in raw_img.get_features().items(): local_label = MAP_TO_LOCAL_LABELS[label] if local_label is not None: for feature in features: x1, y1, x2, y2 = feature # if box was drawn from the bottom right to the top left, # switch the order if (x1 > x2) and (y1 > y2): _x1 = x1 _y1 = y1 x1 = x2 y1 = y2 x2 = _x1 y2 = _y2 img.append_feature(local_label, [ int(float(x1 * xratio)), int(float(y1 * yratio)), int(float(x2 * xratio)), int(float(y2 * yratio)) ]) ds.append(img) return ds
else: y_pred += inv_sigmoid(model.predict(images))[:, :, :, 0] print('Done predicting -- now doing post-processing') y_pred = y_pred / num_folds y_pred = sigmoid(y_pred) y_pred = task1_post_process(y_prediction=y_pred, threshold=0.5, gauss_sigma=2.) output_dir = submission_dir + '/task1_' + pred_set mkdir_if_not_exist([output_dir]) for i_image, i_name in enumerate(image_names): current_pred = y_pred[i_image] current_pred = current_pred * 255 resized_pred = sk_resize(current_pred, output_shape=image_sizes[i_image], preserve_range=True, mode='reflect', anti_aliasing=True) resized_pred[resized_pred > 128] = 255 resized_pred[resized_pred <= 128] = 0 im = Image.fromarray(resized_pred.astype(np.uint8)) im.save(output_dir + '/' + i_name + '_segmentation.png')
def __call__(self, mask): return sk_resize(mask, (self.height, self.width))
def __call__(self, im, size=None): return sk_resize(im, self.size)