|
| 1 | +import functools |
| 2 | + |
| 3 | +from keras import layers |
| 4 | + |
| 5 | +from keras_hub.src.api_export import keras_hub_export |
| 6 | +from keras_hub.src.models.backbone import Backbone |
| 7 | +from keras_hub.src.utils.keras_utils import standardize_data_format |
| 8 | + |
| 9 | + |
| 10 | +@keras_hub_export("keras_hub.models.XceptionBackbone") |
| 11 | +class XceptionBackbone(Backbone): |
| 12 | + """Xception core network with hyperparameters. |
| 13 | +
|
| 14 | + This class implements a Xception backbone as described in |
| 15 | + [Xception: Deep Learning with Depthwise Separable Convolutions](https://arxiv.org/abs/1610.02357). |
| 16 | +
|
| 17 | + Most users will want the pretrained presets available with this model. If |
| 18 | + you are creating a custom backbone, this model provides customizability |
| 19 | + through the `stackwise_conv_filters` and `stackwise_pooling` arguments. This |
| 20 | + backbone assumes the same basic structure as the original Xception mode: |
| 21 | + * Residuals and pre-activation everywhere but the first and last block. |
| 22 | + * Conv layers for the first block only, separable conv layers elsewhere. |
| 23 | +
|
| 24 | + Args: |
| 25 | + stackwise_conv_filters: list of list of ints. Each outermost list |
| 26 | + entry represents a block, and each innermost list entry a conv |
| 27 | + layer. The integer value specifies the number of filters for the |
| 28 | + conv layer. |
| 29 | + stackwise_pooling: list of bools. A list of booleans per block, where |
| 30 | + each entry is true if the block should includes a max pooling layer |
| 31 | + and false if it should not. |
| 32 | + image_shape: tuple. The input shape without the batch size. |
| 33 | + Defaults to `(None, None, 3)`. |
| 34 | + data_format: `None` or str. If specified, either `"channels_last"` or |
| 35 | + `"channels_first"`. If unspecified, the Keras default will be used. |
| 36 | + dtype: `None` or str or `keras.mixed_precision.DTypePolicy`. The dtype |
| 37 | + to use for the model's computations and weights. |
| 38 | +
|
| 39 | + Examples: |
| 40 | + ```python |
| 41 | + input_data = np.random.uniform(0, 1, size=(2, 224, 224, 3)) |
| 42 | +
|
| 43 | + # Pretrained Xception backbone. |
| 44 | + model = keras_hub.models.Backbone.from_preset("exception_41_imagenet") |
| 45 | + model(input_data) |
| 46 | +
|
| 47 | + # Randomly initialized Xception backbone with a custom config. |
| 48 | + model = keras_hub.models.XceptionBackbone( |
| 49 | + stackwise_conv_filters=[[32, 64], [64, 128], [256, 256]], |
| 50 | + stackwise_pooling=[True, True, False], |
| 51 | + ) |
| 52 | + model(input_data) |
| 53 | + ``` |
| 54 | + """ |
| 55 | + |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + stackwise_conv_filters, |
| 59 | + stackwise_pooling, |
| 60 | + image_shape=(None, None, 3), |
| 61 | + data_format=None, |
| 62 | + dtype=None, |
| 63 | + **kwargs, |
| 64 | + ): |
| 65 | + if len(stackwise_conv_filters) != len(stackwise_pooling): |
| 66 | + raise ValueError("All stackwise args should have the same length.") |
| 67 | + |
| 68 | + data_format = standardize_data_format(data_format) |
| 69 | + channel_axis = -1 if data_format == "channels_last" else 1 |
| 70 | + num_blocks = len(stackwise_conv_filters) |
| 71 | + |
| 72 | + # Layer shorcuts with common args. |
| 73 | + norm = functools.partial( |
| 74 | + layers.BatchNormalization, |
| 75 | + axis=channel_axis, |
| 76 | + dtype=dtype, |
| 77 | + ) |
| 78 | + act = functools.partial( |
| 79 | + layers.Activation, |
| 80 | + activation="relu", |
| 81 | + dtype=dtype, |
| 82 | + ) |
| 83 | + conv = functools.partial( |
| 84 | + layers.Conv2D, |
| 85 | + kernel_size=(3, 3), |
| 86 | + use_bias=False, |
| 87 | + data_format=data_format, |
| 88 | + dtype=dtype, |
| 89 | + ) |
| 90 | + sep_conv = functools.partial( |
| 91 | + layers.SeparableConv2D, |
| 92 | + kernel_size=(3, 3), |
| 93 | + padding="same", |
| 94 | + use_bias=False, |
| 95 | + data_format=data_format, |
| 96 | + dtype=dtype, |
| 97 | + ) |
| 98 | + point_conv = functools.partial( |
| 99 | + layers.Conv2D, |
| 100 | + kernel_size=(1, 1), |
| 101 | + strides=(2, 2), |
| 102 | + padding="same", |
| 103 | + use_bias=False, |
| 104 | + data_format=data_format, |
| 105 | + dtype=dtype, |
| 106 | + ) |
| 107 | + pool = functools.partial( |
| 108 | + layers.MaxPool2D, |
| 109 | + pool_size=(3, 3), |
| 110 | + strides=(2, 2), |
| 111 | + padding="same", |
| 112 | + data_format=data_format, |
| 113 | + dtype=dtype, |
| 114 | + ) |
| 115 | + |
| 116 | + # === Functional Model === |
| 117 | + image_input = layers.Input(shape=image_shape) |
| 118 | + x = image_input # Intermediate result. |
| 119 | + |
| 120 | + # Iterate through the blocks. |
| 121 | + for block_i in range(num_blocks): |
| 122 | + first_block, last_block = block_i == 0, block_i == num_blocks - 1 |
| 123 | + block_filters = stackwise_conv_filters[block_i] |
| 124 | + use_pooling = stackwise_pooling[block_i] |
| 125 | + |
| 126 | + # Save the block input as a residual. |
| 127 | + residual = x |
| 128 | + for conv_i, filters in enumerate(block_filters): |
| 129 | + # First block has post activation and strides on first conv. |
| 130 | + if first_block: |
| 131 | + prefix = f"block{block_i + 1}_conv{conv_i + 1}" |
| 132 | + strides = (2, 2) if conv_i == 0 else (1, 1) |
| 133 | + x = conv(filters, strides=strides, name=prefix)(x) |
| 134 | + x = norm(name=f"{prefix}_bn")(x) |
| 135 | + x = act(name=f"{prefix}_act")(x) |
| 136 | + # Last block has post activation. |
| 137 | + elif last_block: |
| 138 | + prefix = f"block{block_i + 1}_sepconv{conv_i + 1}" |
| 139 | + x = sep_conv(filters, name=prefix)(x) |
| 140 | + x = norm(name=f"{prefix}_bn")(x) |
| 141 | + x = act(name=f"{prefix}_act")(x) |
| 142 | + else: |
| 143 | + prefix = f"block{block_i + 1}_sepconv{conv_i + 1}" |
| 144 | + # The first conv in second block has no activation. |
| 145 | + if block_i != 1 or conv_i != 0: |
| 146 | + x = act(name=f"{prefix}_act")(x) |
| 147 | + x = sep_conv(filters, name=prefix)(x) |
| 148 | + x = norm(name=f"{prefix}_bn")(x) |
| 149 | + |
| 150 | + # Optional block pooling. |
| 151 | + if use_pooling: |
| 152 | + x = pool(name=f"block{block_i + 1}_pool")(x) |
| 153 | + |
| 154 | + # Sum residual, first and last block do not have a residual. |
| 155 | + if not first_block and not last_block: |
| 156 | + prefix = f"block{block_i + 1}_residual" |
| 157 | + filters = x.shape[channel_axis] |
| 158 | + # Match filters with a pointwise conv if needed. |
| 159 | + if filters != residual.shape[channel_axis]: |
| 160 | + residual = point_conv(filters, name=f"{prefix}_conv")( |
| 161 | + residual |
| 162 | + ) |
| 163 | + residual = norm(name=f"{prefix}_bn")(residual) |
| 164 | + x = layers.Add(name=f"{prefix}_add", dtype=dtype)([x, residual]) |
| 165 | + |
| 166 | + super().__init__( |
| 167 | + inputs=image_input, |
| 168 | + outputs=x, |
| 169 | + dtype=dtype, |
| 170 | + **kwargs, |
| 171 | + ) |
| 172 | + |
| 173 | + # === Config === |
| 174 | + self.stackwise_conv_filters = stackwise_conv_filters |
| 175 | + self.stackwise_pooling = stackwise_pooling |
| 176 | + self.image_shape = image_shape |
| 177 | + self.data_format = data_format |
| 178 | + |
| 179 | + def get_config(self): |
| 180 | + config = super().get_config() |
| 181 | + config.update( |
| 182 | + { |
| 183 | + "stackwise_conv_filters": self.stackwise_conv_filters, |
| 184 | + "stackwise_pooling": self.stackwise_pooling, |
| 185 | + "image_shape": self.image_shape, |
| 186 | + } |
| 187 | + ) |
| 188 | + return config |
0 commit comments