1# Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15# pylint: disable=invalid-name 16"""ResNet v2 models for Keras. 17 18Reference: 19 - [Identity Mappings in Deep Residual Networks] 20 (https://arxiv.org/abs/1603.05027) (CVPR 2016) 21""" 22 23from tensorflow.python.keras.applications import imagenet_utils 24from tensorflow.python.keras.applications import resnet 25from tensorflow.python.util.tf_export import keras_export 26 27 28@keras_export('keras.applications.resnet_v2.ResNet50V2', 29 'keras.applications.ResNet50V2') 30def ResNet50V2( 31 include_top=True, 32 weights='imagenet', 33 input_tensor=None, 34 input_shape=None, 35 pooling=None, 36 classes=1000, 37 classifier_activation='softmax'): 38 """Instantiates the ResNet50V2 architecture.""" 39 def stack_fn(x): 40 x = resnet.stack2(x, 64, 3, name='conv2') 41 x = resnet.stack2(x, 128, 4, name='conv3') 42 x = resnet.stack2(x, 256, 6, name='conv4') 43 return resnet.stack2(x, 512, 3, stride1=1, name='conv5') 44 45 return resnet.ResNet( 46 stack_fn, 47 True, 48 True, 49 'resnet50v2', 50 include_top, 51 weights, 52 input_tensor, 53 input_shape, 54 pooling, 55 classes, 56 classifier_activation=classifier_activation) 57 58 59@keras_export('keras.applications.resnet_v2.ResNet101V2', 60 'keras.applications.ResNet101V2') 61def ResNet101V2( 62 include_top=True, 63 weights='imagenet', 64 input_tensor=None, 65 input_shape=None, 66 pooling=None, 67 classes=1000, 68 classifier_activation='softmax'): 69 """Instantiates the ResNet101V2 architecture.""" 70 def stack_fn(x): 71 x = resnet.stack2(x, 64, 3, name='conv2') 72 x = resnet.stack2(x, 128, 4, name='conv3') 73 x = resnet.stack2(x, 256, 23, name='conv4') 74 return resnet.stack2(x, 512, 3, stride1=1, name='conv5') 75 76 return resnet.ResNet( 77 stack_fn, 78 True, 79 True, 80 'resnet101v2', 81 include_top, 82 weights, 83 input_tensor, 84 input_shape, 85 pooling, 86 classes, 87 classifier_activation=classifier_activation) 88 89 90@keras_export('keras.applications.resnet_v2.ResNet152V2', 91 'keras.applications.ResNet152V2') 92def ResNet152V2( 93 include_top=True, 94 weights='imagenet', 95 input_tensor=None, 96 input_shape=None, 97 pooling=None, 98 classes=1000, 99 classifier_activation='softmax'): 100 """Instantiates the ResNet152V2 architecture.""" 101 def stack_fn(x): 102 x = resnet.stack2(x, 64, 3, name='conv2') 103 x = resnet.stack2(x, 128, 8, name='conv3') 104 x = resnet.stack2(x, 256, 36, name='conv4') 105 return resnet.stack2(x, 512, 3, stride1=1, name='conv5') 106 107 return resnet.ResNet( 108 stack_fn, 109 True, 110 True, 111 'resnet152v2', 112 include_top, 113 weights, 114 input_tensor, 115 input_shape, 116 pooling, 117 classes, 118 classifier_activation=classifier_activation) 119 120 121@keras_export('keras.applications.resnet_v2.preprocess_input') 122def preprocess_input(x, data_format=None): 123 return imagenet_utils.preprocess_input( 124 x, data_format=data_format, mode='tf') 125 126 127@keras_export('keras.applications.resnet_v2.decode_predictions') 128def decode_predictions(preds, top=5): 129 return imagenet_utils.decode_predictions(preds, top=top) 130 131 132preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( 133 mode='', 134 ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, 135 error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC) 136decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ 137 138DOC = """ 139 140 Reference: 141 - [Identity Mappings in Deep Residual Networks] 142 (https://arxiv.org/abs/1603.05027) (CVPR 2016) 143 144 For image classification use cases, see 145 [this page for detailed examples]( 146 https://keras.io/api/applications/#usage-examples-for-image-classification-models). 147 148 For transfer learning use cases, make sure to read the 149 [guide to transfer learning & fine-tuning]( 150 https://keras.io/guides/transfer_learning/). 151 152 Note: each Keras Application expects a specific kind of input preprocessing. 153 For ResNetV2, call `tf.keras.applications.resnet_v2.preprocess_input` on your 154 inputs before passing them to the model. 155 `resnet_v2.preprocess_input` will scale input pixels between -1 and 1. 156 157 Args: 158 include_top: whether to include the fully-connected 159 layer at the top of the network. 160 weights: one of `None` (random initialization), 161 'imagenet' (pre-training on ImageNet), 162 or the path to the weights file to be loaded. 163 input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) 164 to use as image input for the model. 165 input_shape: optional shape tuple, only to be specified 166 if `include_top` is False (otherwise the input shape 167 has to be `(224, 224, 3)` (with `'channels_last'` data format) 168 or `(3, 224, 224)` (with `'channels_first'` data format). 169 It should have exactly 3 inputs channels, 170 and width and height should be no smaller than 32. 171 E.g. `(200, 200, 3)` would be one valid value. 172 pooling: Optional pooling mode for feature extraction 173 when `include_top` is `False`. 174 - `None` means that the output of the model will be 175 the 4D tensor output of the 176 last convolutional block. 177 - `avg` means that global average pooling 178 will be applied to the output of the 179 last convolutional block, and thus 180 the output of the model will be a 2D tensor. 181 - `max` means that global max pooling will 182 be applied. 183 classes: optional number of classes to classify images 184 into, only to be specified if `include_top` is True, and 185 if no `weights` argument is specified. 186 classifier_activation: A `str` or callable. The activation function to use 187 on the "top" layer. Ignored unless `include_top=True`. Set 188 `classifier_activation=None` to return the logits of the "top" layer. 189 When loading pretrained weights, `classifier_activation` can only 190 be `None` or `"softmax"`. 191 192 Returns: 193 A `keras.Model` instance. 194""" 195 196setattr(ResNet50V2, '__doc__', ResNet50V2.__doc__ + DOC) 197setattr(ResNet101V2, '__doc__', ResNet101V2.__doc__ + DOC) 198setattr(ResNet152V2, '__doc__', ResNet152V2.__doc__ + DOC) 199