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

160 statements  

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

1# Copyright 2022 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"""ConvNeXt models for Keras. 

18 

19References: 

20 

21- [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) 

22 (CVPR 2022) 

23""" 

24 

25import numpy as np 

26import tensorflow.compat.v2 as tf 

27 

28from keras.src import backend 

29from keras.src import initializers 

30from keras.src import layers 

31from keras.src import utils 

32from keras.src.applications import imagenet_utils 

33from keras.src.engine import sequential 

34from keras.src.engine import training as training_lib 

35 

36# isort: off 

37from tensorflow.python.util.tf_export import keras_export 

38 

39BASE_WEIGHTS_PATH = ( 

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

41) 

42 

43WEIGHTS_HASHES = { 

44 "convnext_tiny": ( 

45 "8ae6e78ce2933352b1ef4008e6dd2f17bc40771563877d156bc6426c7cf503ff", 

46 "d547c096cabd03329d7be5562c5e14798aa39ed24b474157cef5e85ab9e49ef1", 

47 ), 

48 "convnext_small": ( 

49 "ce1277d8f1ee5a0ef0e171469089c18f5233860ceaf9b168049cb9263fd7483c", 

50 "6fc8009faa2f00c1c1dfce59feea9b0745eb260a7dd11bee65c8e20843da6eab", 

51 ), 

52 "convnext_base": ( 

53 "52cbb006d3dadd03f6e095a8ca1aca47aecdd75acb4bc74bce1f5c695d0086e6", 

54 "40a20c5548a5e9202f69735ecc06c990e6b7c9d2de39f0361e27baeb24cb7c45", 

55 ), 

56 "convnext_large": ( 

57 "070c5ed9ed289581e477741d3b34beffa920db8cf590899d6d2c67fba2a198a6", 

58 "96f02b6f0753d4f543261bc9d09bed650f24dd6bc02ddde3066135b63d23a1cd", 

59 ), 

60 "convnext_xlarge": ( 

61 "c1f5ccab661354fc3a79a10fa99af82f0fbf10ec65cb894a3ae0815f17a889ee", 

62 "de3f8a54174130e0cecdc71583354753d557fcf1f4487331558e2a16ba0cfe05", 

63 ), 

64} 

65 

66 

67MODEL_CONFIGS = { 

68 "tiny": { 

69 "depths": [3, 3, 9, 3], 

70 "projection_dims": [96, 192, 384, 768], 

71 "default_size": 224, 

72 }, 

73 "small": { 

74 "depths": [3, 3, 27, 3], 

75 "projection_dims": [96, 192, 384, 768], 

76 "default_size": 224, 

77 }, 

78 "base": { 

79 "depths": [3, 3, 27, 3], 

80 "projection_dims": [128, 256, 512, 1024], 

81 "default_size": 224, 

82 }, 

83 "large": { 

84 "depths": [3, 3, 27, 3], 

85 "projection_dims": [192, 384, 768, 1536], 

86 "default_size": 224, 

87 }, 

88 "xlarge": { 

89 "depths": [3, 3, 27, 3], 

90 "projection_dims": [256, 512, 1024, 2048], 

91 "default_size": 224, 

92 }, 

93} 

94 

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

96 

97 References: 

98 - [A ConvNet for the 2020s](https://arxiv.org/abs/2201.03545) 

99 (CVPR 2022) 

100 

101 For image classification use cases, see 

102 [this page for detailed examples]( 

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

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

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

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

107 

108 The `base`, `large`, and `xlarge` models were first pre-trained on the 

109 ImageNet-21k dataset and then fine-tuned on the ImageNet-1k dataset. The 

110 pre-trained parameters of the models were assembled from the 

111 [official repository](https://github.com/facebookresearch/ConvNeXt). To get a 

112 sense of how these parameters were converted to Keras compatible parameters, 

113 please refer to 

114 [this repository](https://github.com/sayakpaul/keras-convnext-conversion). 

115 

116 Note: Each Keras Application expects a specific kind of input preprocessing. 

117 For ConvNeXt, preprocessing is included in the model using a `Normalization` 

118 layer. ConvNeXt models expect their inputs to be float or uint8 tensors of 

119 pixels with values in the [0-255] range. 

120 

121 When calling the `summary()` method after instantiating a ConvNeXt model, 

122 prefer setting the `expand_nested` argument `summary()` to `True` to better 

123 investigate the instantiated model. 

124 

125 Args: 

126 include_top: Whether to include the fully-connected 

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

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

129 `"imagenet"` (pre-training on ImageNet-1k), or the path to the weights 

130 file to be loaded. Defaults to `"imagenet"`. 

131 input_tensor: Optional Keras tensor 

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

133 to use as image input for the model. 

134 input_shape: Optional shape tuple, only to be specified 

135 if `include_top` is False. 

136 It should have exactly 3 inputs channels. 

137 pooling: Optional pooling mode for feature extraction 

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

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

140 the 4D tensor output of the last convolutional layer. 

141 - `avg` means that global average pooling 

142 will be applied to the output of the 

143 last convolutional layer, and thus 

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

145 - `max` means that global max pooling will 

146 be applied. 

147 classes: Optional number of classes to classify images 

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

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

150 ImageNet classes). 

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

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

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

154 Defaults to `"softmax"`. 

155 When loading pretrained weights, `classifier_activation` can only 

156 be `None` or `"softmax"`. 

157 

158 Returns: 

159 A `keras.Model` instance. 

160""" 

161 

162 

163class StochasticDepth(layers.Layer): 

164 """Stochastic Depth module. 

165 

166 It performs batch-wise dropping rather than sample-wise. In libraries like 

167 `timm`, it's similar to `DropPath` layers that drops residual paths 

168 sample-wise. 

169 

170 References: 

171 - https://github.com/rwightman/pytorch-image-models 

172 

173 Args: 

174 drop_path_rate (float): Probability of dropping paths. Should be within 

175 [0, 1]. 

176 

177 Returns: 

178 Tensor either with the residual path dropped or kept. 

179 """ 

180 

181 def __init__(self, drop_path_rate, **kwargs): 

182 super().__init__(**kwargs) 

183 self.drop_path_rate = drop_path_rate 

184 

185 def call(self, x, training=None): 

186 if training: 

187 keep_prob = 1 - self.drop_path_rate 

188 shape = (tf.shape(x)[0],) + (1,) * (len(tf.shape(x)) - 1) 

189 random_tensor = keep_prob + tf.random.uniform(shape, 0, 1) 

190 random_tensor = tf.floor(random_tensor) 

191 return (x / keep_prob) * random_tensor 

192 return x 

193 

194 def get_config(self): 

195 config = super().get_config() 

196 config.update({"drop_path_rate": self.drop_path_rate}) 

197 return config 

198 

199 

200class LayerScale(layers.Layer): 

201 """Layer scale module. 

202 

203 References: 

204 - https://arxiv.org/abs/2103.17239 

205 

206 Args: 

207 init_values (float): Initial value for layer scale. Should be within 

208 [0, 1]. 

209 projection_dim (int): Projection dimensionality. 

210 

211 Returns: 

212 Tensor multiplied to the scale. 

213 """ 

214 

215 def __init__(self, init_values, projection_dim, **kwargs): 

216 super().__init__(**kwargs) 

217 self.init_values = init_values 

218 self.projection_dim = projection_dim 

219 

220 def build(self, input_shape): 

221 self.gamma = self.add_weight( 

222 shape=(self.projection_dim,), 

223 initializer=initializers.Constant(self.init_values), 

224 trainable=True, 

225 ) 

226 

227 def call(self, x): 

228 return x * self.gamma 

229 

230 def get_config(self): 

231 config = super().get_config() 

232 config.update( 

233 { 

234 "init_values": self.init_values, 

235 "projection_dim": self.projection_dim, 

236 } 

237 ) 

238 return config 

239 

240 

241def ConvNeXtBlock( 

242 projection_dim, drop_path_rate=0.0, layer_scale_init_value=1e-6, name=None 

243): 

244 """ConvNeXt block. 

245 

246 References: 

247 - https://arxiv.org/abs/2201.03545 

248 - https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py 

249 

250 Notes: 

251 In the original ConvNeXt implementation (linked above), the authors use 

252 `Dense` layers for pointwise convolutions for increased efficiency. 

253 Following that, this implementation also uses the same. 

254 

255 Args: 

256 projection_dim (int): Number of filters for convolution layers. In the 

257 ConvNeXt paper, this is referred to as projection dimension. 

258 drop_path_rate (float): Probability of dropping paths. Should be within 

259 [0, 1]. 

260 layer_scale_init_value (float): Layer scale value. Should be a small float 

261 number. 

262 name: name to path to the keras layer. 

263 

264 Returns: 

265 A function representing a ConvNeXtBlock block. 

266 """ 

267 if name is None: 

268 name = "prestem" + str(backend.get_uid("prestem")) 

269 

270 def apply(inputs): 

271 x = inputs 

272 

273 x = layers.Conv2D( 

274 filters=projection_dim, 

275 kernel_size=7, 

276 padding="same", 

277 groups=projection_dim, 

278 name=name + "_depthwise_conv", 

279 )(x) 

280 x = layers.LayerNormalization(epsilon=1e-6, name=name + "_layernorm")(x) 

281 x = layers.Dense(4 * projection_dim, name=name + "_pointwise_conv_1")(x) 

282 x = layers.Activation("gelu", name=name + "_gelu")(x) 

283 x = layers.Dense(projection_dim, name=name + "_pointwise_conv_2")(x) 

284 

285 if layer_scale_init_value is not None: 

286 x = LayerScale( 

287 layer_scale_init_value, 

288 projection_dim, 

289 name=name + "_layer_scale", 

290 )(x) 

291 if drop_path_rate: 

292 layer = StochasticDepth( 

293 drop_path_rate, name=name + "_stochastic_depth" 

294 ) 

295 else: 

296 layer = layers.Activation("linear", name=name + "_identity") 

297 

298 return inputs + layer(x) 

299 

300 return apply 

301 

302 

303def PreStem(name=None): 

304 """Normalizes inputs with ImageNet-1k mean and std. 

305 

306 Args: 

307 name (str): Name prefix. 

308 

309 Returns: 

310 A presemt function. 

311 """ 

312 if name is None: 

313 name = "prestem" + str(backend.get_uid("prestem")) 

314 

315 def apply(x): 

316 x = layers.Normalization( 

317 mean=[0.485 * 255, 0.456 * 255, 0.406 * 255], 

318 variance=[ 

319 (0.229 * 255) ** 2, 

320 (0.224 * 255) ** 2, 

321 (0.225 * 255) ** 2, 

322 ], 

323 name=name + "_prestem_normalization", 

324 )(x) 

325 return x 

326 

327 return apply 

328 

329 

330def Head(num_classes=1000, classifier_activation=None, name=None): 

331 """Implementation of classification head of ConvNeXt. 

332 

333 Args: 

334 num_classes: number of classes for Dense layer 

335 classifier_activation: activation function for the Dense layer 

336 name: name prefix 

337 

338 Returns: 

339 Classification head function. 

340 """ 

341 if name is None: 

342 name = str(backend.get_uid("head")) 

343 

344 def apply(x): 

345 x = layers.GlobalAveragePooling2D(name=name + "_head_gap")(x) 

346 x = layers.LayerNormalization( 

347 epsilon=1e-6, name=name + "_head_layernorm" 

348 )(x) 

349 x = layers.Dense( 

350 num_classes, 

351 activation=classifier_activation, 

352 name=name + "_head_dense", 

353 )(x) 

354 return x 

355 

356 return apply 

357 

358 

359def ConvNeXt( 

360 depths, 

361 projection_dims, 

362 drop_path_rate=0.0, 

363 layer_scale_init_value=1e-6, 

364 default_size=224, 

365 model_name="convnext", 

366 include_preprocessing=True, 

367 include_top=True, 

368 weights=None, 

369 input_tensor=None, 

370 input_shape=None, 

371 pooling=None, 

372 classes=1000, 

373 classifier_activation="softmax", 

374): 

375 """Instantiates ConvNeXt architecture given specific configuration. 

376 

377 Args: 

378 depths: An iterable containing depths for each individual stages. 

379 projection_dims: An iterable containing output number of channels of 

380 each individual stages. 

381 drop_path_rate: Stochastic depth probability. If 0.0, then stochastic 

382 depth won't be used. 

383 layer_scale_init_value: Layer scale coefficient. If 0.0, layer scaling 

384 won't be used. 

385 default_size: Default input image size. 

386 model_name: An optional name for the model. 

387 include_preprocessing: boolean denoting whther to include preprocessing in 

388 the model. When `weights="imagenet"` this should be always set to True. 

389 But for other models (e.g., randomly initialized) users should set it 

390 to False and apply preprocessing to data accordingly. 

391 include_top: Boolean denoting whether to include classification head to 

392 the model. 

393 weights: one of `None` (random initialization), `"imagenet"` (pre-training 

394 on ImageNet-1k), or the path to the weights file to be loaded. 

395 input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to 

396 use as image input for the model. 

397 input_shape: optional shape tuple, only to be specified if `include_top` 

398 is False. It should have exactly 3 inputs channels. 

399 pooling: optional pooling mode for feature extraction when `include_top` 

400 is `False`. 

401 - `None` means that the output of the model will be the 4D tensor output 

402 of the last convolutional layer. 

403 - `avg` means that global average pooling will be applied to the output 

404 of the last convolutional layer, and thus the output of the model will 

405 be a 2D tensor. 

406 - `max` means that global max pooling will be applied. 

407 classes: optional number of classes to classify images into, only to be 

408 specified if `include_top` is True, and if no `weights` argument is 

409 specified. 

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

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

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

413 

414 Returns: 

415 A `keras.Model` instance. 

416 

417 Raises: 

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

419 or invalid input shape. 

420 ValueError: if `classifier_activation` is not `softmax`, or `None` 

421 when using a pretrained top layer. 

422 ValueError: if `include_top` is True but `num_classes` is not 1000 

423 when using ImageNet. 

424 """ 

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

426 raise ValueError( 

427 "The `weights` argument should be either " 

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

429 "(pre-training on ImageNet), " 

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

431 ) 

432 

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

434 raise ValueError( 

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

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

437 ) 

438 

439 # Determine proper input shape. 

440 input_shape = imagenet_utils.obtain_input_shape( 

441 input_shape, 

442 default_size=default_size, 

443 min_size=32, 

444 data_format=backend.image_data_format(), 

445 require_flatten=include_top, 

446 weights=weights, 

447 ) 

448 

449 if input_tensor is None: 

450 img_input = layers.Input(shape=input_shape) 

451 else: 

452 if not backend.is_keras_tensor(input_tensor): 

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

454 else: 

455 img_input = input_tensor 

456 

457 if input_tensor is not None: 

458 inputs = utils.layer_utils.get_source_inputs(input_tensor)[0] 

459 else: 

460 inputs = img_input 

461 

462 x = inputs 

463 if include_preprocessing: 

464 channel_axis = ( 

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

466 ) 

467 num_channels = input_shape[channel_axis - 1] 

468 if num_channels == 3: 

469 x = PreStem(name=model_name)(x) 

470 

471 # Stem block. 

472 stem = sequential.Sequential( 

473 [ 

474 layers.Conv2D( 

475 projection_dims[0], 

476 kernel_size=4, 

477 strides=4, 

478 name=model_name + "_stem_conv", 

479 ), 

480 layers.LayerNormalization( 

481 epsilon=1e-6, name=model_name + "_stem_layernorm" 

482 ), 

483 ], 

484 name=model_name + "_stem", 

485 ) 

486 

487 # Downsampling blocks. 

488 downsample_layers = [] 

489 downsample_layers.append(stem) 

490 

491 num_downsample_layers = 3 

492 for i in range(num_downsample_layers): 

493 downsample_layer = sequential.Sequential( 

494 [ 

495 layers.LayerNormalization( 

496 epsilon=1e-6, 

497 name=model_name + "_downsampling_layernorm_" + str(i), 

498 ), 

499 layers.Conv2D( 

500 projection_dims[i + 1], 

501 kernel_size=2, 

502 strides=2, 

503 name=model_name + "_downsampling_conv_" + str(i), 

504 ), 

505 ], 

506 name=model_name + "_downsampling_block_" + str(i), 

507 ) 

508 downsample_layers.append(downsample_layer) 

509 

510 # Stochastic depth schedule. 

511 # This is referred from the original ConvNeXt codebase: 

512 # https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py#L86 

513 depth_drop_rates = [ 

514 float(x) for x in np.linspace(0.0, drop_path_rate, sum(depths)) 

515 ] 

516 

517 # First apply downsampling blocks and then apply ConvNeXt stages. 

518 cur = 0 

519 

520 num_convnext_blocks = 4 

521 for i in range(num_convnext_blocks): 

522 x = downsample_layers[i](x) 

523 for j in range(depths[i]): 

524 x = ConvNeXtBlock( 

525 projection_dim=projection_dims[i], 

526 drop_path_rate=depth_drop_rates[cur + j], 

527 layer_scale_init_value=layer_scale_init_value, 

528 name=model_name + f"_stage_{i}_block_{j}", 

529 )(x) 

530 cur += depths[i] 

531 

532 if include_top: 

533 imagenet_utils.validate_activation(classifier_activation, weights) 

534 x = Head( 

535 num_classes=classes, 

536 classifier_activation=classifier_activation, 

537 name=model_name, 

538 )(x) 

539 

540 else: 

541 if pooling == "avg": 

542 x = layers.GlobalAveragePooling2D()(x) 

543 elif pooling == "max": 

544 x = layers.GlobalMaxPooling2D()(x) 

545 x = layers.LayerNormalization(epsilon=1e-6)(x) 

546 

547 model = training_lib.Model(inputs=inputs, outputs=x, name=model_name) 

548 

549 # Load weights. 

550 if weights == "imagenet": 

551 if include_top: 

552 file_suffix = ".h5" 

553 file_hash = WEIGHTS_HASHES[model_name][0] 

554 else: 

555 file_suffix = "_notop.h5" 

556 file_hash = WEIGHTS_HASHES[model_name][1] 

557 file_name = model_name + file_suffix 

558 weights_path = utils.data_utils.get_file( 

559 file_name, 

560 BASE_WEIGHTS_PATH + file_name, 

561 cache_subdir="models", 

562 file_hash=file_hash, 

563 ) 

564 model.load_weights(weights_path) 

565 elif weights is not None: 

566 model.load_weights(weights) 

567 

568 return model 

569 

570 

571## Instantiating variants ## 

572 

573 

574@keras_export( 

575 "keras.applications.convnext.ConvNeXtTiny", 

576 "keras.applications.ConvNeXtTiny", 

577) 

578def ConvNeXtTiny( 

579 model_name="convnext_tiny", 

580 include_top=True, 

581 include_preprocessing=True, 

582 weights="imagenet", 

583 input_tensor=None, 

584 input_shape=None, 

585 pooling=None, 

586 classes=1000, 

587 classifier_activation="softmax", 

588): 

589 return ConvNeXt( 

590 depths=MODEL_CONFIGS["tiny"]["depths"], 

591 projection_dims=MODEL_CONFIGS["tiny"]["projection_dims"], 

592 drop_path_rate=0.0, 

593 layer_scale_init_value=1e-6, 

594 default_size=MODEL_CONFIGS["tiny"]["default_size"], 

595 model_name=model_name, 

596 include_top=include_top, 

597 include_preprocessing=include_preprocessing, 

598 weights=weights, 

599 input_tensor=input_tensor, 

600 input_shape=input_shape, 

601 pooling=pooling, 

602 classes=classes, 

603 classifier_activation=classifier_activation, 

604 ) 

605 

606 

607@keras_export( 

608 "keras.applications.convnext.ConvNeXtSmall", 

609 "keras.applications.ConvNeXtSmall", 

610) 

611def ConvNeXtSmall( 

612 model_name="convnext_small", 

613 include_top=True, 

614 include_preprocessing=True, 

615 weights="imagenet", 

616 input_tensor=None, 

617 input_shape=None, 

618 pooling=None, 

619 classes=1000, 

620 classifier_activation="softmax", 

621): 

622 return ConvNeXt( 

623 depths=MODEL_CONFIGS["small"]["depths"], 

624 projection_dims=MODEL_CONFIGS["small"]["projection_dims"], 

625 drop_path_rate=0.0, 

626 layer_scale_init_value=1e-6, 

627 default_size=MODEL_CONFIGS["small"]["default_size"], 

628 model_name=model_name, 

629 include_top=include_top, 

630 include_preprocessing=include_preprocessing, 

631 weights=weights, 

632 input_tensor=input_tensor, 

633 input_shape=input_shape, 

634 pooling=pooling, 

635 classes=classes, 

636 classifier_activation=classifier_activation, 

637 ) 

638 

639 

640@keras_export( 

641 "keras.applications.convnext.ConvNeXtBase", 

642 "keras.applications.ConvNeXtBase", 

643) 

644def ConvNeXtBase( 

645 model_name="convnext_base", 

646 include_top=True, 

647 include_preprocessing=True, 

648 weights="imagenet", 

649 input_tensor=None, 

650 input_shape=None, 

651 pooling=None, 

652 classes=1000, 

653 classifier_activation="softmax", 

654): 

655 return ConvNeXt( 

656 depths=MODEL_CONFIGS["base"]["depths"], 

657 projection_dims=MODEL_CONFIGS["base"]["projection_dims"], 

658 drop_path_rate=0.0, 

659 layer_scale_init_value=1e-6, 

660 default_size=MODEL_CONFIGS["base"]["default_size"], 

661 model_name=model_name, 

662 include_top=include_top, 

663 include_preprocessing=include_preprocessing, 

664 weights=weights, 

665 input_tensor=input_tensor, 

666 input_shape=input_shape, 

667 pooling=pooling, 

668 classes=classes, 

669 classifier_activation=classifier_activation, 

670 ) 

671 

672 

673@keras_export( 

674 "keras.applications.convnext.ConvNeXtLarge", 

675 "keras.applications.ConvNeXtLarge", 

676) 

677def ConvNeXtLarge( 

678 model_name="convnext_large", 

679 include_top=True, 

680 include_preprocessing=True, 

681 weights="imagenet", 

682 input_tensor=None, 

683 input_shape=None, 

684 pooling=None, 

685 classes=1000, 

686 classifier_activation="softmax", 

687): 

688 return ConvNeXt( 

689 depths=MODEL_CONFIGS["large"]["depths"], 

690 projection_dims=MODEL_CONFIGS["large"]["projection_dims"], 

691 drop_path_rate=0.0, 

692 layer_scale_init_value=1e-6, 

693 default_size=MODEL_CONFIGS["large"]["default_size"], 

694 model_name=model_name, 

695 include_top=include_top, 

696 include_preprocessing=include_preprocessing, 

697 weights=weights, 

698 input_tensor=input_tensor, 

699 input_shape=input_shape, 

700 pooling=pooling, 

701 classes=classes, 

702 classifier_activation=classifier_activation, 

703 ) 

704 

705 

706@keras_export( 

707 "keras.applications.convnext.ConvNeXtXLarge", 

708 "keras.applications.ConvNeXtXLarge", 

709) 

710def ConvNeXtXLarge( 

711 model_name="convnext_xlarge", 

712 include_top=True, 

713 include_preprocessing=True, 

714 weights="imagenet", 

715 input_tensor=None, 

716 input_shape=None, 

717 pooling=None, 

718 classes=1000, 

719 classifier_activation="softmax", 

720): 

721 return ConvNeXt( 

722 depths=MODEL_CONFIGS["xlarge"]["depths"], 

723 projection_dims=MODEL_CONFIGS["xlarge"]["projection_dims"], 

724 drop_path_rate=0.0, 

725 layer_scale_init_value=1e-6, 

726 default_size=MODEL_CONFIGS["xlarge"]["default_size"], 

727 model_name=model_name, 

728 include_top=include_top, 

729 include_preprocessing=include_preprocessing, 

730 weights=weights, 

731 input_tensor=input_tensor, 

732 input_shape=input_shape, 

733 pooling=pooling, 

734 classes=classes, 

735 classifier_activation=classifier_activation, 

736 ) 

737 

738 

739ConvNeXtTiny.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtTiny") 

740ConvNeXtSmall.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtSmall") 

741ConvNeXtBase.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtBase") 

742ConvNeXtLarge.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtLarge") 

743ConvNeXtXLarge.__doc__ = BASE_DOCSTRING.format(name="ConvNeXtXLarge") 

744 

745 

746@keras_export("keras.applications.convnext.preprocess_input") 

747def preprocess_input(x, data_format=None): 

748 """A placeholder method for backward compatibility. 

749 

750 The preprocessing logic has been included in the convnext model 

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

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

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

754 

755 Args: 

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

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

758 None, in which case the global setting 

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

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

761 

762 Returns: 

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

764 """ 

765 return x 

766 

767 

768@keras_export("keras.applications.convnext.decode_predictions") 

769def decode_predictions(preds, top=5): 

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

771 

772 

773decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__ 

774