Mikecode123 commited on
Commit
684e1fb
·
verified ·
1 Parent(s): 6251f9d

Create predict.py

Browse files
Files changed (1) hide show
  1. predict.py +193 -0
predict.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==============================================================
2
+ # predict.py – How to use the trained Walnut Multimodal Models
3
+ # ==============================================================
4
+
5
+ import torch
6
+ import torch.nn as nn
7
+ from torchvision import models
8
+ from torchvision.transforms import functional as TF
9
+ from PIL import Image
10
+ from pathlib import Path
11
+ import pandas as pd
12
+
13
+ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+
15
+ # ----------------------------------------------------------
16
+ # Model definitions (must match training)
17
+ # ----------------------------------------------------------
18
+ class HistoEncoder(nn.Module):
19
+ def __init__(self, embed_dim=512):
20
+ super().__init__()
21
+ backbone = models.densenet121(weights=None)
22
+ self.features = backbone.features
23
+ self.pool = nn.AdaptiveAvgPool2d(1)
24
+ self.proj = nn.Sequential(
25
+ nn.Linear(1024, embed_dim),
26
+ nn.ReLU(),
27
+ nn.LayerNorm(embed_dim)
28
+ )
29
+ def forward(self, x):
30
+ x = self.features(x)
31
+ x = self.pool(x).flatten(1)
32
+ return self.proj(x)
33
+
34
+ class IHCEncoder(nn.Module):
35
+ def __init__(self, embed_dim=512):
36
+ super().__init__()
37
+ backbone = models.densenet121(weights=None)
38
+ self.features = backbone.features
39
+ self.pool = nn.AdaptiveAvgPool2d(1)
40
+ self.proj = nn.Sequential(
41
+ nn.Linear(1024, embed_dim),
42
+ nn.ReLU(),
43
+ nn.LayerNorm(embed_dim)
44
+ )
45
+ def forward(self, x):
46
+ x = self.features(x)
47
+ x = self.pool(x).flatten(1)
48
+ return self.proj(x)
49
+
50
+ class MolecularMLP(nn.Module):
51
+ def __init__(self, in_dim=5, embed_dim=128):
52
+ super().__init__()
53
+ self.encoder = nn.Sequential(
54
+ nn.Linear(in_dim, 64), nn.ReLU(), nn.BatchNorm1d(64), nn.Dropout(0.3),
55
+ nn.Linear(64, embed_dim), nn.ReLU(), nn.LayerNorm(embed_dim)
56
+ )
57
+ self.classifier = nn.Sequential(
58
+ nn.Linear(embed_dim, 64), nn.ReLU(), nn.Dropout(0.3),
59
+ nn.Linear(64, 3)
60
+ )
61
+ def forward(self, x):
62
+ return self.classifier(self.encoder(x))
63
+ def extract_features(self, x):
64
+ return self.encoder(x)
65
+
66
+ class FusionModel(nn.Module):
67
+ def __init__(self):
68
+ super().__init__()
69
+ self.histo_enc = HistoEncoder(512)
70
+ self.ihc_enc = IHCEncoder(512)
71
+ self.mol_enc = MolecularMLP(5, 128)
72
+ self.fusion = nn.Sequential(
73
+ nn.Linear(512+512+128, 256),
74
+ nn.ReLU(),
75
+ nn.Dropout(0.4),
76
+ nn.Linear(256, 64),
77
+ nn.ReLU(),
78
+ nn.Dropout(0.3),
79
+ nn.Linear(64, 3)
80
+ )
81
+ def forward(self, histo, ihc, mol):
82
+ h = self.histo_enc(histo)
83
+ i = self.ihc_enc(ihc)
84
+ m = self.mol_enc.extract_features(mol)
85
+ x = torch.cat([h, i, m], dim=1)
86
+ return self.fusion(x)
87
+
88
+ # ----------------------------------------------------------
89
+ # Preprocessing
90
+ # ----------------------------------------------------------
91
+ def preprocess_image(img_path):
92
+ img = Image.open(img_path).convert("RGB")
93
+ img = TF.resize(img, [224, 224])
94
+ img = TF.to_tensor(img)
95
+ img = TF.normalize(img, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
96
+ return img.unsqueeze(0) # add batch dimension
97
+
98
+ # ----------------------------------------------------------
99
+ # Load models
100
+ # ----------------------------------------------------------
101
+ def load_models(model_dir="."):
102
+ model_dir = Path(model_dir)
103
+
104
+ # Molecular only
105
+ mol_model = MolecularMLP().to(DEVICE)
106
+ mol_ckpt = torch.load(model_dir / "best_molecular_mlp.pt", map_location=DEVICE)
107
+ mol_model.load_state_dict(mol_ckpt["model_state"])
108
+ mol_model.eval()
109
+
110
+ # Fusion model
111
+ fusion_model = FusionModel().to(DEVICE)
112
+ fusion_path = model_dir / "best_fusion_model.pt"
113
+ if fusion_path.exists():
114
+ fusion_model.load_state_dict(torch.load(fusion_path, map_location=DEVICE))
115
+ fusion_model.eval()
116
+ else:
117
+ fusion_model = None
118
+ print("Fusion model not found.")
119
+
120
+ return mol_model, fusion_model
121
+
122
+ # ----------------------------------------------------------
123
+ # Predict function
124
+ # ----------------------------------------------------------
125
+ STATE_NAMES = {
126
+ 0: "Normal",
127
+ 1: "Partial_Restoration",
128
+ 2: "Induced_Neoplastic"
129
+ }
130
+
131
+ def predict_molecular(mol_model, flavonoid, alkaloid, terpenoid, phenol, steroid):
132
+ x = torch.tensor([[flavonoid, alkaloid, terpenoid, phenol, steroid]], dtype=torch.float32).to(DEVICE)
133
+ with torch.no_grad():
134
+ logits = mol_model(x)
135
+ probs = torch.softmax(logits, dim=1)[0]
136
+ pred = logits.argmax(dim=1).item()
137
+ return {
138
+ "predicted_state": STATE_NAMES[pred],
139
+ "state_id": pred,
140
+ "probabilities": {
141
+ "Normal": probs[0].item(),
142
+ "Partial_Restoration": probs[1].item(),
143
+ "Induced_Neoplastic": probs[2].item()
144
+ }
145
+ }
146
+
147
+ def predict_fusion(fusion_model, histo_path, ihc_path, flavonoid, alkaloid, terpenoid, phenol, steroid):
148
+ histo = preprocess_image(histo_path).to(DEVICE)
149
+ ihc = preprocess_image(ihc_path).to(DEVICE)
150
+ mol = torch.tensor([[flavonoid, alkaloid, terpenoid, phenol, steroid]], dtype=torch.float32).to(DEVICE)
151
+
152
+ with torch.no_grad():
153
+ logits = fusion_model(histo, ihc, mol)
154
+ probs = torch.softmax(logits, dim=1)[0]
155
+ pred = logits.argmax(dim=1).item()
156
+
157
+ return {
158
+ "predicted_state": STATE_NAMES[pred],
159
+ "state_id": pred,
160
+ "probabilities": {
161
+ "Normal": probs[0].item(),
162
+ "Partial_Restoration": probs[1].item(),
163
+ "Induced_Neoplastic": probs[2].item()
164
+ }
165
+ }
166
+
167
+ # ----------------------------------------------------------
168
+ # Example usage
169
+ # ----------------------------------------------------------
170
+ if __name__ == "__main__":
171
+ print("Loading models...")
172
+ mol_model, fusion_model = load_models("/kaggle/working") # or path to downloaded models
173
+
174
+ # Example 1: Molecular only prediction
175
+ print("\n--- Molecular Only Prediction ---")
176
+ result = predict_molecular(
177
+ mol_model,
178
+ flavonoid=14.30,
179
+ alkaloid=4.07,
180
+ terpenoid=2.57,
181
+ phenol=1.80,
182
+ steroid=0.22
183
+ )
184
+ print(result)
185
+
186
+ # Example 2: Full Fusion prediction (if you have images)
187
+ # result = predict_fusion(
188
+ # fusion_model,
189
+ # histo_path="path/to/H&E.png",
190
+ # ihc_path="path/to/p63.png",
191
+ # flavonoid=14.30, alkaloid=4.07, terpenoid=2.57, phenol=1.80, steroid=0.22
192
+ # )
193
+ # print(result)