From b8564bda5baf226b9b084921531e01715859e234 Mon Sep 17 00:00:00 2001 From: Geeks-Sid Date: Wed, 26 Oct 2022 21:11:55 -0400 Subject: [PATCH 01/10] updating files --- BrainMaGe/trainer/lightning_networks.py | 32 ++------------------- BrainMaGe/trainer/trainer_main.py | 38 +++++++++++++++++++------ setup.cfg | 2 +- setup.py | 6 ++-- 4 files changed, 36 insertions(+), 42 deletions(-) diff --git a/BrainMaGe/trainer/lightning_networks.py b/BrainMaGe/trainer/lightning_networks.py index 7a1efc1..f98a182 100755 --- a/BrainMaGe/trainer/lightning_networks.py +++ b/BrainMaGe/trainer/lightning_networks.py @@ -6,13 +6,11 @@ @author: siddhesh """ -import torch -from torch.utils.data import DataLoader import pytorch_lightning as ptl +import torch from BrainMaGe.models.networks import fetch_model from BrainMaGe.utils.cyclicLR import CyclicCosAnnealingLR -from BrainMaGe.utils.losses import dice_loss, dice -from BrainMaGe.utils.data import SkullStripDataset +from BrainMaGe.utils.losses import dice, dice_loss from BrainMaGe.utils.optimizers import fetch_optimizer @@ -79,29 +77,3 @@ def configure_optimizers(self): eta_min=5e-6, ) return [optimizer], [scheduler] - - @ptl.data_loader - def train_dataloader(self): - dataset_train = SkullStripDataset( - self.params["train_csv"], self.params, test=False - ) - return DataLoader( - dataset_train, - batch_size=int(self.params["batch_size"]), - shuffle=True, - num_workers=4, - pin_memory=True, - ) - - @ptl.data_loader - def val_dataloader(self): - dataset_valid = SkullStripDataset( - self.params["validation_csv"], self.params, test=False - ) - return DataLoader( - dataset_valid, - batch_size=int(self.params["batch_size"]), - shuffle=False, - num_workers=4, - pin_memory=True, - ) diff --git a/BrainMaGe/trainer/trainer_main.py b/BrainMaGe/trainer/trainer_main.py index 5a4c563..10681ff 100755 --- a/BrainMaGe/trainer/trainer_main.py +++ b/BrainMaGe/trainer/trainer_main.py @@ -8,14 +8,17 @@ import os -import time import sys -import torch +import time + import pandas as pd -from pytorch_lightning import Trainer -from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping -from BrainMaGe.utils.csv_creator_adv import generate_csv +import torch from BrainMaGe.trainer.lightning_networks import SkullStripper +from BrainMaGe.utils.csv_creator_adv import generate_csv +from BrainMaGe.utils.data import SkullStripDataset +from pytorch_lightning import Trainer +from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint +from torch.utils.data import DataLoader def train_network(cfg, device, weights): @@ -99,8 +102,8 @@ def train_network(cfg, device, weights): print("Using device:", device) if device.type == "cuda": print("Memory Usage:") - print("Allocated:", round(torch.cuda.memory_allocated(0) / 1024 ** 3, 1), "GB") - print("Cached: ", round(torch.cuda.memory_cached(0) / 1024 ** 3, 1), "GB") + print("Allocated:", round(torch.cuda.memory_allocated(0) / 1024**3, 1), "GB") + print("Cached: ", round(torch.cuda.memory_cached(0) / 1024**3, 1), "GB") sys.stdout.flush() # We generate CSV for training if not provided @@ -163,4 +166,23 @@ def train_network(cfg, device, weights): num_sanity_val_steps=5, resume_from_checkpoint=res_ckpt, ) - trainer.fit(model) + + dataset_train = SkullStripDataset(params["train_csv"], params, test=False) + train_dataloader = DataLoader( + dataset_train, + batch_size=int(params["batch_size"]), + shuffle=True, + num_workers=4, + pin_memory=True, + ) + + dataset_valid = SkullStripDataset(params["validation_csv"], params, test=False) + val_dataloader = DataLoader( + dataset_valid, + batch_size=int(params["batch_size"]), + shuffle=False, + num_workers=4, + pin_memory=True, + ) + + trainer.fit(model, train_dataloader, val_dataloader) diff --git a/setup.cfg b/setup.cfg index 224a779..b88034e 100755 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,2 @@ [metadata] -description-file = README.md \ No newline at end of file +description-file = README.md diff --git a/setup.py b/setup.py index 1e1d2d4..740843d 100755 --- a/setup.py +++ b/setup.py @@ -22,10 +22,10 @@ zip_safe=False, install_requires=[ "numpy", - "torch>=1.5.1", - "scikit-image==0.16.2", + "torch==1.8.2", + "scikit-image", "nibabel", - "pytorch-lightning==0.8.1", + "pytorch-lightning==1.6.0", "bids" ], scripts=[ From 769504f02c77ca0a42c4a2fa44259f29e79bd08f Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Mon, 24 Apr 2023 15:52:36 -0400 Subject: [PATCH 02/10] applied black and updated requirements --- BrainMaGe/tester/test_ma.py | 3 +- BrainMaGe/tester/test_single_multi_4.py | 8 +- BrainMaGe/utils/convert_ckpt_to_pt.py | 1 - brain_mage_single_run | 4 +- brain_mage_single_run_multi_4 | 6 +- openvino/benchmark-ov-corescaling.py | 149 +++++++++++------- openvino/benchmark-pt-ov-memprofile.py | 94 ++++++------ openvino/benchmark-pt-ov.py | 149 ++++++++++-------- openvino/compare_utils.py | 14 +- openvino/export-to-ov.py | 41 ++--- openvino/generate-csv-nfbs-dataset.py | 46 ++++-- openvino/quantize.py | 194 ++++++++++++++---------- setup.py | 8 +- 13 files changed, 414 insertions(+), 303 deletions(-) diff --git a/BrainMaGe/tester/test_ma.py b/BrainMaGe/tester/test_ma.py index 28f6873..17ae59a 100755 --- a/BrainMaGe/tester/test_ma.py +++ b/BrainMaGe/tester/test_ma.py @@ -38,7 +38,6 @@ def postprocess_prediction(seg): def infer_ma(cfg, device, save_brain, weights): - cfg = os.path.abspath(cfg) if os.path.isfile(cfg): @@ -152,7 +151,7 @@ def infer_ma(cfg, device, save_brain, weights): int(params["num_classes"]), int(params["base_filters"]), ) - checkpoint = torch.load(str(params["weights"]),map_location=torch.device(device)) + checkpoint = torch.load(str(params["weights"]), map_location=torch.device(device)) model.load_state_dict(checkpoint["model_state_dict"]) if device != "cpu": diff --git a/BrainMaGe/tester/test_single_multi_4.py b/BrainMaGe/tester/test_single_multi_4.py index 02af6c3..b708142 100755 --- a/BrainMaGe/tester/test_single_multi_4.py +++ b/BrainMaGe/tester/test_single_multi_4.py @@ -32,7 +32,9 @@ def postprocess_prediction(seg): return seg -def infer_single_multi_4(input_paths, output_path, weights, mask_path=None, device="cpu"): +def infer_single_multi_4( + input_paths, output_path, weights, mask_path=None, device="cpu" +): """ Inference using multi modality network @@ -69,7 +71,7 @@ def infer_single_multi_4(input_paths, output_path, weights, mask_path=None, devi num_filters=16, ) - checkpoint = torch.load(str(weights), map_location=torch.device('cpu')) + checkpoint = torch.load(str(weights), map_location=torch.device("cpu")) model.load_state_dict(checkpoint["model_state_dict"]) if device != "cpu": @@ -105,7 +107,7 @@ def infer_single_multi_4(input_paths, output_path, weights, mask_path=None, devi print("Done with running the model.") if mask_path is not None: - raise NotImplementedError('Sorry, masking is not implemented (yet).') + raise NotImplementedError("Sorry, masking is not implemented (yet).") print("Final output stored in : %s" % (output_path)) print("Thank you for using BrainMaGe") diff --git a/BrainMaGe/utils/convert_ckpt_to_pt.py b/BrainMaGe/utils/convert_ckpt_to_pt.py index ec79cf3..e876172 100755 --- a/BrainMaGe/utils/convert_ckpt_to_pt.py +++ b/BrainMaGe/utils/convert_ckpt_to_pt.py @@ -11,7 +11,6 @@ import torch if __name__ == "__main__": - parser = ArgumentParser(description="Convert the .ckpt files to .pt files") parser.add_argument( "-i", diff --git a/brain_mage_single_run b/brain_mage_single_run index 29fa488..a3b2b9c 100755 --- a/brain_mage_single_run +++ b/brain_mage_single_run @@ -99,7 +99,7 @@ if __name__ == "__main__": if os.path.isdir(base_dir): weights = os.path.join(base_dir, "resunet_ma.pt") else: - # this control path is needed if someone installs brainmage into their virtual environment directly + # this control path is needed if someone installs brainmage into their virtual environment directly base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) base_dir = os.path.join(os.path.dirname(base_dir), "BrainMaGe/weights") if os.path.isdir(base_dir): @@ -108,7 +108,7 @@ if __name__ == "__main__": if os.path.isfile(weights): print("Weight file used :", weights) else: - sys.exit('Weights file at \'' + weights + '\' was not found...') + sys.exit("Weights file at '" + weights + "' was not found...") # Running Inference test_single_run.infer_single_ma( diff --git a/brain_mage_single_run_multi_4 b/brain_mage_single_run_multi_4 index c1b119f..68f2f78 100755 --- a/brain_mage_single_run_multi_4 +++ b/brain_mage_single_run_multi_4 @@ -88,7 +88,7 @@ if __name__ == "__main__": args = parser.parse_args() - input_paths = args.input_paths.split(',') + input_paths = args.input_paths.split(",") output_path = args.output_path mask_path = args.mask_path DEVICE = args.device @@ -98,7 +98,7 @@ if __name__ == "__main__": if os.path.isdir(base_dir): weights = os.path.join(base_dir, "resunet_multi_4.pt") else: - # this control path is needed if someone installs brainmage into their virtual environment directly + # this control path is needed if someone installs brainmage into their virtual environment directly base_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) base_dir = os.path.join(os.path.dirname(base_dir), "BrainMaGe/weights") if os.path.isdir(base_dir): @@ -107,7 +107,7 @@ if __name__ == "__main__": if os.path.isfile(weights): print("Weight file used :", weights) else: - sys.exit('Weights file at \'' + weights + '\' was not found...') + sys.exit("Weights file at '" + weights + "' was not found...") # Running Inference test_single_multi_4.infer_single_multi_4( diff --git a/openvino/benchmark-ov-corescaling.py b/openvino/benchmark-ov-corescaling.py index 8b42bfb..db256dc 100644 --- a/openvino/benchmark-ov-corescaling.py +++ b/openvino/benchmark-ov-corescaling.py @@ -1,4 +1,3 @@ - import os import sys import torch @@ -12,7 +11,7 @@ postprocess_output, dice, get_mask_image, - get_input_image + get_input_image, ) from tqdm import tqdm import pandas as pd @@ -23,21 +22,25 @@ from openvino.inference_engine import IECore -def bench_pytorch_fp32(num_cores): - pytorch_model_path = brainmage_root / 'BrainMaGe/weights/resunet_ma.pt' +def bench_pytorch_fp32(num_cores): + pytorch_model_path = brainmage_root / "BrainMaGe/weights/resunet_ma.pt" ### Load PyTorch model - pt_model = fetch_model(modelname="resunet", num_channels=1, num_classes=2, num_filters=16) - checkpoint = torch.load(pytorch_model_path, map_location=torch.device('cpu')) + pt_model = fetch_model( + modelname="resunet", num_channels=1, num_classes=2, num_filters=16 + ) + checkpoint = torch.load(pytorch_model_path, map_location=torch.device("cpu")) pt_model.load_state_dict(checkpoint["model_state_dict"]) ### Run PyTorch Inference - print (f"\n Starting PyTorch inference with {pytorch_model_path} using {num_cores} cores...") + print( + f"\n Starting PyTorch inference with {pytorch_model_path} using {num_cores} cores..." + ) _ = pt_model.eval() - pt_stats =[] + pt_stats = [] torch.set_num_threads(int(num_cores)) @@ -61,38 +64,40 @@ def bench_pytorch_fp32(num_cores): pt_dice_score = dice(pt_to_save, mask_image) p_end = timer() - pt_stat = [i, sub_id, pt_dice_score, i_end-i_start, p_end-p_start] + pt_stat = [i, sub_id, pt_dice_score, i_end - i_start, p_end - p_start] pt_stats.append(pt_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done PyTorch inference with {pytorch_model_path} ...") + print(f"Done PyTorch inference with {pytorch_model_path} ...") pt_stats_df = pd.DataFrame(pt_stats) date_time_str = datetime.now().strftime("%b-%d-%Y_%H-%M-%S") csv_name = f"pt_fp32_stats_nc_{num_cores}_{date_time_str}.csv" - pt_stats_df.to_csv(csv_name, sep=',', header=False, index=False) - print (f"Saved {csv_name} ...") + pt_stats_df.to_csv(csv_name, sep=",", header=False, index=False) + print(f"Saved {csv_name} ...") - print (f"\n PyTorch Dice Mean: {pt_stats_df[:][2].mean():.5f}") - print (f"PyTorch Total Inf Time: {pt_stats_df[:][3].sum():.2f} sec, Mean: {pt_stats_df[:][3].mean():.2f} sec") + print(f"\n PyTorch Dice Mean: {pt_stats_df[:][2].mean():.5f}") + print( + f"PyTorch Total Inf Time: {pt_stats_df[:][3].sum():.2f} sec, Mean: {pt_stats_df[:][3].mean():.2f} sec" + ) return pt_stats_df def bench_ov_fp32(num_cores, device): #### Load OpenVINO model - ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp32' + ov_model_dir = brainmage_root / "BrainMaGe/weights/ov/fp32" modelname = "resunet_ma" - model_xml = f'{ov_model_dir}/{modelname}.xml' - model_bin = f'{ov_model_dir}/{modelname}.bin' + model_xml = f"{ov_model_dir}/{modelname}.xml" + model_bin = f"{ov_model_dir}/{modelname}.bin" # Load network to the plugin ie = IECore() net = ie.read_network(model=model_xml, weights=model_bin) config = {} - config['CPU_THREADS_NUM'] = str(num_cores) + config["CPU_THREADS_NUM"] = str(num_cores) exec_net = ie.load_network(network=net, device_name=device, config=config) del net @@ -100,9 +105,11 @@ def bench_ov_fp32(num_cores, device): output_layer = next(iter(exec_net.outputs)) # # #### Run OpenVINO Inference - print (f"Starting OpenVINO FP32 inference with {ov_model_dir} using {num_cores} cores...") + print( + f"Starting OpenVINO FP32 inference with {ov_model_dir} using {num_cores} cores..." + ) - ov_stats =[] + ov_stats = [] for i, row in tqdm(dataset_df.iterrows()): sub_id = row[sub_idx] @@ -123,18 +130,18 @@ def bench_ov_fp32(num_cores, device): ov_dice_score = dice(ov_to_save, mask_image) p_end = timer() - ov_stat = [i, sub_id, ov_dice_score, i_end-i_start, p_end-p_start] + ov_stat = [i, sub_id, ov_dice_score, i_end - i_start, p_end - p_start] ov_stats.append(ov_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done OpenVINO inference with {ov_model_dir} ...") + print(f"Done OpenVINO inference with {ov_model_dir} ...") ov_stats_df = pd.DataFrame(ov_stats) date_time_str = datetime.now().strftime("%b-%d-%Y_%H-%M-%S") csv_name = f"ov_fp32_stats_nc_{num_cores}_{date_time_str}.csv" - ov_stats_df.to_csv(csv_name, sep=',', header=False, index=False) - print (f"Saved {csv_name} ...") + ov_stats_df.to_csv(csv_name, sep=",", header=False, index=False) + print(f"Saved {csv_name} ...") print_stats("FP32", ov_stats_df) @@ -142,19 +149,18 @@ def bench_ov_fp32(num_cores, device): def bench_ov_int8(num_cores, device): - # #### Load INT8 OpenVINO model - ov_model_dir = brainmage_root / 'openvino/int8_openvino_model' + ov_model_dir = brainmage_root / "openvino/int8_openvino_model" modelname = "resunet_ma_int8" - model_xml = f'{ov_model_dir}/{modelname}.xml' - model_bin = f'{ov_model_dir}/{modelname}.bin' + model_xml = f"{ov_model_dir}/{modelname}.xml" + model_bin = f"{ov_model_dir}/{modelname}.bin" # Load network to the plugin ie = IECore() net = ie.read_network(model=model_xml, weights=model_bin) config = {} - config['CPU_THREADS_NUM'] = str(num_cores) + config["CPU_THREADS_NUM"] = str(num_cores) exec_net = ie.load_network(network=net, device_name=device, config=config) del net @@ -162,9 +168,9 @@ def bench_ov_int8(num_cores, device): output_layer = next(iter(exec_net.outputs)) # #### Run OpenVINO Inference - print (f"Starting OpenVINO inference with {ov_model_dir} using {num_cores} cores...") + print(f"Starting OpenVINO inference with {ov_model_dir} using {num_cores} cores...") - ov_int8_stats =[] + ov_int8_stats = [] for i, row in tqdm(dataset_df.iterrows()): sub_id = row[sub_idx] @@ -185,18 +191,18 @@ def bench_ov_int8(num_cores, device): ov_dice_score = dice(ov_to_save, mask_image) p_end = timer() - ov_int8_stat = [i, sub_id, ov_dice_score, i_end-i_start, p_end-p_start] + ov_int8_stat = [i, sub_id, ov_dice_score, i_end - i_start, p_end - p_start] ov_int8_stats.append(ov_int8_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done OpenVINO inference with {ov_model_dir} ...") + print(f"Done OpenVINO inference with {ov_model_dir} ...") ov_int8_stats_df = pd.DataFrame(ov_int8_stats) date_time_str = datetime.now().strftime("%b-%d-%Y_%H-%M-%S") csv_name = f"ov_int8_stats_nc_{num_cores}_{date_time_str}.csv" - ov_int8_stats_df.to_csv(csv_name, sep=',', header=False, index=False) - print (f"Saved {csv_name} ...") + ov_int8_stats_df.to_csv(csv_name, sep=",", header=False, index=False) + print(f"Saved {csv_name} ...") print_stats("INT8", ov_int8_stats_df) @@ -205,37 +211,61 @@ def bench_ov_int8(num_cores, device): ## Print Summary def print_stats(data_type, stats_df): - print (f"OpenVINO {data_type} Dice Mean +/- STD: {stats_df[:][2].mean():.5f} +/- {stats_df[:][2].std():.1f}") - print (f"OpenVINO {data_type}: Total Inf Time (sec) : {stats_df[:][3].sum():.2f} ") - print (f"OpenVINO {data_type}: Mean Inf Time (sec) +/- STD: {stats_df[:][3].mean():.2f} +/- {stats_df[:][3].std():.2f} ") + print( + f"OpenVINO {data_type} Dice Mean +/- STD: {stats_df[:][2].mean():.5f} +/- {stats_df[:][2].std():.1f}" + ) + print(f"OpenVINO {data_type}: Total Inf Time (sec) : {stats_df[:][3].sum():.2f} ") + print( + f"OpenVINO {data_type}: Mean Inf Time (sec) +/- STD: {stats_df[:][3].mean():.2f} +/- {stats_df[:][3].std():.2f} " + ) print("\n") -if __name__ == '__main__': +if __name__ == "__main__": parser = argparse.ArgumentParser() - parser.add_argument('--dataset_csv', dest='dataset_csv', default='nfbs-dataset-test', help='Path to dataset used for testing') - parser.add_argument('--device', dest='device', default='cpu', help='Device to be used for inference') - parser.add_argument('--nc', dest='num_cores', default=1, help='Number of cores used for inference') - parser.add_argument('--data_type', dest='data_type', default='FP32', help='Inference datatype') - parser.add_argument('--sub_idx', dest='sub_idx', default=0, help='0th col is sub-id') - parser.add_argument('--input_path_idx', dest='input_path_idx', default=1, help='1st col is input path') - parser.add_argument('--mask_path_idx', dest='mask_path_idx', default=2, help='2nd col is mask_path') + parser.add_argument( + "--dataset_csv", + dest="dataset_csv", + default="nfbs-dataset-test", + help="Path to dataset used for testing", + ) + parser.add_argument( + "--device", dest="device", default="cpu", help="Device to be used for inference" + ) + parser.add_argument( + "--nc", dest="num_cores", default=1, help="Number of cores used for inference" + ) + parser.add_argument( + "--data_type", dest="data_type", default="FP32", help="Inference datatype" + ) + parser.add_argument( + "--sub_idx", dest="sub_idx", default=0, help="0th col is sub-id" + ) + parser.add_argument( + "--input_path_idx", + dest="input_path_idx", + default=1, + help="1st col is input path", + ) + parser.add_argument( + "--mask_path_idx", dest="mask_path_idx", default=2, help="2nd col is mask_path" + ) args = parser.parse_args() - brainmage_root = Path('../') + brainmage_root = Path("../") dataset_csv = args.dataset_csv sub_idx = args.sub_idx input_path_idx = args.input_path_idx mask_path_idx = args.mask_path_idx - pt_output_path = 'pt-outfile' # PyTorch output file - ov_output_path = 'ov-outfile' # ONNX output file + pt_output_path = "pt-outfile" # PyTorch output file + ov_output_path = "ov-outfile" # ONNX output file - device=args.device + device = args.device # ### Load Dataset csv - dataset_df = pd.read_csv(dataset_csv, header = None) + dataset_df = pd.read_csv(dataset_csv, header=None) print(f"Number of rows: {dataset_df.shape[0]}") print(f"Input Image Sample: {dataset_df.iloc[0][input_path_idx]}") print(f"Mask Image Sample: {dataset_df.iloc[0][mask_path_idx]}") @@ -243,12 +273,13 @@ def print_stats(data_type, stats_df): ## ## Run Benchmark ## - print (f"Starting inference with datatype: {args.data_type} Num Cores: {args.num_cores} ...") + print( + f"Starting inference with datatype: {args.data_type} Num Cores: {args.num_cores} ..." + ) - if 'FP32' == args.data_type: + if "FP32" == args.data_type: ov_stats_df = bench_ov_fp32(args.num_cores, args.device) - elif 'INT8' == args.data_type: + elif "INT8" == args.data_type: ov_int8_stats_df = bench_ov_int8(args.num_cores, args.device) - elif 'PTFP32' == args.data_type: + elif "PTFP32" == args.data_type: pt_stats_df = bench_pytorch_fp32(args.num_cores) - diff --git a/openvino/benchmark-pt-ov-memprofile.py b/openvino/benchmark-pt-ov-memprofile.py index c6ac057..08cebef 100644 --- a/openvino/benchmark-pt-ov-memprofile.py +++ b/openvino/benchmark-pt-ov-memprofile.py @@ -1,4 +1,3 @@ - import os import sys import torch @@ -12,7 +11,7 @@ postprocess_output, dice, get_mask_image, - get_input_image + get_input_image, ) from tqdm import tqdm import pandas as pd @@ -21,40 +20,42 @@ from datetime import timedelta -brainmage_root = Path('../') +brainmage_root = Path("../") -nfbs_dataset_csv = 'nfbs-dataset.csv' +nfbs_dataset_csv = "nfbs-dataset.csv" -pt_output_path = 'pt-outfile' # PyTorch output file -ov_output_path = 'ov-outfile' # ONNX output file +pt_output_path = "pt-outfile" # PyTorch output file +ov_output_path = "ov-outfile" # ONNX output file -pytorch_model_path = brainmage_root / 'BrainMaGe/weights/resunet_ma.pt' -ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp32/' +pytorch_model_path = brainmage_root / "BrainMaGe/weights/resunet_ma.pt" +ov_model_dir = brainmage_root / "BrainMaGe/weights/ov/fp32/" -device="cpu" +device = "cpu" # ### Load Dataset csv -nfbs_dataset_df = pd.read_csv(nfbs_dataset_csv, header = None) +nfbs_dataset_df = pd.read_csv(nfbs_dataset_csv, header=None) print("Number of rows:", nfbs_dataset_df.shape[0]) + @profile def bench_pytorch_fp32(): - ### Load PyTorch model - pt_model = fetch_model(modelname="resunet", num_channels=1, num_classes=2, num_filters=16) - checkpoint = torch.load(pytorch_model_path, map_location=torch.device('cpu')) + pt_model = fetch_model( + modelname="resunet", num_channels=1, num_classes=2, num_filters=16 + ) + checkpoint = torch.load(pytorch_model_path, map_location=torch.device("cpu")) pt_model.load_state_dict(checkpoint["model_state_dict"]) ### Run PyTorch Inference - print (f"Starting PyTorch inference with {pytorch_model_path} ...") + print(f"Starting PyTorch inference with {pytorch_model_path} ...") _ = pt_model.eval() - pt_stats =[] + pt_stats = [] with torch.no_grad(): for i, row in tqdm(nfbs_dataset_df.iterrows()): @@ -76,30 +77,31 @@ def bench_pytorch_fp32(): pt_dice_score = dice(pt_to_save, mask_image) p_end = timer() - pt_stat = [i, sub_id, pt_dice_score, i_end-i_start, p_end-p_start] + pt_stat = [i, sub_id, pt_dice_score, i_end - i_start, p_end - p_start] pt_stats.append(pt_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done PyTorch inference with {pytorch_model_path} ...") + print(f"Done PyTorch inference with {pytorch_model_path} ...") pt_stats_df = pd.DataFrame(pt_stats) - pt_stats_df.to_csv('pt_stats.csv', sep=',', header=False, index=False) - print (f"Saved pt_stats.csv ...") + pt_stats_df.to_csv("pt_stats.csv", sep=",", header=False, index=False) + print(f"Saved pt_stats.csv ...") return pt_stats_df + @profile def bench_ov_fp32(): #### Load OpenVINO model - ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp32' + ov_model_dir = brainmage_root / "BrainMaGe/weights/ov/fp32" modelname = "resunet_ma" from openvino.inference_engine import IECore - model_xml = f'{ov_model_dir}/{modelname}.xml' - model_bin = f'{ov_model_dir}/{modelname}.bin' + model_xml = f"{ov_model_dir}/{modelname}.xml" + model_bin = f"{ov_model_dir}/{modelname}.bin" # Load network to the plugin ie = IECore() @@ -110,12 +112,11 @@ def bench_ov_fp32(): input_layer = next(iter(exec_net.input_info)) output_layer = next(iter(exec_net.outputs)) - # # #### Run OpenVINO Inference - print (f"Starting OpenVINO FP32 inference with {ov_model_dir} ...") + print(f"Starting OpenVINO FP32 inference with {ov_model_dir} ...") - ov_stats =[] + ov_stats = [] for i, row in tqdm(nfbs_dataset_df.iterrows()): sub_id = row[0] @@ -136,32 +137,31 @@ def bench_ov_fp32(): ov_dice_score = dice(ov_to_save, mask_image) p_end = timer() - ov_stat = [i, sub_id, ov_dice_score, i_end-i_start, p_end-p_start] + ov_stat = [i, sub_id, ov_dice_score, i_end - i_start, p_end - p_start] ov_stats.append(ov_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done OpenVINO inference with {ov_model_dir} ...") + print(f"Done OpenVINO inference with {ov_model_dir} ...") ov_stats_df = pd.DataFrame(ov_stats) - ov_stats_df.to_csv('ov_fp32_stats.csv', sep=',', header=False, index=False) - print (f"Saved ov_fp32_stats.csv ...") + ov_stats_df.to_csv("ov_fp32_stats.csv", sep=",", header=False, index=False) + print(f"Saved ov_fp32_stats.csv ...") return ov_stats_df + @profile def bench_ov_int8(): - # #### Load INT8 OpenVINO model - ov_model_dir = brainmage_root / 'openvino/int8_openvino_model' + ov_model_dir = brainmage_root / "openvino/int8_openvino_model" modelname = "resunet_ma_int8" - from openvino.inference_engine import IECore - model_xml = f'{ov_model_dir}/{modelname}.xml' - model_bin = f'{ov_model_dir}/{modelname}.bin' + model_xml = f"{ov_model_dir}/{modelname}.xml" + model_bin = f"{ov_model_dir}/{modelname}.bin" # Load network to the plugin ie = IECore() @@ -172,12 +172,11 @@ def bench_ov_int8(): input_layer = next(iter(exec_net.input_info)) output_layer = next(iter(exec_net.outputs)) - # #### Run OpenVINO Inference - print (f"Starting OpenVINO inference with {ov_model_dir} ...") + print(f"Starting OpenVINO inference with {ov_model_dir} ...") - ov_int8_stats =[] + ov_int8_stats = [] for i, row in tqdm(nfbs_dataset_df.iterrows()): sub_id = row[0] @@ -198,19 +197,20 @@ def bench_ov_int8(): ov_dice_score = dice(ov_to_save, mask_image) p_end = timer() - ov_int8_stat = [i, sub_id, ov_dice_score, i_end-i_start, p_end-p_start] + ov_int8_stat = [i, sub_id, ov_dice_score, i_end - i_start, p_end - p_start] ov_int8_stats.append(ov_int8_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done OpenVINO inference with {ov_model_dir} ...") + print(f"Done OpenVINO inference with {ov_model_dir} ...") ov_int8_stats_df = pd.DataFrame(ov_int8_stats) - ov_int8_stats_df.to_csv('ov_int8_stats.csv', sep=',', header=False, index=False) - print (f"Saved ov_int8_stats.csv ...") + ov_int8_stats_df.to_csv("ov_int8_stats.csv", sep=",", header=False, index=False) + print(f"Saved ov_int8_stats.csv ...") return ov_int8_stats_df + ## ## Run Benchmark ## Uncomment to select which model you want to do memory-profiling @@ -219,9 +219,3 @@ def bench_ov_int8(): # pt_stats_df = bench_pytorch_fp32() # ov_stats_df = bench_ov_fp32() ov_int8_stats_df = bench_ov_int8() - - - - - - diff --git a/openvino/benchmark-pt-ov.py b/openvino/benchmark-pt-ov.py index 9e89e20..7d78a57 100644 --- a/openvino/benchmark-pt-ov.py +++ b/openvino/benchmark-pt-ov.py @@ -1,4 +1,3 @@ - import os import sys import torch @@ -12,7 +11,7 @@ postprocess_output, dice, get_mask_image, - get_input_image + get_input_image, ) from tqdm import tqdm import pandas as pd @@ -23,25 +22,25 @@ from medpy.metric.binary import hd95 from openvino.inference_engine import IECore -brainmage_root = Path('../') -dataset_csv = 'nfbs-dataset-test.csv' +brainmage_root = Path("../") +dataset_csv = "nfbs-dataset-test.csv" # For NFBS dataset - 0th col is sub-id, 1st col is input path, 2nd col is mask_path sub_idx = 0 input_path_idx = 1 mask_path_idx = 2 -pt_output_path = 'pt-outfile' # PyTorch output file -ov_output_path = 'ov-outfile' # ONNX output file +pt_output_path = "pt-outfile" # PyTorch output file +ov_output_path = "ov-outfile" # ONNX output file -pytorch_model_path = brainmage_root / 'BrainMaGe/weights/resunet_ma.pt' -ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp32/' +pytorch_model_path = brainmage_root / "BrainMaGe/weights/resunet_ma.pt" +ov_model_dir = brainmage_root / "BrainMaGe/weights/ov/fp32/" -device="cpu" +device = "cpu" # ### Load Dataset csv -dataset_df = pd.read_csv(dataset_csv, header = None) +dataset_df = pd.read_csv(dataset_csv, header=None) print(f"Number of rows: {dataset_df.shape[0]}") print(f"Input Image Sample: {dataset_df.iloc[0][input_path_idx]}") print(f"Mask Image Sample: {dataset_df.iloc[0][mask_path_idx]}") @@ -49,16 +48,18 @@ def bench_pytorch_fp32(): ### Load PyTorch model - pt_model = fetch_model(modelname="resunet", num_channels=1, num_classes=2, num_filters=16) - checkpoint = torch.load(pytorch_model_path, map_location=torch.device('cpu')) + pt_model = fetch_model( + modelname="resunet", num_channels=1, num_classes=2, num_filters=16 + ) + checkpoint = torch.load(pytorch_model_path, map_location=torch.device("cpu")) pt_model.load_state_dict(checkpoint["model_state_dict"]) ### Run PyTorch Inference - print (f"\n Starting PyTorch inference with {pytorch_model_path} ...") + print(f"\n Starting PyTorch inference with {pytorch_model_path} ...") _ = pt_model.eval() - pt_stats =[] + pt_stats = [] with torch.no_grad(): for i, row in tqdm(dataset_df.iterrows()): @@ -82,32 +83,34 @@ def bench_pytorch_fp32(): pt_dice_score = dice(pt_to_save, mask_image) pt_hdorff_score = hd95(pt_to_save, mask_image) - pt_stat = [i, sub_id, pt_hdorff_score, i_end-i_start, p_end-p_start] + pt_stat = [i, sub_id, pt_hdorff_score, i_end - i_start, p_end - p_start] pt_stats.append(pt_stat) except Exception as e: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") print(e) - print (f"Done PyTorch inference with {pytorch_model_path} ...") + print(f"Done PyTorch inference with {pytorch_model_path} ...") pt_stats_df = pd.DataFrame(pt_stats) date_time_str = datetime.now().strftime("%b-%d-%Y_%H-%M-%S") csv_name = f"pt_stats_{date_time_str}.csv" - pt_stats_df.to_csv(csv_name, sep=',', header=False, index=False) - print (f"Saved {csv_name} ...") + pt_stats_df.to_csv(csv_name, sep=",", header=False, index=False) + print(f"Saved {csv_name} ...") - print (f"\n PyTorch Hausdorff Distance Mean: {pt_stats_df[:][2].mean():.5f}") - print (f"PyTorch Total Inf Time: {pt_stats_df[:][3].sum():.2f} sec, Mean: {pt_stats_df[:][3].mean():.2f} sec") + print(f"\n PyTorch Hausdorff Distance Mean: {pt_stats_df[:][2].mean():.5f}") + print( + f"PyTorch Total Inf Time: {pt_stats_df[:][3].sum():.2f} sec, Mean: {pt_stats_df[:][3].mean():.2f} sec" + ) return pt_stats_df def bench_ov_fp32(): #### Load OpenVINO model - ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp32' + ov_model_dir = brainmage_root / "BrainMaGe/weights/ov/fp32" modelname = "resunet_ma" - model_xml = f'{ov_model_dir}/{modelname}.xml' + model_xml = f"{ov_model_dir}/{modelname}.xml" # Load network to the plugin ie = IECore() @@ -119,9 +122,9 @@ def bench_ov_fp32(): output_layer = next(iter(exec_net.outputs)) # # #### Run OpenVINO Inference - print (f"Starting OpenVINO FP32 inference with {ov_model_dir} ...") + print(f"Starting OpenVINO FP32 inference with {ov_model_dir} ...") - ov_stats =[] + ov_stats = [] for i, row in tqdm(dataset_df.iterrows()): sub_id = row[sub_idx] @@ -144,32 +147,33 @@ def bench_ov_fp32(): ov_dice_score = dice(ov_to_save, mask_image) ov_hdorff_score = hd95(ov_to_save, mask_image) - ov_stat = [i, sub_id, ov_hdorff_score, i_end-i_start, p_end-p_start] + ov_stat = [i, sub_id, ov_hdorff_score, i_end - i_start, p_end - p_start] ov_stats.append(ov_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done OpenVINO inference with {ov_model_dir} ...") + print(f"Done OpenVINO inference with {ov_model_dir} ...") ov_stats_df = pd.DataFrame(ov_stats) date_time_str = datetime.now().strftime("%b-%d-%Y_%H-%M-%S") csv_name = f"ov_fp32_stats_{date_time_str}.csv" - ov_stats_df.to_csv(csv_name, sep=',', header=False, index=False) - print (f"Saved {csv_name} ...") + ov_stats_df.to_csv(csv_name, sep=",", header=False, index=False) + print(f"Saved {csv_name} ...") - print (f"\n OpenVINO FP32 Hausdorff Distance Mean: {ov_stats_df[:][2].mean():.5f}") - print (f"OpenVINO FP32 Total Inf Time: {ov_stats_df[:][3].sum():.2f} sec, Mean: {ov_stats_df[:][3].mean():.2f}") + print(f"\n OpenVINO FP32 Hausdorff Distance Mean: {ov_stats_df[:][2].mean():.5f}") + print( + f"OpenVINO FP32 Total Inf Time: {ov_stats_df[:][3].sum():.2f} sec, Mean: {ov_stats_df[:][3].mean():.2f}" + ) return ov_stats_df def bench_ov_int8(): - # #### Load INT8 OpenVINO model - ov_model_dir = brainmage_root / 'openvino/int8_openvino_model' + ov_model_dir = brainmage_root / "openvino/int8_openvino_model" modelname = "resunet_ma_int8" - model_xml = f'{ov_model_dir}/{modelname}.xml' + model_xml = f"{ov_model_dir}/{modelname}.xml" # Load network to the plugin ie = IECore() @@ -181,9 +185,9 @@ def bench_ov_int8(): output_layer = next(iter(exec_net.outputs)) # #### Run OpenVINO Inference - print (f"Starting OpenVINO inference with {ov_model_dir} ...") + print(f"Starting OpenVINO inference with {ov_model_dir} ...") - ov_int8_stats =[] + ov_int8_stats = [] for i, row in tqdm(dataset_df.iterrows()): sub_id = row[sub_idx] @@ -206,24 +210,29 @@ def bench_ov_int8(): ov_dice_score = dice(ov_to_save, mask_image) ov_hdorff_score = hd95(ov_to_save, mask_image) - ov_stat = [i, sub_id, ov_hdorff_score, i_end-i_start, p_end-p_start] + ov_stat = [i, sub_id, ov_hdorff_score, i_end - i_start, p_end - p_start] ov_int8_stats.append(ov_stat) except: - print (f" Inference Failed: {sub_id} ") + print(f" Inference Failed: {sub_id} ") - print (f"Done OpenVINO inference with {ov_model_dir} ...") + print(f"Done OpenVINO inference with {ov_model_dir} ...") ov_int8_stats_df = pd.DataFrame(ov_int8_stats) date_time_str = datetime.now().strftime("%b-%d-%Y_%H-%M-%S") csv_name = f"ov_int8_stats_{date_time_str}.csv" - ov_int8_stats_df.to_csv(csv_name, sep=',', header=False, index=False) - print (f"Saved {csv_name} ...") + ov_int8_stats_df.to_csv(csv_name, sep=",", header=False, index=False) + print(f"Saved {csv_name} ...") - print (f"\n OpenVINO INT8 Hausdorff Distance Mean: {ov_int8_stats_df[:][2].mean():.5f}") - print (f"OpenVINO INT8 Total Inf Time: {ov_int8_stats_df[:][3].sum():.2f} sec, Mean: {ov_int8_stats_df[:][3].mean():.2f} sec") + print( + f"\n OpenVINO INT8 Hausdorff Distance Mean: {ov_int8_stats_df[:][2].mean():.5f}" + ) + print( + f"OpenVINO INT8 Total Inf Time: {ov_int8_stats_df[:][3].sum():.2f} sec, Mean: {ov_int8_stats_df[:][3].mean():.2f} sec" + ) return ov_int8_stats_df + ## ## Run Benchmark ## @@ -237,31 +246,51 @@ def bench_ov_int8(): ## print(f"\n Mean Hausdorff Distance Scores for {dataset_df.shape[0]} images") -print (f"PyTorch Hausdorff Distance Mean +/- STD: {pt_stats_df[:][2].mean():.5f} +/- {pt_stats_df[:][2].std():.1f}") -print (f"OpenVINO FP32 Hausdorff Distance Mean +/- STD: {ov_stats_df[:][2].mean():.5f} +/- {ov_stats_df[:][2].std():.1f}") -print (f"OpenVINO INT8 Hausdorff Distance Mean +/- STD: {ov_int8_stats_df[:][2].mean():.5f} +/- {ov_int8_stats_df[:][2].std():.1f}") +print( + f"PyTorch Hausdorff Distance Mean +/- STD: {pt_stats_df[:][2].mean():.5f} +/- {pt_stats_df[:][2].std():.1f}" +) +print( + f"OpenVINO FP32 Hausdorff Distance Mean +/- STD: {ov_stats_df[:][2].mean():.5f} +/- {ov_stats_df[:][2].std():.1f}" +) +print( + f"OpenVINO INT8 Hausdorff Distance Mean +/- STD: {ov_int8_stats_df[:][2].mean():.5f} +/- {ov_int8_stats_df[:][2].std():.1f}" +) dice_diff_pt_ov = pt_stats_df[:][2] - ov_stats_df[:][2] dice_diff_pt_ov_int8 = pt_stats_df[:][2] - ov_int8_stats_df[:][2] print() -print(f"\n Accuracy Hausdorff Distance difference with OV FP32 {dice_diff_pt_ov.sum():.6f}") -#print(dice_diff_pt_ov.value_counts()) -print(f" Accuracy Hausdorff Distance difference with OV INT8 {dice_diff_pt_ov_int8.sum():.6f}") -#print(dice_diff_pt_ov_int8.value_counts()) +print( + f"\n Accuracy Hausdorff Distance difference with OV FP32 {dice_diff_pt_ov.sum():.6f}" +) +# print(dice_diff_pt_ov.value_counts()) +print( + f" Accuracy Hausdorff Distance difference with OV INT8 {dice_diff_pt_ov_int8.sum():.6f}" +) +# print(dice_diff_pt_ov_int8.value_counts()) pt_total_inf_time = pt_stats_df[:][3].sum() ov_total_inf_time = ov_stats_df[:][3].sum() ov_int8_total_inf_time = ov_int8_stats_df[:][3].sum() print() print(f"\n Total Inference Time (sec) for {dataset_df.shape[0]} images") -print (f"PyTorch: {pt_total_inf_time:.2f} , Mean +/- STD: {pt_stats_df[:][3].mean():.2f} +/- {pt_stats_df[:][3].std():.2f} ") -print (f"OpenVINO FP32: {ov_total_inf_time:.2f} , Mean +/- STD: {ov_stats_df[:][3].mean():.2f} +/- {ov_stats_df[:][3].std():.2f} ") -print (f"OpenVINO INT8: {ov_int8_total_inf_time:.2f} , Mean +/- STD: {ov_int8_stats_df[:][3].mean():.2f} +/- {ov_int8_stats_df[:][3].std():.2f} ") +print( + f"PyTorch: {pt_total_inf_time:.2f} , Mean +/- STD: {pt_stats_df[:][3].mean():.2f} +/- {pt_stats_df[:][3].std():.2f} " +) +print( + f"OpenVINO FP32: {ov_total_inf_time:.2f} , Mean +/- STD: {ov_stats_df[:][3].mean():.2f} +/- {ov_stats_df[:][3].std():.2f} " +) +print( + f"OpenVINO INT8: {ov_int8_total_inf_time:.2f} , Mean +/- STD: {ov_int8_stats_df[:][3].mean():.2f} +/- {ov_int8_stats_df[:][3].std():.2f} " +) -speedup_fp32 = pt_total_inf_time/ov_total_inf_time -speedup_int8 = pt_total_inf_time/ov_int8_total_inf_time -speedup_fp32_int8 = ov_total_inf_time/ov_int8_total_inf_time +speedup_fp32 = pt_total_inf_time / ov_total_inf_time +speedup_int8 = pt_total_inf_time / ov_int8_total_inf_time +speedup_fp32_int8 = ov_total_inf_time / ov_int8_total_inf_time print() -print (f"\n Speedup with OpenVINO FP32 for {pt_stats_df.shape[0]} images: {speedup_fp32:.1f}x") -print (f"Speedup with OpenVINO INT8 for {pt_stats_df.shape[0]} images: {speedup_int8:.1f}x") -print (f"Speedup with OpenVINO INT8 over FP32: {speedup_fp32_int8:.1f}x") +print( + f"\n Speedup with OpenVINO FP32 for {pt_stats_df.shape[0]} images: {speedup_fp32:.1f}x" +) +print( + f"Speedup with OpenVINO INT8 for {pt_stats_df.shape[0]} images: {speedup_int8:.1f}x" +) +print(f"Speedup with OpenVINO INT8 over FP32: {speedup_fp32_int8:.1f}x") diff --git a/openvino/compare_utils.py b/openvino/compare_utils.py index eb345d1..7aadbb7 100644 --- a/openvino/compare_utils.py +++ b/openvino/compare_utils.py @@ -29,30 +29,32 @@ def postprocess_prediction(seg): seg[lbls != largest_region] = 0 return seg + def postprocess_output(output, patient_nib_shape): to_save = interpolate_image(output, patient_nib_shape) to_save[to_save >= 0.9] = 1 to_save[to_save < 0.9] = 0 to_save = postprocess_prediction(to_save) - + return to_save + def postprocess_save_output(output, patient_nib, output_path, save_nib_to_disk=False): to_save = interpolate_image(output, patient_nib.shape) to_save[to_save >= 0.9] = 1 to_save[to_save < 0.9] = 0 to_save = postprocess_prediction(to_save) - - + to_save_nib = None if save_nib_to_disk: to_save_nib = nib.Nifti1Image(to_save, patient_nib.affine) nib.save(to_save_nib, os.path.join(output_path)) print(f"to_save Image size: {to_save.shape} , dtype: {to_save.dtype} ") print("Output saved at: ", output_path) - + return to_save, to_save_nib + def dice(inp, target): smooth = 1e-7 iflat = inp.flatten() @@ -61,6 +63,7 @@ def dice(inp, target): dice_score = (2 * intersection + smooth) / (iflat.sum() + tflat.sum() + smooth) return dice_score + def get_mask_image(mask_path): mask = nib.load(mask_path) mask_data = mask.get_fdata().astype(np.float32) @@ -68,6 +71,7 @@ def get_mask_image(mask_path): mask_data[mask_data > 0] = 1 return mask_data + def get_input_image(input_path): patient_nib = nib.load(input_path) image_data = patient_nib.get_fdata() @@ -78,4 +82,4 @@ def get_input_image(input_path): ) image = image[np.newaxis, np.newaxis, ...] image = torch.FloatTensor(image) - return image, patient_nib \ No newline at end of file + return image, patient_nib diff --git a/openvino/export-to-ov.py b/openvino/export-to-ov.py index 4eb5bd7..147b104 100644 --- a/openvino/export-to-ov.py +++ b/openvino/export-to-ov.py @@ -15,16 +15,18 @@ import mo_pytorch from openvino.inference_engine import IECore -brainmage_root = Path('../') -pytorch_model_path = brainmage_root / 'BrainMaGe/weights/resunet_ma.pt' -#ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp16' -ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp32' +brainmage_root = Path("../") +pytorch_model_path = brainmage_root / "BrainMaGe/weights/resunet_ma.pt" +# ov_model_dir = brainmage_root / 'BrainMaGe/weights/ov/fp16' +ov_model_dir = brainmage_root / "BrainMaGe/weights/ov/fp32" if not os.path.exists(ov_model_dir): os.makedirs(ov_model_dir) -pt_model = fetch_model(modelname="resunet", num_channels=1, num_classes=2, num_filters=16) -checkpoint = torch.load(pytorch_model_path, map_location=torch.device('cpu')) +pt_model = fetch_model( + modelname="resunet", num_channels=1, num_classes=2, num_filters=16 +) +checkpoint = torch.load(pytorch_model_path, map_location=torch.device("cpu")) pt_model.load_state_dict(checkpoint["model_state_dict"]) pt_model.eval() @@ -33,7 +35,9 @@ # Create dummy data np.random.seed(123) -input_image = torch.Tensor(np.random.standard_normal([1, 1, 128, 128, 128]).astype(np.float32)) +input_image = torch.Tensor( + np.random.standard_normal([1, 1, 128, 128, 128]).astype(np.float32) +) print("Running PyTorch Inference on random data...") # Run Pytorch Inference @@ -44,27 +48,28 @@ print("Converting PyTorch model to OpenVINO IR...") -ov_model_name = 'resunet_ma' +ov_model_name = "resunet_ma" model_name = ov_model_dir / ov_model_name -mo_pytorch.convert(pt_model, input_shape=[1, 1, 128, 128, 128], data_type="FP32", model_name=model_name) +mo_pytorch.convert( + pt_model, input_shape=[1, 1, 128, 128, 128], data_type="FP32", model_name=model_name +) -print (f"\nOpenVINO model saved at {ov_model_dir} \n") +print(f"\nOpenVINO model saved at {ov_model_dir} \n") # Run OpenVINO Inference and Compare Inference results -model_xml = f'{ov_model_dir}/{ov_model_name}.xml' +model_xml = f"{ov_model_dir}/{ov_model_name}.xml" ie = IECore() net = ie.read_network(model_xml) -exec_net = ie.load_network(net, 'CPU') +exec_net = ie.load_network(net, "CPU") inp_name = next(iter(net.inputs.keys())) out = exec_net.infer({inp_name: input_image}) out = next(iter(out.values())) # Print Results. -print('PyTorch output shape:', pt_out.shape) -print('OpenVINO output shape:', out.shape) -print('PyTorch output min and max:', np.min(pt_out), np.max(pt_out)) -print('OpenVINO output min and max:', np.min(out), np.max(out)) -print('PyTorch and OpenVINO Output difference:', np.max(np.abs(out - pt_out))) - +print("PyTorch output shape:", pt_out.shape) +print("OpenVINO output shape:", out.shape) +print("PyTorch output min and max:", np.min(pt_out), np.max(pt_out)) +print("OpenVINO output min and max:", np.min(out), np.max(out)) +print("PyTorch and OpenVINO Output difference:", np.max(np.abs(out - pt_out))) diff --git a/openvino/generate-csv-nfbs-dataset.py b/openvino/generate-csv-nfbs-dataset.py index 2e8cf75..2a56c2e 100644 --- a/openvino/generate-csv-nfbs-dataset.py +++ b/openvino/generate-csv-nfbs-dataset.py @@ -13,40 +13,57 @@ def generate_csv(data_dir): sub_dirs = sorted(os.listdir(data_dir)) rows = [] # There are inference errors with the following subjects, so remove them from the manifest csv. - skip_sub_dirs = ["A00040944", "A00043704", "A00053850", "A00054914", "A00058218", "A00058552", "A00060430", "A00062942"] + skip_sub_dirs = [ + "A00040944", + "A00043704", + "A00053850", + "A00054914", + "A00058218", + "A00058552", + "A00060430", + "A00062942", + ] sub_dirs = list(set(sub_dirs) - set(skip_sub_dirs)) for sub_dir in sub_dirs: - sub_dir_path = data_dir + '/' + sub_dir + sub_dir_path = data_dir + "/" + sub_dir f_names = sorted(os.listdir(sub_dir_path)) - row = [ sub_dir_path + '/' + f for f in f_names ] + row = [sub_dir_path + "/" + f for f in f_names] row.insert(0, sub_dir) rows.append(row) # Sample Row: sub_id, input_img_T1w, input_img_T1w_brain, mask_img # A00037112,/home/ubuntu/NFBS_Dataset/A00037112/sub-A00037112_ses-NFB3_T1w.nii.gz,/home/ubuntu/NFBS_Dataset/A00037112/sub-A00037112_ses-NFB3_T1w_brain.nii.gz,/home/sdp/ravi/upenn/data/NFBS_Dataset/A00037112/sub-A00037112_ses-NFB3_T1w_brainmask.nii.gz - nfbs_ds_csv = 'nfbs-dataset.csv' - nfbs_ds_train_csv = 'nfbs-dataset-train.csv' - nfbs_ds_test_csv = 'nfbs-dataset-test.csv' + nfbs_ds_csv = "nfbs-dataset.csv" + nfbs_ds_train_csv = "nfbs-dataset-train.csv" + nfbs_ds_test_csv = "nfbs-dataset-test.csv" nfbs_dataset_df = pd.DataFrame(rows) - nfbs_dataset_df.to_csv(nfbs_ds_csv, sep=',', header=False, index=False) + nfbs_dataset_df.to_csv(nfbs_ds_csv, sep=",", header=False, index=False) print(f"Number of rows written: {nfbs_dataset_df.shape[0]} in {nfbs_ds_csv}") - #nfbs_dataset_df = pd.read_csv(nfbs_dataset_csv, header = None) - train = nfbs_dataset_df.sample(frac=0.8,random_state=200) #random state is a seed value + # nfbs_dataset_df = pd.read_csv(nfbs_dataset_csv, header = None) + train = nfbs_dataset_df.sample( + frac=0.8, random_state=200 + ) # random state is a seed value test = nfbs_dataset_df.drop(train.index) - train.to_csv(nfbs_ds_train_csv, sep=',', header=False, index=False) - test.to_csv(nfbs_ds_test_csv, sep=',', header=False, index=False) + train.to_csv(nfbs_ds_train_csv, sep=",", header=False, index=False) + test.to_csv(nfbs_ds_test_csv, sep=",", header=False, index=False) print(f"Number of rows written: {train.shape[0]} in {nfbs_ds_train_csv}") print(f"Number of rows written: {test.shape[0]} in {nfbs_ds_test_csv}") -if __name__ == '__main__': + +if __name__ == "__main__": parser = argparse.ArgumentParser() - parser.add_argument('--data-dir', dest='data_dir', default=f"{os.getcwd()}/NFBS_Dataset/", help='Path to the Dataset root directory') + parser.add_argument( + "--data-dir", + dest="data_dir", + default=f"{os.getcwd()}/NFBS_Dataset/", + help="Path to the Dataset root directory", + ) args = parser.parse_args() data_dir = args.data_dir @@ -58,6 +75,5 @@ def generate_csv(data_dir): print(f"ERROR: Empty directory: {data_dir}") sys.exit() - print(f"Generating csv files with {data_dir} ...") - generate_csv(data_dir) \ No newline at end of file + generate_csv(data_dir) diff --git a/openvino/quantize.py b/openvino/quantize.py index 2f41bb3..0b52444 100644 --- a/openvino/quantize.py +++ b/openvino/quantize.py @@ -36,7 +36,7 @@ postprocess_output, dice, get_mask_image, - get_input_image + get_input_image, ) from compression.api import Metric, DataLoader @@ -55,43 +55,63 @@ parser = argparse.ArgumentParser( description="Quantizes an OpenVINO model to INT8.", - add_help=True, formatter_class=argparse.ArgumentDefaultsHelpFormatter) - -parser.add_argument("--xml_file", default=brainmage_root+"BrainMaGe/weights/ov/fp32/resunet_ma.xml", - help="XML file for OpenVINO to quantize") -parser.add_argument("--bin_file", default=brainmage_root+"BrainMaGe/weights/ov/fp32/resunet_ma.bin", - help="BIN file for OpenVINO to quantize") -parser.add_argument("--manifest", default=brainmage_root+"openvino/nfbs-dataset-test.csv", - help="Manifest file (CSV with filenames of images and labels)") -parser.add_argument("--data_dir", default="./data", - help="Data directory root") -parser.add_argument("--int8_directory", default="./int8_openvino_model", - help="INT8 directory for calibrated OpenVINO model") -parser.add_argument("--maximum_metric_drop", default=1.0, - help="AccuracyAwareQuantization: Maximum allowed drop in metric") -parser.add_argument("--accuracy_aware_quantization", - help="use accuracy aware quantization", - action="store_true", default=False) + add_help=True, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, +) + +parser.add_argument( + "--xml_file", + default=brainmage_root + "BrainMaGe/weights/ov/fp32/resunet_ma.xml", + help="XML file for OpenVINO to quantize", +) +parser.add_argument( + "--bin_file", + default=brainmage_root + "BrainMaGe/weights/ov/fp32/resunet_ma.bin", + help="BIN file for OpenVINO to quantize", +) +parser.add_argument( + "--manifest", + default=brainmage_root + "openvino/nfbs-dataset-test.csv", + help="Manifest file (CSV with filenames of images and labels)", +) +parser.add_argument("--data_dir", default="./data", help="Data directory root") +parser.add_argument( + "--int8_directory", + default="./int8_openvino_model", + help="INT8 directory for calibrated OpenVINO model", +) +parser.add_argument( + "--maximum_metric_drop", + default=1.0, + help="AccuracyAwareQuantization: Maximum allowed drop in metric", +) +parser.add_argument( + "--accuracy_aware_quantization", + help="use accuracy aware quantization", + action="store_true", + default=False, +) args = parser.parse_args() + class bcolors: """ Just gives us some colors for the text """ - HEADER = '\033[95m' - OKBLUE = '\033[94m' - OKGREEN = '\033[92m' - WARNING = '\033[93m' - FAIL = '\033[91m' - ENDC = '\033[0m' - BOLD = '\033[1m' - UNDERLINE = '\033[4m' -class MyDataLoader(DataLoader): + HEADER = "\033[95m" + OKBLUE = "\033[94m" + OKGREEN = "\033[92m" + WARNING = "\033[93m" + FAIL = "\033[91m" + ENDC = "\033[0m" + BOLD = "\033[1m" + UNDERLINE = "\033[4m" - def __init__(self, config): +class MyDataLoader(DataLoader): + def __init__(self, config): super().__init__(config) """ @@ -104,25 +124,34 @@ def __init__(self, config): bounding boxes if using a localization model). """ - self.manifest = config["manifest"] # Filename for manifest file with image and label filenames + self.manifest = config[ + "manifest" + ] # Filename for manifest file with image and label filenames self.images = [] self.labels = [] - - dataset_df = pd.read_csv(self.manifest, header = None) - + + dataset_df = pd.read_csv(self.manifest, header=None) + for i, row in dataset_df.iterrows(): - self.images.append(row[2]) #image path - self.labels.append(row[3]) #mask path - + self.images.append(row[2]) # image path + self.labels.append(row[3]) # mask path + self.items = np.arange(dataset_df.shape[0]) self.batch_size = 1 - print(bcolors.UNDERLINE + "\nQuantizing FP32 OpenVINO model to INT8\n" + bcolors.ENDC) - - print(bcolors.OKBLUE + "There are {:,} samples in the test dataset ".format(len(self.items)) + \ - bcolors.OKGREEN + "{}\n".format(self.manifest) + bcolors.ENDC) - + print( + bcolors.UNDERLINE + + "\nQuantizing FP32 OpenVINO model to INT8\n" + + bcolors.ENDC + ) + print( + bcolors.OKBLUE + + "There are {:,} samples in the test dataset ".format(len(self.items)) + + bcolors.OKGREEN + + "{}\n".format(self.manifest) + + bcolors.ENDC + ) def set_subset(self, indices): self._subset = None @@ -144,7 +173,6 @@ def myPreprocess(self, image_filename, label_filename): For this example, we show how to process the brain tumor data. Change this to preprocess you data as necessary. """ - """ Load the image and label for this item @@ -152,7 +180,6 @@ def myPreprocess(self, image_filename, label_filename): msk = get_mask_image(label_filename) img, patient_nib = get_input_image(image_filename) - return img, msk def __getitem__(self, item): @@ -174,11 +201,12 @@ def __getitem__(self, item): # IMPORTANT! # OpenVINO expects channels first so transpose channels to first dimension -# image = np.transpose(image, [3,0,1,2]) # Channels first -# label = np.transpose(label, [3,0,1,2]) # Channels first + # image = np.transpose(image, [3,0,1,2]) # Channels first + # label = np.transpose(label, [3,0,1,2]) # Channels first return (item, label), image + class MyMetric(Metric): def __init__(self): super().__init__() @@ -188,12 +216,12 @@ def __init__(self): @property def value(self): - """ Returns accuracy metric value for the last model output. """ + """Returns accuracy metric value for the last model output.""" return {self.name: [self._values[-1]]} @property def avg_value(self): - """ Returns accuracy metric value for all model outputs. """ + """Returns accuracy metric value for all model outputs.""" value = np.ravel(self._values).mean() print("Round #{} Mean {} = {}".format(self.round, self.name, value)) @@ -202,7 +230,7 @@ def avg_value(self): return {self.name: value} def update(self, outputs, labels): - """ Updates prediction matches. + """Updates prediction matches. Args: outputs: model output @@ -220,10 +248,10 @@ def update(self, outputs, labels): metric = dice(labels[0], pt_to_save) self._values.append(metric) except: - print (f" Inference Failed. ") + print(f" Inference Failed. ") def reset(self): - """ Resets collected matches """ + """Resets collected matches""" self._values = [] @property @@ -234,23 +262,16 @@ def higher_better(self): def get_attributes(self): return {self.name: {"direction": "higher-better", "type": ""}} -model_config = Dict({ - "model_name": "resunet_ma", - "model": args.xml_file, - "weights": args.bin_file -}) -engine_config = Dict({ - "device": "CPU", - "stat_requests_number": 4, - "eval_requests_number": 4 -}) +model_config = Dict( + {"model_name": "resunet_ma", "model": args.xml_file, "weights": args.bin_file} +) + +engine_config = Dict( + {"device": "CPU", "stat_requests_number": 4, "eval_requests_number": 4} +) -dataset_config = { - "manifest": args.manifest, - "images": "image", - "labels": "label" -} +dataset_config = {"manifest": args.manifest, "images": "image", "labels": "label"} default_quantization_algorithm = [ { @@ -258,30 +279,31 @@ def get_attributes(self): "params": { "target_device": "CPU", "preset": "performance", - #"stat_subset_size": 10 - } + # "stat_subset_size": 10 + }, } ] accuracy_aware_quantization_algorithm = [ { - "name": "AccuracyAwareQuantization", # compression algorithm name + "name": "AccuracyAwareQuantization", # compression algorithm name "params": { "target_device": "CPU", "preset": "performance", "stat_subset_size": 10, - "metric_subset_ratio": 0.5, # A part of the validation set that is used to compare full-precision and quantized models - "ranking_subset_size": 300, # A size of a subset which is used to rank layers by their contribution to the accuracy drop - "max_iter_num": 10, # Maximum number of iterations of the algorithm (maximum of layers that may be reverted back to full-precision) - "maximal_drop": args.maximum_metric_drop, # Maximum metric drop which has to be achieved after the quantization - "drop_type": "absolute", # Drop type of the accuracy metric: relative or absolute (default) - "use_prev_if_drop_increase": True, # Whether to use NN snapshot from the previous algorithm iteration in case if drop increases - "base_algorithm": "DefaultQuantization" # Base algorithm that is used to quantize model at the beginning - } + "metric_subset_ratio": 0.5, # A part of the validation set that is used to compare full-precision and quantized models + "ranking_subset_size": 300, # A size of a subset which is used to rank layers by their contribution to the accuracy drop + "max_iter_num": 10, # Maximum number of iterations of the algorithm (maximum of layers that may be reverted back to full-precision) + "maximal_drop": args.maximum_metric_drop, # Maximum metric drop which has to be achieved after the quantization + "drop_type": "absolute", # Drop type of the accuracy metric: relative or absolute (default) + "use_prev_if_drop_increase": True, # Whether to use NN snapshot from the previous algorithm iteration in case if drop increases + "base_algorithm": "DefaultQuantization", # Base algorithm that is used to quantize model at the beginning + }, } ] + class GraphAttrs(object): def __init__(self): self.keep_quantize_ops_in_IR = True @@ -314,12 +336,24 @@ def __init__(self): compressed_model = pipeline.run(model) -compression.graph.model_utils.save_model(compressed_model, save_path=args.int8_directory, model_name="resunet_ma_int8", for_stat_collection=False) +compression.graph.model_utils.save_model( + compressed_model, + save_path=args.int8_directory, + model_name="resunet_ma_int8", + for_stat_collection=False, +) -print(bcolors.BOLD + "\nThe INT8 version of the model has been saved to the directory ".format(args.int8_directory) + \ - bcolors.HEADER + "{}\n".format(args.int8_directory) + bcolors.ENDC) +print( + bcolors.BOLD + + "\nThe INT8 version of the model has been saved to the directory ".format( + args.int8_directory + ) + + bcolors.HEADER + + "{}\n".format(args.int8_directory) + + bcolors.ENDC +) -#save_model(compressed_model, "./int8_openvino_model/") +# save_model(compressed_model, "./int8_openvino_model/") print(bcolors.BOLD + "\Evaluating INT8 Model..." + bcolors.ENDC) @@ -333,5 +367,3 @@ def __init__(self): if metric_results_INT8: for name, value in metric_results_INT8.items(): print(bcolors.OKBLUE + "{: <27s} INT8: {}".format(name, value) + bcolors.ENDC) - - diff --git a/setup.py b/setup.py index 740843d..a7f485f 100755 --- a/setup.py +++ b/setup.py @@ -15,18 +15,18 @@ version="1.0.5-dev", description="Skull stripping using multiple and single modalities", url="https://github.com/CBICA/BrainMaGe", - python_requires=">=3.6", + python_requires=">=3.8", author="Siddhesh Thakur", author_email="software@cbica.upenn.edu", license="BSD-3-Clause", zip_safe=False, install_requires=[ "numpy", - "torch==1.8.2", + "torch==1.13.1", "scikit-image", "nibabel", - "pytorch-lightning==1.6.0", - "bids" + "pytorch-lightning==2.0.2", + "bids", ], scripts=[ "brain_mage_run", From 4aac5da9c3095d57492e336ac6f8f0a9c0d06405 Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Mon, 24 Apr 2023 17:28:19 -0400 Subject: [PATCH 03/10] update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a7f485f..48ec260 100755 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ zip_safe=False, install_requires=[ "numpy", - "torch==1.13.1", + "torch==1.12.0", "scikit-image", "nibabel", "pytorch-lightning==2.0.2", From 38747484737364aa4f925515260303e7aec2e00d Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Tue, 25 Apr 2023 11:51:34 -0400 Subject: [PATCH 04/10] add a new way to infer faster --- BrainMaGe/tester/test_ma_new.py | 164 ++++++++++++++++++++++++++++++++ brain_mage_run | 2 +- 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 BrainMaGe/tester/test_ma_new.py diff --git a/BrainMaGe/tester/test_ma_new.py b/BrainMaGe/tester/test_ma_new.py new file mode 100644 index 0000000..5eaac5c --- /dev/null +++ b/BrainMaGe/tester/test_ma_new.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +Created on Wed Mar 25 19:18:35 2020 + +@author: siddhesh +""" + +from __future__ import print_function, division +import os +import sys +import time +import pandas as pd +import torch +import nibabel as nib +import tqdm +import numpy as np +from skimage.transform import resize +from skimage.measure import label +from scipy.ndimage.morphology import binary_fill_holes +from BrainMaGe.models.networks import fetch_model +from BrainMaGe.utils import csv_creator_adv +from BrainMaGe.utils.utils_test import ( + pad_image, + process_image, + interpolate_image, + padder_and_cropper, +) + + +def postprocess_prediction(seg): + mask = seg != 0 + lbls = label(mask, connectivity=3) + lbls_sizes = [np.sum(lbls == i) for i in np.unique(lbls)] + largest_region = np.argmax(lbls_sizes[1:]) + 1 + seg[lbls != largest_region] = 0 + return seg + + +def infer_ma(cfg, device, save_brain, weights): + cfg = os.path.abspath(cfg) + + if os.path.isfile(cfg): + params_df = pd.read_csv( + cfg, + sep=" = ", + names=["param_name", "param_value"], + comment="#", + skip_blank_lines=True, + engine="python", + ).fillna(" ") + else: + print("Missing test_params.cfg file? Please give one!") + sys.exit(0) + params = {} + for i in range(params_df.shape[0]): + params[params_df.iloc[i, 0]] = params_df.iloc[i, 1] + params["weights"] = weights + start = time.asctime() + startstamp = time.time() + print("\nHostname :" + str(os.getenv("HOSTNAME"))) + print("\nStart Time :" + str(start)) + print("\nStart Stamp:" + str(startstamp)) + sys.stdout.flush() + + print("Generating Test csv") + if not os.path.exists(os.path.join(params["results_dir"])): + os.mkdir(params["results_dir"]) + if not params["csv_provided"] == "True": + print("Since CSV were not provided, we are gonna create for you") + csv_creator_adv.generate_csv( + params["test_dir"], + to_save=params["results_dir"], + mode=params["mode"], + ftype="test", + modalities=params["modalities"], + ) + test_csv = os.path.join(params["results_dir"], "test.csv") + else: + test_csv = params["test_csv"] + + model = fetch_model( + params["model"], + int(params["num_modalities"]), + int(params["num_classes"]), + int(params["base_filters"]), + ) + checkpoint = torch.load(str(params["weights"]), map_location=torch.device(device)) + model.load_state_dict(checkpoint["model_state_dict"]) + + if device != "cpu": + model.cuda() + model.eval() + + test_df = pd.read_csv(test_csv) + test_df.ID = test_df.ID.astype(str) + temp_dir = os.path.join(params["results_dir"], "Temp") + os.makedirs(temp_dir, exist_ok=True) + + patients_dict = {} + + print("Resampling the images to isotropic resolution of 1mm x 1mm x 1mm") + print("Also Converting the images to RAI and brats for smarter use.") + + for patient in tqdm.tqdm(test_df.values): + os.makedirs(os.path.join(temp_dir, patient[0]), exist_ok=True) + patient_path = patient[1] + + image = nib.load(patient_path) + + image_data = image.get_fdata() + image = process_image(image_data) + image = resize( + image, (128, 128, 128), order=3, mode="edge", cval=0, anti_aliasing=False + ) + image = image[np.newaxis, np.newaxis, ...] + image = torch.FloatTensor(image) + + if device != "cpu": + image = image.cuda() + + with torch.no_grad(): + output = model(image) + output = output.cpu().numpy()[0][0] + to_save = interpolate_image(output, patient_nib.shape) + to_save[to_save >= 0.9] = 1 + to_save[to_save < 0.9] = 0 + for i in range(to_save.shape[2]): + if np.any(to_save[:, :, i]): + to_save[:, :, i] = binary_fill_holes(to_save[:, :, i]) + to_save = postprocess_prediction(to_save) + to_save_nib = nib.Nifti1Image(to_save, patient_nib.affine) + + os.makedirs(os.path.join(params["results_dir"], patient[0]), exist_ok=True) + + output_path = os.path.join( + params["results_dir"], patient[0], patient[0] + "_mask.nii.gz" + ) + + nib.save(to_save_nib, output_path) + + if save_brain: + for patient in tqdm.tqdm(test_df.values): + image = nib.load(patient[1]) + image_data = image.get_fdata() + mask = nib.load( + os.path.join( + params["results_dir"], patient[0], patient[0] + "_mask.nii.gz" + ) + ) + mask_data = mask.get_fdata().astype(np.int8) + image_data[mask_data == 0] = 0 + to_save_brain = nib.Nifti1Image(image_data, image.affine) + nib.save( + to_save_brain, + os.path.join( + params["results_dir"], patient[0], patient[0] + "_brain.nii.gz" + ), + ) + + print("*" * 60) + print("Final output stored in : %s" % (params["results_dir"])) + print("Thank you for using BrainMaGe") + print("*" * 60) diff --git a/brain_mage_run b/brain_mage_run index 11b74ba..4d7e971 100755 --- a/brain_mage_run +++ b/brain_mage_run @@ -210,7 +210,7 @@ if __name__ == "__main__": trainer_main.train_network(params_file, DEVICE, weights) elif args.test == "True": if args.mode.lower() == "ma" or args.mode.lower() == "bids": - test_ma.infer_ma(params_file, DEVICE, args.save_brain, weights) + test_ma_new.infer_ma(params_file, DEVICE, args.save_brain, weights) elif args.mode.lower() == "multi-4": test_multi_4.infer_multi_4(params_file, DEVICE, args.save_brain, weights) else: From 63f7759190621039e8546d816b8cbf5c66fc477e Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Tue, 25 Apr 2023 11:51:49 -0400 Subject: [PATCH 05/10] fix import --- brain_mage_run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brain_mage_run b/brain_mage_run index 4d7e971..2ef2e37 100755 --- a/brain_mage_run +++ b/brain_mage_run @@ -11,7 +11,7 @@ import argparse import os import pandas as pd from BrainMaGe.trainer import trainer_main -from BrainMaGe.tester import test_ma, test_multi_4 +from BrainMaGe.tester import test_ma_new, test_multi_4 import pkg_resources From becc9cd25c90373cb09eecfe4b80067b3cf733b5 Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Tue, 25 Apr 2023 13:07:54 -0400 Subject: [PATCH 06/10] create new way to infer --- BrainMaGe/tester/test_ma_new.py | 51 ++++++++++++++++++--------------- setup.py | 2 +- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/BrainMaGe/tester/test_ma_new.py b/BrainMaGe/tester/test_ma_new.py index 5eaac5c..fc8fb5d 100644 --- a/BrainMaGe/tester/test_ma_new.py +++ b/BrainMaGe/tester/test_ma_new.py @@ -102,13 +102,13 @@ def infer_ma(cfg, device, save_brain, weights): print("Resampling the images to isotropic resolution of 1mm x 1mm x 1mm") print("Also Converting the images to RAI and brats for smarter use.") - for patient in tqdm.tqdm(test_df.values): - os.makedirs(os.path.join(temp_dir, patient[0]), exist_ok=True) - patient_path = patient[1] + for index, patient in tqdm.tqdm(test_df.iterrows()): + os.makedirs(os.path.join(temp_dir, patient["ID"]), exist_ok=True) + patient_path = patient["Image_path"] - image = nib.load(patient_path) + patient_nib = nib.load(patient_path) - image_data = image.get_fdata() + image_data = patient_nib.get_fdata() image = process_image(image_data) image = resize( image, (128, 128, 128), order=3, mode="edge", cval=0, anti_aliasing=False @@ -131,32 +131,37 @@ def infer_ma(cfg, device, save_brain, weights): to_save = postprocess_prediction(to_save) to_save_nib = nib.Nifti1Image(to_save, patient_nib.affine) - os.makedirs(os.path.join(params["results_dir"], patient[0]), exist_ok=True) + os.makedirs( + os.path.join(params["results_dir"], patient["ID"]), exist_ok=True + ) output_path = os.path.join( - params["results_dir"], patient[0], patient[0] + "_mask.nii.gz" + params["results_dir"], patient["ID"], patient["ID"] + "_mask.nii.gz" ) nib.save(to_save_nib, output_path) if save_brain: - for patient in tqdm.tqdm(test_df.values): - image = nib.load(patient[1]) - image_data = image.get_fdata() - mask = nib.load( - os.path.join( - params["results_dir"], patient[0], patient[0] + "_mask.nii.gz" - ) - ) - mask_data = mask.get_fdata().astype(np.int8) - image_data[mask_data == 0] = 0 - to_save_brain = nib.Nifti1Image(image_data, image.affine) - nib.save( - to_save_brain, - os.path.join( - params["results_dir"], patient[0], patient[0] + "_brain.nii.gz" - ), + image = nib.load(patient["Image_path"]) + image_data = image.get_fdata() + mask = nib.load( + os.path.join( + params["results_dir"], + patient["ID"], + patient["ID"] + "_mask.nii.gz", ) + ) + mask_data = mask.get_fdata().astype(np.int8) + image_data[mask_data == 0] = 0 + to_save_brain = nib.Nifti1Image(image_data, image.affine) + nib.save( + to_save_brain, + os.path.join( + params["results_dir"], + patient["ID"], + patient["ID"] + "_brain.nii.gz", + ), + ) print("*" * 60) print("Final output stored in : %s" % (params["results_dir"])) diff --git a/setup.py b/setup.py index 48ec260..06bc323 100755 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ zip_safe=False, install_requires=[ "numpy", - "torch==1.12.0", + "torch>=1.8.2", "scikit-image", "nibabel", "pytorch-lightning==2.0.2", From 1b7b49ba0c9d863c9bbd6b8547dc3ba40213d6af Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Tue, 25 Apr 2023 13:18:25 -0400 Subject: [PATCH 07/10] update to int8 --- BrainMaGe/tester/test_ma_new.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BrainMaGe/tester/test_ma_new.py b/BrainMaGe/tester/test_ma_new.py index fc8fb5d..9abad36 100644 --- a/BrainMaGe/tester/test_ma_new.py +++ b/BrainMaGe/tester/test_ma_new.py @@ -97,8 +97,6 @@ def infer_ma(cfg, device, save_brain, weights): temp_dir = os.path.join(params["results_dir"], "Temp") os.makedirs(temp_dir, exist_ok=True) - patients_dict = {} - print("Resampling the images to isotropic resolution of 1mm x 1mm x 1mm") print("Also Converting the images to RAI and brats for smarter use.") @@ -128,7 +126,7 @@ def infer_ma(cfg, device, save_brain, weights): for i in range(to_save.shape[2]): if np.any(to_save[:, :, i]): to_save[:, :, i] = binary_fill_holes(to_save[:, :, i]) - to_save = postprocess_prediction(to_save) + to_save = postprocess_prediction(to_save).astype(np.uint8) to_save_nib = nib.Nifti1Image(to_save, patient_nib.affine) os.makedirs( From 4ad9d8f94427003386b266863f6f7e01babe0401 Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Tue, 25 Apr 2023 13:35:14 -0400 Subject: [PATCH 08/10] update requirements --- requirements.yml | 444 ++++++++++++----------------------------------- setup.py | 3 +- 2 files changed, 116 insertions(+), 331 deletions(-) diff --git a/requirements.yml b/requirements.yml index 551dd90..3c45297 100755 --- a/requirements.yml +++ b/requirements.yml @@ -1,337 +1,121 @@ name: brainmage channels: - pytorch + - conda-forge - defaults dependencies: - - _anaconda_depends=2020.02=py36_0 - - _libgcc_mutex=0.1=main - - alabaster=0.7.12=py36_0 - - anaconda=custom=py36_1 - - anaconda-client=1.7.2=py36_0 - - anaconda-project=0.8.4=py_0 - - argh=0.26.2=py36_0 - - asn1crypto=1.3.0=py36_0 - - astroid=2.3.3=py36_0 - - astropy=4.0.1.post1=py36h7b6447c_1 - - atomicwrites=1.4.0=py_0 - - attrs=19.3.0=py_0 - - autopep8=1.4.3=py36_0 - - babel=2.8.0=py_0 - - backcall=0.2.0=py_0 - - backports=1.0=py_2 - - backports.shutil_get_terminal_size=1.0.0=py36_2 - - beautifulsoup4=4.9.1=py36_0 - - bitarray=1.2.2=py36h7b6447c_0 - - bkcharts=0.2=py36_0 + - _libgcc_mutex=0.1=conda_forge + - _openmp_mutex=4.5=2_gnu - blas=1.0=mkl - - bleach=3.1.5=py_0 - - blosc=1.19.0=hd408876_0 - - bokeh=2.1.0=py36_0 - - boto=2.49.0=py36_0 - - bottleneck=1.3.2=py36heb32a55_1 - - brotlipy=0.7.0=py36h7b6447c_1000 - - bzip2=1.0.8=h7b6447c_0 - - ca-certificates=2020.1.1=0 - - cairo=1.14.12=h8948797_3 - - certifi=2020.6.20=py36_0 - - cffi=1.14.0=py36h2e261b9_0 - - chardet=3.0.4=py36_1003 - - click=7.1.2=py_0 - - cloudpickle=1.4.1=py_0 - - clyent=1.2.2=py36_1 - - colorama=0.4.3=py_0 - - contextlib2=0.6.0.post1=py_0 - - contextvars=2.4=py_0 - - cryptography=2.9.2=py36h1ba5d50_0 - - cudatoolkit=9.2=0 - - curl=7.69.1=hbc83047_0 - - cycler=0.10.0=py36_0 - - cython=0.29.20=py36he6710b0_0 - - cytoolz=0.10.1=py36h7b6447c_0 - - dask=2.19.0=py_0 - - dask-core=2.19.0=py_0 - - dbus=1.13.14=hb2f20db_0 - - decorator=4.4.2=py_0 - - defusedxml=0.6.0=py_0 - - diff-match-patch=20181111=py_0 - - distributed=2.19.0=py36_0 - - docutils=0.16=py36_1 - - entrypoints=0.3=py36_0 - - et_xmlfile=1.0.1=py36_0 - - expat=2.2.6=he6710b0_0 - - fastcache=1.1.0=py36h7b6447c_0 - - filelock=3.0.12=py_0 - - flake8=3.7.9=py36_0 - - flask=1.1.2=py_0 - - fontconfig=2.13.0=h9420a91_0 - - freetype=2.10.2=h5ab3b9f_0 - - fribidi=1.0.5=h7b6447c_0 - - fsspec=0.7.4=py_0 - - future=0.18.2=py36_1 - - get_terminal_size=1.0.0=haa9412d_0 - - gevent=1.4.0=py36h7b6447c_0 - - glib=2.63.1=h5a9c865_0 - - glob2=0.7=py_0 - - gmp=6.1.2=h6c8ec71_1 - - gmpy2=2.0.8=py36h10f8cd9_2 - - graphite2=1.3.13=h23475e2_0 - - greenlet=0.4.16=py36h7b6447c_0 - - gst-plugins-base=1.14.0=hbbd80ab_1 - - gstreamer=1.14.0=hb453b48_1 - - h5py=2.10.0=py36h7918eee_0 - - harfbuzz=1.8.8=hffaf4a1_0 - - hdf5=1.10.4=hb1b8bf9_0 - - heapdict=1.0.1=py_0 - - html5lib=1.0.1=py36_0 - - hypothesis=5.16.1=py_0 - - icu=58.2=he6710b0_3 - - idna=2.9=py_1 - - imageio=2.8.0=py_0 - - imagesize=1.2.0=py_0 - - immutables=0.14=py36h7b6447c_0 - - importlib-metadata=1.6.1=py36_0 - - importlib_metadata=1.6.1=0 - - intel-openmp=2020.1=217 - - intervaltree=3.0.2=py_0 - - ipykernel=5.3.0=py36h5ca1d4c_0 - - ipython=7.15.0=py36_0 - - ipython_genutils=0.2.0=py36_0 - - ipywidgets=7.5.1=py_0 - - isort=4.3.21=py36_0 - - itsdangerous=1.1.0=py36_0 - - jbig=2.1=hdba287a_0 - - jdcal=1.4.1=py_0 - - jedi=0.15.2=py36_0 - - jeepney=0.4.3=py_0 - - jinja2=2.11.2=py_0 - - joblib=0.15.1=py_0 - - jpeg=9b=h024ee3a_2 - - json5=0.9.5=py_0 - - jsonschema=3.2.0=py36_0 - - jupyter=1.0.0=py36_7 - - jupyter_client=6.1.3=py_0 - - jupyter_console=6.1.0=py_0 - - jupyter_core=4.6.3=py36_0 - - jupyterlab=2.1.4=py_1 - - jupyterlab_server=1.1.5=py_0 - - keyring=21.1.1=py36_2 - - kiwisolver=1.2.0=py36hfd86e86_0 - - krb5=1.17.1=h173b8e3_0 - - lazy-object-proxy=1.5.0=py36h7b6447c_0 - - ld_impl_linux-64=2.33.1=h53a641e_7 - - libarchive=3.4.2=h62408e4_0 - - libcurl=7.69.1=h20c2e04_0 - - libedit=3.1.20191231=h7b6447c_0 - - libffi=3.2.1=hd88cf55_4 - - libgcc-ng=9.1.0=hdf63c60_0 - - libgfortran-ng=7.3.0=hdf63c60_0 - - liblief=0.10.1=he6710b0_0 - - libpng=1.6.37=hbc83047_0 - - libsodium=1.0.16=h1bed415_0 - - libspatialindex=1.9.3=he6710b0_0 - - libssh2=1.9.0=h1ba5d50_1 - - libstdcxx-ng=9.1.0=hdf63c60_0 - - libtiff=4.1.0=h2733197_1 - - libtool=2.4.6=h7b6447c_5 - - libuuid=1.0.3=h1bed415_2 - - libxcb=1.13=h1bed415_1 - - libxml2=2.9.10=he19cac6_1 - - llvmlite=0.32.1=py36hd408876_0 - - locket=0.2.0=py36_1 - - lxml=4.5.1=py36hefd8a0e_0 - - lz4-c=1.9.2=he6710b0_0 - - lzo=2.10=h7b6447c_2 - - markupsafe=1.1.1=py36h7b6447c_0 - - matplotlib=3.2.2=0 - - matplotlib-base=3.2.2=py36hef1b27d_0 - - mccabe=0.6.1=py36_1 - - mistune=0.8.4=py36h7b6447c_0 - - mkl=2020.1=217 - - mkl-service=2.3.0=py36he904b0f_0 - - mkl_fft=1.1.0=py36h23d657b_0 - - mkl_random=1.1.1=py36h0573a6f_0 - - mock=4.0.2=py_0 - - more-itertools=8.4.0=py_0 - - mpc=1.1.0=h10f8cd9_1 - - mpfr=4.0.1=hdf1c602_3 - - mpmath=1.1.0=py36_0 - - msgpack-python=1.0.0=py36hfd86e86_1 - - multipledispatch=0.6.0=py36_0 - - nbconvert=5.6.1=py36_0 - - nbformat=5.0.7=py_0 - - ncurses=6.2=he6710b0_1 - - networkx=2.4=py_0 - - ninja=1.9.0=py36hfd86e86_0 - - nltk=3.5=py_0 - - nose=1.3.7=py36_2 - - notebook=6.0.3=py36_0 - - numba=0.49.1=py36h0573a6f_0 - - numexpr=2.7.1=py36h423224d_0 - - numpy=1.18.5=py36ha1c710e_0 - - numpy-base=1.18.5=py36hde5b4d6_0 - - numpydoc=1.0.0=py_0 - - olefile=0.46=py36_0 - - openpyxl=3.0.3=py_0 - - openssl=1.1.1g=h7b6447c_0 - - packaging=20.4=py_0 - - pandas=1.0.5=py36h0573a6f_0 - - pandoc=2.2.3.2=0 - - pandocfilters=1.4.2=py36_1 - - pango=1.42.4=h049681c_0 - - parso=0.5.2=py_0 - - partd=1.1.0=py_0 - - patchelf=0.11=he6710b0_0 - - path=13.1.0=py36_0 - - path.py=12.4.0=0 - - pathlib2=2.3.5=py36_0 - - pathtools=0.1.2=py_1 - - patsy=0.5.1=py36_0 - - pcre=8.43=he6710b0_0 - - pep8=1.7.1=py36_0 - - pexpect=4.8.0=py36_0 - - pickleshare=0.7.5=py36_0 - - pillow=7.1.2=py36hb39fc2d_0 - - pip=20.1.1=py36_1 - - pixman=0.38.0=h7b6447c_0 - - pkginfo=1.5.0.1=py36_0 - - pluggy=0.13.1=py36_0 - - ply=3.11=py36_0 - - prometheus_client=0.8.0=py_0 - - prompt-toolkit=3.0.5=py_0 - - prompt_toolkit=3.0.5=0 - - psutil=5.7.0=py36h7b6447c_0 - - ptyprocess=0.6.0=py36_0 - - py=1.8.2=py_0 - - py-lief=0.10.1=py36h403a769_0 - - pycodestyle=2.5.0=py36_0 - - pycosat=0.6.3=py36h7b6447c_0 - - pycparser=2.20=py_0 - - pycrypto=2.6.1=py36h7b6447c_10 - - pycurl=7.43.0.5=py36h1ba5d50_0 - - pydocstyle=5.0.2=py_0 - - pyflakes=2.1.1=py36_0 - - pygments=2.6.1=py_0 - - pylint=2.4.4=py36_0 - - pyodbc=4.0.30=py36he6710b0_0 - - pyopenssl=19.1.0=py36_0 - - pyparsing=2.4.7=py_0 - - pyqt=5.9.2=py36h05f1152_2 - - pyrsistent=0.16.0=py36h7b6447c_0 - - pysocks=1.7.1=py36_0 - - pytables=3.6.1=py36h71ec239_0 - - pytest=5.4.3=py36_0 - - pytest-arraydiff=0.3=py36h39e3cac_0 - - pytest-astropy=0.8.0=py_0 - - pytest-astropy-header=0.1.2=py_0 - - pytest-doctestplus=0.7.0=py_0 - - pytest-openfiles=0.5.0=py_0 - - pytest-remotedata=0.3.2=py36_0 - - python=3.6.10=hcf32534_1 - - python-dateutil=2.8.1=py_0 - - python-jsonrpc-server=0.3.4=py_0 - - python-language-server=0.31.10=py36_0 - - python-libarchive-c=2.9=py_0 - - pytorch=1.5.1=py3.6_cuda9.2.148_cudnn7.6.3_0 - - pytz=2020.1=py_0 - - pywavelets=1.1.1=py36h7b6447c_0 - - pyxdg=0.26=py_0 - - pyyaml=5.3.1=py36h7b6447c_0 - - pyzmq=18.1.1=py36he6710b0_0 - - qdarkstyle=2.8.1=py_0 - - qt=5.9.7=h5867ecd_1 - - qtawesome=0.7.2=py_0 - - qtconsole=4.7.5=py_0 - - qtpy=1.9.0=py_0 - - readline=8.0=h7b6447c_0 - - regex=2020.5.14=py36h7b6447c_0 - - requests=2.24.0=py_0 - - ripgrep=11.0.2=he32d670_0 - - rope=0.17.0=py_0 - - rtree=0.9.4=py36_1 - - ruamel_yaml=0.15.87=py36h7b6447c_0 - - scikit-image=0.16.2=py36h0573a6f_0 - - scikit-learn=0.23.1=py36h423224d_0 - - scipy=1.4.1=py36h0b6359f_0 - - seaborn=0.10.1=py_0 - - secretstorage=3.1.2=py36_0 - - send2trash=1.5.0=py36_0 - - setuptools=47.3.1=py36_0 - - simplegeneric=0.8.1=py36_2 - - singledispatch=3.4.0.3=py36_0 - - sip=4.19.8=py36hf484d3e_0 - - six=1.15.0=py_0 - - snappy=1.1.7=hbae5bb6_3 - - snowballstemmer=2.0.0=py_0 - - sortedcollections=1.2.1=py_0 - - sortedcontainers=2.2.2=py_0 - - soupsieve=2.0.1=py_0 - - sphinx=3.1.1=py_0 - - sphinxcontrib=1.0=py36_1 - - sphinxcontrib-applehelp=1.0.2=py_0 - - sphinxcontrib-devhelp=1.0.2=py_0 - - sphinxcontrib-htmlhelp=1.0.3=py_0 - - sphinxcontrib-jsmath=1.0.1=py_0 - - sphinxcontrib-qthelp=1.0.3=py_0 - - sphinxcontrib-serializinghtml=1.1.4=py_0 - - sphinxcontrib-websupport=1.2.2=py_0 - - spyder=4.1.3=py36_0 - - spyder-kernels=1.9.1=py36_0 - - sqlalchemy=1.3.17=py36h7b6447c_0 - - sqlite=3.32.3=h62c20be_0 - - statsmodels=0.11.1=py36h7b6447c_0 - - sympy=1.6=py36_0 - - tbb=2020.0=hfd86e86_0 - - tblib=1.6.0=py_0 - - terminado=0.8.3=py36_0 - - testpath=0.4.4=py_0 - - threadpoolctl=2.1.0=pyh5ca1d4c_0 - - tk=8.6.10=hbc83047_0 - - toolz=0.10.0=py_0 - - torchvision=0.6.1=py36_cu92 - - tornado=6.0.4=py36h7b6447c_1 - - tqdm=4.46.1=py_0 - - traitlets=4.3.3=py36_0 - - typed-ast=1.4.1=py36h7b6447c_0 - - typing_extensions=3.7.4.2=py_0 - - ujson=1.35=py36h14c3975_0 - - unicodecsv=0.14.1=py36_0 - - unixodbc=2.3.7=h14c3975_0 - - urllib3=1.25.9=py_0 - - watchdog=0.10.2=py36_0 - - wcwidth=0.2.4=py_0 - - webencodings=0.5.1=py36_1 - - werkzeug=1.0.1=py_0 - - wheel=0.34.2=py36_0 - - widgetsnbextension=3.5.1=py36_0 - - wrapt=1.12.1=py36h7b6447c_1 - - wurlitzer=2.0.0=py36_0 - - xlrd=1.2.0=py36_0 - - xlsxwriter=1.2.9=py_0 - - xlwt=1.3.0=py36_0 - - xz=5.2.5=h7b6447c_0 - - yaml=0.1.7=had09818_2 - - yapf=0.29.0=py_0 - - zeromq=4.3.1=he6710b0_3 - - zict=2.0.0=py_0 - - zipp=3.1.0=py_0 - - zlib=1.2.11=h7b6447c_3 - - zstd=1.4.4=h0b5b093_3 + - brotlipy=0.7.0=py38h0a891b7_1005 + - bzip2=1.0.8=h7f98852_4 + - ca-certificates=2022.12.7=ha878542_0 + - cffi=1.15.1=py38h4a40e3a_3 + - cryptography=40.0.2=py38h3d167d9_0 + - cudatoolkit=11.3.1=h9edb442_11 + - ffmpeg=4.3=hf484d3e_0 + - freetype=2.12.1=hca18f0e_1 + - gmp=6.2.1=h58526e2_0 + - gnutls=3.6.13=h85f3911_1 + - intel-openmp=2021.4.0=h06a4308_3561 + - jpeg=9e=h0b41bf4_3 + - lame=3.100=h166bdaf_1003 + - lcms2=2.15=hfd0df8a_0 + - ld_impl_linux-64=2.40=h41732ed_0 + - lerc=4.0.0=h27087fc_0 + - libdeflate=1.17=h0b41bf4_0 + - libffi=3.4.2=h7f98852_5 + - libgcc-ng=12.2.0=h65d4601_19 + - libgomp=12.2.0=h65d4601_19 + - libiconv=1.17=h166bdaf_0 + - libnsl=2.0.0=h7f98852_0 + - libpng=1.6.39=h753d276_0 + - libsqlite=3.40.0=h753d276_1 + - libstdcxx-ng=12.2.0=h46fd767_19 + - libtiff=4.5.0=h6adf6a1_2 + - libuuid=2.38.1=h0b41bf4_0 + - libwebp-base=1.3.0=h0b41bf4_0 + - libxcb=1.13=h7f98852_1004 + - libzlib=1.2.13=h166bdaf_4 + - mkl=2021.4.0=h06a4308_640 + - mkl-service=2.4.0=py38h95df7f1_0 + - mkl_fft=1.3.1=py38h8666266_1 + - mkl_random=1.2.2=py38h1abd341_0 + - ncurses=6.3=h27087fc_1 + - nettle=3.6=he412f7d_0 + - numpy-base=1.23.5=py38h31eccc5_0 + - openh264=2.1.1=h780b84a_0 + - openjpeg=2.5.0=hfec8fc6_2 + - openssl=3.1.0=hd590300_2 + - pip=23.1.1=pyhd8ed1ab_0 + - pthread-stubs=0.4=h36c2ea0_1001 + - pycparser=2.21=pyhd8ed1ab_0 + - pyopenssl=23.1.1=pyhd8ed1ab_0 + - pysocks=1.7.1=pyha2e5f31_6 + - python=3.8.16=he550d4f_1_cpython + - python_abi=3.8=3_cp38 + - pytorch=1.12.0=py3.8_cuda11.3_cudnn8.3.2_0 + - pytorch-mutex=1.0=cuda + - readline=8.2=h8228510_1 + - setuptools=67.7.2=pyhd8ed1ab_0 + - tk=8.6.12=h27826a3_0 + - torchaudio=0.12.0=py38_cu113 + - torchvision=0.13.0=py38_cu113 + - typing_extensions=4.5.0=pyha770c72_0 + - wheel=0.40.0=pyhd8ed1ab_0 + - xorg-libxau=1.0.9=h7f98852_0 + - xorg-libxdmcp=1.1.3=h7f98852_0 + - xz=5.2.6=h166bdaf_0 + - zlib=1.2.13=h166bdaf_4 + - zstd=1.5.2=h3eb15da_6 - pip: - - absl-py==0.9.0 - - cachetools==4.1.0 - - google-auth==1.18.0 - - google-auth-oauthlib==0.4.1 - - grpcio==1.29.0 - - markdown==3.2.2 - - nibabel==3.1.0 - - oauthlib==3.1.0 - - protobuf==3.12.2 - - pyasn1==0.4.8 - - pyasn1-modules==0.2.8 - - pytorch-lightning==0.8.1 - - requests-oauthlib==1.3.0 - - rsa==4.6 - - tensorboard==2.2.2 - - tensorboard-plugin-wit==1.6.0.post3 - - pybids==0.11.1 -prefix: /home/siddhesh/anaconda3/envs/deepbet + - aiohttp==3.8.4 + - aiosignal==1.3.1 + - astor==0.8.1 + - async-timeout==4.0.2 + - attrs==23.1.0 + - bids==0.0 + - bids-validator==1.11.1.dev0 + - brainmage==1.0.5.dev0 + - certifi==2022.12.7 + - charset-normalizer==3.1.0 + - click==8.1.3 + - formulaic==0.5.2 + - frozenlist==1.3.3 + - fsspec==2023.4.0 + - graphlib-backport==1.0.3 + - idna==3.4 + - imageio==2.28.0 + - importlib-resources==5.12.0 + - interface-meta==1.3.0 + - lazy-loader==0.2 + - lightning-utilities==0.8.0 + - multidict==6.0.4 + - networkx==3.1 + - nibabel==5.1.0 + - num2words==0.5.12 + - numpy==1.24.3 + - packaging==23.1 + - pandas==2.0.1 + - pillow==9.5.0 + - pybids==0.15.6 + - python-dateutil==2.8.2 + - pytorch-lightning==2.0.2 + - pytz==2023.3 + - pywavelets==1.4.1 + - pyyaml==6.0 + - requests==2.28.2 + - scikit-image==0.21.0rc0 + - scipy==1.10.1 + - six==1.16.0 + - sqlalchemy==1.3.24 + - tifffile==2023.4.12 + - torch==1.12.0 + - torchmetrics==0.11.4 + - tqdm==4.65.0 + - typing-extensions==4.5.0 + - tzdata==2023.3 + - urllib3==1.26.15 + - wrapt==1.15.0 + - yarl==1.9.1 + - zipp==3.15.0 diff --git a/setup.py b/setup.py index 06bc323..16731c2 100755 --- a/setup.py +++ b/setup.py @@ -22,11 +22,12 @@ zip_safe=False, install_requires=[ "numpy", - "torch>=1.8.2", + "torch==1.12.0", "scikit-image", "nibabel", "pytorch-lightning==2.0.2", "bids", + "pandas", ], scripts=[ "brain_mage_run", From 05f24bd35d3b59f2a1f303080b8dd2d4a2638ed1 Mon Sep 17 00:00:00 2001 From: Geeks-sid Date: Tue, 25 Apr 2023 13:40:19 -0400 Subject: [PATCH 09/10] update correct requirements --- requirements.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/requirements.yml b/requirements.yml index 3c45297..670cef4 100755 --- a/requirements.yml +++ b/requirements.yml @@ -76,7 +76,6 @@ dependencies: - attrs==23.1.0 - bids==0.0 - bids-validator==1.11.1.dev0 - - brainmage==1.0.5.dev0 - certifi==2022.12.7 - charset-normalizer==3.1.0 - click==8.1.3 From 7c99f353974b4ece0f2c85984f58663e128efe3b Mon Sep 17 00:00:00 2001 From: Siddhesh Thakur Date: Wed, 24 May 2023 22:09:50 -0400 Subject: [PATCH 10/10] go back to old brainmage --- brain_mage_run | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brain_mage_run b/brain_mage_run index 2ef2e37..4d7e971 100755 --- a/brain_mage_run +++ b/brain_mage_run @@ -11,7 +11,7 @@ import argparse import os import pandas as pd from BrainMaGe.trainer import trainer_main -from BrainMaGe.tester import test_ma_new, test_multi_4 +from BrainMaGe.tester import test_ma, test_multi_4 import pkg_resources