def get_val_dice(cnn, config): val_dsc = [] taskdir = cfg['taskdir'] # load array from selected val to test dsc and compare with cart vw label if os.path.exists(taskdir + '/valarray_cart.npy'): xarray_val = np.load(taskdir + '/valarray_cart.npy') xlabel_val = np.load(taskdir + '/vallabel_cart.npy') else: val_exams = dbloader.list_exams('val', shuffle=False) xarray_val = [] xlabel_val = [] bi = 0 for ei in val_exams: caseloader = CaseLoader(ei) print(caseloader) for slicei in caseloader.slices: if caseloader.valid_slicei(slicei) == False: continue if bi % 20 == 0: polarstack = caseloader.loadstack(slicei, 'cart_patch', nei=config['depth'] // 2) xarray_val.append(polarstack[..., None]) cartabel = caseloader.loadstack(slicei, 'cart_label', nei=config['depth'] // 2)[..., config['depth'] // 2] xlabel_val.append(cartabel[:, :, 1] - cartabel[:, :, 0]) bi += 1 xarray_val = np.array(xarray_val) xlabel_val = np.array(xlabel_val) print(xarray_val.shape, xlabel_val.shape) np.save(taskdir + '/valarray_cart.npy', xarray_val) np.save(taskdir + '/vallabel_cart.npy', xlabel_val) polarbd_all = cnn.predict(xarray_val, batch_size=config['G'] * config['batchsize']) for ti in range(xarray_val.shape[0]): carlabel = xlabel_val[ti] polarimg = xarray_val[ti] polarbd = polarbd_all[ti] * config['width'] // 2 # polarprob = polarprobr[:,:,0,1]-polarprobr[:,:,0,0] contourin, contourout = toctbd(polarbd, config['patchheight'], config['patchheight']) carseg = plotct(polarimg.shape[1], contourin, contourout) cdsc = DSC(carlabel, carseg) val_dsc.append(cdsc) # pxconf = (np.sum(polarprob[polarseg>0])-np.sum(polarprob[polarseg==0]))/np.sum(polarseg>0) # sconf.append(pxconf) print('\rPred', ti, '/', xarray_val.shape[0], 'val dice', '%.5f' % np.mean(val_dsc), end="") return np.mean(val_dsc)
def polar_seg_slice(vwcnn, vwcfg, dicomstack, slicei, cts, SCALE=4, usegrad=False, ITERPRED=True, cartreg=False): DEBUG = 0 contourin = None contourout = None polarconsistency = None recenters = None if DEBUG: print(slicei, 'cts', cts) if len(cts) == 1: ct = cts[0] ctx = ct[0] cty = ct[1] if ITERPRED == False: cartstack = crop_dcm_stack(dicomstack, slicei, cty, ctx, 64, 1) cartstack = cartstack / np.max(cartstack) polarimg = topolar(cartstack, 64, 64) polar_img_rz = cv2.resize(polarimg, (0, 0), fx=SCALE, fy=SCALE) if cartreg: cart_stack_rz = cv2.resize(cartstack, (0, 0), fx=2, fy=2) contbd = vwcnn.predict(cart_stack_rz[None, :, :, :, None])[0] * 256 contour_in_roi = contbd[:, :2] + 256 contour_out_roi = contbd[:, 2:] + 256 contourin = to_dcm_cord(contour_in_roi, BB(ctx, cty, 0, 0)) contourout = to_dcm_cord(contour_out_roi, BB(ctx, cty, 0, 0)) polarconsistency = None else: polarbd, polarsd = polar_pred_cont_cst(polar_img_rz[:, :, :, None], vwcfg, vwcnn, usegrad=usegrad) polarconsistency = 1 - np.mean(polarsd, axis=0) # contour positions [x, y] in original dicom space contourin, contourout = toctbd(polarbd / SCALE, ctx, cty) if DEBUG: contourin, contourout = toctbd(polarbd, 256, 256) cartvw = plotct(512, contourin, contourout) plt.subplot(1, 2, 1) cartstackdsp = copy.copy(cartstack) cartstackdsp[:, cartstackdsp.shape[1] // 2] = np.max(cartstackdsp) cartstackdsp[cartstackdsp.shape[0] // 2] = np.max(cartstackdsp) plt.imshow(cartstackdsp) plt.subplot(1, 2, 2) cartvwdsp = copy.copy(cartvw) cartvwdsp[:, cartvwdsp.shape[1] // 2] = np.max(cartvwdsp) cartvwdsp[cartvwdsp.shape[0] // 2] = np.max(cartvwdsp) plt.imshow(cartvwdsp) plt.show() # offset in 512 dcm cordinate else: # iterative pred and refine center REPS = 20 MOVEFACTOR = 0.5 MAX_OFF_STEP = 1 mincty = cty minctx = ctx mindiff = np.inf lastoffset = [0, 0] if DEBUG: print('init', ctx, cty) for repi in range(REPS): if repi == REPS - 1 or MAX_OFF_STEP < 0.1: if DEBUG: print('use min diff cts', minctx, mincty, 'with mindiff', mindiff) cty = mincty ctx = minctx cartstack = crop_dcm_stack(dicomstack, slicei, cty, ctx, 64, 1) cartstack = cartstack / np.max(cartstack) polarimg = topolar(cartstack, 64, 64) polar_img_rz = cv2.resize(polarimg, (0, 0), fx=SCALE, fy=SCALE) polarbd, polarsd = polar_pred_cont_cst(polar_img_rz[:, :, :, None], vwcfg, vwcnn, usegrad=usegrad) if DEBUG: contourin, contourout = toctbd(polarbd, 256, 256) cartvw = plotct(512, contourin, contourout) plt.subplot(1, 2, 1) cartstackdsp = copy.copy(cartstack) cartstackdsp[:, cartstackdsp.shape[1] // 2] = np.max(cartstackdsp) cartstackdsp[cartstackdsp.shape[0] // 2] = np.max(cartstackdsp) plt.imshow(cartstackdsp) plt.subplot(1, 2, 2) cartvwdsp = copy.copy(cartvw) cartvwdsp[:, cartvwdsp.shape[1] // 2] = np.max(cartvwdsp) cartvwdsp[cartvwdsp.shape[0] // 2] = np.max(cartvwdsp) plt.imshow(cartvwdsp) plt.show() #offset in 512 dcm cordinate polar_cont_offset = cal_polar_offset(polarbd) if repi > 0 and MAX_OFF_STEP > 0.1 and lastoffset == polar_cont_offset: #MAX_OFF_STEP += 0.5/SCALE MAX_OFF_STEP = 0.09 if DEBUG: print('move ct no change', MAX_OFF_STEP, lastoffset, polar_cont_offset) continue cofftype = [polar_cont_offset[0] > 0, polar_cont_offset[1] > 0] if repi == 0: offtype = cofftype if repi > 2 and cofftype != offtype: MAX_OFF_STEP /= 2 if DEBUG: print('reduce max move to', MAX_OFF_STEP) offtype = cofftype polarconsistency = 1 - np.mean(polarsd, axis=0) #ccstl = polarconsistency[0] #ccstw = polarconsistency[1] # contour positions [x, y] in original dicom space contourin, contourout = toctbd(polarbd / SCALE, ctx, cty) cdif = np.max(abs(np.array(polar_cont_offset))) if cdif < 1 or MAX_OFF_STEP < 0.1: if cdif < 1: print(polar_cont_offset) print('==', slicei, 'Break tracklet ref', polar_cont_offset) break if cdif < mindiff: mindiff = cdif mincty = cty minctx = ctx cofx = polar_cont_offset[0] cofy = polar_cont_offset[1] if abs(polar_cont_offset[0]) < 1: cofx = 0 if abs(polar_cont_offset[1]) < 1: cofy = 0 if repi < 2: ctx += cofx * MOVEFACTOR cty += cofy * MOVEFACTOR else: ctx = ctx + max(-MAX_OFF_STEP, min(MAX_OFF_STEP, cofx * MOVEFACTOR)) cty = cty + max(-MAX_OFF_STEP, min(MAX_OFF_STEP, cofy * MOVEFACTOR)) print('repeat', repi, 'offset', polar_cont_offset, ctx, cty) lastoffset = polar_cont_offset recenters = [[ctx, cty]] else: print('multiple cts', cts) all_contour_in = [] all_contour_out = [] all_polarconsistency = [] for ct in cts: ctx = ct[0] cty = ct[1] cartstack = crop_dcm_stack(dicomstack, slicei, cty, ctx, 64, 1) cartstack = cartstack / np.max(cartstack) polarimg = topolar(cartstack, 64, 64) polar_img_rz = cv2.resize(polarimg, (0, 0), fx=SCALE, fy=SCALE) polarbd, polarsd = polar_pred_cont_cst(polar_img_rz[:, :, :, None], vwcfg, vwcnn) polarconsistency_c = 1 - np.mean(polarsd, axis=0) contour_in_c, contour_out_c = toctbd(polarbd / SCALE, ctx, cty) if DEBUG: plt.subplot(1, 2, 1) plt.imshow(polarimg) plt.subplot(1, 2, 2) polarseg = plotpolar(256, polarbd) plt.imshow(polarseg) plt.show() all_contour_in.append(contour_in_c) all_contour_out.append(contour_out_c) all_polarconsistency.append(polarconsistency_c) contourin = mergecont(all_contour_in, cts) contourout = mergecont(all_contour_out, cts) ctx, cty = np.mean(cts, axis=0) if DEBUG: seg_vw_merge = plotct(512 * SCALE, contourin * SCALE, contourout * SCALE) predseg = croppatch(seg_vw_merge, cty * SCALE, ctx * SCALE, 64 * SCALE, 64 * SCALE) plt.imshow(predseg) plt.show() polarconsistency = np.mean(np.array(all_polarconsistency), axis=0) #no center adjustment for multi ct prediction recenters = [[ctx, cty]] return [contourin, contourout], polarconsistency, recenters
def gen_aug_patch(caseloader, slicei, dcmstack=None): DEBUG = 0 ctx, cty = caseloader.loadct(slicei) if dcmstack is None: dcmstack = caseloader.loadstack(slicei, 'dcm') cartcont = caseloader.load_cart_cont(slicei) #cart_patch = croppatch(dcmstack,cty,ctx,256,256) #plt.imshow(cart_patch) #plt.show() mindist = caseloader.load_cart_min_dist(slicei) dist_pred_ct_gt, nms_dist_map_p_gt = find_nms_center(mindist, fig=1) validy, validx = np.where(nms_dist_map_p_gt[:, :, 0] > 128) ''' for rd in range(5): rnd = random.randint(0,validx.shape[0]-1) ckey = '%d-%d'%(validx[rnd],validy[rnd]) dist_pred_ct_gt[ckey] = nms_dist_map_p_gt[validy[rnd],validx[rnd],0]/256 ''' xypos = [[validx[i], validy[i]] for i in range(len(validx))] kmeans = KMeans(n_clusters=5, random_state=0).fit(xypos) for kmcti in kmeans.cluster_centers_: kmcx = int(round(kmcti[0])) kmcy = int(round(kmcti[1])) ckey = '%d-%d' % (kmcx, kmcy) if nms_dist_map_p_gt[kmcy, kmcx, 0] < 127: print('kmean cluster center not large enough', kmcti, nms_dist_map_p_gt[kmcy, kmcx, 0]) dist_pred_ct_gt[ckey] = nms_dist_map_p_gt[kmcy, kmcx, 0] / 256 #print(dist_pred_ct_gt) aug_patch_obj = {} targetdir = DATADESKTOPdir + '/DVWIMAGES/' aug_cart_patch_batch = [] new_cart_patch_batch_filename = '/casepatch/'+caseloader.art+'/'+caseloader.pjname+'/augcart/'+caseloader.pi+os.path.basename(caseloader.dcmpath(slicei))[:-4]+\ caseloader.side+'.npy' aug_patch_obj['cartpatchbatchname'] = new_cart_patch_batch_filename if not os.path.exists(targetdir + '/casepatch/' + caseloader.art + '/' + caseloader.pjname + '/augcart/'): os.mkdir(targetdir + '/casepatch/' + caseloader.art + '/' + caseloader.pjname + '/augcart/') aug_polar_patch_batch = [] new_polar_patch_batch_filename = '/casepatch/'+caseloader.art+'/'+caseloader.pjname+'/augpolar/'+caseloader.pi+os.path.basename(caseloader.dcmpath(slicei))[:-4]+\ caseloader.side+'.npy' aug_patch_obj['polarpatchbatchname'] = new_polar_patch_batch_filename if not os.path.exists(targetdir + '/casepatch/' + caseloader.art + '/' + caseloader.pjname + '/augpolar/'): os.mkdir(targetdir + '/casepatch/' + caseloader.art + '/' + caseloader.pjname + '/augpolar/') aug_polar_cont_batch = [] new_polar_cont_batch_filename = '/casepatch/'+caseloader.art+'/'+caseloader.pjname+'/augpolarcont/'+caseloader.pi+os.path.basename(caseloader.dcmpath(slicei))[:-4]+\ caseloader.side+'.npy' aug_patch_obj['polarcoutbatchname'] = new_polar_cont_batch_filename if not os.path.exists(targetdir + '/casepatch/' + caseloader.art + '/' + caseloader.pjname + '/augpolarcont/'): os.mkdir(targetdir + '/casepatch/' + caseloader.art + '/' + caseloader.pjname + '/augpolarcont/') aug_patch_obj['auginfo'] = [] #plt.imshow(dcmstack) #plt.title('dcmstack') #plt.show() for cti in dist_pred_ct_gt.keys(): cctx = int(round(int(cti.split('-')[0]) + ctx - 256)) ccty = int(round(int(cti.split('-')[1]) + cty - 256)) #print(cctx,ccty) tx = cctx - ctx ty = ccty - cty new_cart_patch = croppatch(dcmstack, ccty, cctx, 256, 256) #plt.imshow(new_cart_patch[:,:,1]) #plt.title('new_cart_patch') #plt.show() new_cart_patch = new_cart_patch / np.max(new_cart_patch) aug_cart_patch_batch.append(new_cart_patch) new_polar_patch = topolar(new_cart_patch, 256, 256) new_polar_patch = new_polar_patch / np.max(new_polar_patch) #plt.imshow(new_polar_patch) #plt.show() aug_polar_patch_batch.append(new_polar_patch) #rebase polar cont new_polar_cont_lumen = tocordpolar(cartcont[0], cctx, ccty) new_polar_cont_wall = tocordpolar(cartcont[1], cctx, ccty) aug_polar_cont = np.zeros((256, 2)) aug_polar_cont[:, 0] = new_polar_cont_lumen aug_polar_cont[:, 1] = new_polar_cont_wall #exportpolarcontour(new_polar_cont_filename,[new_polar_cont_lumen,new_polar_cont_wall]) aug_polar_cont_batch.append(aug_polar_cont) if DEBUG: #check polar ct matches with cart patch cart_patch_dsp = np.zeros( (new_cart_patch.shape[0], new_cart_patch.shape[1], 3)) cart_patch_dsp[:, :, 0] = new_cart_patch[:, :, new_cart_patch.shape[2] // 2] polarbd = np.concatenate( [new_polar_cont_lumen[:, None], new_polar_cont_wall[:, None]], axis=1) * 256 contourin, contourout = toctbd(polarbd, 256, 256) cart_vw_seg = plotct(512, contourin, contourout) plt.imshow(cart_vw_seg) cart_patch_dsp[:, :, 1] = cart_vw_seg plt.imshow(cart_patch_dsp) plt.show() augobj = {} augobj['ctofx'] = cctx augobj['ctofy'] = ccty augobj['transx'] = tx augobj['transy'] = ty augobj['augid'] = len(aug_cart_patch_batch) - 1 aug_patch_obj['auginfo'].append(copy.copy(augobj)) #print(augobj) np.save(targetdir + new_cart_patch_batch_filename, np.array(aug_cart_patch_batch, dtype=np.float16)) np.save(targetdir + new_polar_patch_batch_filename, np.array(aug_polar_patch_batch, dtype=np.float16)) np.save(targetdir + new_polar_cont_batch_filename, np.array(aug_polar_cont_batch, dtype=np.float16)) caseloader.caselist['slices'][slicei]['augpatch'] = aug_patch_obj with open(caseloader.caselistname, 'wb') as fp: pickle.dump(caseloader.caselist, fp)
def plotvw(self,slicei): if self.seg['cartcont']['lumen'][slicei] is None: return np.zeros((512,512)) vw_seg = plotct(512, self.seg['cartcont']['lumen'][slicei], self.seg['cartcont']['wall'][slicei]) return vw_seg