clip

Implementation of CLIP (Contrastive Langage-Image Pre-training) model. Based on the original https://arxiv.org/abs/2103.00020.

class mfai.pytorch.models.clip.Clip(settings)[source]

Bases: Module

Implementation of CLIP (Contrastive Langage-Image Pre-training) model. - Based on the original article from OpenAI:

Parameters:

settings (ClipSettings)

encode_text(text_tokens)[source]
Return type:

Tensor

Parameters:

text_tokens (Tensor)

forward(text_tokens, image_input)[source]

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tuple[Tensor, Tensor]

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Parameters:
Return type:

Tuple[Tensor, Tensor]

save_vision_encoder(path)[source]

Save the weights and parameters of the image encoder ResNet50.

Return type:

None

Parameters:

path (Path)

class mfai.pytorch.models.clip.ClipSettings(image_encoder, text_encoder, emb_dim=1024, init_temperature=14.285714285714285)[source]

Bases: object

Parameters:
emb_dim: int
classmethod from_dict(kvs, *, infer_missing=False)
Return type:

TypeVar(A, bound= DataClassJsonMixin)

Parameters:

kvs (dict | list | str | int | float | bool | None)

classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)
Return type:

TypeVar(A, bound= DataClassJsonMixin)

Parameters:

s (str | bytes | bytearray)

image_encoder: ResNet50
init_temperature: float
classmethod schema(*, infer_missing=False, only=None, exclude=(), many=False, context=None, load_only=(), dump_only=(), partial=False, unknown=None)
Return type:

SchemaF[TypeVar(A, bound= DataClassJsonMixin)]

Parameters:
text_encoder: Union[GPT2, Llama2]
to_dict(encode_json=False)
Return type:

Dict[str, Union[dict, list, str, int, float, bool, None]]

to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)
Return type:

str

Parameters: