Skip to content

Commit

Permalink
Make ISIC multimodal again (#168)
Browse files Browse the repository at this point in the history
* First steps in adding meta data into ISIC pipeline - currently working

* added aug for data

* added one-hot augmentation

* changed runner to run mm

* format changes and ops relocations

* added new ops file

* docu

* docu and style

* restore notebook as master

* added docu

* format

* remove comments

* remote test run

* solved PL warning

* test remote run

* test run remote

* final edits

* speedup cmmd test

* use OpRandApply for prob

* epoch and batch size

* addressed moshiko's CR

* added op to readme

* test run ci

* added reset split as a dynamic arg

* format

* fixed test run issue

* final small changes

* final changes +1

* modified jenkins run

* switched to v100 GPU

* changed python ver back to 3.8
  • Loading branch information
SagiPolaczek authored Sep 21, 2022
1 parent 1d0ef10 commit c72ab31
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 65 deletions.
7 changes: 4 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ ignore =

# Files to exclude:
exclude =
# ignore all tests
test_*
# ignore local examples source files
# local private runs
_examples

# TEMP - ignore all tests
test_*

## General Excludes (TEMP, double check)
# .py files
__init__.py
Expand Down
5 changes: 4 additions & 1 deletion .mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ follow_imports = skip

exclude = (?x)(
^(
##### local examples runs
_examples/

##### examples
examples/setup.py
| examples/setup.py
| examples/fuse_examples/imaging/classification/bright
| examples/fuse_examples/imaging/classification/cmmd
| examples/fuse_examples/imaging/classification/knight
Expand Down
67 changes: 51 additions & 16 deletions examples/fuse_examples/imaging/classification/isic/isic_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os
import copy
import logging
from typing import OrderedDict
from typing import OrderedDict, Sequence, Tuple

import torch
import torch.optim as optim
Expand Down Expand Up @@ -65,40 +65,56 @@
debug = FuseDebug(mode)

##########################################
# Output Paths
# GPUs
##########################################

NUM_GPUS = 1
ROOT = "_examples/isic/"
model_dir = os.path.join(ROOT, "model_dir")

##########################################
# Modality
##########################################

multimodality = False # Set: 'False' to use only imaging, 'True' to use imaging & meta-data

##########################################
# Output Paths
##########################################


ROOT = "./_examples/isic/"
DATA = os.path.join(ROOT, "data_dir")
modality = "multimodality" if multimodality else "imaging"
model_dir = os.path.join(ROOT, f"model_dir_{modality}_final")
PATHS = {
"model_dir": model_dir,
"inference_dir": os.path.join(model_dir, "infer_dir"),
"eval_dir": os.path.join(model_dir, "eval_dir"),
"data_dir": os.path.join(ROOT, "data_dir"),
"data_dir": DATA,
"cache_dir": os.path.join(ROOT, "cache_dir"),
"data_split_filename": os.path.join(ROOT, "isic_split.pkl"),
"data_split_filename": os.path.join(model_dir, "isic_split.pkl"),
}


##########################################
# Train Common Params
##########################################
TRAIN_COMMON_PARAMS = {}
# ============
# Data
# ============
TRAIN_COMMON_PARAMS["data.batch_size"] = 8
TRAIN_COMMON_PARAMS["data.batch_size"] = 32
TRAIN_COMMON_PARAMS["data.train_num_workers"] = 8
TRAIN_COMMON_PARAMS["data.validation_num_workers"] = 8
TRAIN_COMMON_PARAMS["data.num_folds"] = 5
TRAIN_COMMON_PARAMS["data.train_folds"] = [0, 1, 2]
TRAIN_COMMON_PARAMS["data.validation_folds"] = [3]
TRAIN_COMMON_PARAMS["data.samples_ids"] = FULL_GOLDEN_MEMBERS # Change to None to use all members
TRAIN_COMMON_PARAMS["data.samples_ids"] = {"all": None, "golden": FULL_GOLDEN_MEMBERS}["all"]


# ===============
# PL Trainer
# ===============
TRAIN_COMMON_PARAMS["trainer.num_epochs"] = 20
TRAIN_COMMON_PARAMS["trainer.num_epochs"] = 30
TRAIN_COMMON_PARAMS["trainer.num_devices"] = NUM_GPUS
TRAIN_COMMON_PARAMS["trainer.accelerator"] = "gpu"
TRAIN_COMMON_PARAMS["trainer.ckpt_path"] = None
Expand All @@ -112,10 +128,20 @@
# ===============
# Model
# ===============
TRAIN_COMMON_PARAMS["model"] = dict(dropout_rate=0.5)


def create_model(dropout_rate: float) -> torch.nn.Module:
TRAIN_COMMON_PARAMS["model"] = dict(
dropout_rate=0.5,
layers_description=(256,),
tabular_data_inputs=[("data.input.clinical.all", 19)] if multimodality else None,
tabular_layers_description=(128,) if multimodality else tuple(),
)


def create_model(
dropout_rate: float,
layers_description: Sequence[int],
tabular_data_inputs: Sequence[Tuple[str, int]],
tabular_layers_description: Sequence[int],
) -> torch.nn.Module:
"""
creates the model
"""
Expand All @@ -130,6 +156,9 @@ def create_model(dropout_rate: float) -> torch.nn.Module:
head_name="head_0",
dropout_rate=dropout_rate,
conv_inputs=[("model.backbone_features", 1536)],
tabular_data_inputs=tabular_data_inputs,
layers_description=layers_description,
tabular_layers_description=tabular_layers_description,
num_classes=8,
pooling="avg",
),
Expand Down Expand Up @@ -158,11 +187,10 @@ def run_train(paths: dict, train_common_params: dict) -> None:
# Train Data
lgr.info("Train Data:", {"attrs": "bold"})

# split to folds randomly - temp
# split to folds randomly
all_dataset = ISIC.dataset(
paths["data_dir"],
paths["cache_dir"],
reset_cache=False,
num_workers=train_common_params["data.train_num_workers"],
samples_ids=train_common_params["data.samples_ids"],
)
Expand All @@ -181,7 +209,13 @@ def run_train(paths: dict, train_common_params: dict) -> None:
for fold in train_common_params["data.validation_folds"]:
validation_sample_ids += folds[fold]

train_dataset = ISIC.dataset(paths["data_dir"], paths["cache_dir"], samples_ids=train_sample_ids, train=True)
train_dataset = ISIC.dataset(
paths["data_dir"],
paths["cache_dir"],
samples_ids=train_sample_ids,
train=True,
num_workers=train_common_params["data.train_num_workers"],
)

lgr.info("- Create sampler:")
sampler = BatchSamplerDefault(
Expand Down Expand Up @@ -361,6 +395,7 @@ def run_infer(paths: dict, infer_common_params: dict) -> None:
accelerator=infer_common_params["trainer.accelerator"],
devices=infer_common_params["trainer.num_devices"],
auto_select_gpus=True,
max_epochs=0,
)
predictions = pl_trainer.predict(pl_module, infer_dataloader, return_predictions=True)

Expand Down
10 changes: 5 additions & 5 deletions examples/fuse_examples/tests/test_classification_cmmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ def run_cmmd(root: str) -> None:
"target": "classification",
"reset_cache": False,
"num_workers": 10,
"num_folds": 5,
"train_folds": [0, 1, 2],
"validation_folds": [3],
"num_folds": 10,
"train_folds": [0],
"validation_folds": [1],
"batch_size": 2,
"learning_rate": 0.0001,
"weight_decay": 0,
"resume_checkpoint_filename": None,
"trainer": {"accelerator": "gpu", "devices": 1, "num_epochs": 2, "ckpt_path": None},
"trainer": {"accelerator": "gpu", "devices": 1, "num_epochs": 1, "ckpt_path": None},
},
"infer": {
"infer_filename": "validation_set_infer.gz",
"checkpoint": "best_epoch.ckpt",
"infer_folds": [4],
"infer_folds": [2],
"target": "classification",
"num_workers": 10,
},
Expand Down
9 changes: 6 additions & 3 deletions examples/fuse_examples/tests/test_classification_isic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
run_train,
run_infer,
run_eval,
PATHS,
)
from fuse_examples.imaging.classification.isic.golden_members import FULL_GOLDEN_MEMBERS

Expand All @@ -51,11 +50,15 @@ def run_isic(root: str) -> None:
"data_split_filename": os.path.join(root, "split.pkl"),
}

dd = paths["data_dir"]
print(f"data_dir={dd}")

train_common_params = TRAIN_COMMON_PARAMS
train_common_params["trainer.num_epochs"] = 2
train_common_params["samples_ids"] = FULL_GOLDEN_MEMBERS
train_common_params["trainer.num_epochs"] = 1
train_common_params["data.samples_ids"] = FULL_GOLDEN_MEMBERS

infer_common_params = INFER_COMMON_PARAMS

eval_common_params = EVAL_COMMON_PARAMS

# Must use GPU due a long running time
Expand Down
Loading

0 comments on commit c72ab31

Please sign in to comment.