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

163 statements  

« 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# ============================================================================== 

15 

16 

17"""EfficientNet models for Keras. 

18 

19Reference: 

20 - [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks]( 

21 https://arxiv.org/abs/1905.11946) (ICML 2019) 

22""" 

23 

24import copy 

25import math 

26 

27import tensorflow.compat.v2 as tf 

28 

29from keras.src import backend 

30from keras.src.applications import imagenet_utils 

31from keras.src.engine import training 

32from keras.src.layers import VersionAwareLayers 

33from keras.src.utils import data_utils 

34from keras.src.utils import layer_utils 

35 

36# isort: off 

37from tensorflow.python.util.tf_export import keras_export 

38 

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

40 

41WEIGHTS_HASHES = { 

42 "b0": ( 

43 "902e53a9f72be733fc0bcb005b3ebbac", 

44 "50bc09e76180e00e4465e1a485ddc09d", 

45 ), 

46 "b1": ( 

47 "1d254153d4ab51201f1646940f018540", 

48 "74c4e6b3e1f6a1eea24c589628592432", 

49 ), 

50 "b2": ( 

51 "b15cce36ff4dcbd00b6dd88e7857a6ad", 

52 "111f8e2ac8aa800a7a99e3239f7bfb39", 

53 ), 

54 "b3": ( 

55 "ffd1fdc53d0ce67064dc6a9c7960ede0", 

56 "af6d107764bb5b1abb91932881670226", 

57 ), 

58 "b4": ( 

59 "18c95ad55216b8f92d7e70b3a046e2fc", 

60 "ebc24e6d6c33eaebbd558eafbeedf1ba", 

61 ), 

62 "b5": ( 

63 "ace28f2a6363774853a83a0b21b9421a", 

64 "38879255a25d3c92d5e44e04ae6cec6f", 

65 ), 

66 "b6": ( 

67 "165f6e37dce68623721b423839de8be5", 

68 "9ecce42647a20130c1f39a5d4cb75743", 

69 ), 

70 "b7": ( 

71 "8c03f828fec3ef71311cd463b6759d99", 

72 "cbcfe4450ddf6f3ad90b1b398090fe4a", 

73 ), 

74} 

75 

76DEFAULT_BLOCKS_ARGS = [ 

77 { 

78 "kernel_size": 3, 

79 "repeats": 1, 

80 "filters_in": 32, 

81 "filters_out": 16, 

82 "expand_ratio": 1, 

83 "id_skip": True, 

84 "strides": 1, 

85 "se_ratio": 0.25, 

86 }, 

87 { 

88 "kernel_size": 3, 

89 "repeats": 2, 

90 "filters_in": 16, 

91 "filters_out": 24, 

92 "expand_ratio": 6, 

93 "id_skip": True, 

94 "strides": 2, 

95 "se_ratio": 0.25, 

96 }, 

97 { 

98 "kernel_size": 5, 

99 "repeats": 2, 

100 "filters_in": 24, 

101 "filters_out": 40, 

102 "expand_ratio": 6, 

103 "id_skip": True, 

104 "strides": 2, 

105 "se_ratio": 0.25, 

106 }, 

107 { 

108 "kernel_size": 3, 

109 "repeats": 3, 

110 "filters_in": 40, 

111 "filters_out": 80, 

112 "expand_ratio": 6, 

113 "id_skip": True, 

114 "strides": 2, 

115 "se_ratio": 0.25, 

116 }, 

117 { 

118 "kernel_size": 5, 

119 "repeats": 3, 

120 "filters_in": 80, 

121 "filters_out": 112, 

122 "expand_ratio": 6, 

123 "id_skip": True, 

124 "strides": 1, 

125 "se_ratio": 0.25, 

126 }, 

127 { 

128 "kernel_size": 5, 

129 "repeats": 4, 

130 "filters_in": 112, 

131 "filters_out": 192, 

132 "expand_ratio": 6, 

133 "id_skip": True, 

134 "strides": 2, 

135 "se_ratio": 0.25, 

136 }, 

137 { 

138 "kernel_size": 3, 

139 "repeats": 1, 

140 "filters_in": 192, 

141 "filters_out": 320, 

142 "expand_ratio": 6, 

143 "id_skip": True, 

144 "strides": 1, 

145 "se_ratio": 0.25, 

146 }, 

147] 

148 

149CONV_KERNEL_INITIALIZER = { 

150 "class_name": "VarianceScaling", 

151 "config": { 

152 "scale": 2.0, 

153 "mode": "fan_out", 

154 "distribution": "truncated_normal", 

155 }, 

156} 

157 

158DENSE_KERNEL_INITIALIZER = { 

159 "class_name": "VarianceScaling", 

160 "config": { 

161 "scale": 1.0 / 3.0, 

162 "mode": "fan_out", 

163 "distribution": "uniform", 

164 }, 

165} 

166 

167layers = VersionAwareLayers() 

168 

169BASE_DOCSTRING = """Instantiates the {name} architecture. 

170 

171 Reference: 

172 - [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks]( 

173 https://arxiv.org/abs/1905.11946) (ICML 2019) 

174 

175 This function returns a Keras image classification model, 

176 optionally loaded with weights pre-trained on ImageNet. 

177 

178 For image classification use cases, see 

179 [this page for detailed examples]( 

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

181 

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

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

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

185 

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

187 For EfficientNet, input preprocessing is included as part of the model 

188 (as a `Rescaling` layer), and thus 

189 `tf.keras.applications.efficientnet.preprocess_input` is actually a 

190 pass-through function. EfficientNet models expect their inputs to be float 

191 tensors of pixels with values in the [0-255] range. 

192 

193 Args: 

194 include_top: Whether to include the fully-connected 

195 layer at the top of the network. Defaults to True. 

196 weights: One of `None` (random initialization), 

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

198 or the path to the weights file to be loaded. Defaults to 'imagenet'. 

199 input_tensor: Optional Keras tensor 

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

201 to use as image input for the model. 

202 input_shape: Optional shape tuple, only to be specified 

203 if `include_top` is False. 

204 It should have exactly 3 inputs channels. 

205 pooling: Optional pooling mode for feature extraction 

206 when `include_top` is `False`. Defaults to None. 

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

208 the 4D tensor output of the 

209 last convolutional layer. 

210 - `avg` means that global average pooling 

211 will be applied to the output of the 

212 last convolutional layer, and thus 

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

214 - `max` means that global max pooling will 

215 be applied. 

216 classes: Optional number of classes to classify images 

217 into, only to be specified if `include_top` is True, and 

218 if no `weights` argument is specified. Defaults to 1000 (number of 

219 ImageNet classes). 

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

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

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

223 Defaults to 'softmax'. 

224 When loading pretrained weights, `classifier_activation` can only 

225 be `None` or `"softmax"`. 

226 

227 Returns: 

228 A `keras.Model` instance. 

229""" 

230 

231 

232IMAGENET_STDDEV_RGB = [0.229, 0.224, 0.225] 

233 

234 

235def EfficientNet( 

236 width_coefficient, 

237 depth_coefficient, 

238 default_size, 

239 dropout_rate=0.2, 

240 drop_connect_rate=0.2, 

241 depth_divisor=8, 

242 activation="swish", 

243 blocks_args="default", 

244 model_name="efficientnet", 

245 include_top=True, 

246 weights="imagenet", 

247 input_tensor=None, 

248 input_shape=None, 

249 pooling=None, 

250 classes=1000, 

251 classifier_activation="softmax", 

252): 

253 """Instantiates the EfficientNet architecture. 

254 

255 Args: 

256 width_coefficient: float, scaling coefficient for network width. 

257 depth_coefficient: float, scaling coefficient for network depth. 

258 default_size: integer, default input image size. 

259 dropout_rate: float, dropout rate before final classifier layer. 

260 drop_connect_rate: float, dropout rate at skip connections. 

261 depth_divisor: integer, a unit of network width. 

262 activation: activation function. 

263 blocks_args: list of dicts, parameters to construct block modules. 

264 model_name: string, model name. 

265 include_top: whether to include the fully-connected 

266 layer at the top of the network. 

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

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

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

270 input_tensor: optional Keras tensor 

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

272 to use as image input for the model. 

273 input_shape: optional shape tuple, only to be specified 

274 if `include_top` is False. 

275 It should have exactly 3 inputs channels. 

276 pooling: optional pooling mode for feature extraction 

277 when `include_top` is `False`. 

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

279 the 4D tensor output of the 

280 last convolutional layer. 

281 - `avg` means that global average pooling 

282 will be applied to the output of the 

283 last convolutional layer, and thus 

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

285 - `max` means that global max pooling will 

286 be applied. 

287 classes: optional number of classes to classify images 

288 into, only to be specified if `include_top` is True, and 

289 if no `weights` argument is specified. 

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

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

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

293 

294 Returns: 

295 A `keras.Model` instance. 

296 

297 Raises: 

298 ValueError: in case of invalid argument for `weights`, 

299 or invalid input shape. 

300 ValueError: if `classifier_activation` is not `softmax` or `None` when 

301 using a pretrained top layer. 

302 """ 

303 if blocks_args == "default": 

304 blocks_args = DEFAULT_BLOCKS_ARGS 

305 

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

307 raise ValueError( 

308 "The `weights` argument should be either " 

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

310 "(pre-training on ImageNet), " 

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

312 ) 

313 

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

315 raise ValueError( 

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

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

318 ) 

319 

320 # Determine proper input shape 

321 input_shape = imagenet_utils.obtain_input_shape( 

322 input_shape, 

323 default_size=default_size, 

324 min_size=32, 

325 data_format=backend.image_data_format(), 

326 require_flatten=include_top, 

327 weights=weights, 

328 ) 

329 

330 if input_tensor is None: 

331 img_input = layers.Input(shape=input_shape) 

332 else: 

333 if not backend.is_keras_tensor(input_tensor): 

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

335 else: 

336 img_input = input_tensor 

337 

338 bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 

339 

340 def round_filters(filters, divisor=depth_divisor): 

341 """Round number of filters based on depth multiplier.""" 

342 filters *= width_coefficient 

343 new_filters = max( 

344 divisor, int(filters + divisor / 2) // divisor * divisor 

345 ) 

346 # Make sure that round down does not go down by more than 10%. 

347 if new_filters < 0.9 * filters: 

348 new_filters += divisor 

349 return int(new_filters) 

350 

351 def round_repeats(repeats): 

352 """Round number of repeats based on depth multiplier.""" 

353 return int(math.ceil(depth_coefficient * repeats)) 

354 

355 # Build stem 

356 x = img_input 

357 x = layers.Rescaling(1.0 / 255.0)(x) 

358 x = layers.Normalization(axis=bn_axis)(x) 

359 if weights == "imagenet": 

360 # Note that the normaliztion layer uses square value of STDDEV as the 

361 # variance for the layer: result = (input - mean) / sqrt(var) 

362 # However, the original implemenetation uses (input - mean) / var to 

363 # normalize the input, we need to divide another sqrt(var) to match the 

364 # original implementation. 

365 # See https://github.com/tensorflow/tensorflow/issues/49930 for more 

366 # details 

367 x = layers.Rescaling( 

368 [1.0 / math.sqrt(stddev) for stddev in IMAGENET_STDDEV_RGB] 

369 )(x) 

370 

371 x = layers.ZeroPadding2D( 

372 padding=imagenet_utils.correct_pad(x, 3), name="stem_conv_pad" 

373 )(x) 

374 x = layers.Conv2D( 

375 round_filters(32), 

376 3, 

377 strides=2, 

378 padding="valid", 

379 use_bias=False, 

380 kernel_initializer=CONV_KERNEL_INITIALIZER, 

381 name="stem_conv", 

382 )(x) 

383 x = layers.BatchNormalization(axis=bn_axis, name="stem_bn")(x) 

384 x = layers.Activation(activation, name="stem_activation")(x) 

385 

386 # Build blocks 

387 blocks_args = copy.deepcopy(blocks_args) 

388 

389 b = 0 

390 blocks = float(sum(round_repeats(args["repeats"]) for args in blocks_args)) 

391 for i, args in enumerate(blocks_args): 

392 assert args["repeats"] > 0 

393 # Update block input and output filters based on depth multiplier. 

394 args["filters_in"] = round_filters(args["filters_in"]) 

395 args["filters_out"] = round_filters(args["filters_out"]) 

396 

397 for j in range(round_repeats(args.pop("repeats"))): 

398 # The first block needs to take care of stride and filter size 

399 # increase. 

400 if j > 0: 

401 args["strides"] = 1 

402 args["filters_in"] = args["filters_out"] 

403 x = block( 

404 x, 

405 activation, 

406 drop_connect_rate * b / blocks, 

407 name=f"block{i + 1}{chr(j + 97)}_", 

408 **args, 

409 ) 

410 b += 1 

411 

412 # Build top 

413 x = layers.Conv2D( 

414 round_filters(1280), 

415 1, 

416 padding="same", 

417 use_bias=False, 

418 kernel_initializer=CONV_KERNEL_INITIALIZER, 

419 name="top_conv", 

420 )(x) 

421 x = layers.BatchNormalization(axis=bn_axis, name="top_bn")(x) 

422 x = layers.Activation(activation, name="top_activation")(x) 

423 if include_top: 

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

425 if dropout_rate > 0: 

426 x = layers.Dropout(dropout_rate, name="top_dropout")(x) 

427 imagenet_utils.validate_activation(classifier_activation, weights) 

428 x = layers.Dense( 

429 classes, 

430 activation=classifier_activation, 

431 kernel_initializer=DENSE_KERNEL_INITIALIZER, 

432 name="predictions", 

433 )(x) 

434 else: 

435 if pooling == "avg": 

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

437 elif pooling == "max": 

438 x = layers.GlobalMaxPooling2D(name="max_pool")(x) 

439 

440 # Ensure that the model takes into account 

441 # any potential predecessors of `input_tensor`. 

442 if input_tensor is not None: 

443 inputs = layer_utils.get_source_inputs(input_tensor) 

444 else: 

445 inputs = img_input 

446 

447 # Create model. 

448 model = training.Model(inputs, x, name=model_name) 

449 

450 # Load weights. 

451 if weights == "imagenet": 

452 if include_top: 

453 file_suffix = ".h5" 

454 file_hash = WEIGHTS_HASHES[model_name[-2:]][0] 

455 else: 

456 file_suffix = "_notop.h5" 

457 file_hash = WEIGHTS_HASHES[model_name[-2:]][1] 

458 file_name = model_name + file_suffix 

459 weights_path = data_utils.get_file( 

460 file_name, 

461 BASE_WEIGHTS_PATH + file_name, 

462 cache_subdir="models", 

463 file_hash=file_hash, 

464 ) 

465 model.load_weights(weights_path) 

466 elif weights is not None: 

467 model.load_weights(weights) 

468 return model 

469 

470 

471def block( 

472 inputs, 

473 activation="swish", 

474 drop_rate=0.0, 

475 name="", 

476 filters_in=32, 

477 filters_out=16, 

478 kernel_size=3, 

479 strides=1, 

480 expand_ratio=1, 

481 se_ratio=0.0, 

482 id_skip=True, 

483): 

484 """An inverted residual block. 

485 

486 Args: 

487 inputs: input tensor. 

488 activation: activation function. 

489 drop_rate: float between 0 and 1, fraction of the input units to drop. 

490 name: string, block label. 

491 filters_in: integer, the number of input filters. 

492 filters_out: integer, the number of output filters. 

493 kernel_size: integer, the dimension of the convolution window. 

494 strides: integer, the stride of the convolution. 

495 expand_ratio: integer, scaling coefficient for the input filters. 

496 se_ratio: float between 0 and 1, fraction to squeeze the input filters. 

497 id_skip: boolean. 

498 

499 Returns: 

500 output tensor for the block. 

501 """ 

502 bn_axis = 3 if backend.image_data_format() == "channels_last" else 1 

503 

504 # Expansion phase 

505 filters = filters_in * expand_ratio 

506 if expand_ratio != 1: 

507 x = layers.Conv2D( 

508 filters, 

509 1, 

510 padding="same", 

511 use_bias=False, 

512 kernel_initializer=CONV_KERNEL_INITIALIZER, 

513 name=name + "expand_conv", 

514 )(inputs) 

515 x = layers.BatchNormalization(axis=bn_axis, name=name + "expand_bn")(x) 

516 x = layers.Activation(activation, name=name + "expand_activation")(x) 

517 else: 

518 x = inputs 

519 

520 # Depthwise Convolution 

521 if strides == 2: 

522 x = layers.ZeroPadding2D( 

523 padding=imagenet_utils.correct_pad(x, kernel_size), 

524 name=name + "dwconv_pad", 

525 )(x) 

526 conv_pad = "valid" 

527 else: 

528 conv_pad = "same" 

529 x = layers.DepthwiseConv2D( 

530 kernel_size, 

531 strides=strides, 

532 padding=conv_pad, 

533 use_bias=False, 

534 depthwise_initializer=CONV_KERNEL_INITIALIZER, 

535 name=name + "dwconv", 

536 )(x) 

537 x = layers.BatchNormalization(axis=bn_axis, name=name + "bn")(x) 

538 x = layers.Activation(activation, name=name + "activation")(x) 

539 

540 # Squeeze and Excitation phase 

541 if 0 < se_ratio <= 1: 

542 filters_se = max(1, int(filters_in * se_ratio)) 

543 se = layers.GlobalAveragePooling2D(name=name + "se_squeeze")(x) 

544 if bn_axis == 1: 

545 se_shape = (filters, 1, 1) 

546 else: 

547 se_shape = (1, 1, filters) 

548 se = layers.Reshape(se_shape, name=name + "se_reshape")(se) 

549 se = layers.Conv2D( 

550 filters_se, 

551 1, 

552 padding="same", 

553 activation=activation, 

554 kernel_initializer=CONV_KERNEL_INITIALIZER, 

555 name=name + "se_reduce", 

556 )(se) 

557 se = layers.Conv2D( 

558 filters, 

559 1, 

560 padding="same", 

561 activation="sigmoid", 

562 kernel_initializer=CONV_KERNEL_INITIALIZER, 

563 name=name + "se_expand", 

564 )(se) 

565 x = layers.multiply([x, se], name=name + "se_excite") 

566 

567 # Output phase 

568 x = layers.Conv2D( 

569 filters_out, 

570 1, 

571 padding="same", 

572 use_bias=False, 

573 kernel_initializer=CONV_KERNEL_INITIALIZER, 

574 name=name + "project_conv", 

575 )(x) 

576 x = layers.BatchNormalization(axis=bn_axis, name=name + "project_bn")(x) 

577 if id_skip and strides == 1 and filters_in == filters_out: 

578 if drop_rate > 0: 

579 x = layers.Dropout( 

580 drop_rate, noise_shape=(None, 1, 1, 1), name=name + "drop" 

581 )(x) 

582 x = layers.add([x, inputs], name=name + "add") 

583 return x 

584 

585 

586@keras_export( 

587 "keras.applications.efficientnet.EfficientNetB0", 

588 "keras.applications.EfficientNetB0", 

589) 

590def EfficientNetB0( 

591 include_top=True, 

592 weights="imagenet", 

593 input_tensor=None, 

594 input_shape=None, 

595 pooling=None, 

596 classes=1000, 

597 classifier_activation="softmax", 

598 **kwargs, 

599): 

600 return EfficientNet( 

601 1.0, 

602 1.0, 

603 224, 

604 0.2, 

605 model_name="efficientnetb0", 

606 include_top=include_top, 

607 weights=weights, 

608 input_tensor=input_tensor, 

609 input_shape=input_shape, 

610 pooling=pooling, 

611 classes=classes, 

612 classifier_activation=classifier_activation, 

613 **kwargs, 

614 ) 

615 

616 

617@keras_export( 

618 "keras.applications.efficientnet.EfficientNetB1", 

619 "keras.applications.EfficientNetB1", 

620) 

621def EfficientNetB1( 

622 include_top=True, 

623 weights="imagenet", 

624 input_tensor=None, 

625 input_shape=None, 

626 pooling=None, 

627 classes=1000, 

628 classifier_activation="softmax", 

629 **kwargs, 

630): 

631 return EfficientNet( 

632 1.0, 

633 1.1, 

634 240, 

635 0.2, 

636 model_name="efficientnetb1", 

637 include_top=include_top, 

638 weights=weights, 

639 input_tensor=input_tensor, 

640 input_shape=input_shape, 

641 pooling=pooling, 

642 classes=classes, 

643 classifier_activation=classifier_activation, 

644 **kwargs, 

645 ) 

646 

647 

648@keras_export( 

649 "keras.applications.efficientnet.EfficientNetB2", 

650 "keras.applications.EfficientNetB2", 

651) 

652def EfficientNetB2( 

653 include_top=True, 

654 weights="imagenet", 

655 input_tensor=None, 

656 input_shape=None, 

657 pooling=None, 

658 classes=1000, 

659 classifier_activation="softmax", 

660 **kwargs, 

661): 

662 return EfficientNet( 

663 1.1, 

664 1.2, 

665 260, 

666 0.3, 

667 model_name="efficientnetb2", 

668 include_top=include_top, 

669 weights=weights, 

670 input_tensor=input_tensor, 

671 input_shape=input_shape, 

672 pooling=pooling, 

673 classes=classes, 

674 classifier_activation=classifier_activation, 

675 **kwargs, 

676 ) 

677 

678 

679@keras_export( 

680 "keras.applications.efficientnet.EfficientNetB3", 

681 "keras.applications.EfficientNetB3", 

682) 

683def EfficientNetB3( 

684 include_top=True, 

685 weights="imagenet", 

686 input_tensor=None, 

687 input_shape=None, 

688 pooling=None, 

689 classes=1000, 

690 classifier_activation="softmax", 

691 **kwargs, 

692): 

693 return EfficientNet( 

694 1.2, 

695 1.4, 

696 300, 

697 0.3, 

698 model_name="efficientnetb3", 

699 include_top=include_top, 

700 weights=weights, 

701 input_tensor=input_tensor, 

702 input_shape=input_shape, 

703 pooling=pooling, 

704 classes=classes, 

705 classifier_activation=classifier_activation, 

706 **kwargs, 

707 ) 

708 

709 

710@keras_export( 

711 "keras.applications.efficientnet.EfficientNetB4", 

712 "keras.applications.EfficientNetB4", 

713) 

714def EfficientNetB4( 

715 include_top=True, 

716 weights="imagenet", 

717 input_tensor=None, 

718 input_shape=None, 

719 pooling=None, 

720 classes=1000, 

721 classifier_activation="softmax", 

722 **kwargs, 

723): 

724 return EfficientNet( 

725 1.4, 

726 1.8, 

727 380, 

728 0.4, 

729 model_name="efficientnetb4", 

730 include_top=include_top, 

731 weights=weights, 

732 input_tensor=input_tensor, 

733 input_shape=input_shape, 

734 pooling=pooling, 

735 classes=classes, 

736 classifier_activation=classifier_activation, 

737 **kwargs, 

738 ) 

739 

740 

741@keras_export( 

742 "keras.applications.efficientnet.EfficientNetB5", 

743 "keras.applications.EfficientNetB5", 

744) 

745def EfficientNetB5( 

746 include_top=True, 

747 weights="imagenet", 

748 input_tensor=None, 

749 input_shape=None, 

750 pooling=None, 

751 classes=1000, 

752 classifier_activation="softmax", 

753 **kwargs, 

754): 

755 return EfficientNet( 

756 1.6, 

757 2.2, 

758 456, 

759 0.4, 

760 model_name="efficientnetb5", 

761 include_top=include_top, 

762 weights=weights, 

763 input_tensor=input_tensor, 

764 input_shape=input_shape, 

765 pooling=pooling, 

766 classes=classes, 

767 classifier_activation=classifier_activation, 

768 **kwargs, 

769 ) 

770 

771 

772@keras_export( 

773 "keras.applications.efficientnet.EfficientNetB6", 

774 "keras.applications.EfficientNetB6", 

775) 

776def EfficientNetB6( 

777 include_top=True, 

778 weights="imagenet", 

779 input_tensor=None, 

780 input_shape=None, 

781 pooling=None, 

782 classes=1000, 

783 classifier_activation="softmax", 

784 **kwargs, 

785): 

786 return EfficientNet( 

787 1.8, 

788 2.6, 

789 528, 

790 0.5, 

791 model_name="efficientnetb6", 

792 include_top=include_top, 

793 weights=weights, 

794 input_tensor=input_tensor, 

795 input_shape=input_shape, 

796 pooling=pooling, 

797 classes=classes, 

798 classifier_activation=classifier_activation, 

799 **kwargs, 

800 ) 

801 

802 

803@keras_export( 

804 "keras.applications.efficientnet.EfficientNetB7", 

805 "keras.applications.EfficientNetB7", 

806) 

807def EfficientNetB7( 

808 include_top=True, 

809 weights="imagenet", 

810 input_tensor=None, 

811 input_shape=None, 

812 pooling=None, 

813 classes=1000, 

814 classifier_activation="softmax", 

815 **kwargs, 

816): 

817 return EfficientNet( 

818 2.0, 

819 3.1, 

820 600, 

821 0.5, 

822 model_name="efficientnetb7", 

823 include_top=include_top, 

824 weights=weights, 

825 input_tensor=input_tensor, 

826 input_shape=input_shape, 

827 pooling=pooling, 

828 classes=classes, 

829 classifier_activation=classifier_activation, 

830 **kwargs, 

831 ) 

832 

833 

834EfficientNetB0.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB0") 

835EfficientNetB1.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB1") 

836EfficientNetB2.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB2") 

837EfficientNetB3.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB3") 

838EfficientNetB4.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB4") 

839EfficientNetB5.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB5") 

840EfficientNetB6.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB6") 

841EfficientNetB7.__doc__ = BASE_DOCSTRING.format(name="EfficientNetB7") 

842 

843 

844@keras_export("keras.applications.efficientnet.preprocess_input") 

845def preprocess_input(x, data_format=None): 

846 """A placeholder method for backward compatibility. 

847 

848 The preprocessing logic has been included in the efficientnet model 

849 implementation. Users are no longer required to call this method to 

850 normalize the input data. This method does nothing and only kept as a 

851 placeholder to align the API surface between old and new version of model. 

852 

853 Args: 

854 x: A floating point `numpy.array` or a `tf.Tensor`. 

855 data_format: Optional data format of the image tensor/array. Defaults to 

856 None, in which case the global setting 

857 `tf.keras.backend.image_data_format()` is used (unless you changed it, 

858 it defaults to "channels_last").{mode} 

859 

860 Returns: 

861 Unchanged `numpy.array` or `tf.Tensor`. 

862 """ 

863 return x 

864 

865 

866@keras_export("keras.applications.efficientnet.decode_predictions") 

867def decode_predictions(preds, top=5): 

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

869 

870 

871decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ 

872