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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=================================================================
|
| 3 |
+
INFERENCE v1.0.0 — Full-image + SCSE attention + TTA
|
| 4 |
+
=================================================================
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os, cv2, torch, numpy as np, json, time, csv
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
import segmentation_models_pytorch as smp
|
| 11 |
+
from segmentation_models_pytorch.decoders.unet.decoder import UnetDecoder
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
# ============================================================
|
| 15 |
+
# MODEL v1.0.0
|
| 16 |
+
# ============================================================
|
| 17 |
+
class TubuleSegModel(nn.Module):
|
| 18 |
+
def __init__(self, attention_type='scse'):
|
| 19 |
+
super().__init__()
|
| 20 |
+
self.encoder = smp.encoders.get_encoder(
|
| 21 |
+
'timm-efficientnet-b4', in_channels=3, depth=5, weights=None
|
| 22 |
+
)
|
| 23 |
+
ec = self.encoder.out_channels
|
| 24 |
+
self.seg_decoder = UnetDecoder(
|
| 25 |
+
encoder_channels=ec, decoder_channels=(256,128,64,32,16),
|
| 26 |
+
n_blocks=5, use_norm='batchnorm', attention_type=attention_type
|
| 27 |
+
)
|
| 28 |
+
self.seg_head = nn.Conv2d(16, 3, kernel_size=1)
|
| 29 |
+
self.border_decoder = UnetDecoder(
|
| 30 |
+
encoder_channels=ec, decoder_channels=(256,128,64,32,16),
|
| 31 |
+
n_blocks=5, use_norm='batchnorm', attention_type=attention_type
|
| 32 |
+
)
|
| 33 |
+
self.border_head = nn.Conv2d(16, 2, kernel_size=1)
|
| 34 |
+
|
| 35 |
+
def forward(self, x):
|
| 36 |
+
features = self.encoder(x)
|
| 37 |
+
return (
|
| 38 |
+
self.seg_head(self.seg_decoder(features)),
|
| 39 |
+
self.border_head(self.border_decoder(features))
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# ============================================================
|
| 44 |
+
# TTA — Test-Time Augmentation
|
| 45 |
+
# ============================================================
|
| 46 |
+
def predict_with_tta(model, img_t, device):
|
| 47 |
+
"""
|
| 48 |
+
8 pasadas: 4 rotaciones (0, 90, 180, 270) x 2 flips (original, horizontal).
|
| 49 |
+
Promedia probabilidades softmax en el espacio original.
|
| 50 |
+
img_t: [1, 3, H, W] tensor normalizado en device.
|
| 51 |
+
Retorna: [3, H, W] numpy array de probabilidades promediadas.
|
| 52 |
+
"""
|
| 53 |
+
preds = []
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
for flip in [False, True]:
|
| 56 |
+
x = img_t.flip(-1) if flip else img_t
|
| 57 |
+
for k in range(4): # rotaciones 0, 90, 180, 270
|
| 58 |
+
x_rot = torch.rot90(x, k, dims=[-2, -1])
|
| 59 |
+
seg_logits, _ = model(x_rot)
|
| 60 |
+
prob = F.softmax(seg_logits, dim=1) # [1, 3, H, W]
|
| 61 |
+
# Revertir la rotacion
|
| 62 |
+
prob = torch.rot90(prob, -k, dims=[-2, -1])
|
| 63 |
+
# Revertir el flip
|
| 64 |
+
if flip:
|
| 65 |
+
prob = prob.flip(-1)
|
| 66 |
+
preds.append(prob.cpu().numpy()[0]) # [3, H, W]
|
| 67 |
+
|
| 68 |
+
return np.mean(preds, axis=0) # [3, H, W]
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# ============================================================
|
| 72 |
+
# POST-PROCESSING (identico a inference_v22.py)
|
| 73 |
+
# ============================================================
|
| 74 |
+
def postprocess(pred, h, w):
|
| 75 |
+
epi_mask = (pred == 1).astype(np.uint8)
|
| 76 |
+
n, labels = cv2.connectedComponents(epi_mask)
|
| 77 |
+
if n > 1:
|
| 78 |
+
areas = [(lid, (labels==lid).sum()) for lid in range(1, n)]
|
| 79 |
+
areas.sort(key=lambda x: x[1], reverse=True)
|
| 80 |
+
epi_clean = (labels == areas[0][0]).astype(np.uint8)
|
| 81 |
+
else:
|
| 82 |
+
epi_clean = epi_mask
|
| 83 |
+
|
| 84 |
+
k_close = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
|
| 85 |
+
epi_closed = cv2.morphologyEx(epi_clean, cv2.MORPH_CLOSE, k_close)
|
| 86 |
+
|
| 87 |
+
flood = epi_closed.copy()
|
| 88 |
+
fm = np.zeros((h+2, w+2), dtype=np.uint8)
|
| 89 |
+
for y in range(0, h, 3):
|
| 90 |
+
if flood[y, 0] == 0: cv2.floodFill(flood, fm, (0, y), 2)
|
| 91 |
+
if flood[y, w-1] == 0: cv2.floodFill(flood, fm, (w-1, y), 2)
|
| 92 |
+
for x in range(0, w, 3):
|
| 93 |
+
if flood[0, x] == 0: cv2.floodFill(flood, fm, (x, 0), 2)
|
| 94 |
+
if flood[h-1, x] == 0: cv2.floodFill(flood, fm, (x, h-1), 2)
|
| 95 |
+
holes = (flood == 0).astype(np.uint8)
|
| 96 |
+
epi_filled = epi_closed | holes
|
| 97 |
+
|
| 98 |
+
final = np.zeros((h, w), dtype=np.uint8)
|
| 99 |
+
final[epi_filled > 0] = 1
|
| 100 |
+
|
| 101 |
+
lum_mask = (pred == 2).astype(np.uint8)
|
| 102 |
+
lum_inside = lum_mask & epi_filled
|
| 103 |
+
n, labels = cv2.connectedComponents(lum_inside)
|
| 104 |
+
if n > 1:
|
| 105 |
+
lum_clean = np.zeros_like(lum_inside)
|
| 106 |
+
for lid in range(1, n):
|
| 107 |
+
comp = (labels == lid).astype(np.uint8)
|
| 108 |
+
if comp.sum() < 300: continue
|
| 109 |
+
dilated = cv2.dilate(comp, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)))
|
| 110 |
+
border_px = dilated & (~epi_filled.astype(bool)).astype(np.uint8)
|
| 111 |
+
if border_px.sum() < comp.sum() * 0.05:
|
| 112 |
+
lum_clean[labels == lid] = 1
|
| 113 |
+
final[lum_clean > 0] = 2
|
| 114 |
+
|
| 115 |
+
return final
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
# ============================================================
|
| 119 |
+
# MAIN
|
| 120 |
+
# ============================================================
|
| 121 |
+
def main():
|
| 122 |
+
base_dir = Path(r"D:\Lu\AI\Tubules"); os.chdir(str(base_dir))
|
| 123 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 124 |
+
print(f"Device: {device}")
|
| 125 |
+
|
| 126 |
+
model_file = 'best_model_segmentation_v1.0.0.pt'
|
| 127 |
+
attention = 'scse'
|
| 128 |
+
|
| 129 |
+
model = TubuleSegModel(attention_type=attention)
|
| 130 |
+
ckpt = torch.load(str(base_dir / model_file), map_location='cpu', weights_only=False)
|
| 131 |
+
model.load_state_dict(ckpt['model_state_dict'])
|
| 132 |
+
model.to(device); model.eval()
|
| 133 |
+
|
| 134 |
+
from huggingface_hub import snapshot_download
|
| 135 |
+
data_repo = base_dir / 'data_repo'
|
| 136 |
+
if not data_repo.exists():
|
| 137 |
+
snapshot_download('LuGot16/tubules', repo_type='dataset', local_dir=str(data_repo))
|
| 138 |
+
|
| 139 |
+
test_dir = data_repo / 'area_test'
|
| 140 |
+
out_dir = base_dir / f'inference_results_{model_file.replace("best_model_","").replace(".pt","")}'
|
| 141 |
+
out_dir.mkdir(exist_ok=True)
|
| 142 |
+
|
| 143 |
+
SCALE = 0.32 # um/pixel
|
| 144 |
+
results = []
|
| 145 |
+
files = sorted(test_dir.glob('*.tif'))
|
| 146 |
+
t0 = time.time()
|
| 147 |
+
|
| 148 |
+
for i, fp in enumerate(files):
|
| 149 |
+
img = cv2.imread(str(fp))
|
| 150 |
+
if img is None: continue
|
| 151 |
+
h, w = img.shape[:2]
|
| 152 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 153 |
+
|
| 154 |
+
img_resized = cv2.resize(img_rgb, (512, 512), interpolation=cv2.INTER_LINEAR)
|
| 155 |
+
img_norm = (img_resized.astype(np.float32)/255.0 - [0.485,0.456,0.406]) / [0.229,0.224,0.225]
|
| 156 |
+
img_t = torch.from_numpy(img_norm.transpose(2,0,1)).float().unsqueeze(0).to(device)
|
| 157 |
+
|
| 158 |
+
# TTA
|
| 159 |
+
seg_probs = predict_with_tta(model, img_t, device) # [3, 512, 512]
|
| 160 |
+
|
| 161 |
+
pred_512 = seg_probs.argmax(0).astype(np.uint8)
|
| 162 |
+
pred = cv2.resize(pred_512, (w, h), interpolation=cv2.INTER_NEAREST)
|
| 163 |
+
pred = postprocess(pred, h, w)
|
| 164 |
+
|
| 165 |
+
total = h * w
|
| 166 |
+
epi_pct = (pred==1).sum() / total * 100
|
| 167 |
+
lum_pct = (pred==2).sum() / total * 100
|
| 168 |
+
tub_pct = epi_pct + lum_pct
|
| 169 |
+
epi_um2 = (pred==1).sum() * SCALE * SCALE
|
| 170 |
+
lum_um2 = (pred==2).sum() * SCALE * SCALE
|
| 171 |
+
tub_um2 = epi_um2 + lum_um2
|
| 172 |
+
status = "OK" if tub_pct >= 30 and lum_pct > 1 else "CHECK"
|
| 173 |
+
|
| 174 |
+
results.append({
|
| 175 |
+
'image': fp.name,
|
| 176 |
+
'tubule_pct': round(tub_pct, 1),
|
| 177 |
+
'epithelium_pct': round(epi_pct, 1),
|
| 178 |
+
'lumen_pct': round(lum_pct, 1),
|
| 179 |
+
'tubule_um2': round(tub_um2, 1),
|
| 180 |
+
'epithelium_um2': round(epi_um2, 1),
|
| 181 |
+
'lumen_um2': round(lum_um2, 1),
|
| 182 |
+
'lumen_epi_ratio': round(lum_um2 / (epi_um2 + 1e-6), 4),
|
| 183 |
+
'status': status
|
| 184 |
+
})
|
| 185 |
+
|
| 186 |
+
# Overlay
|
| 187 |
+
overlay = img.copy()
|
| 188 |
+
overlay[pred==1] = (overlay[pred==1]*0.5 + np.array([0,180,0])*0.5).astype(np.uint8)
|
| 189 |
+
overlay[pred==2] = (overlay[pred==2]*0.5 + np.array([255,100,0])*0.5).astype(np.uint8)
|
| 190 |
+
|
| 191 |
+
tubule_mask = (pred >= 1).astype(np.uint8)
|
| 192 |
+
contours_outer, _ = cv2.findContours(tubule_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 193 |
+
cv2.drawContours(overlay, contours_outer, -1, (0, 255, 255), 2)
|
| 194 |
+
|
| 195 |
+
lum_mask_vis = (pred == 2).astype(np.uint8)
|
| 196 |
+
contours_lumen, _ = cv2.findContours(lum_mask_vis, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 197 |
+
cv2.drawContours(overlay, contours_lumen, -1, (0, 0, 255), 2)
|
| 198 |
+
|
| 199 |
+
cv2.imwrite(str(out_dir / f"{fp.stem}_overlay.png"), overlay)
|
| 200 |
+
cv2.imwrite(str(out_dir / f"{fp.stem}_mask.png"), pred * 127)
|
| 201 |
+
|
| 202 |
+
if (i+1) % 10 == 0 or status == "CHECK":
|
| 203 |
+
print(f" [{i+1}/{len(files)}] {fp.name}: "
|
| 204 |
+
f"tub={tub_pct:.1f}% epi={epi_pct:.1f}% lum={lum_pct:.1f}% [{status}]")
|
| 205 |
+
|
| 206 |
+
ok = sum(1 for r in results if r['status'] == 'OK')
|
| 207 |
+
tub_vals = [r['tubule_pct'] for r in results]
|
| 208 |
+
epi_vals = [r['epithelium_pct'] for r in results]
|
| 209 |
+
lum_vals = [r['lumen_pct'] for r in results]
|
| 210 |
+
|
| 211 |
+
print(f"\n{'='*60}")
|
| 212 |
+
print(f" RESULTS {model_file} + TTA — {ok}/{len(results)} OK")
|
| 213 |
+
print(f" Tubule: {np.mean(tub_vals):.1f} +/- {np.std(tub_vals):.1f}%")
|
| 214 |
+
print(f" Epithelium: {np.mean(epi_vals):.1f} +/- {np.std(epi_vals):.1f}%")
|
| 215 |
+
print(f" Lumen: {np.mean(lum_vals):.1f} +/- {np.std(lum_vals):.1f}%")
|
| 216 |
+
print(f" Time: {(time.time()-t0)/60:.1f} min")
|
| 217 |
+
print(f"{'='*60}")
|
| 218 |
+
|
| 219 |
+
with open(out_dir / 'results.csv', 'w', newline='') as f:
|
| 220 |
+
w = csv.DictWriter(f, fieldnames=results[0].keys())
|
| 221 |
+
w.writeheader(); w.writerows(results)
|
| 222 |
+
with open(out_dir / 'results.json', 'w') as f:
|
| 223 |
+
json.dump({
|
| 224 |
+
'model': model_file, 'tta': True, 'n_tta': 8,
|
| 225 |
+
'mIoU': float(ckpt['best_val_iou']),
|
| 226 |
+
'n_ok': ok, 'n_total': len(results),
|
| 227 |
+
'mean_tubule_pct': round(np.mean(tub_vals), 1),
|
| 228 |
+
'mean_lumen_pct': round(np.mean(lum_vals), 1),
|
| 229 |
+
'results': results
|
| 230 |
+
}, f, indent=2)
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
if __name__ == '__main__':
|
| 235 |
+
main()
|