-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathimplementation.py
More file actions
39 lines (32 loc) · 1.13 KB
/
implementation.py
File metadata and controls
39 lines (32 loc) · 1.13 KB
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
def conch_extract_features(
input_image: str = "/mount/input/TUM/TUM-TCGA-ACRLPPQE.tif",
) -> dict:
"""
Perform feature extraction on an input image using CONCH.
Args:
input_image: Path to the input image
Returns:
dict with the following structure:
{
'features': list # The feature vector extracted from the input image, as a list of floats
}
"""
import os
import torch
from conch.open_clip_custom.factory import create_model_from_pretrained
from PIL import Image
hf_token = os.environ.get("HF_TOKEN")
model, preprocess = create_model_from_pretrained(
model_cfg="conch_ViT-B-16",
checkpoint_path="/workspace/CONCH/checkpoints/conch/pytorch_model.bin",
device="cpu",
hf_auth_token=hf_token,
)
image = Image.open(input_image).convert("RGB")
image_tensor = preprocess(image).unsqueeze(0)
with torch.inference_mode():
image_embs = model.encode_image(
image_tensor, proj_contrast=False, normalize=False
)
features = image_embs.cpu().numpy().tolist()[0]
return {"features": features}