From 072aa9e8273cc6d1faeea0985dd839ea84c5d6ff Mon Sep 17 00:00:00 2001 From: Calvin Janitra Halim Date: Tue, 28 Sep 2021 23:37:59 +0900 Subject: [PATCH 1/8] Add requirements --- setup/requirements.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/setup/requirements.txt b/setup/requirements.txt index 7b9cc5c..3da43ae 100644 --- a/setup/requirements.txt +++ b/setup/requirements.txt @@ -5,4 +5,6 @@ nibabel==3.2.1 pandas==1.1.4 pydicom==2.1.0 scikit-image==0.17.2 -scikit-learn==0.23.2 \ No newline at end of file +scikit-learn==0.23.2 +efficientnet-3D==1.0.1 +Keras-Applications==1.0.8 \ No newline at end of file From 588058129481ab4c7a97995b743ca1aac15c4b95 Mon Sep 17 00:00:00 2001 From: Calvin Janitra Halim Date: Wed, 29 Sep 2021 01:07:16 +0900 Subject: [PATCH 2/8] Add efficientnet 2D and 3D models --- models/__init__.py | 1 + models/efficientnet.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 models/efficientnet.py diff --git a/models/__init__.py b/models/__init__.py index 34ec76e..dfce67c 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -9,4 +9,5 @@ def find_model(model_name): # Import exposed model functions from models.baseline import baseline +from models.efficientnet import efficientnet from models.ensemble import ensemble_model diff --git a/models/efficientnet.py b/models/efficientnet.py new file mode 100644 index 0000000..a917004 --- /dev/null +++ b/models/efficientnet.py @@ -0,0 +1,35 @@ +from tensorflow import keras +import efficientnet_3D.tfkeras as efn + +def efficientnet(input_shape, n_class=2, n_filters=64, **kwargs): + """Baseline model from the paper https://arxiv.org/abs/2007.13224""" + if len(input_shape) == 4: + effnet_layer = efn.EfficientNetB0( + include_top=True, + weights=None, + input_shape=input_shape, + pooling=None, + classes=2, + ) + modal = '3D' + elif len(input_shape) == 3: + effnet_layer = keras.applications.EfficientNetB0( + include_top=True, + weights=None, + input_shape=input_shape, + pooling=None, + classes=2, + classifier_activation='softmax' + ) + modal = '2D' + else: + raise ValueError('input_shape is expected as an array ranked 3 or 4') + + inputs = keras.Input(input_shape) + + outputs = effnet_layer(inputs) + + if modal == '3D': + outputs = keras.layers.Softmax()(outputs) + + return keras.Model(inputs, outputs, name=f'efficientnet_{modal}') From ebf9618d1b5a1c12324dbb9f9153089c366ced3b Mon Sep 17 00:00:00 2001 From: Calvin Janitra Halim Date: Wed, 29 Sep 2021 01:09:44 +0900 Subject: [PATCH 3/8] EfficientNet execution test --- default_params.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/default_params.json b/default_params.json index 4e4d482..6d752e8 100644 --- a/default_params.json +++ b/default_params.json @@ -1,15 +1,15 @@ { - "name": "Ensemble Learning 256 with aux", + "name": "EfficientNet 3D 256 with aux", "seed": 7171, "data": { "dirname": "data/orig", "val_size": 0.2, "batch_size": 1, "volume_size": [256, 256, 64], - "seq_type": "ALL" + "seq_type": "FLAIR" }, "model": { - "name": "ensemble_model", + "name": "efficientnet", "use_aux": true }, "train": { @@ -18,5 +18,5 @@ "decay_rate": 0.96, "epoch": 100 }, - "desc": "Ensemble learning experiment with 256x256x64 voxel size, with aux out." + "desc": "EfficientNet 3D experiment with 256x256x64 voxel size, with aux out." } From 6aef72cf14760797c7e4cd0f2a5160fd5cd400d2 Mon Sep 17 00:00:00 2001 From: Calvin Janitra Halim Date: Wed, 29 Sep 2021 01:21:35 +0900 Subject: [PATCH 4/8] Update description --- models/efficientnet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/efficientnet.py b/models/efficientnet.py index a917004..634ec6f 100644 --- a/models/efficientnet.py +++ b/models/efficientnet.py @@ -2,7 +2,7 @@ import efficientnet_3D.tfkeras as efn def efficientnet(input_shape, n_class=2, n_filters=64, **kwargs): - """Baseline model from the paper https://arxiv.org/abs/2007.13224""" + """EfficientNet model from the paper https://arxiv.org/abs/1905.11946""" if len(input_shape) == 4: effnet_layer = efn.EfficientNetB0( include_top=True, From 355e07c89e4919fcddb1cb77a8b616c8e523ce77 Mon Sep 17 00:00:00 2001 From: Calvin Janitra Halim Date: Wed, 29 Sep 2021 21:49:58 +0900 Subject: [PATCH 5/8] Fix requirements.txt alphabetical order --- setup/requirements.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup/requirements.txt b/setup/requirements.txt index 3da43ae..878eafb 100644 --- a/setup/requirements.txt +++ b/setup/requirements.txt @@ -1,10 +1,10 @@ +efficientnet-3D==1.0.1 imageio==2.9.0 jupyter +Keras-Applications==1.0.8 matplotlib==3.3.2 nibabel==3.2.1 pandas==1.1.4 pydicom==2.1.0 scikit-image==0.17.2 -scikit-learn==0.23.2 -efficientnet-3D==1.0.1 -Keras-Applications==1.0.8 \ No newline at end of file +scikit-learn==0.23.2 \ No newline at end of file From 00cc10a81ea59337365a6238d63f71bb68c615c8 Mon Sep 17 00:00:00 2001 From: Calvin Janitra Halim Date: Wed, 29 Sep 2021 22:01:44 +0900 Subject: [PATCH 6/8] Refactor, Delete unnecessary params --- models/efficientnet.py | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/models/efficientnet.py b/models/efficientnet.py index 634ec6f..fb196fc 100644 --- a/models/efficientnet.py +++ b/models/efficientnet.py @@ -1,35 +1,27 @@ from tensorflow import keras import efficientnet_3D.tfkeras as efn -def efficientnet(input_shape, n_class=2, n_filters=64, **kwargs): +def efficientnet(input_shape, n_class=2, variant='B0', **kwargs): """EfficientNet model from the paper https://arxiv.org/abs/1905.11946""" + default_effnet_params = { + 'include_top': True, + 'weights': None, + 'input_shape': input_shape, + 'pooling': None, + 'classes': n_class, + } if len(input_shape) == 4: - effnet_layer = efn.EfficientNetB0( - include_top=True, - weights=None, - input_shape=input_shape, - pooling=None, - classes=2, - ) + effnet_layer = getattr(efn, f'EfficientNet{variant}')(**default_effnet_params) modal = '3D' elif len(input_shape) == 3: - effnet_layer = keras.applications.EfficientNetB0( - include_top=True, - weights=None, - input_shape=input_shape, - pooling=None, - classes=2, - classifier_activation='softmax' + effnet_layer = getattr(keras.applications, f'EfficientNet{variant}')( + classifier_activation='softmax', **default_effnet_params ) modal = '2D' else: raise ValueError('input_shape is expected as an array ranked 3 or 4') inputs = keras.Input(input_shape) - outputs = effnet_layer(inputs) - if modal == '3D': - outputs = keras.layers.Softmax()(outputs) - - return keras.Model(inputs, outputs, name=f'efficientnet_{modal}') + return keras.Model(inputs, outputs, name=f'efficientnet_{modal}_{variant}') From c4ee7d7457208088ef3a39e3f3b9778706ef9d2e Mon Sep 17 00:00:00 2001 From: Calvin Janitra Halim Date: Wed, 29 Sep 2021 22:18:07 +0900 Subject: [PATCH 7/8] Add try expect --- models/efficientnet.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/models/efficientnet.py b/models/efficientnet.py index fb196fc..e644a3d 100644 --- a/models/efficientnet.py +++ b/models/efficientnet.py @@ -10,16 +10,20 @@ def efficientnet(input_shape, n_class=2, variant='B0', **kwargs): 'pooling': None, 'classes': n_class, } - if len(input_shape) == 4: - effnet_layer = getattr(efn, f'EfficientNet{variant}')(**default_effnet_params) - modal = '3D' - elif len(input_shape) == 3: - effnet_layer = getattr(keras.applications, f'EfficientNet{variant}')( - classifier_activation='softmax', **default_effnet_params - ) - modal = '2D' - else: - raise ValueError('input_shape is expected as an array ranked 3 or 4') + try: + if len(input_shape) == 4: + effnet_layer = getattr(efn, f'EfficientNet{variant}')(**default_effnet_params) + modal = '3D' + elif len(input_shape) == 3: + effnet_layer = getattr(keras.applications, f'EfficientNet{variant}')( + classifier_activation='softmax', **default_effnet_params + ) + modal = '2D' + else: + raise ValueError('input_shape is expected as an array ranked 3 or 4') + except AttributeError: + print(f'No EfficientNet variation found for {variant}') + raise SystemExit inputs = keras.Input(input_shape) outputs = effnet_layer(inputs) From b13ba7f2ecda4116faa44ee2fd4bcb884d6e46c7 Mon Sep 17 00:00:00 2001 From: magnusbarata Date: Thu, 30 Sep 2021 03:44:22 +0900 Subject: [PATCH 8/8] EXP-300 - Fix efficientnet.py docstring --- default_params.json | 12 ++++++------ models/efficientnet.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/default_params.json b/default_params.json index 6d752e8..c18637b 100644 --- a/default_params.json +++ b/default_params.json @@ -1,22 +1,22 @@ { - "name": "EfficientNet 3D 256 with aux", + "name": "EfficientNetB0 3D 256", "seed": 7171, + "ensemble": true, "data": { "dirname": "data/orig", "val_size": 0.2, "batch_size": 1, - "volume_size": [256, 256, 64], - "seq_type": "FLAIR" + "volume_size": [256, 256, 64] }, "model": { "name": "efficientnet", - "use_aux": true + "variant": "B0" }, "train": { "lr": 1e-4, - "decay_steps": 1000, + "decay_steps": 100000, "decay_rate": 0.96, "epoch": 100 }, - "desc": "EfficientNet 3D experiment with 256x256x64 voxel size, with aux out." + "desc": "EfficientNetB0 3D experiment with 256x256x64 voxel size." } diff --git a/models/efficientnet.py b/models/efficientnet.py index e644a3d..6eeee57 100644 --- a/models/efficientnet.py +++ b/models/efficientnet.py @@ -2,7 +2,17 @@ import efficientnet_3D.tfkeras as efn def efficientnet(input_shape, n_class=2, variant='B0', **kwargs): - """EfficientNet model from the paper https://arxiv.org/abs/1905.11946""" + """EfficientNet model from the paper https://arxiv.org/abs/1905.11946 + + Args: + input_shape: Shape of the input tensor, not including the `batch_size` dimension. + n_class: The number of class to be predicted. + variant: The variant of efficient net. Can be `B0`~`B7` for both 2D and 3D. + `L2` is available for 3D version only. + + Returns: + A `keras.Model` instance. + """ default_effnet_params = { 'include_top': True, 'weights': None,