From 550e56a08d79e7e1b3967110a0ecde3dbb7287bd Mon Sep 17 00:00:00 2001 From: DoubleZ Date: Thu, 14 Oct 2021 09:16:30 +0900 Subject: [PATCH] Implement 2D multi channnel network --- models/__init__.py | 1 + models/multi_channel.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 models/multi_channel.py diff --git a/models/__init__.py b/models/__init__.py index 34ec76e..850f7a3 100644 --- a/models/__init__.py +++ b/models/__init__.py @@ -10,3 +10,4 @@ def find_model(model_name): # Import exposed model functions from models.baseline import baseline from models.ensemble import ensemble_model +from models.multi_channel import multi_channel diff --git a/models/multi_channel.py b/models/multi_channel.py new file mode 100644 index 0000000..957182b --- /dev/null +++ b/models/multi_channel.py @@ -0,0 +1,36 @@ +from tensorflow import keras + +def multi_channel(input_shape, n_class=2, **kwargs): + """2D multi_channel model""" + + if len(input_shape) != 4: + raise ValueError('input_shape is expected as an array ranked 4') + + inputs = keras.Input(input_shape) + + new_dim = tuple([x for x in inputs.shape.as_list() if x != 1 and x is not None]) + reshape_layer = keras.layers.Reshape(new_dim)(inputs) + + x = keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu')(reshape_layer) + x = keras.layers.MaxPool2D(pool_size=2)(x) + x = keras.layers.BatchNormalization()(x) + + x = keras.layers.Conv2D(filters=64, kernel_size=3, activation='relu')(x) + x = keras.layers.MaxPool2D(pool_size=2)(x) + x = keras.layers.BatchNormalization()(x) + + x = keras.layers.Conv2D(filters=128, kernel_size=3, activation='relu')(x) + x = keras.layers.MaxPool2D(pool_size=2)(x) + x = keras.layers.BatchNormalization()(x) + + x = keras.layers.Conv2D(filters=256, kernel_size=3, activation='relu')(x) + x = keras.layers.MaxPool2D(pool_size=2)(x) + x = keras.layers.BatchNormalization()(x) + + x = keras.layers.GlobalAveragePooling2D()(x) + x = keras.layers.Dense(units=512, activation='relu')(x) + x = keras.layers.Dropout(0.3)(x) + + outputs = keras.layers.Dense(n_class, activation='softmax')(x) + + return keras.Model(inputs, outputs, name=f'2D_multi_channel')