Skip to content

Commit

Permalink
Add more behaviour (continuous training)
Browse files Browse the repository at this point in the history
  • Loading branch information
rudolphpienaar committed Apr 25, 2024
1 parent ac4824c commit 6eeadb3
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions spleenseg/spleenseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Any, Optional, Callable

from spleenseg.core import neuralnet
from spleenseg.models.data import TrainingParams
from spleenseg.transforms import transforms
from spleenseg.plotting import plotting
import warnings
Expand Down Expand Up @@ -56,6 +57,12 @@
default="training",
help="mode of behaviour: training or inference",
)
parser.add_argument(
"--logTrainingTransformVols",
default=False,
action="store_true",
help="If specified, save training tranformations as NIfTI volumes",
)
parser.add_argument(
"--useModel",
type=str,
Expand Down Expand Up @@ -171,12 +178,21 @@ def envDetail_print(options: Namespace, **kwargs):

def env_outputDirsMake(options: Namespace) -> None:
if "training" in options.mode:
Path(Path(options.outputdir) / "training").mkdir(parents=True, exist_ok=True)
Path(Path(options.outputdir) / "validation").mkdir(parents=True, exist_ok=True)
params: TrainingParams = TrainingParams(options)
params.whileTrainingIO.mkdir(parents=True, exist_ok=True)
params.whileTrainingValidation.mkdir(parents=True, exist_ok=True)
params.postTrainingValidation.mkdir(parents=True, exist_ok=True)
if "inference" in options.mode:
Path(Path(options.outputdir) / "inference").mkdir(parents=True, exist_ok=True)


def modelFile_inputdirGet(options: Namespace) -> Path:
modelFile: Path = Path(Path(options.inputdir) / options.useModel)
if not modelFile.exists():
raise FileNotFoundError(f"The model '{modelFile}' does not exist.")
return modelFile


def training_do(neuralNet: neuralnet.NeuralNet, options: Namespace) -> bool:
trainingOK: bool = True

Expand All @@ -189,6 +205,8 @@ def training_do(neuralNet: neuralnet.NeuralNet, options: Namespace) -> bool:

if options.mode == "training":
neuralNet.train()
if options.mode == "trainingContinue":
neuralNet.train(modelFile_inputdirGet(options))

plotting.plot_trainingMetrics(
neuralNet.trainingLog,
Expand All @@ -206,11 +224,7 @@ def inference_do(neuralNet: neuralnet.NeuralNet, options: Namespace) -> bool:
inferenceOK: bool = True
neuralNet.testingFileSet = testingData_prep(options)

modelFile: Path = Path(Path(options.inputdir) / options.useModel)
if not modelFile.exists():
raise FileNotFoundError(f"The model '{modelFile}' does not exist.")

neuralNet.infer_usingModel(modelFile)
neuralNet.infer_usingModel(modelFile_inputdirGet(options))

return inferenceOK

Expand Down

0 comments on commit 6eeadb3

Please sign in to comment.