Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/applications/resnet_v2.py: 50%
40 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
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# ==============================================================================
16"""ResNet v2 models for Keras.
18Reference:
19 - [Identity Mappings in Deep Residual Networks](
20 https://arxiv.org/abs/1603.05027) (CVPR 2016)
21"""
23from keras.src.applications import imagenet_utils
24from keras.src.applications import resnet
26# isort: off
27from tensorflow.python.util.tf_export import keras_export
30@keras_export(
31 "keras.applications.resnet_v2.ResNet50V2", "keras.applications.ResNet50V2"
32)
33def ResNet50V2(
34 include_top=True,
35 weights="imagenet",
36 input_tensor=None,
37 input_shape=None,
38 pooling=None,
39 classes=1000,
40 classifier_activation="softmax",
41):
42 """Instantiates the ResNet50V2 architecture."""
44 def stack_fn(x):
45 x = resnet.stack2(x, 64, 3, name="conv2")
46 x = resnet.stack2(x, 128, 4, name="conv3")
47 x = resnet.stack2(x, 256, 6, name="conv4")
48 return resnet.stack2(x, 512, 3, stride1=1, name="conv5")
50 return resnet.ResNet(
51 stack_fn,
52 True,
53 True,
54 "resnet50v2",
55 include_top,
56 weights,
57 input_tensor,
58 input_shape,
59 pooling,
60 classes,
61 classifier_activation=classifier_activation,
62 )
65@keras_export(
66 "keras.applications.resnet_v2.ResNet101V2", "keras.applications.ResNet101V2"
67)
68def ResNet101V2(
69 include_top=True,
70 weights="imagenet",
71 input_tensor=None,
72 input_shape=None,
73 pooling=None,
74 classes=1000,
75 classifier_activation="softmax",
76):
77 """Instantiates the ResNet101V2 architecture."""
79 def stack_fn(x):
80 x = resnet.stack2(x, 64, 3, name="conv2")
81 x = resnet.stack2(x, 128, 4, name="conv3")
82 x = resnet.stack2(x, 256, 23, name="conv4")
83 return resnet.stack2(x, 512, 3, stride1=1, name="conv5")
85 return resnet.ResNet(
86 stack_fn,
87 True,
88 True,
89 "resnet101v2",
90 include_top,
91 weights,
92 input_tensor,
93 input_shape,
94 pooling,
95 classes,
96 classifier_activation=classifier_activation,
97 )
100@keras_export(
101 "keras.applications.resnet_v2.ResNet152V2", "keras.applications.ResNet152V2"
102)
103def ResNet152V2(
104 include_top=True,
105 weights="imagenet",
106 input_tensor=None,
107 input_shape=None,
108 pooling=None,
109 classes=1000,
110 classifier_activation="softmax",
111):
112 """Instantiates the ResNet152V2 architecture."""
114 def stack_fn(x):
115 x = resnet.stack2(x, 64, 3, name="conv2")
116 x = resnet.stack2(x, 128, 8, name="conv3")
117 x = resnet.stack2(x, 256, 36, name="conv4")
118 return resnet.stack2(x, 512, 3, stride1=1, name="conv5")
120 return resnet.ResNet(
121 stack_fn,
122 True,
123 True,
124 "resnet152v2",
125 include_top,
126 weights,
127 input_tensor,
128 input_shape,
129 pooling,
130 classes,
131 classifier_activation=classifier_activation,
132 )
135@keras_export("keras.applications.resnet_v2.preprocess_input")
136def preprocess_input(x, data_format=None):
137 return imagenet_utils.preprocess_input(
138 x, data_format=data_format, mode="tf"
139 )
142@keras_export("keras.applications.resnet_v2.decode_predictions")
143def decode_predictions(preds, top=5):
144 return imagenet_utils.decode_predictions(preds, top=top)
147preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format(
148 mode="",
149 ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF,
150 error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC,
151)
152decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__
154DOC = """
156 Reference:
157 - [Identity Mappings in Deep Residual Networks](
158 https://arxiv.org/abs/1603.05027) (CVPR 2016)
160 For image classification use cases, see
161 [this page for detailed examples](
162 https://keras.io/api/applications/#usage-examples-for-image-classification-models).
164 For transfer learning use cases, make sure to read the
165 [guide to transfer learning & fine-tuning](
166 https://keras.io/guides/transfer_learning/).
168 Note: each Keras Application expects a specific kind of input preprocessing.
169 For ResNetV2, call `tf.keras.applications.resnet_v2.preprocess_input` on your
170 inputs before passing them to the model.
171 `resnet_v2.preprocess_input` will scale input pixels between -1 and 1.
173 Args:
174 include_top: whether to include the fully-connected
175 layer at the top of the network.
176 weights: one of `None` (random initialization),
177 'imagenet' (pre-training on ImageNet),
178 or the path to the weights file to be loaded.
179 input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
180 to use as image input for the model.
181 input_shape: optional shape tuple, only to be specified
182 if `include_top` is False (otherwise the input shape
183 has to be `(224, 224, 3)` (with `'channels_last'` data format)
184 or `(3, 224, 224)` (with `'channels_first'` data format).
185 It should have exactly 3 inputs channels,
186 and width and height should be no smaller than 32.
187 E.g. `(200, 200, 3)` would be one valid value.
188 pooling: Optional pooling mode for feature extraction
189 when `include_top` is `False`.
190 - `None` means that the output of the model will be
191 the 4D tensor output of the
192 last convolutional block.
193 - `avg` means that global average pooling
194 will be applied to the output of the
195 last convolutional block, and thus
196 the output of the model will be a 2D tensor.
197 - `max` means that global max pooling will
198 be applied.
199 classes: optional number of classes to classify images
200 into, only to be specified if `include_top` is True, and
201 if no `weights` argument is specified.
202 classifier_activation: A `str` or callable. The activation function to use
203 on the "top" layer. Ignored unless `include_top=True`. Set
204 `classifier_activation=None` to return the logits of the "top" layer.
205 When loading pretrained weights, `classifier_activation` can only
206 be `None` or `"softmax"`.
208 Returns:
209 A `keras.Model` instance.
210"""
212setattr(ResNet50V2, "__doc__", ResNet50V2.__doc__ + DOC)
213setattr(ResNet101V2, "__doc__", ResNet101V2.__doc__ + DOC)
214setattr(ResNet152V2, "__doc__", ResNet152V2.__doc__ + DOC)