DINOV2Backbone
classkeras_hub.models.DINOV2Backbone(
patch_size,
num_layers,
hidden_dim,
num_heads,
intermediate_dim,
layer_scale_init_value=1.0,
num_register_tokens=0,
use_mask_token=True,
use_swiglu_ffn=False,
dropout_rate=0.0,
drop_path_rate=0.0,
image_shape=(224, 224, 3),
position_embedding_shape=(518, 518, 3),
antialias_in_interpolation=False,
data_format=None,
dtype=None,
name=None,
**kwargs
)
DINOV2 core network with hyperparameters.
DINOV2 offers a powerful, generalist visual backbone learned entirely from unlabeled images as described in DINOv2: Learning Robust Visual Features without Supervision
The default constructor gives a fully customizable, randomly initialized
DINOV2 model with any number of layers, heads, and embedding dimensions. To
load preset architectures and weights, use the from_preset
constructor.
Note that this backbone supports interpolation of the position embeddings
to the input image shape. This is useful when the input image shape is
different from the shape used to train the position embeddings. The
position_embedding_shape
argument is used to specify the original shape
used to train the position embeddings.
Arguments
1.0
.0
.True
.False
.0.0
.0.0
.(224, 224, 3)
.(518, 518)
.False
.None
or str. If specified, either "channels_last"
or
"channels_first"
. The ordering of the dimensions in the
inputs. "channels_last"
corresponds to inputs with shape
(batch_size, height, width, channels)
while "channels_first"
corresponds to inputs with shape
(batch_size, channels, height, width)
. It defaults to the
image_data_format
value found in your Keras config file at
~/.keras/keras.json
. If you never set it, then it will be
"channels_last"
.keras.mixed_precision.DTypePolicy
. The dtype to use
for the models computations and weights. Note that some
computations, such as softmax and layer normalization will always
be done a float32 precision regardless of dtype.Example
# Pretrained DINOV2 model.
input_data = {
"images": np.ones(shape=(1, 518, 518, 3), dtype="float32"),
}
model = keras_hub.models.DINOV2Backbone.from_preset(
"dinov2_base"
)
model(input_data)
# Pretrained DINOV2 model with custom image shape.
input_data = {
"images": np.ones(shape=(1, 224, 224, 3), dtype="float32"),
}
model = keras_hub.models.DINOV2Backbone.from_preset(
"dinov2_base", image_shape=(224, 224, 3)
)
model(input_data)
# Randomly initialized DINOV2 model with custom config.
model = keras_hub.models.DINOV2Backbone(
patch_size=14,
num_layers=2,
hidden_dim=32,
num_heads=2,
intermediate_dim=128,
image_shape=(224, 224, 3),
position_embedding_shape=(518, 518),
)
model(input_data)
from_preset
methodDINOV2Backbone.from_preset(preset, load_weights=True, **kwargs)
Instantiate a keras_hub.models.Backbone
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 a
one of:
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./bert_base_en'
This constructor can be called in one of two ways. Either from the base
class like keras_hub.models.Backbone.from_preset()
, or from
a model class like keras_hub.models.GemmaBackbone.from_preset()
.
If calling from the base class, the subclass of the returning object
will be inferred from the config in the preset directory.
For any Backbone
subclass, you can run cls.presets.keys()
to list
all built-in presets available on the class.
Arguments
True
, the weights will be loaded into the
model architecture. If False
, the weights will be randomly
initialized.Examples
# Load a Gemma backbone with pre-trained weights.
model = keras_hub.models.Backbone.from_preset(
"gemma_2b_en",
)
# Load a Bert backbone with a pre-trained config and random weights.
model = keras_hub.models.Backbone.from_preset(
"bert_base_en",
load_weights=False,
)
Preset | Parameters | Description |
---|---|---|
dinov2_small | 22.58M | Vision Transformer (small-sized model) trained using DINOv2. |
dinov2_with_registers_small | 22.58M | Vision Transformer (small-sized model) trained using DINOv2, with registers. |
dinov2_base | 87.63M | Vision Transformer (base-sized model) trained using DINOv2. |
dinov2_with_registers_base | 87.64M | Vision Transformer (base-sized model) trained using DINOv2, with registers. |
dinov2_large | 305.77M | Vision Transformer (large-sized model) trained using DINOv2. |
dinov2_with_registers_large | 305.78M | Vision Transformer (large-sized model) trained using DINOv2, with registers. |
dinov2_giant | 1.14B | Vision Transformer (giant-sized model) trained using DINOv2. |
dinov2_with_registers_giant | 1.14B | Vision Transformer (giant-sized model) trained using DINOv2, with registers. |