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

Function test_inference

tests/simple_test.py:16–30  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

encode_imageMethod · 0.80
encode_textMethod · 0.45

Tested by

no test coverage detected