def evaluate_xpdnet_denoising( model, run_id, n_epochs=200, contrast='CORPD_FBK', noise_std=30, n_samples=100, cuda_visible_devices='0123', ): os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(cuda_visible_devices) val_set = NoisyFastMRIDatasetBuilder( dataset='val', noise_power_spec=noise_std, noise_input=False, contrast=contrast, slice_random=True, scale_factor=1e4, force_determinism=True, ).preprocessed_ds val_set = val_set.take(n_samples) if isinstance(model, tuple): model = build_model_from_specs(*model) model.load_weights( f'{CHECKPOINTS_DIR}checkpoints/{run_id}-{n_epochs:02d}.hdf5') eval_res = Metrics(METRIC_FUNCS) for x, y_true in tqdm(val_set.as_numpy_iterator(), total=n_samples): y_pred = model.predict(x) eval_res.push(y_true[..., 0], y_pred[..., 0]) return METRIC_FUNCS, (list(eval_res.means().values()), list(eval_res.stddevs().values()))
def metrics_zfilled(): metrics = Metrics(METRIC_FUNCS) pred_and_gt = [ reco_and_gt_zfilled_from_val_file(*val_gen_scaled[i]) for i in tqdm(range(len(val_gen_scaled)), desc='Val files for z-filled') ] for im_recos, images in tqdm(pred_and_gt, desc='Stats for z-filled'): metrics.push(images, im_recos) return metrics
def metrics_for_params(reco_function=None, val_gen=None, name=None, **net_params): model = unpack_model(**net_params) metrics = Metrics(METRIC_FUNCS) pred_and_gt = [ reco_function(*val_gen[i], model) for i in tqdm(range(len(val_gen)), desc=f'Val files for {name}') ] for im_recos, images in tqdm(pred_and_gt, desc=f'Stats for {name}'): metrics.push(images, im_recos) return metrics
def evaluate_nc( model, multicoil=False, run_id=None, n_epochs=200, contrast=None, acq_type='radial', n_samples=None, cuda_visible_devices='0123', dcomp=False, **acq_kwargs, ): if multicoil: val_path = f'{FASTMRI_DATA_DIR}multicoil_val/' else: val_path = f'{FASTMRI_DATA_DIR}singlecoil_val/' os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(cuda_visible_devices) if multicoil: dataset = multicoil_dataset else: dataset = singlecoil_dataset val_set = dataset(val_path, IM_SIZE, acq_type=acq_type, compute_dcomp=dcomp, contrast=contrast, inner_slices=None, rand=False, scale_factor=1e6, **acq_kwargs) if n_samples is not None: val_set = val_set.take(n_samples) else: val_set = val_set.take(199) example_input = next(iter(val_set))[0] inputs_shape = _extract_inputs_shape(example_input, no_batch=True) inputs_dtype = _extract_inputs_dtype(example_input) inputs = _zeros_from_shape(inputs_shape, inputs_dtype) # special case for the shape: inputs[-1][0] = tf.constant([[372]]) model(inputs) if run_id is not None: model.load_weights( f'{CHECKPOINTS_DIR}checkpoints/{run_id}-{n_epochs:02d}.hdf5') m = Metrics(METRIC_FUNCS) for x, y_true in tqdm(val_set.as_numpy_iterator(), total=199 if n_samples is None else n_samples): y_pred = model.predict(x, batch_size=1) m.push(y_true[..., 0], y_pred[..., 0]) return METRIC_FUNCS, list(m.means().values())
def evaluate_vnet_postproc( original_run_id, run_id, brain=False, n_epochs=200, contrast=None, af=4, n_samples=None, base_n_filters=16, n_scales=4, non_linearity='prelu', ): if brain: val_path = f'{FASTMRI_DATA_DIR}brain_multicoil_val/' else: val_path = f'{FASTMRI_DATA_DIR}multicoil_val/' af = int(af) run_params = dict( layers_n_channels=[base_n_filters * 2**i for i in range(n_scales)], layers_n_non_lins=2, non_linearity=non_linearity, res=True, ) model = PostProcessVnet(None, run_params) model(tf.zeros([2, 320, 320, 1])) model.load_weights( f'{CHECKPOINTS_DIR}checkpoints/{run_id}-{n_epochs:02d}.hdf5') val_set = train_postproc_dataset_from_tfrecords( val_path, original_run_id, n_samples=n_samples, ) if brain: n_volumes = brain_n_volumes_validation if contrast is not None: n_volumes = brain_volumes_per_contrast['validation'][contrast] else: n_volumes = n_volumes_val if contrast is not None: n_volumes //= 2 n_volumes += 1 if n_samples is not None: val_set = val_set.take(n_samples) else: val_set = val_set.take(n_volumes) eval_res = Metrics(METRIC_FUNCS) for x, y_true in tqdm(val_set.as_numpy_iterator(), total=n_volumes if n_samples is None else n_samples): y_pred = model.predict_batched(x, batch_size=4) eval_res.push(y_true[..., 0], y_pred[..., 0]) return METRIC_FUNCS, (list(eval_res.means().values()), list(eval_res.stddevs().values()))
def metrics_for_params(val_gen=None, name=None, **net_params): if val_gen is None: val_gen = val_gen_mask model = unpack_model(**net_params) metrics = Metrics(METRIC_FUNCS) pred_and_gt = [ reco_and_gt_net_from_val_file(*val_gen[i], model) for i in tqdm(range(len(val_gen)), desc=f'Val files for {name}') ] for im_recos, images in tqdm(pred_and_gt, desc=f'Stats for {name}'): metrics.push(images, im_recos) return metrics
def evaluate_xpdnet_dealiasing( model_fun, model_kwargs, run_id, n_scales=0, n_epochs=200, contrast='CORPD_FBK', af=4, n_samples=None, cuda_visible_devices='0123', ): val_path = f'{FASTMRI_DATA_DIR}singlecoil_val/' os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(cuda_visible_devices) val_set = singlecoil_dataset( val_path, AF=af, contrast=contrast, inner_slices=None, rand=False, scale_factor=1e6, ) if n_samples is not None: val_set = val_set.take(n_samples) else: val_set = val_set.take(199) model = MultiscaleComplex( model_fun=model_fun, model_kwargs=model_kwargs, res=False, n_scales=n_scales, fastmri_format=True, ) model(next(iter(val_set))[0]) model.load_weights( f'{CHECKPOINTS_DIR}checkpoints/{run_id}-{n_epochs:02d}.hdf5') m = Metrics(METRIC_FUNCS) for x, y_true in tqdm(val_set.as_numpy_iterator(), total=199 if n_samples is None else n_samples): y_pred = model.predict(x, batch_size=1) m.push(y_true[..., 0], y_pred[..., 0]) return ['PSNR', 'SSIM'], list(m.means().values())
def evaluate_xpdnet( model_fun, model_kwargs, run_id, multicoil=True, brain=False, n_epochs=200, contrast=None, af=4, n_iter=10, res=True, n_scales=0, n_primal=5, refine_smaps=False, refine_big=False, n_samples=None, cuda_visible_devices='0123', equidistant_fake=False, mask_type=None, primal_only=True, n_dual=1, n_dual_filters=16, multiscale_kspace_learning=False, ): if multicoil: if brain: val_path = f'{FASTMRI_DATA_DIR}brain_multicoil_val/' else: val_path = f'{FASTMRI_DATA_DIR}multicoil_val/' else: val_path = f'{FASTMRI_DATA_DIR}singlecoil_val/' os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(cuda_visible_devices) af = int(af) run_params = { 'n_primal': n_primal, 'multicoil': multicoil, 'n_scales': n_scales, 'n_iter': n_iter, 'refine_smaps': refine_smaps, 'res': res, 'output_shape_spec': brain, 'refine_big': refine_big, 'primal_only': primal_only, 'n_dual': n_dual, 'n_dual_filters': n_dual_filters, 'multiscale_kspace_learning': multiscale_kspace_learning, } if multicoil: dataset = multicoil_dataset if mask_type is None: if brain: if equidistant_fake: mask_type = 'equidistant_fake' else: mask_type = 'equidistant' else: mask_type = 'random' kwargs = { 'parallel': False, 'output_shape_spec': brain, 'mask_type': mask_type, } else: dataset = singlecoil_dataset kwargs = {} val_set = dataset( val_path, AF=af, contrast=contrast, inner_slices=None, rand=False, scale_factor=1e6, **kwargs, ) if brain: n_volumes = brain_n_volumes_validation if contrast is not None: n_volumes = brain_volumes_per_contrast['validation'][contrast] else: n_volumes = n_volumes_val if contrast is not None: n_volumes //= 2 n_volumes += 1 if n_samples is not None: val_set = val_set.take(n_samples) else: val_set = val_set.take(n_volumes) mirrored_strategy = tf.distribute.MirroredStrategy() with mirrored_strategy.scope(): if multicoil: kspace_size = [1, 15, 640, 372] else: kspace_size = [1, 640, 372] model = XPDNet(model_fun, model_kwargs, **run_params) inputs = [ tf.zeros(kspace_size + [1], dtype=tf.complex64), tf.zeros(kspace_size, dtype=tf.complex64), ] if multicoil: inputs.append(tf.zeros(kspace_size, dtype=tf.complex64)) if brain: inputs.append(tf.constant([[320, 320]])) model(inputs) model.load_weights( f'{CHECKPOINTS_DIR}checkpoints/{run_id}-{n_epochs:02d}.hdf5') eval_res = Metrics(METRIC_FUNCS) for x, y_true in tqdm(val_set.as_numpy_iterator(), total=n_volumes if n_samples is None else n_samples): y_pred = model.predict(x, batch_size=4) eval_res.push(y_true[..., 0], y_pred[..., 0]) return METRIC_FUNCS, (list(eval_res.means().values()), list(eval_res.stddevs().values()))
def evaluate_nc( model, multicoil=False, three_d=False, run_id=None, n_epochs=200, contrast=None, acq_type='radial', n_samples=None, cuda_visible_devices='0123', dcomp=False, brain=False, **acq_kwargs, ): if multicoil: if brain: val_path = f'{FASTMRI_DATA_DIR}brain_multicoil_val/' else: val_path = f'{FASTMRI_DATA_DIR}multicoil_val/' elif three_d: val_path = f'{OASIS_DATA_DIR}/val/' else: val_path = f'{FASTMRI_DATA_DIR}singlecoil_val/' os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(cuda_visible_devices) if multicoil: dataset = multicoil_dataset image_size = IM_SIZE elif three_d: dataset = three_d_dataset image_size = VOLUME_SIZE else: dataset = singlecoil_dataset image_size = IM_SIZE if not three_d: add_kwargs = { 'contrast': contrast, 'rand': False, 'inner_slices': None, } if multicoil: add_kwargs.update(brain=brain) else: add_kwargs = {} add_kwargs.update(**acq_kwargs) val_set = dataset(val_path, image_size, acq_type=acq_type, compute_dcomp=dcomp, scale_factor=1e6 if not three_d else 1e-2, **add_kwargs) if n_samples is not None: val_set = val_set.take(n_samples) else: val_set = val_set.take(199) example_input = next(iter(val_set))[0] inputs = _extract_first_elem_of_batch(example_input) model(inputs) if run_id is not None: model.load_weights( f'{CHECKPOINTS_DIR}checkpoints/{run_id}-{n_epochs:02d}.hdf5') if three_d: m = Metrics({'PSNR': METRIC_FUNCS['PSNR']}) else: m = Metrics(METRIC_FUNCS) for x, y_true in tqdm(val_set.as_numpy_iterator(), total=199 if n_samples is None else n_samples): y_pred = model.predict(x, batch_size=1) m.push(y_true[..., 0], y_pred[..., 0]) del x del y_true del y_pred print(METRIC_FUNCS.keys()) print(list(m.means().values())) return METRIC_FUNCS, list(m.means().values())
Path(CHECKPOINTS_DIR) / model_path.format(n_coils=n_coils)) model_checkpoint = model_checkpoints.get(n_coils, None) model_kwargs.update(n_coils=n_coils) else: output_shape = (320, 320) save_path = str(Path(CHECKPOINTS_DIR) / model_path) x = x[0:2] y_pred = reconstruct_dip( x[1], x[0], model_checkpoint=model_checkpoint, save_model=model_checkpoint is None, save_path=save_path, multicoil=multicoil, n_iter=n_iter if model_checkpoint is None else n_iter // 10, output_shape=output_shape, **model_kwargs, ) if brain: model_checkpoints[n_coils] = save_path else: model_checkpoint = save_path m.push(y_true[..., 0], y_pred[..., 0].numpy()) del x del y_true del y_pred print(METRIC_FUNCS.keys()) print(list(m.means().values())) m.to_csv() return METRIC_FUNCS, list(m.means().values())
def evaluate_updnet( multicoil=True, brain=False, run_id='updnet_sense_af4_1588609141', n_epochs=200, contrast=None, af=4, n_iter=10, n_layers=3, base_n_filter=16, non_linearity='relu', channel_attention_kwargs=None, refine_smaps=False, n_samples=None, cuda_visible_devices='0123', verbose=False, equidistant_fake=False, mask_type=None, ): if verbose: print(f'Evaluating {run_id}') if multicoil: if brain: val_path = f'{FASTMRI_DATA_DIR}brain_multicoil_val/' else: val_path = f'{FASTMRI_DATA_DIR}multicoil_val/' else: val_path = f'{FASTMRI_DATA_DIR}singlecoil_val/' os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(cuda_visible_devices) af = int(af) run_params = { 'n_primal': 5, 'n_dual': 1, 'primal_only': True, 'multicoil': multicoil, 'n_layers': n_layers, 'layers_n_channels': [base_n_filter * 2**i for i in range(n_layers)], 'non_linearity': non_linearity, 'n_iter': n_iter, 'channel_attention_kwargs': channel_attention_kwargs, 'refine_smaps': refine_smaps, 'output_shape_spec': brain, } if multicoil: dataset = multicoil_dataset if mask_type is None: if brain: if equidistant_fake: mask_type = 'equidistant_fake' else: mask_type = 'equidistant' else: mask_type = 'random' kwargs = { 'parallel': False, 'output_shape_spec': brain, 'mask_type': mask_type, } else: dataset = singlecoil_dataset kwargs = {} val_set = dataset( val_path, AF=af, contrast=contrast, inner_slices=None, rand=False, scale_factor=1e6, **kwargs, ) if brain: n_volumes = brain_n_volumes_validation if contrast is not None: n_volumes = brain_volumes_per_contrast['validation'][contrast] else: n_volumes = n_volumes_val if contrast is not None: n_volumes //= 2 n_volumes += 1 if n_samples is not None: val_set = val_set.take(n_samples) else: val_set = val_set.take(n_volumes) mirrored_strategy = tf.distribute.MirroredStrategy() with mirrored_strategy.scope(): if multicoil: kspace_size = [1, 15, 640, 372] else: kspace_size = [1, 640, 372] model = UPDNet(**run_params) inputs = [ tf.zeros(kspace_size + [1], dtype=tf.complex64), tf.zeros(kspace_size, dtype=tf.complex64), ] if multicoil: inputs.append(tf.zeros(kspace_size, dtype=tf.complex64)) if brain: inputs.append(tf.constant([[320, 320]])) model(inputs) model.load_weights(f'{CHECKPOINTS_DIR}checkpoints/{run_id}-{n_epochs:02d}.hdf5') m = Metrics(METRIC_FUNCS) for x, y_true in tqdm(val_set.as_numpy_iterator(), total=n_volumes if n_samples is None else n_samples): y_pred = model.predict(x, batch_size=4) m.push(y_true[..., 0], y_pred[..., 0]) return METRIC_FUNCS, list(m.means().values())