File size: 16,315 Bytes
0803a67 | 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | from tkinter import E
import torch
import torch.nn as nn
import torch.nn.functional as F
import json
import openai
import matplotlib.pyplot as plt
import numpy as np
import io
from PIL import Image
import base64
from monai.networks.nets import DynUNet
from Segmentation.dynunet_pipeline.create_network import get_kernels_strides
from Segmentation.dynunet_pipeline.task_params import deep_supr_num
from Synthesis.Segmentation.TumorGeneration.utils import synt_model_prepare, synthesize_tumor
from Survival.model.aggregator_wMask import aggregator_wMask
from dataset import ImageDataset
from torch.utils.data import DataLoader
import random
from tqdm import tqdm
from openai import OpenAI
class Policy:
def __init__(self, api_key):
self.client = OpenAI(api_key=api_key)
def get_drug_embolism_sets(self, prompt):
try:
response = self.client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": (
"You are a medical AI assistant. Based on clinical guidelines, generate a JSON with 'drug_set' and 'embolism_set' for TACE treatment.")},
{"role": "user", "content": prompt}
],
temperature=0.2,
)
content = response.choices[0].message.content
result = json.loads(content)
return result.get("drug_set", []), result.get("embolism_set", [])
except Exception as e:
print(f"[GPT-4o Text Prompt Error] {e}")
return [], []
def get_drug_embolism_sets_with_image(self, prompt, image_bytes):
try:
image_base64 = base64.b64encode(image_bytes.getvalue()).decode()
response = self.client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": (
"You are a medical AI assistant. Based on the CT image and prompt, return a JSON with 'drug_set' and 'embolism_set' for TACE treatment.")},
{"role": "user", "content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}}
]}
],
temperature=0.2,
)
content = response.choices[0].message.content
result = json.loads(content)
return result.get("drug_set", []), result.get("embolism_set", [])
except Exception as e:
print(f"[GPT-4o Image+Text Prompt Error] {e}")
return [], []
class SegmentationModel(nn.Module):
def __init__(self, model_path):
super(SegmentationModel, self).__init__()
task_id = 'custom'
kernels, strides = get_kernels_strides(task_id)
self.model = DynUNet(
spatial_dims=3,
in_channels=1,
out_channels=3,
kernel_size=kernels,
strides=strides,
upsample_kernel_size=strides[1:],
norm_name="instance",
deep_supervision=False,
deep_supr_num=deep_supr_num[task_id],
)
self.model.load_state_dict(torch.load(model_path)['state_dict'])
def forward(self, x):
seg_pred = self.model(x)
return seg_pred
class TumorGenerativeWorldModel(nn.Module):
def __init__(self, device, cfg):
super(TumorGenerativeWorldModel, self).__init__()
self.vqgan, self.tester = synt_model_prepare(
device=device,
cfg=cfg,
)
def forward(self, x, tumor_mask, action):
generated_volumes, blur_mask = synthesize_tumor(
ct_volume=x,
tumor_mask=tumor_mask,
# organ_type=cfg.model.organ,
vqgan=self.vqgan,
tester=self.tester,
text_description=action
)
return generated_volumes, blur_mask
class InverseDynamics(nn.Module):
def __init__(self, model_path):
super(InverseDynamics, self).__init__()
from Survival.config import create_arg_parser
args = create_arg_parser()
self.model = aggregator_wMask(args)
loc = "cuda:0"
self.model.load_state_dict(torch.load(model_path, map_location=loc)["state_dict"])
def forward(self, x, gen_x, tumor_mask, pred_mask):
risk_score,_,_,_ = self.model([x, gen_x], [tumor_mask, pred_mask])
return risk_score
def get_conflicting_drugs(drug):
# Rule 1
conflicts = {
'Oxaliplatin': ['Lobaplatin', 'Cisplatin'],
'Lobaplatin': ['Oxaliplatin', 'Cisplatin'],
'Cisplatin': ['Oxaliplatin', 'Lobaplatin'],
'Epirubicin': ['Idarubicin', 'THP'],
'Idarubicin': ['Epirubicin', 'THP'],
'THP': ['Epirubicin', 'Idarubicin']
}
return conflicts.get(drug, [])
def get_conflicting_embolisms(emb):
# Rule 2
conflicts = {
'Absolute Alcohol': ['PVA'],
'PVA': ['Absolute Alcohol', 'Gelatin Sponge'],
'Gelatin Sponge': ['PVA']
}
return conflicts.get(emb, [])
def has_conflicts(item, plan, is_drug=True):
if is_drug:
conflicts = get_conflicting_drugs(item)
else:
conflicts = get_conflicting_embolisms(item)
return any(conflict in plan for conflict in conflicts)
def argmin_drug(fgm_model, hsurv_model, hseg_model, x, tumor_mask, drug_set, plan, scores, T):
best_score = scores
best_drug = None
# 1. Filter out used drugs and conflicting drugs
available_drugs = [d for d in drug_set if d not in plan and not has_conflicts(d, plan, is_drug=True)]
if not available_drugs:
return None, best_score
# 2. Batch processing, each batch processes 2 drugs
BATCH_SIZE = 1
scaler = torch.cuda.amp.GradScaler()
for i in range(0, len(available_drugs), BATCH_SIZE):
batch_drugs = available_drugs[i:i + BATCH_SIZE]
# 3. Build the actions for the current batch
base_action = ';'.join(plan)
actions = []
for d in batch_drugs:
action = d if not base_action else base_action + ';' + d
actions.extend([action] * T)
# 4. Build the input for the current batch
batch_size = len(batch_drugs)
x_batch = x.repeat(batch_size, 1, 1, 1, 1)
tumor_mask_batch = tumor_mask.repeat(batch_size, 1, 1, 1, 1)
# 5. Use mixed precision to calculate the score of the current batch
# with torch.cuda.amp.autocast():
with torch.no_grad():
gen_x, gen_mask = fgm_model(x_batch, tumor_mask_batch, actions)
pred_mask = hseg_model(gen_x)
pred_mask = torch.softmax(pred_mask, 1)
pred_mask = torch.argmax(pred_mask, dim=1).unsqueeze(1)
# print(gen_x.shape, pred_mask.shape, tumor_mask_batch.shape, tumor_mask_post_batch.shape)
scores_batch = hsurv_model(x_batch, gen_x, tumor_mask_batch, pred_mask)
scores_batch = scores_batch.view(batch_size, T).mean(dim=1)
# 6. Update the best score
min_score_idx = torch.argmin(scores_batch)
min_score = scores_batch[min_score_idx]
if min_score < best_score:
best_score = min_score
best_drug = batch_drugs[min_score_idx]
print('score:', min_score, 'best_score:', best_score)
# 7. Clean up memory
del gen_x, gen_mask, pred_mask, scores_batch
torch.cuda.empty_cache()
return best_drug, best_score
def argmin_embolism(fgm_model, hsurv_model, hseg_model, x, tumor_mask, embolism_set, plan, scores, T):
best_score = scores
best_emb = None
# 1. Filter out used embolisms and conflicting embolisms
available_embs = [e for e in embolism_set if e not in plan and not has_conflicts(e, plan, is_drug=False)]
if not available_embs:
return None, best_score
# 2. Batch processing, each batch processes 2 embolisms
BATCH_SIZE = 1
scaler = torch.cuda.amp.GradScaler()
for i in range(0, len(available_embs), BATCH_SIZE):
batch_embs = available_embs[i:i + BATCH_SIZE]
# 3. Build the actions for the current batch
base_action = ';'.join(plan)
actions = []
for e in batch_embs:
action = e + ';' if not base_action else base_action + ';' + e
actions.extend([action] * T)
# 4. Build the input for the current batch
batch_size = len(batch_embs)
x_batch = x.repeat(batch_size, 1, 1, 1, 1)
tumor_mask_batch = tumor_mask.repeat(batch_size, 1, 1, 1, 1)
# 5. Use mixed precision to calculate the score of the current batch
# with torch.cuda.amp.autocast():
with torch.no_grad():
gen_x, gen_mask = fgm_model(x_batch, tumor_mask_batch, actions)
pred_mask = hseg_model(gen_x)
pred_mask = torch.softmax(pred_mask, 1)
pred_mask = torch.argmax(pred_mask, dim=1).unsqueeze(1)
scores_batch = hsurv_model(x_batch, gen_x, tumor_mask_batch, pred_mask)
scores_batch = scores_batch.view(batch_size, T).mean(dim=1)
# 6. Update the best score
min_score_idx = torch.argmin(scores_batch)
min_score = scores_batch[min_score_idx]
if min_score < best_score:
best_score = min_score
best_emb = batch_embs[min_score_idx]
print('score:', min_score, 'best_score:', best_score)
# 7. Clean up memory
del gen_x, gen_mask, pred_mask, scores_batch
torch.cuda.empty_cache()
return best_emb, best_score
def ct_tensor_to_png_bytes(ct_tensor):
# ct_tensor: torch.Tensor, shape [1, D, H, W] or [D, H, W]
if ct_tensor.ndim == 4:
ct_tensor = ct_tensor.squeeze(0)
D = ct_tensor.shape[0]
mid_slice = ct_tensor[D // 2].cpu().numpy()
img = (mid_slice + 1) / 2 * 255
img = np.clip(img, 0, 255)
img = img.astype(np.uint8)
pil_img = Image.fromarray(img)
buf = io.BytesIO()
pil_img.save(buf, format='PNG')
buf.seek(0)
return buf
##############################################################
# 3. Main function: TACE Protocol Exploration with MeWM
##############################################################
def tace_protocol_exploration(
x, tumor_mask,
drug_set, embolism_set,
fgm_model, hsurv_model, hseg_model,
T, B, H_d, H_e
):
"""
Implement the main inference logic.
"""
# Initialize B plans, each starting with a random drug
plans = [[] for _ in range(B)]
scores = [float('inf') for _ in range(B)]
# Drug horizon
for h in range(H_d):
for b in range(B):
drug, score = argmin_drug(fgm_model, hsurv_model, hseg_model, x, tumor_mask, drug_set, plans[b], scores[b], T)
if drug is not None:
plans[b].append(drug)
scores[b] = score
min_score_idx = scores.index(min(scores))
max_score_idx = scores.index(max(scores))
plans[max_score_idx] = plans[min_score_idx].copy()
scores[max_score_idx] = scores[min_score_idx]
# cTACE Rule 3
for b in range(B):
plans[b].append('Lipiodol')
# Continue searching for additional embolisms
for h in range(H_e-1):
for b in range(B):
emb, score = argmin_embolism(fgm_model, hsurv_model, hseg_model, x, tumor_mask, embolism_set, plans[b], scores[b], T)
if emb is not None:
plans[b].append(emb)
scores[b] = score
min_score_idx = scores.index(min(scores))
max_score_idx = scores.index(max(scores))
plans[max_score_idx] = plans[min_score_idx].copy()
scores[max_score_idx] = scores[min_score_idx]
# Select the best plan
min_score_idx = scores.index(min(scores))
best_plan = plans[min_score_idx]
best_score = scores[min_score_idx]
return best_plan, best_score
##############################################################
# 4. Example inference script call
##############################################################
if __name__ == "__main__":
# Model loading
fgm_model = TumorGenerativeWorldModel(device='cuda:0', cfg='Synthesis/model/ddpm.yaml')
hsurv_model = InverseDynamics(model_path='checkpoint_best.pth.tar')
hseg_model = SegmentationModel(model_path='Segmentation/runs/hcc.fold0.nnunet/model.pt')
fgm_model = fgm_model.to('cuda:0')
hseg_model = hseg_model.to('cuda:0')
hsurv_model = hsurv_model.to('cuda:0')
fgm_model.eval()
hseg_model.eval()
hsurv_model.eval()
# Parameter settings
T = 5 # Assume there are 5 tumors
B = 5 # Generate 5 different plans
H_d = 2 # drug horizon
H_e = 2 # embolism h
prompt = (
"You are a radiation oncologist, please **list potential TACE drug and embolism sets** based on the clinical guidelines and patient's pre-treatment CT image. Please follow the below description:"
"###* * Task Description**"
"1. Analyze the input CT images and output potential TACE chemotherapy drug and embolization material sets for treatment. You can include any drugs and embolisms that you think may be helpful for the treatment. Chemotherapy drugs and embolization materials are limited to those in the Action Base."
"2. The TACE action set is output in JSON format, including treatment plan keywords such as drugs and embolization materials."
"### **Action Base**"
"#### **Chemotherapy Drugs**"
"- Raltitrexed"
"- Epirubicin"
"- Oxaliplatin"
"- Lobaplatin"
"- Mitomycin"
"- Idarubicin"
"- Nedaplatin"
"- Pirarubicin"
"- Cisplatin"
"- Idarubicin"
"- THP"
"#### ** Embolization Materials **"
"- Lipiodol"
"- Gelatin Sponge"
"- PVA"
"- Absolute Alcohol"
"- NBCA"
"---"
"output format as JSON: {\"drug_set\": [...], \"embolism_set\": [...]}."
)
# You can also manually set the available drug and embolism sets of your institution
client = Policy(api_key="YOUR_API_KEY")
# drug_set, embolism_set = client.get_drug_embolism_sets(prompt)
# print("Policy generated drug_set:", drug_set)
# print("Policy generated embolism_set:", embolism_set)
# Data loading
data_root = 'your-data-path'
data_list_file = 'data_list.txt'
dataset = ImageDataset(data_root, data_list_file)
dataloader = DataLoader(dataset, batch_size=1, shuffle=False, num_workers=8, pin_memory=True)
# Inference and save results
results_dict = {}
for i, data_dict in tqdm(enumerate(dataloader)):
x = data_dict['input_CT_pre'].cuda()
tumor_mask = data_dict['label_CT_pre'].cuda()
name = data_dict['name']
if name[0] in results_dict:
print(f"Processed {name}: {results_dict[name[0]]}")
continue
x = x.squeeze(0).unsqueeze(1)
tumor_mask = tumor_mask.squeeze(0).unsqueeze(1)
img_bytes = ct_tensor_to_png_bytes(x.cpu().squeeze(1)[0]) # [B, 1, D, H, W] -> [D, H, W]
drug_set, embolism_set = client.get_drug_embolism_sets_with_image(prompt, img_bytes)
print("Policy generated drug_set:", drug_set)
print("Policy generated embolism_set:", embolism_set)
with torch.no_grad():
best_plan, best_risk = tace_protocol_exploration(
x, tumor_mask,
drug_set, embolism_set,
fgm_model, hsurv_model, hseg_model,
T, B, H_d, H_e
)
# Convert tensor to float
best_risk = best_risk.item() if isinstance(best_risk, torch.Tensor) else float(best_risk)
# Add new results
results_dict[name[0]] = {
'keywords': best_plan,
'best_risk': best_risk
}
with open('best_plan.json', 'w') as f:
json.dump(results_dict, f, indent=4)
print(f"Processed {name}: {results_dict[name[0]]}")
|