Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev rh pythae #450

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8be5f43
Added dataset for neuroimages working with Pythae
ravih18 Dec 8, 2022
7f14a68
First version of Pythae VAE in clinicadl
ravih18 Dec 8, 2022
d744149
Added to the list of model importable
ravih18 Dec 8, 2022
ad8afbd
added pythae models
ravih18 Jan 25, 2023
e784dad
Added pythae argument to config file
ravih18 Jan 27, 2023
d953cbd
Adding missing files that has been changed
ravih18 Jan 27, 2023
514b561
Updating maps and task manager
ravih18 Jan 27, 2023
bd9a258
Updated code for easy eval with pythae
ravih18 Jan 30, 2023
30ebeef
add modified files
ravih18 Feb 16, 2023
420072d
Add random search for vae architecture
ravih18 Apr 17, 2023
5dfbd90
syntax hotfix
ravih18 Apr 17, 2023
023c8b0
syntax hotfix
ravih18 Apr 17, 2023
449fca4
Update decoder padding for RS
ravih18 Apr 17, 2023
ecee89d
changed default value for feature size
ravih18 Apr 17, 2023
aebb16d
updated toml
ravih18 Apr 21, 2023
3971b7a
Added AMP as default for VAE
ravih18 May 4, 2023
f91beb7
Corrected batch size for vae
ravih18 May 4, 2023
cc8ec8b
updated batch size argument for trainer config
ravih18 May 4, 2023
2a46855
Revert "add modified files"
camillebrianceau Jun 13, 2023
c9def03
remove test/data folder
camillebrianceau Jun 13, 2023
525b165
remove extract folder
camillebrianceau Jun 13, 2023
6fef200
remove old getlabels folder
camillebrianceau Jun 13, 2023
3ae0ccb
remove restrict folder
camillebrianceau Jun 13, 2023
b330b03
remove old tests files
camillebrianceau Jun 13, 2023
68275bd
pre commit
camillebrianceau Jun 13, 2023
5251ef5
pre-commit
camillebrianceau Jun 13, 2023
413f2d7
pre-commit
camillebrianceau Jun 13, 2023
cc75a2b
add pythae dependency
camillebrianceau Jun 13, 2023
84d307d
os.pathlib to Path + others
camillebrianceau Jun 13, 2023
5f61079
JZ modification
ravih18 Jun 23, 2023
9032697
solve caps_dataset
camillebrianceau Jun 26, 2023
83c203b
pb with adaptation to pythae
camillebrianceau Jun 27, 2023
05beafe
test gpu
camillebrianceau Jun 27, 2023
0d74274
poetry update
camillebrianceau Jun 27, 2023
875df0f
tests
camillebrianceau Jun 28, 2023
0412e52
tests
camillebrianceau Jun 29, 2023
40ef44d
tests
camillebrianceau Jun 30, 2023
76763a9
tests
camillebrianceau Jun 30, 2023
5f7dd87
tests
camillebrianceau Jun 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified clinicadl/quality_check/t1_linear/cli.py
100755 → 100644
Empty file.
Empty file modified clinicadl/quality_check/t1_linear/utils.py
100755 → 100644
Empty file.
37 changes: 32 additions & 5 deletions clinicadl/random_search/random_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,48 @@
Launch a random network training.
"""
from pathlib import Path

from clinicadl.random_search.random_search_utils import get_space_dict, random_sampling
from clinicadl.train import train
from random import sample


def launch_search(launch_directory: Path, job_name):
from clinicadl.random_search.random_search_classification_utils import (
classification_random_sampling,
get_classification_space_dict,
)
from clinicadl.train import train

if not (launch_directory / "random_search.toml").is_file():
raise FileNotFoundError(
f"TOML file 'random_search.toml' must be written in directory: {launch_directory}."
)
space_options = get_space_dict(launch_directory)
options = random_sampling(space_options)
space_options = get_classification_space_dict(launch_directory)
options = classification_random_sampling(space_options)

maps_directory = launch_directory / job_name
split = options.pop("split")
options["architecture"] = "RandomArchitecture"

train(maps_directory, options, split)


def launch_vae_search(launch_directory, job_name):
from clinicadl.random_search.random_search_vae_utils import (
get_vae_space_dict,
vae_random_sampling,
)
from clinicadl.utils.maps_manager import MapsManager

space_options = get_vae_space_dict(launch_directory)
parameters = vae_random_sampling(space_options)
parameters["architecture"] = "pythae_VAE"
print("Parameters:", parameters)

# Select 3 splits randomly
split_list = sample(range(6), 3)
print("Split list:", split_list)

# initialise maps
maps_dir = launch_directory / job_name
maps_manager = MapsManager(maps_dir, parameters, verbose="info")
# launch training procedure for Pythae
maps_manager.train_pythae(split_list=split_list)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from clinicadl.utils.preprocessing import read_preprocessing


def get_space_dict(launch_directory: Path) -> Dict[str, Any]:
def get_classification_space_dict(launch_directory: str) -> Dict[str, Any]:
"""Transforms the TOML dictionary in one dimension dictionary."""
toml_path = launch_directory / "random_search.toml"
toml_options = toml.load(toml_path)
Expand Down Expand Up @@ -98,7 +98,7 @@ def sampling_fn(value, sampling_type: str):
return value


def random_sampling(rs_options: Dict[str, Any]) -> Dict[str, Any]:
def classification_random_sampling(rs_options: Dict[str, Any]) -> Dict[str, Any]:
"""
Samples all the hyperparameters of the model.
Args:
Expand Down
12 changes: 10 additions & 2 deletions clinicadl/random_search/random_search_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@


@click.command("random-search", no_args_is_help=True)
@click.argument("task", type=click.Choice(["classification", "vae-architecture"]))
@click.argument(
"launch_directory",
type=click.Path(exists=True, path_type=Path),
)
@click.argument("name", type=str)
def cli(
task,
launch_directory,
name,
):
Expand All @@ -19,9 +21,15 @@ def cli(

NAME is the name of the output folder containing the experiment.
"""
from .random_search import launch_search

launch_search(launch_directory, name)
if task == "classification":
from .random_search import launch_search

launch_search(task, launch_directory, name)
elif task == "vae-architecture":
from .random_search import launch_vae_search

launch_vae_search(launch_directory, name)


if __name__ == "__main__":
Expand Down
137 changes: 137 additions & 0 deletions clinicadl/random_search/random_search_vae_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import random
from os import path
from typing import Any, Dict, Tuple

import toml

from clinicadl.random_search.random_search_classification_utils import sampling_fn
from clinicadl.train.train_utils import build_train_dict
from clinicadl.utils.exceptions import ClinicaDLConfigurationError
from clinicadl.utils.preprocessing import read_preprocessing


def get_vae_space_dict(launch_directory):
"""
Takes a launch directory with a "random_search.toml" file with all the parameters to explore.
Return a parameters dictionnary randomly sampled
"""

toml_path = path.join(launch_directory, "random_search.toml")
if not path.exists(toml_path):
raise FileNotFoundError(
f"TOML file 'random_search.toml' must be written in directory {launch_directory}."
)

# load TOML file and create space dict
toml_options = toml.load(toml_path)
space_dict = dict()

# check and read TOML
if "Random_Search" not in toml_options:
raise ClinicaDLConfigurationError(
"Category 'Random_Search' must be defined in the random_search.toml file. "
"All random search arguments AND options must be defined in this category."
)

for key in toml_options["Random_Search"]:
space_dict[key] = toml_options["Random_Search"][key]

# Check presence of mandatory arguments
mandatory_arguments = [
"network_task",
"tsv_path",
"caps_directory",
"preprocessing_json",
"first_layer_channels",
"n_conv_encoder",
"feature_size",
"latent_space_size",
"n_conv_decoder",
"last_layer_channels",
"last_layer_conv",
]

for argument in mandatory_arguments:
if argument not in space_dict:
raise ClinicaDLConfigurationError(
f"The argument {argument} must be specified in the random_search.toml file (Random_Search category)."
)

# Make training parameter dict
train_default = build_train_dict(toml_path, space_dict["network_task"])

# Mode and preprocessing
preprocessing_json = path.join(
space_dict["caps_directory"],
"tensor_extraction",
space_dict.pop("preprocessing_json"),
)

preprocessing_dict = read_preprocessing(preprocessing_json)
train_default["preprocessing_dict"] = preprocessing_dict
train_default["mode"] = preprocessing_dict["mode"]

# Add the other default parameters to the dictionnary
# space_dict.update(train_default)
for k, v in train_default.items():
if k not in space_dict:
space_dict[k] = v
return space_dict


def vae_random_sampling(space_dict):
# Create parameters dict
parameters = dict()

sampling_vae_dict = {
"accumulation_steps": "fixed",
"baseline": "fixed",
"batch_size": "fixed",
"caps_directory": "fixed",
"channels_limit": "fixed",
"compensation": "fixed",
"data_augmentation": "fixed",
"deterministic": "fixed",
"diagnoses": "fixed",
"epochs": "fixed",
"evaluation_steps": "fixed",
"gpu": "fixed",
"label": "fixed",
"learning_rate": "fixed",
"mode": "fixed",
"multi_cohort": "fixed",
"n_splits": "fixed",
"n_proc": "fixed",
"network_task": "fixed",
"normalize": "fixed",
"optimizer": "fixed",
"patience": "fixed",
"preprocessing_dict": "fixed",
"sampler": "fixed",
"seed": "fixed",
"selection_metrics": "fixed",
"size_reduction": "fixed",
"size_reduction_factor": "fixed",
"split": "fixed",
"tolerance": "fixed",
"transfer_path": "fixed",
"transfer_selection_metric": "fixed",
"tsv_path": "fixed",
"wd_bool": "fixed",
"weight_decay": "fixed",
# VAE architecture
"first_layer_channels": "choice",
"n_conv_encoder": "randint",
"feature_size": "choice",
"latent_space_size": "choice",
"n_conv_decoder": "randint",
"last_layer_channels": "choice",
"last_layer_conv": "choice",
}

for name, sampling_type in sampling_vae_dict.items():
if name in space_dict:
sampled_value = sampling_fn(space_dict[name], sampling_type)
parameters[name] = sampled_value

return parameters
89 changes: 86 additions & 3 deletions clinicadl/resources/config/train_config.toml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ multi_network = false
dropout = 0.0 # between 0 and 1
# architecture.n_layers = 4
# VAE
first_layer_channels = 32
n_conv_encoder = 4
feature_size = 0
latent_space_size = 128
feature_size = 1024
n_conv = 4
io_layer_channels = 8
n_conv_decoder = 4
last_layer_channels = 32
last_layer_conv = false
recons_weight = 1
kl_weight = 1
normalization = "batch"
Expand All @@ -34,6 +37,86 @@ loss = "MSELoss"
selection_metrics = ["loss"]
loss = "MSELoss"

[Pythae]
# Beta VAE
beta=5
# Linear Normalizing Flow VAE
flows=['Planar', 'Radial', 'Planar']
# Inverse Autoregressive Flows
n_made_blocks=2
n_hidden_in_made=3
hidden_size=128
# Disentangled Beta VAE
#beta=5
C=30.0
warmup_epoch=25
# Factor VAE
gamma=10
# Beta TC VAE
#beta=2.
alpha=1
#gamma=1
# MS SSIM VAE
#beta=1e-2
window_size=3
# Info VAE
kernel_choice='imq'
#alpha=-2
lbd=10
kernel_bandwidth=1
# Wasserstein Autoencoder
#kernel_choice='imq'
reg_weight=100
#kernel_bandwidth=2
# Hyperspherical VAE
# Poincare VAE
reconstruction_loss="bce"
prior_distribution="riemannian_normal"
posterior_distribution="wrapped_normal"
curvature=0.7
# Adversarial AE
adversarial_loss_scale=0.9
# VAE GAN
#adversarial_loss_scale=0.8
reconstruction_layer=3
margin=0.4
equilibrium= 0.68
# VQ VAE
commitment_loss_factor=0.25
quantization_loss_factor=1.0
num_embeddings=128
use_ema=true
decay=0.99
# Hamiltonian VAE
n_lf=1
eps_lf=0.001
beta_zero=0.3
# Riemannian Hamiltonian VAE
#n_lf=1
#eps_lf=0.001
#beta_zero=0.3
temperature=1.5
regularization=0.001
# Importance Weighted Autoencoder
number_samples=3
# Multiply Importance Weighted Autoencoder
number_gradient_estimates=4
#number_samples=4
# Partially Importance Weighted Autoencoder
#number_gradient_estimates=4
#number_samples=4
# Combination Importance Weighted Autoencoder
#beta=0.05
#number_samples=4
# VAMP Autoencoder
number_components=50
# Regularized AE with L2 decoder param
embedding_weight=1e-2
#reg_weight=1e-4
# Regularized AE with gradient penalty
#embedding_weight=1e-2
#reg_weight=1e-4

[Computational]
gpu = true
n_proc = 2
Expand Down
Loading