BASNetImageSegmenter model

[source]

BASNetImageSegmenter class

keras_hub.models.BASNetImageSegmenter(backbone, preprocessor=None, **kwargs)

BASNet image segmentation task.

Arguments

Example

import keras_hub

images = np.ones(shape=(1, 288, 288, 3))
labels = np.zeros(shape=(1, 288, 288, 1))

image_encoder = keras_hub.models.ResNetBackbone.from_preset(
    "resnet_18_imagenet",
    load_weights=False
)
backbone = keras_hub.models.BASNetBackbone(
    image_encoder,
    num_classes=1,
    image_shape=[288, 288, 3]
)
model = keras_hub.models.BASNetImageSegmenter(backbone)

# Evaluate the model
pred_labels = model(images)

# Train the model
model.compile(
    optimizer="adam",
    loss=keras.losses.BinaryCrossentropy(from_logits=False),
    metrics=["accuracy"],
)
model.fit(images, labels, epochs=3)

[source]

from_preset method

BASNetImageSegmenter.from_preset(preset, load_weights=True, **kwargs)

Instantiate a keras_hub.models.Task 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 Task subclass, you can run cls.presets.keys() to list all built-in presets available on the class.

This constructor can be called in one of two ways. Either from a task specific base class like keras_hub.models.CausalLM.from_preset(), or from a model class like keras_hub.models.BertTextClassifier.from_preset(). If calling from the a base class, the subclass of the returning object will be inferred from the config in the preset directory.

Arguments

  • preset: string. A built-in preset identifier, a Kaggle Models handle, a Hugging Face handle, or a path to a local directory.
  • load_weights: bool. If True, saved weights will be loaded into the model architecture. If False, all weights will be randomly initialized.

Examples

# Load a Gemma generative task.
causal_lm = keras_hub.models.CausalLM.from_preset(
    "gemma_2b_en",
)

# Load a Bert classification task.
model = keras_hub.models.TextClassifier.from_preset(
    "bert_base_en",
    num_classes=2,
)
Preset Parameters Description
basnet_duts 108.89M BASNet model with a 34-layer ResNet backbone, pre-trained on the DUTS image dataset at a 288x288 resolution. Model training was performed by Hamid Ali (https://github.com/hamidriasat/BASNet).

backbone property

keras_hub.models.BASNetImageSegmenter.backbone

A keras_hub.models.Backbone model with the core architecture.


preprocessor property

keras_hub.models.BASNetImageSegmenter.preprocessor

A keras_hub.models.Preprocessor layer used to preprocess input.