Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/keras/src/applications/xception.py: 17%

117 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2016 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 

16"""Xception V1 model for Keras. 

17 

18On ImageNet, this model gets to a top-1 validation accuracy of 0.790 

19and a top-5 validation accuracy of 0.945. 

20 

21Reference: 

22 - [Xception: Deep Learning with Depthwise Separable Convolutions]( 

23 https://arxiv.org/abs/1610.02357) (CVPR 2017) 

24""" 

25 

26import tensorflow.compat.v2 as tf 

27 

28from keras.src import backend 

29from keras.src.applications import imagenet_utils 

30from keras.src.engine import training 

31from keras.src.layers import VersionAwareLayers 

32from keras.src.utils import data_utils 

33from keras.src.utils import layer_utils 

34 

35# isort: off 

36from tensorflow.python.util.tf_export import keras_export 

37 

38TF_WEIGHTS_PATH = ( 

39 "https://storage.googleapis.com/tensorflow/keras-applications/" 

40 "xception/xception_weights_tf_dim_ordering_tf_kernels.h5" 

41) 

42TF_WEIGHTS_PATH_NO_TOP = ( 

43 "https://storage.googleapis.com/tensorflow/keras-applications/" 

44 "xception/xception_weights_tf_dim_ordering_tf_kernels_notop.h5" 

45) 

46 

47layers = VersionAwareLayers() 

48 

49 

50@keras_export( 

51 "keras.applications.xception.Xception", "keras.applications.Xception" 

52) 

53def Xception( 

54 include_top=True, 

55 weights="imagenet", 

56 input_tensor=None, 

57 input_shape=None, 

58 pooling=None, 

59 classes=1000, 

60 classifier_activation="softmax", 

61): 

62 """Instantiates the Xception architecture. 

63 

64 Reference: 

65 - [Xception: Deep Learning with Depthwise Separable Convolutions]( 

66 https://arxiv.org/abs/1610.02357) (CVPR 2017) 

67 

68 For image classification use cases, see 

69 [this page for detailed examples]( 

70 https://keras.io/api/applications/#usage-examples-for-image-classification-models). 

71 

72 For transfer learning use cases, make sure to read the 

73 [guide to transfer learning & fine-tuning]( 

74 https://keras.io/guides/transfer_learning/). 

75 

76 The default input image size for this model is 299x299. 

77 

78 Note: each Keras Application expects a specific kind of input preprocessing. 

79 For Xception, call `tf.keras.applications.xception.preprocess_input` on your 

80 inputs before passing them to the model. 

81 `xception.preprocess_input` will scale input pixels between -1 and 1. 

82 

83 Args: 

84 include_top: whether to include the fully-connected 

85 layer at the top of the network. 

86 weights: one of `None` (random initialization), 

87 'imagenet' (pre-training on ImageNet), 

88 or the path to the weights file to be loaded. 

89 input_tensor: optional Keras tensor 

90 (i.e. output of `layers.Input()`) 

91 to use as image input for the model. 

92 input_shape: optional shape tuple, only to be specified 

93 if `include_top` is False (otherwise the input shape 

94 has to be `(299, 299, 3)`. 

95 It should have exactly 3 inputs channels, 

96 and width and height should be no smaller than 71. 

97 E.g. `(150, 150, 3)` would be one valid value. 

98 pooling: Optional pooling mode for feature extraction 

99 when `include_top` is `False`. 

100 - `None` means that the output of the model will be 

101 the 4D tensor output of the 

102 last convolutional block. 

103 - `avg` means that global average pooling 

104 will be applied to the output of the 

105 last convolutional block, and thus 

106 the output of the model will be a 2D tensor. 

107 - `max` means that global max pooling will 

108 be applied. 

109 classes: optional number of classes to classify images 

110 into, only to be specified if `include_top` is True, 

111 and if no `weights` argument is specified. 

112 classifier_activation: A `str` or callable. The activation function to use 

113 on the "top" layer. Ignored unless `include_top=True`. Set 

114 `classifier_activation=None` to return the logits of the "top" layer. 

115 When loading pretrained weights, `classifier_activation` can only 

116 be `None` or `"softmax"`. 

117 

118 Returns: 

119 A `keras.Model` instance. 

120 """ 

121 if not (weights in {"imagenet", None} or tf.io.gfile.exists(weights)): 

122 raise ValueError( 

123 "The `weights` argument should be either " 

124 "`None` (random initialization), `imagenet` " 

125 "(pre-training on ImageNet), " 

126 "or the path to the weights file to be loaded." 

127 ) 

128 

129 if weights == "imagenet" and include_top and classes != 1000: 

130 raise ValueError( 

131 'If using `weights` as `"imagenet"` with `include_top`' 

132 " as true, `classes` should be 1000" 

133 ) 

134 

135 # Determine proper input shape 

136 input_shape = imagenet_utils.obtain_input_shape( 

137 input_shape, 

138 default_size=299, 

139 min_size=71, 

140 data_format=backend.image_data_format(), 

141 require_flatten=include_top, 

142 weights=weights, 

143 ) 

144 

145 if input_tensor is None: 

146 img_input = layers.Input(shape=input_shape) 

147 else: 

148 if not backend.is_keras_tensor(input_tensor): 

149 img_input = layers.Input(tensor=input_tensor, shape=input_shape) 

150 else: 

151 img_input = input_tensor 

152 

153 channel_axis = 1 if backend.image_data_format() == "channels_first" else -1 

154 

155 x = layers.Conv2D( 

156 32, (3, 3), strides=(2, 2), use_bias=False, name="block1_conv1" 

157 )(img_input) 

158 x = layers.BatchNormalization(axis=channel_axis, name="block1_conv1_bn")(x) 

159 x = layers.Activation("relu", name="block1_conv1_act")(x) 

160 x = layers.Conv2D(64, (3, 3), use_bias=False, name="block1_conv2")(x) 

161 x = layers.BatchNormalization(axis=channel_axis, name="block1_conv2_bn")(x) 

162 x = layers.Activation("relu", name="block1_conv2_act")(x) 

163 

164 residual = layers.Conv2D( 

165 128, (1, 1), strides=(2, 2), padding="same", use_bias=False 

166 )(x) 

167 residual = layers.BatchNormalization(axis=channel_axis)(residual) 

168 

169 x = layers.SeparableConv2D( 

170 128, (3, 3), padding="same", use_bias=False, name="block2_sepconv1" 

171 )(x) 

172 x = layers.BatchNormalization(axis=channel_axis, name="block2_sepconv1_bn")( 

173 x 

174 ) 

175 x = layers.Activation("relu", name="block2_sepconv2_act")(x) 

176 x = layers.SeparableConv2D( 

177 128, (3, 3), padding="same", use_bias=False, name="block2_sepconv2" 

178 )(x) 

179 x = layers.BatchNormalization(axis=channel_axis, name="block2_sepconv2_bn")( 

180 x 

181 ) 

182 

183 x = layers.MaxPooling2D( 

184 (3, 3), strides=(2, 2), padding="same", name="block2_pool" 

185 )(x) 

186 x = layers.add([x, residual]) 

187 

188 residual = layers.Conv2D( 

189 256, (1, 1), strides=(2, 2), padding="same", use_bias=False 

190 )(x) 

191 residual = layers.BatchNormalization(axis=channel_axis)(residual) 

192 

193 x = layers.Activation("relu", name="block3_sepconv1_act")(x) 

194 x = layers.SeparableConv2D( 

195 256, (3, 3), padding="same", use_bias=False, name="block3_sepconv1" 

196 )(x) 

197 x = layers.BatchNormalization(axis=channel_axis, name="block3_sepconv1_bn")( 

198 x 

199 ) 

200 x = layers.Activation("relu", name="block3_sepconv2_act")(x) 

201 x = layers.SeparableConv2D( 

202 256, (3, 3), padding="same", use_bias=False, name="block3_sepconv2" 

203 )(x) 

204 x = layers.BatchNormalization(axis=channel_axis, name="block3_sepconv2_bn")( 

205 x 

206 ) 

207 

208 x = layers.MaxPooling2D( 

209 (3, 3), strides=(2, 2), padding="same", name="block3_pool" 

210 )(x) 

211 x = layers.add([x, residual]) 

212 

213 residual = layers.Conv2D( 

214 728, (1, 1), strides=(2, 2), padding="same", use_bias=False 

215 )(x) 

216 residual = layers.BatchNormalization(axis=channel_axis)(residual) 

217 

218 x = layers.Activation("relu", name="block4_sepconv1_act")(x) 

219 x = layers.SeparableConv2D( 

220 728, (3, 3), padding="same", use_bias=False, name="block4_sepconv1" 

221 )(x) 

222 x = layers.BatchNormalization(axis=channel_axis, name="block4_sepconv1_bn")( 

223 x 

224 ) 

225 x = layers.Activation("relu", name="block4_sepconv2_act")(x) 

226 x = layers.SeparableConv2D( 

227 728, (3, 3), padding="same", use_bias=False, name="block4_sepconv2" 

228 )(x) 

229 x = layers.BatchNormalization(axis=channel_axis, name="block4_sepconv2_bn")( 

230 x 

231 ) 

232 

233 x = layers.MaxPooling2D( 

234 (3, 3), strides=(2, 2), padding="same", name="block4_pool" 

235 )(x) 

236 x = layers.add([x, residual]) 

237 

238 for i in range(8): 

239 residual = x 

240 prefix = "block" + str(i + 5) 

241 

242 x = layers.Activation("relu", name=prefix + "_sepconv1_act")(x) 

243 x = layers.SeparableConv2D( 

244 728, 

245 (3, 3), 

246 padding="same", 

247 use_bias=False, 

248 name=prefix + "_sepconv1", 

249 )(x) 

250 x = layers.BatchNormalization( 

251 axis=channel_axis, name=prefix + "_sepconv1_bn" 

252 )(x) 

253 x = layers.Activation("relu", name=prefix + "_sepconv2_act")(x) 

254 x = layers.SeparableConv2D( 

255 728, 

256 (3, 3), 

257 padding="same", 

258 use_bias=False, 

259 name=prefix + "_sepconv2", 

260 )(x) 

261 x = layers.BatchNormalization( 

262 axis=channel_axis, name=prefix + "_sepconv2_bn" 

263 )(x) 

264 x = layers.Activation("relu", name=prefix + "_sepconv3_act")(x) 

265 x = layers.SeparableConv2D( 

266 728, 

267 (3, 3), 

268 padding="same", 

269 use_bias=False, 

270 name=prefix + "_sepconv3", 

271 )(x) 

272 x = layers.BatchNormalization( 

273 axis=channel_axis, name=prefix + "_sepconv3_bn" 

274 )(x) 

275 

276 x = layers.add([x, residual]) 

277 

278 residual = layers.Conv2D( 

279 1024, (1, 1), strides=(2, 2), padding="same", use_bias=False 

280 )(x) 

281 residual = layers.BatchNormalization(axis=channel_axis)(residual) 

282 

283 x = layers.Activation("relu", name="block13_sepconv1_act")(x) 

284 x = layers.SeparableConv2D( 

285 728, (3, 3), padding="same", use_bias=False, name="block13_sepconv1" 

286 )(x) 

287 x = layers.BatchNormalization( 

288 axis=channel_axis, name="block13_sepconv1_bn" 

289 )(x) 

290 x = layers.Activation("relu", name="block13_sepconv2_act")(x) 

291 x = layers.SeparableConv2D( 

292 1024, (3, 3), padding="same", use_bias=False, name="block13_sepconv2" 

293 )(x) 

294 x = layers.BatchNormalization( 

295 axis=channel_axis, name="block13_sepconv2_bn" 

296 )(x) 

297 

298 x = layers.MaxPooling2D( 

299 (3, 3), strides=(2, 2), padding="same", name="block13_pool" 

300 )(x) 

301 x = layers.add([x, residual]) 

302 

303 x = layers.SeparableConv2D( 

304 1536, (3, 3), padding="same", use_bias=False, name="block14_sepconv1" 

305 )(x) 

306 x = layers.BatchNormalization( 

307 axis=channel_axis, name="block14_sepconv1_bn" 

308 )(x) 

309 x = layers.Activation("relu", name="block14_sepconv1_act")(x) 

310 

311 x = layers.SeparableConv2D( 

312 2048, (3, 3), padding="same", use_bias=False, name="block14_sepconv2" 

313 )(x) 

314 x = layers.BatchNormalization( 

315 axis=channel_axis, name="block14_sepconv2_bn" 

316 )(x) 

317 x = layers.Activation("relu", name="block14_sepconv2_act")(x) 

318 

319 if include_top: 

320 x = layers.GlobalAveragePooling2D(name="avg_pool")(x) 

321 imagenet_utils.validate_activation(classifier_activation, weights) 

322 x = layers.Dense( 

323 classes, activation=classifier_activation, name="predictions" 

324 )(x) 

325 else: 

326 if pooling == "avg": 

327 x = layers.GlobalAveragePooling2D()(x) 

328 elif pooling == "max": 

329 x = layers.GlobalMaxPooling2D()(x) 

330 

331 # Ensure that the model takes into account 

332 # any potential predecessors of `input_tensor`. 

333 if input_tensor is not None: 

334 inputs = layer_utils.get_source_inputs(input_tensor) 

335 else: 

336 inputs = img_input 

337 # Create model. 

338 model = training.Model(inputs, x, name="xception") 

339 

340 # Load weights. 

341 if weights == "imagenet": 

342 if include_top: 

343 weights_path = data_utils.get_file( 

344 "xception_weights_tf_dim_ordering_tf_kernels.h5", 

345 TF_WEIGHTS_PATH, 

346 cache_subdir="models", 

347 file_hash="0a58e3b7378bc2990ea3b43d5981f1f6", 

348 ) 

349 else: 

350 weights_path = data_utils.get_file( 

351 "xception_weights_tf_dim_ordering_tf_kernels_notop.h5", 

352 TF_WEIGHTS_PATH_NO_TOP, 

353 cache_subdir="models", 

354 file_hash="b0042744bf5b25fce3cb969f33bebb97", 

355 ) 

356 model.load_weights(weights_path) 

357 elif weights is not None: 

358 model.load_weights(weights) 

359 

360 return model 

361 

362 

363@keras_export("keras.applications.xception.preprocess_input") 

364def preprocess_input(x, data_format=None): 

365 return imagenet_utils.preprocess_input( 

366 x, data_format=data_format, mode="tf" 

367 ) 

368 

369 

370@keras_export("keras.applications.xception.decode_predictions") 

371def decode_predictions(preds, top=5): 

372 return imagenet_utils.decode_predictions(preds, top=top) 

373 

374 

375preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format( 

376 mode="", 

377 ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF, 

378 error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC, 

379) 

380decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ 

381