def __getitem__(self, idx): if self.mode=='train': item = self.data[idx] img_path = self.path + '%s.png'%item['img_id'] img = plt.imread(img_path) width, height = img.shape img = cv2.resize(img, (self.IMG_SIZE, self.IMG_SIZE)) img = np.expand_dims(img, 0) cnt_masks = item['cnt_masks'] masks_in_rle = item['masks'] if cnt_masks==1: mask = rle2mask(masks_in_rle[0], width, height).T elif cnt_masks>1: #v1: just simply merge overlapping masks to get union of masks masks = [] for mask_in_rle in masks_in_rle: mask = rle2mask(mask_in_rle, width, height).T masks.append(mask) mask = (np.array(masks).sum(axis=0)>=1).astype(np.int)#mask as 1 if at least one of the mask is 1 else: mask = np.zeros((self.IMG_SIZE, self.IMG_SIZE)) mask = cv2.resize(mask.astype(np.float), (self.IMG_SIZE, self.IMG_SIZE)) mask = np.expand_dims(mask, 0) ##augmentation if self.augmentation: img, mask = do_augmentation(img, mask) return img, mask elif self.mode=='test': img_id = self.img_id_list[idx] img_path = self.path + '%s.png'%img_id img = plt.imread(img_path) width, height = img.shape img = cv2.resize(img, (self.IMG_SIZE, self.IMG_SIZE)) img = np.expand_dims(img, 0) return img
def csv2mask(in_path, out_dir, resize=None, v=0): """ Converts given .csv annotations, creates masks in out_dir :param in_path: str: file path to `.csv` :param out_dir: str: path to out dir where to save .png images :param resize: tuple(int, int): if provided saves resized images :param v: int: verbosity, if 1 shows `tqdm` :return: NoneType: None object """ df = pd.read_csv(in_path, dtype=str, sep=", ") df = df.groupby('ImageId')["EncodedPixels"].apply(list) it = df.items() if v: it = tqdm(it) for image_id, row in it: mask = blank_mask(WIDTH, HEIGHT) for i, rle_mask in enumerate(row, 1): if rle_mask == "-1": continue i_mask = rle2mask(rle_mask, WIDTH, HEIGHT) mask[i_mask > 125] = i out_fname = "{}.png".format(image_id) out_fp = os.path.join(out_dir, out_fname) if resize: mask = cv2.resize(mask, resize) cv2.imwrite(out_fp, mask)
def test(): df = pd.read_csv('../input/train-rle.csv', header=None, index_col=0) Y_train = np.zeros((1024, 1024, 1)) for x in df.loc['1.2.276.0.7230010.3.1.4.8323329.307.1517875162.311533', 1]: # plt.imshow(rle2mask(x, 1024, 1024).T) # plt.show() Y_train = Y_train + np.expand_dims(rle2mask(x, 1024, 1024).T, axis=2) print(np.shape(Y_train)) Y_train = np.squeeze(Y_train, 2) Y_train = np.where(Y_train > 0, 255, 0) plt.imshow(Y_train) plt.show()
accu_bug_best = pd.read_csv( "./submissions/submission_private_accum_bug_tta_best.csv", dtype=str) best_new_dfs = [accu_bug_best, best_align, best_align_div] dfs = [our_best] + best_new_dfs out_values = [] for img_id in tqdm(our_best.ImageId.unique()): or_between = None skip = False for i, df in enumerate(dfs): val = df[df.ImageId == img_id]["EncodedPixels"].values[0] if val.strip() == "-1": skip = True break mask = (rle2mask(val, 1024, 1024) > 0).astype(np.uint8) if or_between is None: or_between = np.zeros_like(mask, dtype=np.bool) or_between |= mask > 0.5 if skip: out_values.append([img_id, "-1"]) else: out_values.append([img_id, better_mask2rle(or_between * 1 * 255)]) out_best_values = out_values.copy() out_values_df = pd.DataFrame(out_best_values, columns=['ImageId', 'EncodedPixels']) print((out_values_df.EncodedPixels == "-1").sum()) out_values_df.to_csv("private_merge_best.csv", index=None)
def get_mask(encode, height, width): mask = rle2mask(encode[0], height, width) for e in encode[1:]: mask += rle2mask(e, height, width) print(mask) return mask.T
def dcm2jpg(dcm_path, save_jpg_path, csv_path=None, save_mask_path=None, show_info_pic=False): count_no_mask = 0 if not os.path.exists(save_jpg_path): os.makedirs(save_jpg_path) # if has .csv if csv_path: # header=None : Instead of using the first row as a column index, the index is automatically generated # index_col=0 : Use the first column as an index column df = pd.read_csv(csv_path, header=None, index_col=0) if not os.path.exists(save_mask_path): os.makedirs(save_mask_path) for file_path in glob.glob(dcm_path): # convert dcm to jpg dataset = pydicom.dcmread(file_path) if show_info_pic: show_dcm_info(dataset, file_path) plot_pixel_array(dataset) print(file_path) if not csv_path: misc.imsave( os.path.join(save_jpg_path, file_path.split('/')[-1][:-4] + '.jpg'), dataset.pixel_array) # if has .csv, convert csv to mask matrix # notice, .loc[row_index, col_index], and if the mask matrix must be transposed if csv_path: try: # there is no mask if '-1' in df.loc[file_path.split('/')[-1][:-4], 1] or ' -1' in df.loc[ file_path.split('/')[-1][:-4], 1]: print('no mask') mask = np.zeros((1024, 1024)) else: # if has one mask if type(df.loc[file_path.split('/')[-1][:-4], 1]) == str: print('one mask') mask = rle2mask( df.loc[file_path.split('/')[-1][:-4], 1], 1024, 1024).T mask = np.where(mask > 0, 255, 0) # more than one mask else: print('more than one mask') mask = np.zeros((1024, 1024)) for x in df.loc[file_path.split('/')[-1][:-4], 1]: mask = mask + rle2mask(x, 1024, 1024).T mask = np.where(mask > 0, 255, 0) print('save') misc.imsave( os.path.join(save_jpg_path, file_path.split('/')[-1][:-4] + '.jpg'), dataset.pixel_array) misc.imsave( os.path.join(save_mask_path, file_path.split('/')[-1][:-4] + '.png'), mask) except KeyError: print("Key" + file_path.split('/')[-1][:-4] + ",without mask") count_no_mask += 1 print('count_no_mask:', count_no_mask)