or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

activations.mdapplications.mddata-utils.mdindex.mdinitializers.mdlayers.mdmodels.mdoperations.mdrandom.mdregularizers.mdsaving.mdtraining.md

applications.mddocs/

0

# Pre-trained Models

1

2

Ready-to-use pre-trained models for computer vision tasks. Keras provides a comprehensive collection of state-of-the-art models pre-trained on ImageNet, enabling transfer learning and feature extraction for various applications.

3

4

## Capabilities

5

6

### VGG Models

7

8

VGG (Visual Geometry Group) models with deep convolutional architectures using small 3x3 filters.

9

10

```python { .api }

11

def VGG16(include_top=True, weights='imagenet', input_tensor=None,

12

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

13

"""

14

VGG16 model with 16 layers.

15

16

Parameters:

17

- include_top: Whether to include fully connected layers at top

18

- weights: 'imagenet' (pre-training on ImageNet) or None (random initialization)

19

- input_tensor: Optional tensor to use as image input

20

- input_shape: Shape of input images (only if include_top=False)

21

- pooling: Pooling mode for feature extraction ('avg', 'max', or None)

22

- classes: Number of classes to classify (only if include_top=True and weights=None)

23

- classifier_activation: Activation for final classification layer

24

25

Returns:

26

Keras Model instance

27

"""

28

29

def VGG19(include_top=True, weights='imagenet', input_tensor=None,

30

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

31

"""VGG19 model with 19 layers."""

32

```

33

34

### ResNet Models

35

36

Residual Networks with skip connections that enable training of very deep networks.

37

38

```python { .api }

39

def ResNet50(include_top=True, weights='imagenet', input_tensor=None,

40

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

41

"""

42

ResNet50 model with 50 layers.

43

44

Parameters:

45

- include_top: Whether to include fully connected layers at top

46

- weights: 'imagenet' or None

47

- input_tensor: Optional tensor to use as image input

48

- input_shape: Shape of input images (only if include_top=False)

49

- pooling: Pooling mode for feature extraction

50

- classes: Number of classes to classify

51

- classifier_activation: Activation for final classification layer

52

53

Returns:

54

Keras Model instance

55

"""

56

57

def ResNet101(include_top=True, weights='imagenet', input_tensor=None,

58

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

59

"""ResNet101 model with 101 layers."""

60

61

def ResNet152(include_top=True, weights='imagenet', input_tensor=None,

62

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

63

"""ResNet152 model with 152 layers."""

64

65

def ResNet50V2(include_top=True, weights='imagenet', input_tensor=None,

66

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

67

"""ResNet50V2 model with improved residual blocks."""

68

69

def ResNet101V2(include_top=True, weights='imagenet', input_tensor=None,

70

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

71

"""ResNet101V2 model with improved residual blocks."""

72

73

def ResNet152V2(include_top=True, weights='imagenet', input_tensor=None,

74

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

75

"""ResNet152V2 model with improved residual blocks."""

76

```

77

78

### Inception Models

79

80

Inception architectures using multi-scale convolutional features and efficient factorized convolutions.

81

82

```python { .api }

83

def InceptionV3(include_top=True, weights='imagenet', input_tensor=None,

84

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

85

"""

86

InceptionV3 model.

87

88

Parameters:

89

- include_top: Whether to include fully connected layers at top

90

- weights: 'imagenet' or None

91

- input_tensor: Optional tensor to use as image input

92

- input_shape: Shape of input images (only if include_top=False)

93

- pooling: Pooling mode for feature extraction

94

- classes: Number of classes to classify

95

- classifier_activation: Activation for final classification layer

96

97

Returns:

98

Keras Model instance

99

"""

100

101

def InceptionResNetV2(include_top=True, weights='imagenet', input_tensor=None,

102

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

103

"""InceptionResNetV2 combining Inception and ResNet architectures."""

104

105

def Xception(include_top=True, weights='imagenet', input_tensor=None,

106

input_shape=None, pooling=None, classes=1000, classifier_activation='softmax'):

107

"""

108

Xception model with depthwise separable convolutions.

109

110

Parameters:

111

- include_top: Whether to include fully connected layers at top

112

- weights: 'imagenet' or None

113

- input_tensor: Optional tensor to use as image input

114

- input_shape: Shape of input images (only if include_top=False)

115

- pooling: Pooling mode for feature extraction

116

- classes: Number of classes to classify

117

- classifier_activation: Activation for final classification layer

118

119

Returns:

120

Keras Model instance

121

"""

122

```

123

124

### MobileNet Models

125

126

Efficient models designed for mobile and embedded devices using depthwise separable convolutions.

127

128

```python { .api }

129

def MobileNet(input_shape=None, alpha=1.0, depth_multiplier=1, dropout=1e-3,

130

include_top=True, weights='imagenet', input_tensor=None,

131

pooling=None, classes=1000, classifier_activation='softmax'):

132

"""

133

MobileNet model optimized for mobile devices.

134

135

Parameters:

136

- input_shape: Shape of input images

137

- alpha: Width multiplier for network

138

- depth_multiplier: Depth multiplier for depthwise convolution

139

- dropout: Dropout rate

140

- include_top: Whether to include fully connected layers at top

141

- weights: 'imagenet' or None

142

- input_tensor: Optional tensor to use as image input

143

- pooling: Pooling mode for feature extraction

144

- classes: Number of classes to classify

145

- classifier_activation: Activation for final classification layer

146

147

Returns:

148

Keras Model instance

149

"""

150

151

def MobileNetV2(input_shape=None, alpha=1.0, include_top=True, weights='imagenet',

152

input_tensor=None, pooling=None, classes=1000, classifier_activation='softmax'):

153

"""MobileNetV2 with inverted residuals and linear bottlenecks."""

154

155

def MobileNetV3Large(input_shape=None, alpha=1.0, minimalistic=False, include_top=True,

156

weights='imagenet', input_tensor=None, classes=1000, pooling=None,

157

dropout_rate=0.2, classifier_activation='softmax'):

158

"""MobileNetV3Large optimized for high resource use cases."""

159

160

def MobileNetV3Small(input_shape=None, alpha=1.0, minimalistic=False, include_top=True,

161

weights='imagenet', input_tensor=None, classes=1000, pooling=None,

162

dropout_rate=0.2, classifier_activation='softmax'):

163

"""MobileNetV3Small optimized for low resource use cases."""

164

```

165

166

### EfficientNet Models

167

168

EfficientNet family of models that systematically scale network width, depth, and resolution.

169

170

```python { .api }

171

def EfficientNetB0(include_top=True, weights='imagenet', input_tensor=None,

172

input_shape=None, pooling=None, classes=1000,

173

classifier_activation='softmax', **kwargs):

174

"""

175

EfficientNetB0 model.

176

177

Parameters:

178

- include_top: Whether to include fully connected layers at top

179

- weights: 'imagenet' or None

180

- input_tensor: Optional tensor to use as image input

181

- input_shape: Shape of input images (only if include_top=False)

182

- pooling: Pooling mode for feature extraction

183

- classes: Number of classes to classify

184

- classifier_activation: Activation for final classification layer

185

186

Returns:

187

Keras Model instance

188

"""

189

190

def EfficientNetB1(include_top=True, weights='imagenet', input_tensor=None,

191

input_shape=None, pooling=None, classes=1000,

192

classifier_activation='softmax', **kwargs):

193

"""EfficientNetB1 model."""

194

195

def EfficientNetB2(include_top=True, weights='imagenet', input_tensor=None,

196

input_shape=None, pooling=None, classes=1000,

197

classifier_activation='softmax', **kwargs):

198

"""EfficientNetB2 model."""

199

200

def EfficientNetB3(include_top=True, weights='imagenet', input_tensor=None,

201

input_shape=None, pooling=None, classes=1000,

202

classifier_activation='softmax', **kwargs):

203

"""EfficientNetB3 model."""

204

205

def EfficientNetB4(include_top=True, weights='imagenet', input_tensor=None,

206

input_shape=None, pooling=None, classes=1000,

207

classifier_activation='softmax', **kwargs):

208

"""EfficientNetB4 model."""

209

210

def EfficientNetB5(include_top=True, weights='imagenet', input_tensor=None,

211

input_shape=None, pooling=None, classes=1000,

212

classifier_activation='softmax', **kwargs):

213

"""EfficientNetB5 model."""

214

215

def EfficientNetB6(include_top=True, weights='imagenet', input_tensor=None,

216

input_shape=None, pooling=None, classes=1000,

217

classifier_activation='softmax', **kwargs):

218

"""EfficientNetB6 model."""

219

220

def EfficientNetB7(include_top=True, weights='imagenet', input_tensor=None,

221

input_shape=None, pooling=None, classes=1000,

222

classifier_activation='softmax', **kwargs):

223

"""EfficientNetB7 model."""

224

```

225

226

### EfficientNetV2 Models

227

228

Second generation EfficientNet models with improved training efficiency and better parameter efficiency.

229

230

```python { .api }

231

def EfficientNetV2B0(include_top=True, weights='imagenet', input_tensor=None,

232

input_shape=None, pooling=None, classes=1000,

233

classifier_activation='softmax', include_preprocessing=True):

234

"""EfficientNetV2B0 model."""

235

236

def EfficientNetV2B1(include_top=True, weights='imagenet', input_tensor=None,

237

input_shape=None, pooling=None, classes=1000,

238

classifier_activation='softmax', include_preprocessing=True):

239

"""EfficientNetV2B1 model."""

240

241

def EfficientNetV2B2(include_top=True, weights='imagenet', input_tensor=None,

242

input_shape=None, pooling=None, classes=1000,

243

classifier_activation='softmax', include_preprocessing=True):

244

"""EfficientNetV2B2 model."""

245

246

def EfficientNetV2B3(include_top=True, weights='imagenet', input_tensor=None,

247

input_shape=None, pooling=None, classes=1000,

248

classifier_activation='softmax', include_preprocessing=True):

249

"""EfficientNetV2B3 model."""

250

251

def EfficientNetV2S(include_top=True, weights='imagenet', input_tensor=None,

252

input_shape=None, pooling=None, classes=1000,

253

classifier_activation='softmax', include_preprocessing=True):

254

"""EfficientNetV2S model."""

255

256

def EfficientNetV2M(include_top=True, weights='imagenet', input_tensor=None,

257

input_shape=None, pooling=None, classes=1000,

258

classifier_activation='softmax', include_preprocessing=True):

259

"""EfficientNetV2M model."""

260

261

def EfficientNetV2L(include_top=True, weights='imagenet', input_tensor=None,

262

input_shape=None, pooling=None, classes=1000,

263

classifier_activation='softmax', include_preprocessing=True):

264

"""EfficientNetV2L model."""

265

```

266

267

### DenseNet Models

268

269

Densely connected networks where each layer connects to every other layer in a feed-forward fashion.

270

271

```python { .api }

272

def DenseNet121(include_top=True, weights='imagenet', input_tensor=None,

273

input_shape=None, pooling=None, classes=1000):

274

"""

275

DenseNet121 model with 121 layers.

276

277

Parameters:

278

- include_top: Whether to include fully connected layers at top

279

- weights: 'imagenet' or None

280

- input_tensor: Optional tensor to use as image input

281

- input_shape: Shape of input images (only if include_top=False)

282

- pooling: Pooling mode for feature extraction

283

- classes: Number of classes to classify

284

285

Returns:

286

Keras Model instance

287

"""

288

289

def DenseNet169(include_top=True, weights='imagenet', input_tensor=None,

290

input_shape=None, pooling=None, classes=1000):

291

"""DenseNet169 model with 169 layers."""

292

293

def DenseNet201(include_top=True, weights='imagenet', input_tensor=None,

294

input_shape=None, pooling=None, classes=1000):

295

"""DenseNet201 model with 201 layers."""

296

```

297

298

### ConvNeXt Models

299

300

Modern ConvNet architectures that compete with Vision Transformers.

301

302

```python { .api }

303

def ConvNeXtTiny(model_name='convnext_tiny', include_top=True, include_preprocessing=True,

304

weights='imagenet', input_tensor=None, input_shape=None, pooling=None,

305

classes=1000, classifier_activation='softmax'):

306

"""ConvNeXtTiny model."""

307

308

def ConvNeXtSmall(model_name='convnext_small', include_top=True, include_preprocessing=True,

309

weights='imagenet', input_tensor=None, input_shape=None, pooling=None,

310

classes=1000, classifier_activation='softmax'):

311

"""ConvNeXtSmall model."""

312

313

def ConvNeXtBase(model_name='convnext_base', include_top=True, include_preprocessing=True,

314

weights='imagenet', input_tensor=None, input_shape=None, pooling=None,

315

classes=1000, classifier_activation='softmax'):

316

"""ConvNeXtBase model."""

317

318

def ConvNeXtLarge(model_name='convnext_large', include_top=True, include_preprocessing=True,

319

weights='imagenet', input_tensor=None, input_shape=None, pooling=None,

320

classes=1000, classifier_activation='softmax'):

321

"""ConvNeXtLarge model."""

322

323

def ConvNeXtXLarge(model_name='convnext_xlarge', include_top=True, include_preprocessing=True,

324

weights='imagenet', input_tensor=None, input_shape=None, pooling=None,

325

classes=1000, classifier_activation='softmax'):

326

"""ConvNeXtXLarge model."""

327

```

328

329

### NASNet Models

330

331

Neural Architecture Search networks that were discovered through automated architecture search.

332

333

```python { .api }

334

def NASNetMobile(input_shape=None, include_top=True, weights='imagenet',

335

input_tensor=None, pooling=None, classes=1000):

336

"""

337

NASNetMobile model optimized for mobile devices.

338

339

Parameters:

340

- input_shape: Shape of input images

341

- include_top: Whether to include fully connected layers at top

342

- weights: 'imagenet' or None

343

- input_tensor: Optional tensor to use as image input

344

- pooling: Pooling mode for feature extraction

345

- classes: Number of classes to classify

346

347

Returns:

348

Keras Model instance

349

"""

350

351

def NASNetLarge(input_shape=None, include_top=True, weights='imagenet',

352

input_tensor=None, pooling=None, classes=1000):

353

"""NASNetLarge model for high-accuracy applications."""

354

```

355

356

### Utility Functions

357

358

Functions for preprocessing images and decoding predictions from pre-trained models.

359

360

```python { .api }

361

def preprocess_input(x, data_format=None):

362

"""

363

Preprocess images for pre-trained models.

364

365

Parameters:

366

- x: Input images as numpy array

367

- data_format: Data format ('channels_first' or 'channels_last')

368

369

Returns:

370

Preprocessed images

371

"""

372

373

def decode_predictions(preds, top=5):

374

"""

375

Decode predictions from ImageNet models.

376

377

Parameters:

378

- preds: Numpy array of predictions

379

- top: Number of top predictions to return

380

381

Returns:

382

List of tuples (class_name, class_description, score)

383

"""

384

```

385

386

## Usage Examples

387

388

### Image Classification with Pre-trained Model

389

390

```python

391

import keras

392

from keras.applications import ResNet50

393

from keras.applications.resnet50 import preprocess_input, decode_predictions

394

from keras.utils import load_img, img_to_array

395

import numpy as np

396

397

# Load pre-trained ResNet50 model

398

model = ResNet50(weights='imagenet')

399

400

# Load and preprocess image

401

img_path = 'elephant.jpg'

402

img = load_img(img_path, target_size=(224, 224))

403

x = img_to_array(img)

404

x = np.expand_dims(x, axis=0)

405

x = preprocess_input(x)

406

407

# Make prediction

408

preds = model.predict(x)

409

predictions = decode_predictions(preds, top=3)[0]

410

411

for class_name, description, score in predictions:

412

print(f'{description}: {score:.4f}')

413

```

414

415

### Transfer Learning for Custom Classification

416

417

```python

418

import keras

419

from keras import layers

420

from keras.applications import VGG16

421

422

# Load pre-trained VGG16 without top layers

423

base_model = VGG16(

424

weights='imagenet',

425

include_top=False,

426

input_shape=(224, 224, 3)

427

)

428

429

# Freeze base model layers

430

base_model.trainable = False

431

432

# Add custom classification head

433

model = keras.Sequential([

434

base_model,

435

layers.GlobalAveragePooling2D(),

436

layers.Dense(128, activation='relu'),

437

layers.Dropout(0.2),

438

layers.Dense(num_classes, activation='softmax')

439

])

440

441

# Compile model

442

model.compile(

443

optimizer='adam',

444

loss='categorical_crossentropy',

445

metrics=['accuracy']

446

)

447

448

# Train on custom dataset

449

# model.fit(train_data, epochs=10)

450

```

451

452

### Feature Extraction

453

454

```python

455

import keras

456

from keras.applications import MobileNetV2

457

import numpy as np

458

459

# Load model without classification layers

460

feature_extractor = MobileNetV2(

461

weights='imagenet',

462

include_top=False,

463

pooling='avg'

464

)

465

466

# Extract features from images

467

def extract_features(images):

468

# Preprocess images

469

processed_images = keras.applications.mobilenet_v2.preprocess_input(images)

470

471

# Extract features

472

features = feature_extractor.predict(processed_images)

473

return features

474

475

# Use for similarity search, clustering, etc.

476

# features = extract_features(image_batch)

477

```

478

479

### Fine-tuning with Gradual Unfreezing

480

481

```python

482

import keras

483

from keras.applications import EfficientNetB0

484

485

# Load pre-trained model

486

base_model = EfficientNetB0(

487

weights='imagenet',

488

include_top=False,

489

input_shape=(224, 224, 3)

490

)

491

492

# Initially freeze all layers

493

base_model.trainable = False

494

495

# Add custom head

496

model = keras.Sequential([

497

base_model,

498

layers.GlobalAveragePooling2D(),

499

layers.Dense(num_classes, activation='softmax')

500

])

501

502

# Train with frozen features

503

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

504

# model.fit(train_data, epochs=10)

505

506

# Fine-tune: unfreeze top layers

507

base_model.trainable = True

508

509

# Freeze bottom layers, fine-tune top layers

510

for layer in base_model.layers[:-20]:

511

layer.trainable = False

512

513

# Use lower learning rate for fine-tuning

514

model.compile(

515

optimizer=keras.optimizers.Adam(1e-5),

516

loss='categorical_crossentropy',

517

metrics=['accuracy']

518

)

519

520

# Continue training

521

# model.fit(train_data, epochs=10)

522

```