-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTITAN_embedding.py
55 lines (42 loc) · 2.32 KB
/
TITAN_embedding.py
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
from huggingface_hub import login
from transformers import AutoModel
import torch
import pandas as pd
import os
def titan_embbed(file_path):
# Load the CSV
conch_embbed = pd.read_csv(file_path, sep=",")
# Extract coordinates and features
coords = conch_embbed[['Coord_x', 'Coord_y']].values
features = conch_embbed.loc[:, conch_embbed.columns.str.startswith('Comp_')].values
# Convert to tensors
coords_tensor = torch.tensor(coords, dtype=torch.int64).unsqueeze(0)
features_tensor = torch.tensor(features, dtype=torch.float32).unsqueeze(0)
# Encode the slide and save the embedding
with torch.autocast('cuda', torch.float16), torch.inference_mode():
features_tensor = features_tensor.to(device)
coords_tensor = coords_tensor.to(device)
slide_embedding = titan.encode_slide_from_patch_features(features_tensor, coords_tensor, patch_size_lv0)
file_name=file_path.split("/")
# Save the embedding with a meaningful name
output_path = file_name[-1].replace("Conch", "Titan")
output_path = output_path.replace(".svs.csv", ".pt") # Change extension to .pt
print(output_path)
torch.save(slide_embedding, save_path + output_path)
print(f"Embedding saved to {output_path}")
login() # login with your User Access Token, found at https://huggingface.co/settings/tokens
titan = AutoModel.from_pretrained('MahmoodLab/TITAN', trust_remote_code=True)
conch, eval_transform = titan.return_conch()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
titan = titan.to(device)
patch_size_lv0 = 512 #kepts as in the paper for x20 magnification
path_to_conch_embedd="C:/Users/thiba/Documents/INSA_2024-2025/Semestre1/projet_5BIM/Conch_Embbedings/" #Path to be modified
save_path = "C:/Users/thiba/Documents/INSA_2024-2025/Semestre1/projet_5BIM/Titan_embeddings/" #Path to be modified
# Iterate through the files
for file_name in os.listdir(path_to_conch_embedd):
file_path = os.path.join(path_to_conch_embedd, file_name)
if os.path.isfile(file_path) and file_name.endswith(".csv"): # Process only .csv files
try:
titan_embbed(file_path)
except Exception as e:
print(f"Failed to process {file_name}: {e}")