Spaces:
Running
on
Zero
Running
on
Zero
File size: 8,468 Bytes
c8b42eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
"""
Encoder Factory for UniCeption
"""
import os
from uniception.models.encoders.base import (
EncoderGlobalRepInput,
EncoderInput,
UniCeptionEncoderBase,
UniCeptionViTEncoderBase,
ViTEncoderInput,
ViTEncoderNonImageInput,
ViTEncoderOutput,
)
from uniception.models.encoders.cosmos import CosmosEncoder
from uniception.models.encoders.croco import CroCoEncoder, CroCoIntermediateFeatureReturner
from uniception.models.encoders.dense_rep_encoder import DenseRepresentationEncoder
from uniception.models.encoders.dinov2 import DINOv2Encoder, DINOv2IntermediateFeatureReturner
from uniception.models.encoders.global_rep_encoder import GlobalRepresentationEncoder
from uniception.models.encoders.naradio import NARADIOEncoder
from uniception.models.encoders.patch_embedder import PatchEmbedder
from uniception.models.encoders.radio import RADIOEncoder, RADIOIntermediateFeatureReturner
# Define encoder configurations
ENCODER_CONFIGS = {
"croco": {
"class": CroCoEncoder,
"intermediate_feature_returner_class": CroCoIntermediateFeatureReturner,
"supported_models": ["CroCov2", "DUSt3R", "MASt3R"],
},
"dense_rep_encoder": {
"class": DenseRepresentationEncoder,
"supported_models": ["Dense-Representation-Encoder"],
},
"dinov2": {
"class": DINOv2Encoder,
"intermediate_feature_returner_class": DINOv2IntermediateFeatureReturner,
"supported_models": ["DINOv2", "DINOv2-Registers", "DINOv2-Depth-Anythingv2"],
},
"global_rep_encoder": {
"class": GlobalRepresentationEncoder,
"supported_models": ["Global-Representation-Encoder"],
},
"patch_embedder": {
"class": PatchEmbedder,
"supported_models": ["Patch-Embedder"],
},
"radio": {
"class": RADIOEncoder,
"intermediate_feature_returner_class": RADIOIntermediateFeatureReturner,
"supported_models": ["RADIO", "E-RADIO"],
},
"cosmos": {
"class": CosmosEncoder,
"supported_models": ["Cosmos-Tokenizer CI8x8", "Cosmos-Tokenizer CI16x16"],
},
"naradio": {
"class": NARADIOEncoder,
"supported_models": ["RADIO"],
},
# Add other encoders here
}
def encoder_factory(encoder_str: str, **kwargs) -> UniCeptionEncoderBase:
"""
Encoder factory for UniCeption.
Please use python3 -m uniception.models.encoders.list to see available encoders.
Args:
encoder_str (str): Name of the encoder to create.
**kwargs: Additional keyword arguments to pass to the encoder constructor.
Returns:
UniCeptionEncoderBase: An instance of the specified encoder.
"""
if encoder_str not in ENCODER_CONFIGS:
raise ValueError(
f"Unknown encoder: {encoder_str}. For valid encoder_str options, please use python3 -m uniception.models.encoders.list"
)
encoder_config = ENCODER_CONFIGS[encoder_str]
encoder_class = encoder_config["class"]
return encoder_class(**kwargs)
def feature_returner_encoder_factory(encoder_str: str, **kwargs) -> UniCeptionEncoderBase:
"""
Factory for UniCeption Encoders with support for intermediate feature returning.
Please use python3 -m uniception.models.encoders.list to see available encoders.
Args:
encoder_str (str): Name of the encoder to create.
**kwargs: Additional keyword arguments to pass to the encoder constructor.
Returns:
UniCeptionEncoderBase: An instance of the specified encoder.
"""
if encoder_str not in ENCODER_CONFIGS:
raise ValueError(
f"Unknown encoder: {encoder_str}. For valid encoder_str options, please use python3 -m uniception.models.encoders.list"
)
encoder_config = ENCODER_CONFIGS[encoder_str]
encoder_class = encoder_config["intermediate_feature_returner_class"]
return encoder_class(**kwargs)
def get_available_encoders() -> list:
"""
Get a list of available encoders in UniCeption.
Returns:
list: A list of available encoder names.
"""
return list(ENCODER_CONFIGS.keys())
def print_available_encoder_models():
"""
Print the currently supported encoders in UniCeption.
"""
print("Currently Supported Encoders in UniCeption:\nFormat -> encoder_str: supported_models")
for encoder_name, config in ENCODER_CONFIGS.items():
print(f"{encoder_name}: {', '.join(config['supported_models'])}")
def _make_encoder_test(encoder_str: str, **kwargs) -> UniCeptionEncoderBase:
"Function to create encoders for testing purposes."
current_file_path = os.path.abspath(__file__)
relative_checkpoint_path = os.path.join(os.path.dirname(current_file_path), "../../../checkpoints/encoders")
if encoder_str == "dummy":
return UniCeptionEncoderBase(name="dummy", data_norm_type="dummy")
elif encoder_str == "croco":
return CroCoEncoder(
name="croco",
data_norm_type="croco",
pretrained_checkpoint_path=f"{relative_checkpoint_path}/CroCo_Encoder_224.pth",
patch_embed_cls="PatchEmbedCroCo",
)
elif encoder_str == "dust3r_224":
return CroCoEncoder(
name="dust3r_224",
data_norm_type="dust3r",
pretrained_checkpoint_path=f"{relative_checkpoint_path}/CroCo_Encoder_224_DUSt3R_linear.pth",
patch_embed_cls="PatchEmbedDust3R",
)
elif encoder_str == "dust3r_512":
return CroCoEncoder(
name="dust3r_512",
data_norm_type="dust3r",
pretrained_checkpoint_path=f"{relative_checkpoint_path}/CroCo_Encoder_512_DUSt3R_linear.pth",
patch_embed_cls="ManyAR_PatchEmbed",
img_size=(512, 512),
)
elif encoder_str == "dust3r_512_dpt":
return CroCoEncoder(
name="dust3r_512_dpt",
data_norm_type="dust3r",
pretrained_checkpoint_path=f"{relative_checkpoint_path}/CroCo_Encoder_512_DUSt3R_dpt.pth",
patch_embed_cls="ManyAR_PatchEmbed",
img_size=(512, 512),
)
elif encoder_str == "mast3r_512":
return CroCoEncoder(
name="mast3r_512",
data_norm_type="dust3r",
pretrained_checkpoint_path=f"{relative_checkpoint_path}/CroCo_Encoder_512_MASt3R.pth",
patch_embed_cls="ManyAR_PatchEmbed",
img_size=(512, 512),
)
elif "dinov2" in encoder_str:
size = encoder_str.split("_")[1]
size_single_cap_letter = size[0].upper()
if "reg" in encoder_str:
with_registers = True
pretrained_checkpoint_path = None
elif "dav2" in encoder_str:
with_registers = False
pretrained_checkpoint_path = (
f"{relative_checkpoint_path}/DINOv2_ViT{size_single_cap_letter}_DepthAnythingV2.pth"
)
else:
with_registers = False
pretrained_checkpoint_path = None
return DINOv2Encoder(
name=encoder_str.replace("_reg", ""),
size=size,
with_registers=with_registers,
pretrained_checkpoint_path=pretrained_checkpoint_path,
)
elif "naradio" in encoder_str:
return NARADIOEncoder(
name=encoder_str,
model_version=encoder_str.replace("na", ""),
)
elif "radio" in encoder_str:
if "e-radio" in encoder_str:
eradio_input_shape = (224, 224)
else:
eradio_input_shape = None
return RADIOEncoder(
name=encoder_str,
model_version=encoder_str,
eradio_input_shape=eradio_input_shape,
)
elif "cosmos" in encoder_str:
patch_size = int(encoder_str.split("x")[-1])
return CosmosEncoder(
name=encoder_str,
patch_size=patch_size,
pretrained_checkpoint_path=f"{relative_checkpoint_path}/Cosmos-Tokenizer-CI{patch_size}x{patch_size}/encoder.pth",
)
elif "patch_embedder" in encoder_str:
return PatchEmbedder(
name=encoder_str,
)
else:
raise ValueError(f"Unknown encoder: {encoder_str}")
__all__ = [
"encoder_factory",
"get_available_encoders",
"print_available_encoder_models",
"_make_encoder_test",
"UniCeptionEncoderBase",
"UniCeptionViTEncoderBase",
"EncoderInput",
"ViTEncoderInput",
"ViTEncoderOutput",
]
|