KerasHub: Pretrained Models / API documentation / Model Architectures / EfficientNet / EfficientNetImageClassifierPreprocessor layer

EfficientNetImageClassifierPreprocessor layer

[source]

EfficientNetImageClassifierPreprocessor class

keras_hub.models.EfficientNetImageClassifierPreprocessor(
    image_converter=None, **kwargs
)

Base class for image classification preprocessing layers.

ImageClassifierPreprocessor tasks wraps a keras_hub.layers.ImageConverter to create a preprocessing layer for image classification tasks. It is intended to be paired with a keras_hub.models.ImageClassifier task.

All ImageClassifierPreprocessor take inputs three inputs, x, y, and sample_weight. x, the first input, should always be included. It can be a image or batch of images. See examples below. y and sample_weight are optional inputs that will be passed through unaltered. Usually, y will be the classification label, and sample_weight will not be provided.

The layer will output either x, an (x, y) tuple if labels were provided, or an (x, y, sample_weight) tuple if labels and sample weight were provided. x will be the input images after all model preprocessing has been applied.

All ImageClassifierPreprocessor tasks include a from_preset() constructor which can be used to load a pre-trained config and vocabularies. You can call the from_preset() constructor directly on this base class, in which case the correct class for your model will be automatically instantiated.

Examples.

preprocessor = keras_hub.models.ImageClassifierPreprocessor.from_preset(
    "resnet_50",
)

# Resize a single image for resnet 50.
x = np.random.randint(0, 256, (512, 512, 3))
x = preprocessor(x)

# Resize a labeled image.
x, y = np.random.randint(0, 256, (512, 512, 3)), 1
x, y = preprocessor(x, y)

# Resize a batch of labeled images.
x, y = [
    np.random.randint(0, 256, (512, 512, 3)),
    np.zeros((512, 512, 3))
], [1, 0]
x, y = preprocessor(x, y)

# Use a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset).
ds = tf.data.Dataset.from_tensor_slices((x, y)).batch(2)
ds = ds.map(preprocessor, num_parallel_calls=tf.data.AUTOTUNE)

[source]

from_preset method

EfficientNetImageClassifierPreprocessor.from_preset(
    preset, config_file="preprocessor.json", **kwargs
)

Instantiate a keras_hub.models.Preprocessor from a model preset.

A preset is a directory of configs, weights and other file assets used to save and load a pre-trained model. The preset can be passed as one of:

  1. a built-in preset identifier like 'bert_base_en'
  2. a Kaggle Models handle like 'kaggle://user/bert/keras/bert_base_en'
  3. a Hugging Face handle like 'hf://user/bert_base_en'
  4. a path to a local preset directory like './bert_base_en'

For any Preprocessor subclass, you can run cls.presets.keys() to list all built-in presets available on the class.

As there are usually multiple preprocessing classes for a given model, this method should be called on a specific subclass like keras_hub.models.BertTextClassifierPreprocessor.from_preset().

Arguments

  • preset: string. A built-in preset identifier, a Kaggle Models handle, a Hugging Face handle, or a path to a local directory.

Examples

# Load a preprocessor for Gemma generation.
preprocessor = keras_hub.models.CausalLMPreprocessor.from_preset(
    "gemma_2b_en",
)

# Load a preprocessor for Bert classification.
preprocessor = keras_hub.models.TextClassifierPreprocessor.from_preset(
    "bert_base_en",
)
Preset Parameters Description
efficientnet_lite0_ra_imagenet 4.65M EfficientNet-Lite model fine-trained on the ImageNet 1k dataset with RandAugment recipe.
efficientnet_b0_ra_imagenet 5.29M EfficientNet B0 model pre-trained on the ImageNet 1k dataset with RandAugment recipe.
efficientnet_b0_ra4_e3600_r224_imagenet 5.29M EfficientNet B0 model pre-trained on the ImageNet 1k dataset by Ross Wightman. Trained with timm scripts using hyper-parameters inspired by the MobileNet-V4 small, mixed with go-to hparams from timm and 'ResNet Strikes Back'.
efficientnet_es_ra_imagenet 5.44M EfficientNet-EdgeTPU Small model trained on the ImageNet 1k dataset with RandAugment recipe.
efficientnet_em_ra2_imagenet 6.90M EfficientNet-EdgeTPU Medium model trained on the ImageNet 1k dataset with RandAugment2 recipe.
efficientnet_b1_ft_imagenet 7.79M EfficientNet B1 model fine-tuned on the ImageNet 1k dataset.
efficientnet_b1_ra4_e3600_r240_imagenet 7.79M EfficientNet B1 model pre-trained on the ImageNet 1k dataset by Ross Wightman. Trained with timm scripts using hyper-parameters inspired by the MobileNet-V4 small, mixed with go-to hparams from timm and 'ResNet Strikes Back'.
efficientnet_b2_ra_imagenet 9.11M EfficientNet B2 model pre-trained on the ImageNet 1k dataset with RandAugment recipe.
efficientnet_el_ra_imagenet 10.59M EfficientNet-EdgeTPU Large model trained on the ImageNet 1k dataset with RandAugment recipe.
efficientnet_b3_ra2_imagenet 12.23M EfficientNet B3 model pre-trained on the ImageNet 1k dataset with RandAugment2 recipe.
efficientnet2_rw_t_ra2_imagenet 13.65M EfficientNet-v2 Tiny model trained on the ImageNet 1k dataset with RandAugment2 recipe.
efficientnet_b4_ra2_imagenet 19.34M EfficientNet B4 model pre-trained on the ImageNet 1k dataset with RandAugment2 recipe.
efficientnet2_rw_s_ra2_imagenet 23.94M EfficientNet-v2 Small model trained on the ImageNet 1k dataset with RandAugment2 recipe.
efficientnet_b5_sw_imagenet 30.39M EfficientNet B5 model pre-trained on the ImageNet 12k dataset by Ross Wightman. Based on Swin Transformer train / pretrain recipe with modifications (related to both DeiT and ConvNeXt recipes).
efficientnet_b5_sw_ft_imagenet 30.39M EfficientNet B5 model pre-trained on the ImageNet 12k dataset and fine-tuned on ImageNet-1k by Ross Wightman. Based on Swin Transformer train / pretrain recipe with modifications (related to both DeiT and ConvNeXt recipes).
efficientnet2_rw_m_agc_imagenet 53.24M EfficientNet-v2 Medium model trained on the ImageNet 1k dataset with adaptive gradient clipping.

image_converter property

keras_hub.models.EfficientNetImageClassifierPreprocessor.image_converter

The image converter used to preprocess image data.