SwinTransformerImageClassifier classkeras_hub.models.SwinTransformerImageClassifier(
backbone,
num_classes,
preprocessor=None,
activation=None,
dropout=0.0,
head_dtype=None,
**kwargs
)
Swin Transformer image classification task.
SwinTransformerImageClassifier tasks wrap a
keras_hub.models.SwinTransformerBackbone and a
keras_hub.models.Preprocessor to create a model that can be used
for image classification. The classifier pools the backbone output
over the sequence dimension and applies a dense classification head.
To fine-tune with fit(), pass a dataset containing tuples of
(x, y) labels where x is an image and y is an integer from
[0, num_classes).
Arguments
keras_hub.models.SwinTransformerBackbone instance
or a keras.Model.None, a keras_hub.models.Preprocessor
instance, a keras.Layer instance, or a callable. If
None no preprocessing will be applied to the inputs.None, str, or callable. The activation function
to use on the Dense layer. Set activation=None to
return the output logits. Defaults to None.None, str, or
keras.mixed_precision.DTypePolicy. The dtype to use for
the classification head's computations and weights.0.0.Examples
Call predict() to run inference.
images = np.random.randint(0, 256, size=(2, 224, 224, 3))
classifier = keras_hub.models.SwinTransformerImageClassifier.from_preset(
"swin_tiny_patch4_window7_224"
)
classifier.predict(images)
Call fit() on a single batch.
images = np.random.randint(0, 256, size=(2, 224, 224, 3))
labels = [0, 3]
classifier = keras_hub.models.SwinTransformerImageClassifier.from_preset(
"swin_tiny_patch4_window7_224"
)
classifier.fit(x=images, y=labels, batch_size=2)
Custom backbone.
images = np.random.randint(0, 256, size=(2, 224, 224, 3))
labels = [0, 3]
backbone = keras_hub.models.SwinTransformerBackbone(
image_shape=(224, 224, 3),
embed_dim=96,
depths=(2, 2, 6, 2),
num_heads=(3, 6, 12, 24),
window_size=7,
)
classifier = keras_hub.models.SwinTransformerImageClassifier(
backbone=backbone,
num_classes=4,
)
classifier.fit(x=images, y=labels, batch_size=2)
from_preset methodSwinTransformerImageClassifier.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:
'bert_base_en''kaggle://user/bert/keras/bert_base_en''hf://user/bert_base_en''./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
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 |
|---|---|---|
| swin_tiny_patch4_window7_224 | 27.52M | Swin-Tiny model pre-trained on the ImageNet 1k dataset with image resolution of 224x224. |
| swin_small_patch4_window7_224 | 48.84M | Swin-Small model pre-trained on the ImageNet 1k dataset with image resolution of 224x224. |
| swin_base_patch4_window7_224 | 86.74M | Swin-Base model pre-trained on the ImageNet 1k dataset with image resolution of 224x224. |
| swin_base_patch4_window12_384 | 86.88M | Swin-Base model pre-trained on the ImageNet 1k dataset with image resolution of 384x384. |
| swin_large_patch4_window7_224 | 195.00M | Swin-Large model pre-trained on the ImageNet 1k dataset with image resolution of 224x224. |
| swin_large_patch4_window12_384 | 195.20M | Swin-Large model pre-trained on the ImageNet 1k dataset with image resolution of 384x384. |
backbone propertykeras_hub.models.SwinTransformerImageClassifier.backbone
A keras_hub.models.Backbone model with the core architecture.
preprocessor propertykeras_hub.models.SwinTransformerImageClassifier.preprocessor
A keras_hub.models.Preprocessor layer used to preprocess input.