MCPcopy
hub / github.com/facebookresearch/MetaCLIP / test_inference

Function test_inference

tests/pretrained_test.py:13–33  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11
12
13def test_inference():
14 for model_name in ["ViT-B-32", "ViT-B-32-quickgelu", "ViT-B-16", "ViT-L-14"]:
15 for pretrained in ["metaclip400m", "metaclip2_5b"]:
16 model, _, preprocess = open_clip.create_model_and_transforms(
17 model_name, pretrained=pretrained
18 )
19
20 current_dir = os.path.dirname(os.path.realpath(__file__))
21
22 image = preprocess(Image.open(current_dir + "/../docs/CLIP.png")).unsqueeze(
23 0
24 )
25 text = tokenizer.tokenize(["a diagram", "a dog", "a cat"])
26
27 with torch.no_grad():
28 image_features = model.encode_image(image)
29 text_features = model.encode_text(text)
30
31 text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
32
33 assert text_probs.cpu().numpy()[0].tolist() == [1.0, 0.0, 0.0]

Callers

nothing calls this directly

Calls 2

encode_imageMethod · 0.80
encode_textMethod · 0.45

Tested by

no test coverage detected