From 318fc7f9e32bb0fa63f400e23348520d563806b5 Mon Sep 17 00:00:00 2001 From: pravi Date: Sun, 6 Aug 2023 01:18:49 +0530 Subject: [PATCH 1/4] "Add error handling and sys.exit() in brain_age.py" --- GANDLF/models/brain_age.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/GANDLF/models/brain_age.py b/GANDLF/models/brain_age.py index 436cf732b..0d9c3c634 100644 --- a/GANDLF/models/brain_age.py +++ b/GANDLF/models/brain_age.py @@ -19,10 +19,15 @@ def brainage(parameters): # Check that the input data is 2D if parameters["model"]["dimension"] != 2: - sys.exit("Brain Age predictions only works on 2D data") + print("Error: Brain Age predictions only work on 2D data") + sys.exit(1) - # Load the pretrained VGG16 model - model = torchvision.models.vgg16(pretrained=True) + try: + # Load the pretrained VGG16 model + model = torchvision.models.vgg16(pretrained=True) + except Exception as e: + print("Error: Failed to load VGG16 model:", str(e)) + sys.exit(1) # Remove the final convolutional layer model.final_convolution_layer = None @@ -36,19 +41,13 @@ def brainage(parameters): features = list(model.classifier.children())[:-1] # Remove the last layer features.extend( [ - nn.Linear( - num_features, 1024 - ), # Add a linear layer with 1024 output features + nn.Linear(num_features, 1024), # Add a linear layer with 1024 output features nn.ReLU(True), # Add a ReLU activation function nn.Dropout2d(0.8), # Add a 2D dropout layer with a probability of 0.8 - nn.Linear( - 1024, 1 - ), # Add a linear layer with 1 output feature (for brain age prediction) + nn.Linear(1024, 1), # Add a linear layer with 1 output feature (for brain age prediction) ] ) - model.classifier = nn.Sequential( - *features - ) # Replace the model classifier with the modified one + model.classifier = nn.Sequential(*features) # Replace the model classifier with the modified one # Set the "amp" parameter to False (not yet implemented for VGG) parameters["model"]["amp"] = False From f189e7bcb81e4a5ea7017bd3822ad685e4c18002 Mon Sep 17 00:00:00 2001 From: Praveen <130393640+1Pravi@users.noreply.github.com> Date: Mon, 7 Aug 2023 07:51:51 +0530 Subject: [PATCH 2/4] Update GANDLF/models/brain_age.py Co-authored-by: Sarthak Pati --- GANDLF/models/brain_age.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/GANDLF/models/brain_age.py b/GANDLF/models/brain_age.py index 0d9c3c634..04f1a0591 100644 --- a/GANDLF/models/brain_age.py +++ b/GANDLF/models/brain_age.py @@ -25,9 +25,8 @@ def brainage(parameters): try: # Load the pretrained VGG16 model model = torchvision.models.vgg16(pretrained=True) - except Exception as e: - print("Error: Failed to load VGG16 model:", str(e)) - sys.exit(1) + except Exception: + sys.exit("Error: Failed to load VGG16 model: " + traceback.format_exc()) # Remove the final convolutional layer model.final_convolution_layer = None From 9d56e7068589825b98e480a4ac2468700553bd49 Mon Sep 17 00:00:00 2001 From: pravi Date: Sun, 6 Aug 2023 08:39:18 +0530 Subject: [PATCH 3/4] Add error handling for brain age model --- GANDLF/models/brain_age.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/GANDLF/models/brain_age.py b/GANDLF/models/brain_age.py index 04f1a0591..971f15f4a 100644 --- a/GANDLF/models/brain_age.py +++ b/GANDLF/models/brain_age.py @@ -18,9 +18,7 @@ def brainage(parameters): """ # Check that the input data is 2D - if parameters["model"]["dimension"] != 2: - print("Error: Brain Age predictions only work on 2D data") - sys.exit(1) + assert parameters["model"]["dimension"] == 2, "Brain Age predictions only work on 2D data" try: # Load the pretrained VGG16 model From 13b0cc0cc85ccd81cd5ffe44ebb8d0c41716e660 Mon Sep 17 00:00:00 2001 From: pravi Date: Thu, 10 Aug 2023 17:08:57 +0530 Subject: [PATCH 4/4] Add error handling for brain age model --- GANDLF/models/brain_age.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GANDLF/models/brain_age.py b/GANDLF/models/brain_age.py index 971f15f4a..3bc88e78b 100644 --- a/GANDLF/models/brain_age.py +++ b/GANDLF/models/brain_age.py @@ -1,7 +1,7 @@ import torch.nn as nn import sys import torchvision - +import traceback def brainage(parameters): """