def do_sampling(pat_df, out_dir): for pat,side,view in pat_df.index.unique(): full_fn = const_filename(pat, side, view, full_img_dir) # import pdb; pdb.set_trace() try: if target_width is None: full_img = read_resize_img( full_fn, target_height=target_height) else: full_img = read_resize_img( full_fn, target_size=(target_height, target_width)) img_id = '_'.join([pat, side, view]) print "ID:%s, read image of size=%s" % (img_id, full_img.shape), if segment_breast: full_img, bbox = imprep.segment_breast(full_img) print "size after segmentation=%s" % (str(full_img.shape)) sys.stdout.flush() # Read mask image(s). abn_path = roi_mask_path_df.loc[pat].loc[side].loc[view] if isinstance(abn_path, pd.Series): abn_num = [abn_path['abn_num']] pathology = [abn_path['pathology']] itypes = [abn_path['type']] else: abn_num = abn_path['abn_num'] pathology = abn_path['pathology'] itypes = abn_path['type'] bkg_sampled = False for abn, path, itype in zip(abn_num, pathology, itypes): mask_fn = const_filename(pat, side, view, roi_mask_dir, itype, abn) if target_width is None: mask_img = read_resize_img( mask_fn, target_height=target_height, gs_255=True) else: mask_img = read_resize_img( mask_fn, target_size=(target_height, target_width), gs_255=True) if segment_breast: mask_img = crop_img(mask_img, bbox) # sample using mask and full image. nb_hns_ = nb_hns if not bkg_sampled else 0 if nb_hns_ > 0: hns_sampled = sample_blob_negatives( full_img, mask_img, out_dir, img_id, abn, blob_detector, patch_size, neg_cutoff, nb_hns_, 0, bkg_dir, verbose) else: hns_sampled = 0 pos = path.startswith('MALIGNANT') nb_bkg_ = nb_bkg - hns_sampled if not bkg_sampled else 0 sample_patches(full_img, mask_img, out_dir, img_id, abn, pos, patch_size, pos_cutoff, neg_cutoff, nb_bkg_, nb_abn, hns_sampled, itype, bkg_dir, calc_pos_dir, calc_neg_dir, mass_pos_dir, mass_neg_dir, verbose) bkg_sampled = True except AttributeError: print "Read image error: %s" % (full_fn) except ValueError: print "Error sampling from ROI mask image: %s" % (mask_fn)
def do_sampling(pat_df, out_dir): for pat, side, view in pat_df.index.unique(): full_fn = const_filename(pat, side, view, full_img_dir) # import pdb; pdb.set_trace() try: full_img = read_resize_img(full_fn, target_height=target_height) img_id = '_'.join([pat, side, view]) print "ID:%s, read image of size=%s" % (img_id, full_img.shape), full_img, bbox = imprep.segment_breast(full_img) print "size after segmentation=%s" % (str(full_img.shape)) sys.stdout.flush() # Read mask image(s). abn_path = roi_mask_path_df.loc[pat].loc[side].loc[view] if isinstance(abn_path, pd.Series): abn_num = [abn_path['abn_num']] pathology = [abn_path['pathology']] itypes = [abn_path['type']] else: abn_num = abn_path['abn_num'] pathology = abn_path['pathology'] itypes = abn_path['type'] bkg_sampled = False for abn, path, itype in zip(abn_num, pathology, itypes): mask_fn = const_filename(pat, side, view, roi_mask_dir, itype, abn) mask_img = read_resize_img(mask_fn, target_height=target_height, gs_255=True) mask_img = crop_img(mask_img, bbox) # sample using mask and full image. nb_hns_ = nb_hns if not bkg_sampled else 0 if nb_hns_ > 0: hns_sampled = sample_blob_negatives( full_img, mask_img, out_dir, img_id, abn, blob_detector, patch_size, neg_cutoff, nb_hns_, 0, bkg_dir, verbose) else: hns_sampled = 0 pos = path.startswith('MALIGNANT') nb_bkg_ = nb_bkg - hns_sampled if not bkg_sampled else 0 sample_patches(full_img, mask_img, out_dir, img_id, abn, pos, patch_size, pos_cutoff, neg_cutoff, nb_bkg_, nb_abn, hns_sampled, itype, bkg_dir, calc_pos_dir, calc_neg_dir, mass_pos_dir, mass_neg_dir, verbose) bkg_sampled = True except AttributeError: print "Read image error: %s" % (full_fn) except ValueError: print "Error sampling from ROI mask image: %s" % (mask_fn)
def score_write_patches(img_list, lab_list, target_height, target_scale, patch_size, stride, model, batch_size, neg_out, pos_out, bkg_out, preprocess=None, equalize_hist=False, featurewise_center=False, featurewise_mean=91.6, roi_cutoff=.9, bkg_cutoff=[.5, 1.], sample_bkg=True, img_ext='png', random_seed=12345, parallelized=False): '''Score image patches and write them to an external directory ''' def write_patches(img_fn, patch_dat, idx, out_dir, img_ext='png'): basename = os.path.basename(img_fn) fn_no_ext = os.path.splitext(basename)[0] if img_ext == 'png': max_val = 65535. else: max_val = 255. for i in idx: patch = patch_dat[i] patch_max = patch.max() if patch.max() != 0 else max_val patch *= max_val / patch_max patch = patch.astype('int32') mode = 'I' if img_ext == 'png' else None patch_img = toimage(patch, high=patch.max(), low=patch.min(), mode=mode) filename = fn_no_ext + "_%06d" % (i) + '.' + img_ext fullname = os.path.join(out_dir, filename) patch_img.save(fullname) rng = RandomState(random_seed) nb_roi = 0 nb_bkg = 0 for img_fn, img_lab in zip(img_list, lab_list): img = read_resize_img(img_fn, target_height=target_height) img, _ = prep.segment_breast(img) img = add_img_margins(img, patch_size / 2) patch_dat, nb_row, nb_col = sweep_img_patches( img, patch_size, stride, target_scale=target_scale, equalize_hist=equalize_hist) org_patch_dat = patch_dat.copy() if parallelized and len(patch_dat) % 2 == 1: last_patch = patch_dat[-1:, :, :] patch_dat = np.append(patch_dat, last_patch, axis=0) appended = True else: appended = False if dim_ordering == 'th': patch_X = np.zeros((patch_dat.shape[0], 3, patch_dat.shape[1], patch_dat.shape[2]), dtype='float64') patch_X[:, 0, :, :] = patch_dat patch_X[:, 1, :, :] = patch_dat patch_X[:, 2, :, :] = patch_dat else: patch_X = np.zeros((patch_dat.shape[0], patch_dat.shape[1], patch_dat.shape[2], 3), dtype='float64') patch_X[:, :, :, 0] = patch_dat patch_X[:, :, :, 1] = patch_dat patch_X[:, :, :, 2] = patch_dat if featurewise_center: patch_X -= featurewise_mean elif preprocess is not None: patch_X = preprocess(patch_X) pred = model.predict(patch_X, batch_size=batch_size) # import pdb; pdb.set_trace() if appended: pred = pred[:-1] roi_idx = np.where(pred[:, 0] < 1 - roi_cutoff)[0] bkg_idx = np.where( np.logical_and(pred[:, 0] > bkg_cutoff[0], pred[:, 0] <= bkg_cutoff[1]))[0] if sample_bkg and len(bkg_idx) > len(roi_idx): bkg_idx = rng.choice(bkg_idx, len(roi_idx), replace=False) roi_out = pos_out if img_lab == 1 else neg_out write_patches(img_fn, org_patch_dat, roi_idx, roi_out, img_ext) write_patches(img_fn, org_patch_dat, bkg_idx, bkg_out, img_ext) nb_roi += len(roi_idx) nb_bkg += len(bkg_idx) return nb_roi, nb_bkg
def do_sampling(pat_df, out_dir): #遍历病人信息 for pat, side, view in pat_df.index.unique(): #文件名命名 full_fn = const_filename(pat, side, view, full_img_dir) # import pdb; pdb.set_trace() try: #如果未指定宽度则根据高度调整图片大小 if target_width is None: full_img = read_resize_img(full_fn, target_height=target_height) else: #根据宽高调整大小 full_img = read_resize_img(full_fn, target_size=(target_height, target_width)) #命名图片ID img_id = '_'.join([pat, side, view]) print( "ID:%s, read image of size=%s" % (img_id, full_img.shape), ) #图像中分割出乳腺为true if segment_breast: #获取乳腺图像和坐标宽高 full_img, bbox = imprep.segment_breast(full_img) #输出图像宽高通道数 print("size after segmentation=%s" % (str(full_img.shape))) sys.stdout.flush() # Read mask image(s).读取掩码图 #获取异常路径 abn_path = roi_mask_path_df.loc[pat].loc[side].loc[view] #无论此路径是否为pd.Series类型,都要进行异常数,病理(良恶性),类型(肿块或钙化点)的数据获取 if isinstance(abn_path, pd.Series): abn_num = [abn_path['abn_num']] pathology = [abn_path['pathology']] itypes = [abn_path['type']] else: abn_num = abn_path['abn_num'] pathology = abn_path['pathology'] itypes = abn_path['type'] # bkg_sampled = False #遍历刚才获取的数据,转为掩码文件 for abn, path, itype in zip(abn_num, pathology, itypes): mask_fn = const_filename(pat, side, view, roi_mask_dir, itype, abn) if target_width is None: mask_img = read_resize_img(mask_fn, target_height=target_height, gs_255=True) else: mask_img = read_resize_img(mask_fn, target_size=(target_height, target_width), gs_255=True) if segment_breast: mask_img = crop_img(mask_img, bbox) # sample using mask and full image.样本使用掩码和全图 #获取难类数 nb_hns_ = nb_hns if not bkg_sampled else 0 if nb_hns_ > 0: #返回阴性斑点采样得到的背景样本数 hns_sampled = sample_blob_negatives( full_img, mask_img, out_dir, img_id, abn, blob_detector, patch_size, neg_cutoff, nb_hns_, 0, bkg_dir, verbose) else: hns_sampled = 0 pos = path.startswith('MALIGNANT') #背景数为总背景数-阴性斑点采样数 nb_bkg_ = nb_bkg - hns_sampled if not bkg_sampled else 0 #对块进行采样 sample_patches(full_img, mask_img, out_dir, img_id, abn, pos, patch_size, pos_cutoff, neg_cutoff, nb_bkg_, nb_abn, hns_sampled, itype, bkg_dir, calc_pos_dir, calc_neg_dir, mass_pos_dir, mass_neg_dir, verbose) bkg_sampled = True except AttributeError: print("Read image error: %s" % (full_fn)) except ValueError: print("Error sampling from ROI mask image: %s" % (mask_fn))
def get_image_and_mask(split, patient_id, side, view, image_dir, roi_mask_dir, abn_type, abn_id, target_height, target_width): token_list = [] token_list.append(("Calc" if abn_type == "calcification" else "Mass") + "-" + split) token_list.extend([patient_id, side, view]) # get image directory image_folder_name = "_".join(token_list) if split == "Training": image_dir = os.path.join(image_dir, image_folder_name) else: image_dir = os.path.join(image_dir, "CBIS-DDSM", image_folder_name) # search for the image image_path = None for cur_dir, sub_folders, file_names in os.walk(image_dir): if file_names: for file_name in file_names: if file_name.endswith(".png"): assert file_name == "000000.png" image_path = os.path.join(cur_dir, "000000.png") break if not image_path: raise RuntimeError( "Can not find image file in {!r}.".format(image_dir)) ori_image_shape = cv2.imread(image_path).shape[:2] if target_width is None: full_image = read_resize_img(image_path, target_height=target_height) else: full_image = read_resize_img(image_path, target_size=(target_height, target_width)) # get ROI mask directory # NOTE ROI and mask are stored in one folder, and 0 and 1 does not mean ROI # and mask, may be revsered! token_list.append(str(abn_id)) roi_mask_folder_name = "_".join(token_list) roi_mask_paths = [] if split == "Training": roi_mask_dir = os.path.join(roi_mask_dir, roi_mask_folder_name) else: roi_mask_dir = os.path.join(roi_mask_dir, "CBIS-DDSM", roi_mask_folder_name) for cur_dir, sub_folders, file_names in os.walk(roi_mask_dir): if file_names: for file_name in file_names: if file_name.endswith(".png"): assert file_name in ("000000.png", "000001.png") roi_mask_paths.append(os.path.join(cur_dir, file_name)) if len(roi_mask_paths) != 2: raise RuntimeError("Wrong number of image files in %s." % roi_mask_dir) mask_path = None for path in roi_mask_paths: mask = cv2.imread(path) if mask.shape[:2] == ori_image_shape: mask_path = path break if mask_path is None: raise RuntimeError("Found no mask image with a compatiable shape.") if target_width is None: mask_image = read_resize_img(mask_path, target_height=target_height, gs_255=True) else: mask_image = read_resize_img(mask_path, target_size=(target_height, target_width), gs_255=True) return full_image, mask_image