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 data_generator(config, exams, aug): xarray = np.zeros([ config['batchsize'], config['height'], config['width'], config['depth'], config['channel'] ]) yarray = np.zeros([config['batchsize'], config['patchheight'], 2]) bi = 0 while 1: for ei in exams: caseloader = CaseLoader(ei) # print(caseloader) for slicei in caseloader.slices: polarstack = caseloader.loadstack(slicei, 'cart_patch', nei=config['depth'] // 2) if polarstack is None: # print('skip',caseloader,slicei) continue polarcont = caseloader.loadstack(slicei, 'polar_cont', nei=config['depth'] // 2) polar_cont_center = polarcont[:, :, config['depth'] // 2] xarray[bi] = polarstack[..., None] yarray[bi] = polar_cont_center bi += 1 if bi == config['batchsize']: bi = 0 if aug == True: for offi in random.sample(range(config['patchheight']), config['rottimes']): xarray_off = batch_polar_rot(xarray, offi) yarray_off = batch_polar_rot(yarray, offi) yield (xarray_off, yarray_off) else: yield xarray, yarray
def data_generator(config, exams, aug): xarray = np.zeros([ config['batchsize'], config['height'], config['width'], config['depth'], config['channel'] ]) yarray = np.zeros([config['batchsize'], config['patchheight'], 2]) bi = 0 while 1: for ei in exams: caseloader = CaseLoader(ei) # print(caseloader) for slicei in caseloader.slices: polarstack = caseloader.loadstack(slicei, 'cart_patch', nei=config['depth'] // 2) if polarstack is None: # print('skip',caseloader,slicei) continue polarstack = cv2.resize(polarstack, (256, 256)) polarcont = caseloader.loadstack(slicei, 'polar_cont', nei=config['depth'] // 2) polar_cont_center = polarcont[:, :, config['depth'] // 2] xarray[bi] = polarstack[..., None] yarray[bi] = polar_cont_center bi += 1 if bi == config['batchsize']: bi = 0 if aug == True: for offi in range(config['rottimes']): rotsarr = [] for i in range(xarray.shape[0]): rots = np.arange(4) random.shuffle(rots) rotsarr.append(rots) rotsarr = np.array(rotsarr) for roti in range(4): xarray_off, yarray_off = batch_cart_rot( xarray, yarray, rotsarr[:, roti]) yield (xarray_off, yarray_off) else: yield (xarray, yarray)
def data_generator(config, exams, aug): xarray = np.zeros([ config['batchsize'], config['height'], config['width'], config['depth'], config['channel'] ]) yarray = np.zeros([config['batchsize'], config['patchheight'], 2]) bi = 0 while 1: for ei in exams: caseloader = CaseLoader(ei) #print(caseloader) for slicei in caseloader.slices: if caseloader.valid_slicei(slicei) == False: continue if caseloader.caselist['slices'][slicei]['augpatch'] == []: continue gen_aug_patch(caseloader, slicei) print(caseloader, slicei, 'gen') aug_polar_patch_batch = caseloader.load_aug_patch( slicei, 'polar_patch') aug_polar_cont_batch = caseloader.load_aug_patch( slicei, 'polar_cont') for augi in range(len(aug_polar_patch_batch)): aug_cart_patch = aug_polar_patch_batch[augi] xarray[bi] = aug_polar_patch_batch[augi, :, :, :, None] aug_polar_cont = aug_polar_cont_batch[augi] yarray[bi] = aug_polar_cont_batch[augi] bi += 1 if bi == config['batchsize']: bi = 0 if aug == True: for offi in random.sample(range(config['height']), config['rottimes']): xarray_off = batch_polar_rot(xarray, offi) yarray_off = batch_polar_rot(yarray, offi) yield (xarray_off, yarray_off) else: yield (xarray, yarray)
def data_generator(config, exams, aug): xarray = np.zeros([config['batchsize'], config['height'], config['width'], config['depth'], config['channel']]) yarray = np.zeros([config['batchsize'], config['patchheight'], 2]) bi = 0 while 1: for ei in exams: caseloader = CaseLoader(ei) # print(caseloader) for slicei in caseloader.slices: if caseloader.valid_slicei(slicei) == False: continue if caseloader.caselist['slices'][slicei]['augpatch'] == [] : continue gen_aug_patch(caseloader, slicei) print(caseloader, slicei, 'gen') aug_cart_patch_batch = caseloader.load_aug_patch(slicei, 'cart_patch') aug_polar_cont_batch = caseloader.load_aug_patch(slicei, 'polar_cont') for augi in range(len(aug_cart_patch_batch)): aug_cart_patch = cv2.resize(aug_cart_patch_batch[augi], (config['width'], config['height'])) xarray[bi] = aug_cart_patch[..., None] aug_polar_cont = aug_polar_cont_batch[augi] yarray[bi] = aug_polar_cont_batch[augi] bi += 1 if bi == config['batchsize']: bi = 0 if aug == True: for offi in range(config['rottimes']): rotsarr = [] for i in range(xarray.shape[0]): rots = np.arange(4) random.shuffle(rots) rotsarr.append(rots) rotsarr = np.array(rotsarr) for roti in range(4): xarray_off, yarray_off = batch_cart_rot(xarray, yarray, rotsarr[:, roti]) yield (xarray_off, yarray_off) else: yield (xarray, yarray)
def data_generator(config, exams, aug): xarray = np.zeros([ config['batchsize'], config['height'], config['width'], config['depth'], config['channel'] ]) yarray = np.zeros( [config['batchsize'], config['height'], config['width'], 1, 1]) bi = 0 while 1: for ei in exams: caseloader = CaseLoader(ei) # print(caseloader) for slicei in caseloader.slices: if caseloader.valid_slicei(slicei) == False: continue #load center patch xarray[bi] = caseloader.loadstack(slicei, 'cart_patch')[:, :, :, None] yarray[bi] = caseloader.load_cart_vw(slicei)[:, :, None, None] bi += 1 if bi == config['batchsize']: bi = 0 yield (xarray, yarray) # load aug patch aug_cart_patch_batch = caseloader.load_aug_patch( slicei, 'cart_patch') aug_cart_label_batch = caseloader.load_aug_patch( slicei, 'cart_label') for augi in range(len(aug_cart_patch_batch)): xarray[bi] = aug_cart_patch_batch[augi, :, :, :, None] yarray[bi] = aug_cart_label_batch[augi][:, :, None, None] bi += 1 if bi == config['batchsize']: bi = 0 yield (xarray, yarray)
def data_generator(config, exams, aug): xarray = np.zeros([ config['batchsize'], config['height'], config['width'], config['depth'], config['channel'] ]) yarray = np.zeros([config['batchsize'], config['patchheight'], 2]) bi = 0 while 1: for ei in exams: caseloader = CaseLoader(ei) # print(caseloader) for slicei in caseloader.slices: if caseloader.valid_slicei(slicei) == False: continue cartcont = caseloader.load_cart_cont(slicei) min_dist_map = caseloader.load_cart_min_dist(slicei) dcmstack = caseloader.loadstack(slicei, 'dcm') ctx, cty = caseloader.loadct(slicei) aug_polar_patch_batch = np.zeros((10, 256, 256, 3)) aug_polar_cont_batch = np.zeros((10, 256, 2)) offi = 0 mi = 0 while offi < 10 and mi < 50: mi += 1 if offi == 0: ofx = 0 ofy = 0 else: ofx = random.randint(-20, 20) ofy = random.randint(-20, 20) cctx = ofx + ctx ccty = ofy + cty if min_dist_map[256 + ofx, 256 + ofy] < 0.5: if mi > 40: print('mi', mi) # print('mind',min_dist_map[256+ofx,256+ofy]) continue # print(ofx,ofy) new_polar_cont_lumen = tocordpolar(cartcont[0], cctx, ccty) new_polar_cont_wall = tocordpolar(cartcont[1], cctx, ccty) aug_polar_cont_batch[offi, :, 0] = new_polar_cont_lumen aug_polar_cont_batch[offi, :, 1] = new_polar_cont_wall new_cart_patch = croppatch(dcmstack, ccty, cctx, 256, 256) new_polar_patch = topolar(new_cart_patch, 256, 256) aug_polar_patch_batch[offi] = new_polar_patch / np.max( new_polar_patch) offi += 1 # aug_polar_patch_batch = caseloader.load_aug_patch(slicei,'polar_patch') # aug_polar_cont_batch = caseloader.load_aug_patch(slicei,'polar_cont') for augi in range(len(aug_polar_patch_batch)): xarray[bi] = aug_polar_patch_batch[augi, :, :, :, None] yarray[bi] = aug_polar_cont_batch[augi] bi += 1 if bi == config['batchsize']: bi = 0 if aug == True: for offi in random.sample(range(config['height']), config['rottimes']): xarray_off = batch_polar_rot(xarray, offi) yarray_off = batch_polar_rot(yarray, offi) yield (xarray_off, yarray_off) else: yield (xarray, yarray)