Upload 2 files
Browse files
best_model_segmentation_v1.0.0.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f190c0ca1d68a6fe20d7a4b1841a0a5437f225cb70af0e3b1549c947dfb910f6
|
| 3 |
+
size 92847124
|
train_segmentation_v1.0.0.py
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
=================================================================
|
| 3 |
+
TubuleSegmentation v1.0.0 =================================================================
|
| 4 |
+
- EfficientNet-B4 encoder, depth=5, ImageNet weights
|
| 5 |
+
- UnetDecoder (256,128,64,32,16), n_blocks=5, attention='scse'
|
| 6 |
+
- Macenko stain aug (p=0.5), gradient checkpointing encoder+decoders
|
| 7 |
+
- Dice+CE + clDice(0.04) + SCNP(0.16) + Containment(0.24)
|
| 8 |
+
- Warmup topo lineal epochs 20-40
|
| 9 |
+
- batch=4, LR=3e-4, AdamW, CosineAnnealingWarmRestarts
|
| 10 |
+
|
| 11 |
+
Output: best_model_v1.0.pt
|
| 12 |
+
=================================================================
|
| 13 |
+
"""
|
| 14 |
+
import os, time, numpy as np, cv2, torch, torch.nn as nn
|
| 15 |
+
import torch.nn.functional as F
|
| 16 |
+
import torch.utils.checkpoint as cp
|
| 17 |
+
import segmentation_models_pytorch as smp
|
| 18 |
+
from segmentation_models_pytorch.decoders.unet.decoder import UnetDecoder
|
| 19 |
+
from torch.utils.data import Dataset, DataLoader
|
| 20 |
+
import albumentations as A
|
| 21 |
+
from pathlib import Path
|
| 22 |
+
|
| 23 |
+
# ── Macenko stain augmentation ────────────────────────────────
|
| 24 |
+
|
| 25 |
+
TORCHSTAIN_OK = False
|
| 26 |
+
TorchMacenkoAugmentor = None
|
| 27 |
+
try:
|
| 28 |
+
from torchstain.torch.augmentors.macenko import TorchMacenkoAugmentor
|
| 29 |
+
TORCHSTAIN_OK = True
|
| 30 |
+
except ImportError:
|
| 31 |
+
try: # fallback ruta vieja (<1.3.0)
|
| 32 |
+
from torchstain.augmentors.he_augmentor import HEAugmentor as TorchMacenkoAugmentor
|
| 33 |
+
TORCHSTAIN_OK = True
|
| 34 |
+
except ImportError:
|
| 35 |
+
print(" [!] torchstain no encontrado — pip install torchstain")
|
| 36 |
+
print(" [!] Entrenando sin stain augmentation (igual que v21)")
|
| 37 |
+
|
| 38 |
+
# ============================================================
|
| 39 |
+
# TOPOLOGY LOSSES
|
| 40 |
+
# ============================================================
|
| 41 |
+
def scnp(logits, labels_onehot, w=3, kappa=1e6):
|
| 42 |
+
fg_mask = labels_onehot.float(); bg_mask = 1.0 - fg_mask
|
| 43 |
+
fg_logits = logits * fg_mask + kappa * bg_mask
|
| 44 |
+
min_pooled = -F.max_pool2d(-fg_logits, kernel_size=w, stride=1, padding=w//2)
|
| 45 |
+
bg_logits = logits * bg_mask - kappa * fg_mask
|
| 46 |
+
max_pooled = F.max_pool2d(bg_logits, kernel_size=w, stride=1, padding=w//2)
|
| 47 |
+
return torch.where(labels_onehot == 1, min_pooled, max_pooled)
|
| 48 |
+
|
| 49 |
+
def soft_erode(img):
|
| 50 |
+
p1 = -F.max_pool2d(-img, (3,1), (1,1), (1,0))
|
| 51 |
+
p2 = -F.max_pool2d(-img, (1,3), (1,1), (0,1))
|
| 52 |
+
return torch.min(p1, p2)
|
| 53 |
+
|
| 54 |
+
def soft_dilate(img):
|
| 55 |
+
return F.max_pool2d(img, (3,3), (1,1), (1,1))
|
| 56 |
+
|
| 57 |
+
def soft_open(img):
|
| 58 |
+
return soft_dilate(soft_erode(img))
|
| 59 |
+
|
| 60 |
+
def soft_skel(img, iters=3):
|
| 61 |
+
img1 = soft_open(img); skel = F.relu(img - img1)
|
| 62 |
+
for _ in range(iters):
|
| 63 |
+
img = soft_erode(img); img1 = soft_open(img)
|
| 64 |
+
delta = F.relu(img - img1); skel = skel + F.relu(delta - skel * delta)
|
| 65 |
+
return skel
|
| 66 |
+
|
| 67 |
+
def cldice_loss(pred, target, iters=3, smooth=1e-7):
|
| 68 |
+
skel_pred = soft_skel(pred, iters); skel_target = soft_skel(target, iters)
|
| 69 |
+
tprec = (skel_pred * target).sum() / (skel_pred.sum() + smooth)
|
| 70 |
+
tsens = (skel_target * pred).sum() / (skel_target.sum() + smooth)
|
| 71 |
+
return 1.0 - 2.0 * tprec * tsens / (tprec + tsens + smooth)
|
| 72 |
+
|
| 73 |
+
def containment_loss(pred_lumen, pred_epi, smooth=1e-5):
|
| 74 |
+
tissue = torch.clamp(pred_epi + pred_lumen, 0, 1)
|
| 75 |
+
violation = pred_lumen * (1.0 - tissue.detach())
|
| 76 |
+
return violation.sum() / (pred_lumen.sum() + smooth)
|
| 77 |
+
|
| 78 |
+
# ============================================================
|
| 79 |
+
# MODEL v1.0.0
|
| 80 |
+
# ============================================================
|
| 81 |
+
class TubuleSegModel(nn.Module):
|
| 82 |
+
def __init__(self, use_checkpoint=True):
|
| 83 |
+
super().__init__()
|
| 84 |
+
self.use_checkpoint = use_checkpoint
|
| 85 |
+
self.encoder = smp.encoders.get_encoder(
|
| 86 |
+
'timm-efficientnet-b4', in_channels=3, depth=5, weights='imagenet'
|
| 87 |
+
)
|
| 88 |
+
ec = self.encoder.out_channels
|
| 89 |
+
self.seg_decoder = UnetDecoder(
|
| 90 |
+
encoder_channels=ec, decoder_channels=(256,128,64,32,16),
|
| 91 |
+
n_blocks=5, use_norm='batchnorm', attention_type='scse'
|
| 92 |
+
)
|
| 93 |
+
self.seg_head = nn.Conv2d(16, 3, kernel_size=1)
|
| 94 |
+
self.border_decoder = UnetDecoder(
|
| 95 |
+
encoder_channels=ec, decoder_channels=(256,128,64,32,16),
|
| 96 |
+
n_blocks=5, use_norm='batchnorm', attention_type='scse'
|
| 97 |
+
)
|
| 98 |
+
self.border_head = nn.Conv2d(16, 2, kernel_size=1)
|
| 99 |
+
|
| 100 |
+
def _seg_branch(self, *features):
|
| 101 |
+
return self.seg_head(self.seg_decoder(list(features)))
|
| 102 |
+
|
| 103 |
+
def _border_branch(self, *features):
|
| 104 |
+
return self.border_head(self.border_decoder(list(features)))
|
| 105 |
+
|
| 106 |
+
def _encode(self, x):
|
| 107 |
+
return tuple(self.encoder(x))
|
| 108 |
+
|
| 109 |
+
def forward(self, x):
|
| 110 |
+
if self.use_checkpoint and self.training:
|
| 111 |
+
features = list(cp.checkpoint(self._encode, x, use_reentrant=False))
|
| 112 |
+
seg = cp.checkpoint(self._seg_branch, *features, use_reentrant=False)
|
| 113 |
+
border = cp.checkpoint(self._border_branch, *features, use_reentrant=False)
|
| 114 |
+
return seg, border
|
| 115 |
+
features = self.encoder(x)
|
| 116 |
+
return (
|
| 117 |
+
self.seg_head(self.seg_decoder(features)),
|
| 118 |
+
self.border_head(self.border_decoder(features))
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# ============================================================
|
| 122 |
+
# BASE LOSSES
|
| 123 |
+
# ============================================================
|
| 124 |
+
def multiclass_dice_loss(pred_logits, target_long, smooth=1e-5):
|
| 125 |
+
C = pred_logits.shape[1]; soft = pred_logits.softmax(dim=1)
|
| 126 |
+
oh = F.one_hot(target_long, C).permute(0,3,1,2).float()
|
| 127 |
+
num = 2.0*(soft*oh).sum(dim=(0,2,3))+smooth
|
| 128 |
+
den = soft.sum(dim=(0,2,3))+oh.sum(dim=(0,2,3))+smooth
|
| 129 |
+
return (1.0-(num/den)).mean()
|
| 130 |
+
|
| 131 |
+
def seg_loss_base(logits, target):
|
| 132 |
+
return multiclass_dice_loss(logits, target) + F.cross_entropy(logits, target)
|
| 133 |
+
|
| 134 |
+
# ============================================================
|
| 135 |
+
# DATA
|
| 136 |
+
# ============================================================
|
| 137 |
+
def extract_training_data(data_repo, masks_dir):
|
| 138 |
+
orig_dir=data_repo/'tubules_original';conteo_dir=data_repo/'tubules_area_ok'
|
| 139 |
+
images=[];masks=[];borders_outer=[];borders_lumen=[]
|
| 140 |
+
border_pos=np.zeros(2);border_total=0
|
| 141 |
+
n_loaded=0;n_missing=0;_printed_unique=[False]
|
| 142 |
+
def process(orig_path,conteo_path):
|
| 143 |
+
nonlocal border_pos,border_total,n_loaded,n_missing
|
| 144 |
+
orig=cv2.imread(str(orig_path));conteo=cv2.imread(str(conteo_path))
|
| 145 |
+
if orig is None or conteo is None:return
|
| 146 |
+
oh,ow=orig.shape[:2];ch,cw=conteo.shape[:2]
|
| 147 |
+
r=conteo[:,:,2].astype(float);g=conteo[:,:,1].astype(float);b=conteo[:,:,0].astype(float)
|
| 148 |
+
red=((r>150)&(g<100)&(b<100)&((r-g)>80)).astype(np.uint8)
|
| 149 |
+
if ch>oh:red=red[50:ch-50,50:cw-50]
|
| 150 |
+
red=cv2.resize(red,(ow,oh),interpolation=cv2.INTER_NEAREST)
|
| 151 |
+
# ── v23: carga directa de la mascara pre-generada ──────────
|
| 152 |
+
bid=Path(orig_path).stem
|
| 153 |
+
mask_path=masks_dir/f"{bid}.png"
|
| 154 |
+
if not mask_path.exists():
|
| 155 |
+
n_missing+=1;return
|
| 156 |
+
seg_mask=cv2.imread(str(mask_path),cv2.IMREAD_GRAYSCALE)
|
| 157 |
+
if seg_mask is None:
|
| 158 |
+
n_missing+=1;return
|
| 159 |
+
if seg_mask.shape[:2]!=(oh,ow):
|
| 160 |
+
seg_mask=cv2.resize(seg_mask,(ow,oh),interpolation=cv2.INTER_NEAREST)
|
| 161 |
+
seg_mask=seg_mask.astype(np.uint8)
|
| 162 |
+
if not _printed_unique[0]:
|
| 163 |
+
print(f" [v23] {bid}.png -> labels {np.unique(seg_mask).tolist()} (esperado [0,1,2])")
|
| 164 |
+
_printed_unique[0]=True
|
| 165 |
+
if (seg_mask==1).sum()<oh*ow*0.1:return
|
| 166 |
+
n_loaded+=1
|
| 167 |
+
barrier=cv2.dilate(red,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)),iterations=1)
|
| 168 |
+
flood=barrier.copy();fm=np.zeros((oh+2,ow+2),dtype=np.uint8)
|
| 169 |
+
for y in range(0,oh,5):
|
| 170 |
+
if flood[y,0]==0:cv2.floodFill(flood,fm,(0,y),128)
|
| 171 |
+
if flood[y,ow-1]==0:cv2.floodFill(flood,fm,(ow-1,y),128)
|
| 172 |
+
for x in range(0,ow,5):
|
| 173 |
+
if flood[0,x]==0:cv2.floodFill(flood,fm,(x,0),128)
|
| 174 |
+
if flood[oh-1,x]==0:cv2.floodFill(flood,fm,(x,oh-1),128)
|
| 175 |
+
bg=(flood==128).astype(np.uint8)
|
| 176 |
+
bg_dil=cv2.dilate(bg,cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7)))
|
| 177 |
+
ob=(red&bg_dil).astype(np.float32);lb=(red&(1-bg_dil)).astype(np.float32)
|
| 178 |
+
k5=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))
|
| 179 |
+
ob=cv2.dilate(ob,k5,iterations=1);lb=cv2.dilate(lb,k5,iterations=1)
|
| 180 |
+
images.append(cv2.resize(orig,(512,512),interpolation=cv2.INTER_LINEAR))
|
| 181 |
+
masks.append(cv2.resize(seg_mask,(512,512),interpolation=cv2.INTER_NEAREST))
|
| 182 |
+
borders_outer.append(cv2.resize(ob,(512,512),interpolation=cv2.INTER_NEAREST))
|
| 183 |
+
borders_lumen.append(cv2.resize(lb,(512,512),interpolation=cv2.INTER_NEAREST))
|
| 184 |
+
border_pos[0]+=borders_outer[-1].sum();border_pos[1]+=borders_lumen[-1].sum()
|
| 185 |
+
border_total+=512*512
|
| 186 |
+
for ap in sorted(conteo_dir.glob('*_conteo.png')):
|
| 187 |
+
bid=get_base_id(ap.name)
|
| 188 |
+
if bid.split('_')[-1]=='396':continue
|
| 189 |
+
op=orig_dir/f"{bid}.tif"
|
| 190 |
+
if not op.exists():continue
|
| 191 |
+
process(op,ap)
|
| 192 |
+
for ext_name in ['tubules_extra','tubules_extra2','tubules_extra4']:
|
| 193 |
+
ext_dir=data_repo/ext_name
|
| 194 |
+
if not ext_dir.exists():continue
|
| 195 |
+
for tp in sorted(ext_dir.glob('*.tif')):
|
| 196 |
+
bid=tp.stem;cp_path=ext_dir/f"{bid}_conteo.png"
|
| 197 |
+
if not cp_path.exists():continue
|
| 198 |
+
process(tp,cp_path)
|
| 199 |
+
pw=np.clip((border_total-border_pos)/(border_pos+1e-6),1.0,15.0) if border_total>0 else np.array([10.,10.])
|
| 200 |
+
print(f" [v23] mascaras cargadas: {n_loaded} | sin mascara (omitidas): {n_missing}")
|
| 201 |
+
return images,masks,borders_outer,borders_lumen,pw
|
| 202 |
+
|
| 203 |
+
def get_base_id(f):
|
| 204 |
+
s=os.path.splitext(f)[0]
|
| 205 |
+
for x in['_medida','_conteo','_area']:
|
| 206 |
+
if s.endswith(x):s=s[:-len(x)]
|
| 207 |
+
return s
|
| 208 |
+
|
| 209 |
+
# ============================================================
|
| 210 |
+
# MACENKO STAIN AUGMENTATION — torchstain v1.3.0
|
| 211 |
+
# ============================================================
|
| 212 |
+
def make_stain_augmentor():
|
| 213 |
+
|
| 214 |
+
if not TORCHSTAIN_OK:
|
| 215 |
+
return None
|
| 216 |
+
try:
|
| 217 |
+
aug = TorchMacenkoAugmentor(sigma1=0.2, sigma2=0.2)
|
| 218 |
+
return aug
|
| 219 |
+
except Exception as e:
|
| 220 |
+
print(f" [!] Macenko augmentor init fallo: {e}")
|
| 221 |
+
return None
|
| 222 |
+
|
| 223 |
+
def apply_stain_aug(img_rgb_uint8, augmentor):
|
| 224 |
+
|
| 225 |
+
if augmentor is None:
|
| 226 |
+
return img_rgb_uint8
|
| 227 |
+
try:
|
| 228 |
+
# [H,W,3] uint8 -> [3,H,W] uint8 tensor
|
| 229 |
+
img_t = torch.from_numpy(img_rgb_uint8).permute(2, 0, 1) # uint8, [0,255]
|
| 230 |
+
img_aug, _, _ = augmentor(img_t) # devuelve (augmented, H, E) tensores
|
| 231 |
+
# [3,H,W] -> [H,W,3] numpy uint8
|
| 232 |
+
img_out = img_aug.permute(1, 2, 0).numpy().astype(np.uint8)
|
| 233 |
+
return img_out
|
| 234 |
+
except Exception:
|
| 235 |
+
return img_rgb_uint8
|
| 236 |
+
|
| 237 |
+
# ============================================================
|
| 238 |
+
# DATASET v1.0.0 — (Macenko stain augmentation)
|
| 239 |
+
# ============================================================
|
| 240 |
+
class TubuleDataset(Dataset):
|
| 241 |
+
def __init__(self, images, masks, borders_o, borders_l,
|
| 242 |
+
augment=False, stain_augmentor=None):
|
| 243 |
+
self.images = images
|
| 244 |
+
self.masks = masks
|
| 245 |
+
self.borders_o = borders_o
|
| 246 |
+
self.borders_l = borders_l
|
| 247 |
+
self.augment = augment
|
| 248 |
+
self.stain_augmentor = stain_augmentor
|
| 249 |
+
|
| 250 |
+
self.tf = A.Compose([
|
| 251 |
+
A.HorizontalFlip(p=0.5),
|
| 252 |
+
A.VerticalFlip(p=0.5),
|
| 253 |
+
A.RandomRotate90(p=0.5),
|
| 254 |
+
A.ElasticTransform(alpha=120, sigma=120*0.05, p=0.3),
|
| 255 |
+
A.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.05, p=0.8),
|
| 256 |
+
A.GaussNoise(p=0.3),
|
| 257 |
+
]) if augment else None
|
| 258 |
+
|
| 259 |
+
def __len__(self):
|
| 260 |
+
return len(self.images)
|
| 261 |
+
|
| 262 |
+
def __getitem__(self, idx):
|
| 263 |
+
img = cv2.cvtColor(self.images[idx], cv2.COLOR_BGR2RGB)
|
| 264 |
+
mask = self.masks[idx].copy()
|
| 265 |
+
bo = self.borders_o[idx].copy()
|
| 266 |
+
bl = self.borders_l[idx].copy()
|
| 267 |
+
|
| 268 |
+
# Macenko stain aug (train, p=0.5)
|
| 269 |
+
|
| 270 |
+
if self.stain_augmentor is not None and np.random.random() < 0.5:
|
| 271 |
+
img = apply_stain_aug(img, self.stain_augmentor)
|
| 272 |
+
|
| 273 |
+
if self.tf:
|
| 274 |
+
bou = (bo * 255).astype(np.uint8)
|
| 275 |
+
blu = (bl * 255).astype(np.uint8)
|
| 276 |
+
t = self.tf(image=img, masks=[mask, bou, blu])
|
| 277 |
+
img = t['image']; mask = t['masks'][0]
|
| 278 |
+
bo = t['masks'][1].astype(np.float32) / 255.0
|
| 279 |
+
bl = t['masks'][2].astype(np.float32) / 255.0
|
| 280 |
+
|
| 281 |
+
img_norm = (img.astype(np.float32)/255.0 - [0.485,0.456,0.406]) / [0.229,0.224,0.225]
|
| 282 |
+
return (
|
| 283 |
+
torch.from_numpy(img_norm.transpose(2,0,1)).float(),
|
| 284 |
+
torch.from_numpy(mask.copy()).long(),
|
| 285 |
+
torch.from_numpy(np.stack([bo, bl], axis=0)).float()
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
# ============================================================
|
| 289 |
+
# MAIN
|
| 290 |
+
# ============================================================
|
| 291 |
+
def main():
|
| 292 |
+
base_dir = Path(r"D:\Lu\AI\Tubules"); os.chdir(str(base_dir))
|
| 293 |
+
print("="*60)
|
| 294 |
+
print(" TubuleSegmentation v23")
|
| 295 |
+
print(" EfficientNet-B4 @ 512x512 | batch=4 | SCSE attention")
|
| 296 |
+
print(" Dice+CE + clDice(0.04) + SCNP(0.16) + Containment(0.24)")
|
| 297 |
+
print(" Macenko stain aug (p=0.5) + checkpointing encoder+decoders")
|
| 298 |
+
print(f" torchstain: {'v1.3.0 OK' if TORCHSTAIN_OK else 'NO (pip install torchstain)'}")
|
| 299 |
+
print("="*60)
|
| 300 |
+
|
| 301 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 302 |
+
print(f"\n Device: {device}")
|
| 303 |
+
if device.type == 'cuda':
|
| 304 |
+
print(f" GPU: {torch.cuda.get_device_name(0)}")
|
| 305 |
+
torch.backends.cudnn.benchmark = True
|
| 306 |
+
|
| 307 |
+
from huggingface_hub import snapshot_download
|
| 308 |
+
data_repo = base_dir / 'data_repo'
|
| 309 |
+
if not data_repo.exists():
|
| 310 |
+
snapshot_download('LuGot16/tubules', repo_type='dataset', local_dir=str(data_repo))
|
| 311 |
+
|
| 312 |
+
masks_dir = base_dir / 'masks_v2' / 'masks'
|
| 313 |
+
|
| 314 |
+
print("\n Loading images + masks_v2...")
|
| 315 |
+
images, masks, borders_o, borders_l, border_pw = extract_training_data(data_repo, masks_dir)
|
| 316 |
+
print(f" Total images: {len(images)}")
|
| 317 |
+
print(f" Border pos_weight: outer={border_pw[0]:.1f}, lumen={border_pw[1]:.1f}")
|
| 318 |
+
|
| 319 |
+
np.random.seed(42)
|
| 320 |
+
idx = np.random.permutation(len(images))
|
| 321 |
+
sp = int(len(idx) * 0.85)
|
| 322 |
+
train_idx = idx[:sp]; val_idx = idx[sp:]
|
| 323 |
+
print(f" Train: {len(train_idx)}, Val: {len(val_idx)}")
|
| 324 |
+
|
| 325 |
+
stain_aug = make_stain_augmentor()
|
| 326 |
+
if stain_aug is not None:
|
| 327 |
+
print(" Macenko stain augmentation: YES (sigma=0.2, p=0.5 por imagen)")
|
| 328 |
+
else:
|
| 329 |
+
print(" Macenko stain augmentation: NO")
|
| 330 |
+
|
| 331 |
+
train_ds = TubuleDataset(
|
| 332 |
+
[images[i] for i in train_idx], [masks[i] for i in train_idx],
|
| 333 |
+
[borders_o[i] for i in train_idx], [borders_l[i] for i in train_idx],
|
| 334 |
+
augment=True, stain_augmentor=stain_aug
|
| 335 |
+
)
|
| 336 |
+
val_ds = TubuleDataset(
|
| 337 |
+
[images[i] for i in val_idx], [masks[i] for i in val_idx],
|
| 338 |
+
[borders_o[i] for i in val_idx], [borders_l[i] for i in val_idx],
|
| 339 |
+
augment=False, stain_augmentor=None
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
BATCH = 4
|
| 343 |
+
train_dl = DataLoader(train_ds, batch_size=BATCH, shuffle=True, num_workers=0, pin_memory=True)
|
| 344 |
+
val_dl = DataLoader(val_ds, batch_size=BATCH, shuffle=False, num_workers=0, pin_memory=True)
|
| 345 |
+
|
| 346 |
+
model = TubuleSegModel(use_checkpoint=True); model.to(device)
|
| 347 |
+
n_params = sum(p.numel() for p in model.parameters()) / 1e6
|
| 348 |
+
print(f"\n Model: {n_params:.1f}M params (SCSE attention en decoders)")
|
| 349 |
+
|
| 350 |
+
# ---- SMOKE TEST ----
|
| 351 |
+
model.train()
|
| 352 |
+
if device.type == 'cuda': torch.cuda.reset_peak_memory_stats()
|
| 353 |
+
_x = torch.randn(2, 3, 512, 512, device=device)
|
| 354 |
+
_seg, _bor = model(_x)
|
| 355 |
+
assert _seg.shape == (2,3,512,512) and _bor.shape == (2,2,512,512)
|
| 356 |
+
(_seg.mean() + _bor.mean()).backward()
|
| 357 |
+
assert any(p.grad is not None and torch.isfinite(p.grad).all() for p in model.parameters()), "grad no finito"
|
| 358 |
+
model.zero_grad(set_to_none=True)
|
| 359 |
+
if device.type == 'cuda':
|
| 360 |
+
print(f" Smoke test OK | VRAM_peak smoke={torch.cuda.max_memory_allocated()/1024**2:.0f}MB (batch=2)")
|
| 361 |
+
torch.cuda.reset_peak_memory_stats()
|
| 362 |
+
else:
|
| 363 |
+
print(" Smoke test OK")
|
| 364 |
+
|
| 365 |
+
W_CLDICE = 0.04; W_SCNP = 0.16; W_CONTAIN = 0.24
|
| 366 |
+
TOPO_START = 20; TOPO_END = 40
|
| 367 |
+
EPOCHS = 200; PATIENCE = 80; LR = 3e-4
|
| 368 |
+
|
| 369 |
+
opt = torch.optim.AdamW(model.parameters(), lr=LR, weight_decay=1e-4)
|
| 370 |
+
sched = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(opt, T_0=20, T_mult=2, eta_min=1e-6)
|
| 371 |
+
border_pw_t = torch.tensor(border_pw, dtype=torch.float32).to(device)
|
| 372 |
+
|
| 373 |
+
best = 0; pat = 0
|
| 374 |
+
model_path = base_dir / 'best_model_v23.pt'
|
| 375 |
+
|
| 376 |
+
print(f"\n Epochs: {EPOCHS}, Patience: {PATIENCE}, LR: {LR}")
|
| 377 |
+
print(f" Topo: clDice={W_CLDICE}, SCNP={W_SCNP}, Contain={W_CONTAIN}")
|
| 378 |
+
print(f" Warmup: epochs {TOPO_START}-{TOPO_END} (linear 0->1) | Batch: {BATCH}")
|
| 379 |
+
print(f"{'='*60}")
|
| 380 |
+
t0 = time.time()
|
| 381 |
+
|
| 382 |
+
for ep in range(EPOCHS):
|
| 383 |
+
if ep < TOPO_START: topo_scale = 0.0
|
| 384 |
+
elif ep < TOPO_END: topo_scale = (ep - TOPO_START) / (TOPO_END - TOPO_START)
|
| 385 |
+
else: topo_scale = 1.0
|
| 386 |
+
|
| 387 |
+
if device.type == 'cuda': torch.cuda.reset_peak_memory_stats()
|
| 388 |
+
model.train(); tl = 0; tb = 0
|
| 389 |
+
|
| 390 |
+
for imgs, seg_masks, borders in train_dl:
|
| 391 |
+
imgs = imgs.to(device)
|
| 392 |
+
seg_masks = seg_masks.to(device)
|
| 393 |
+
borders = borders.to(device)
|
| 394 |
+
opt.zero_grad()
|
| 395 |
+
|
| 396 |
+
seg_logits, border_logits = model(imgs)
|
| 397 |
+
l_base = seg_loss_base(seg_logits, seg_masks)
|
| 398 |
+
l_border = F.binary_cross_entropy_with_logits(
|
| 399 |
+
border_logits.float(), borders.float(),
|
| 400 |
+
pos_weight=border_pw_t.view(1,2,1,1))
|
| 401 |
+
loss = l_base + 0.2 * l_border
|
| 402 |
+
|
| 403 |
+
if topo_scale > 0:
|
| 404 |
+
seg_probs = seg_logits.softmax(dim=1)
|
| 405 |
+
epi_pred = seg_probs[:,1:2,:,:]
|
| 406 |
+
epi_target = (seg_masks==1).float().unsqueeze(1)
|
| 407 |
+
l_cldice = cldice_loss(epi_pred, epi_target, iters=3)
|
| 408 |
+
labels_oh = F.one_hot(seg_masks, 3).permute(0,3,1,2).float()
|
| 409 |
+
z_tilde = scnp(seg_logits, labels_oh, w=3)
|
| 410 |
+
l_scnp = F.cross_entropy(z_tilde, seg_masks)
|
| 411 |
+
l_contain = containment_loss(seg_probs[:,2,:,:], seg_probs[:,1,:,:])
|
| 412 |
+
loss = loss + topo_scale * (W_CLDICE*l_cldice + W_SCNP*l_scnp + W_CONTAIN*l_contain)
|
| 413 |
+
|
| 414 |
+
loss.backward()
|
| 415 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
|
| 416 |
+
opt.step()
|
| 417 |
+
tl += loss.item(); tb += 1
|
| 418 |
+
|
| 419 |
+
sched.step()
|
| 420 |
+
|
| 421 |
+
model.eval(); ap = []; at = []
|
| 422 |
+
with torch.no_grad():
|
| 423 |
+
for imgs, seg_masks, _ in val_dl:
|
| 424 |
+
imgs = imgs.to(device)
|
| 425 |
+
seg_logits, _ = model(imgs)
|
| 426 |
+
ap.append(seg_logits.argmax(1).cpu())
|
| 427 |
+
at.append(seg_masks)
|
| 428 |
+
|
| 429 |
+
preds = torch.cat(ap); tgts = torch.cat(at)
|
| 430 |
+
ious = [(((preds==c)&(tgts==c)).sum().float()+1e-6) /
|
| 431 |
+
(((preds==c)|(tgts==c)).sum().float()+1e-6) for c in range(3)]
|
| 432 |
+
miou = np.mean([x.item() for x in ious])
|
| 433 |
+
|
| 434 |
+
peak_mb = (torch.cuda.max_memory_allocated()/1024**2) if device.type=='cuda' else 0
|
| 435 |
+
if (ep+1) % 5 == 0 or miou > best:
|
| 436 |
+
print(f" Ep {ep+1:3d}/{EPOCHS} | loss={tl/tb:.4f} | "
|
| 437 |
+
f"mIoU={miou:.4f} epi={ious[1]:.3f} lum={ious[2]:.3f} | "
|
| 438 |
+
f"{(time.time()-t0)/60:.1f}m | topo={topo_scale:.2f} | "
|
| 439 |
+
f"VRAM_peak={peak_mb:.0f}MB")
|
| 440 |
+
if miou > best:
|
| 441 |
+
best = miou; pat = 0
|
| 442 |
+
torch.save({
|
| 443 |
+
'model_state_dict': model.state_dict(),
|
| 444 |
+
'config': {
|
| 445 |
+
'encoder': 'efficientnet-b4',
|
| 446 |
+
'architecture': 'UNet_FullImage_ShapeConstrained_SCSE',
|
| 447 |
+
'img_size': 512,
|
| 448 |
+
'losses': 'Dice+CE+clDice+SCNP+Containment',
|
| 449 |
+
'attention': 'scse',
|
| 450 |
+
'stain_aug': 'macenko_sigma0.2' if stain_aug else 'none',
|
| 451 |
+
'gradient_checkpointing': 'encoder_full+decoders',
|
| 452 |
+
'topo_weights': {'cldice': W_CLDICE, 'scnp': W_SCNP, 'containment': W_CONTAIN},
|
| 453 |
+
'topo_warmup': 'linear_20_40',
|
| 454 |
+
'from_scratch': True,
|
| 455 |
+
'clean_dataset': True,
|
| 456 |
+
'mask_source': 'masks_v2/masks (regenerate, +107 con --no-is-clean)',
|
| 457 |
+
'version': 'v23'
|
| 458 |
+
},
|
| 459 |
+
'best_val_iou': best
|
| 460 |
+
}, str(model_path))
|
| 461 |
+
print(f" * MEJOR: {best:.4f}")
|
| 462 |
+
else:
|
| 463 |
+
pat += 1
|
| 464 |
+
if pat >= PATIENCE:
|
| 465 |
+
print(f" Early stopping at epoch {ep+1} (best={best:.4f})")
|
| 466 |
+
break
|
| 467 |
+
|
| 468 |
+
print(f"\n{'='*60}\n COMPLETE in {(time.time()-t0)/60:.1f} min — mIoU: {best:.4f}\n{'='*60}")
|
| 469 |
+
try:
|
| 470 |
+
from huggingface_hub import HfApi; api = HfApi()
|
| 471 |
+
api.upload_file(
|
| 472 |
+
path_or_fileobj=str(model_path),
|
| 473 |
+
path_in_repo='best_model_v23.pt',
|
| 474 |
+
repo_id='LuGot16/seminiferous-tubule-segmentation',
|
| 475 |
+
repo_type='model')
|
| 476 |
+
print(" Upload to HF!")
|
| 477 |
+
except Exception as e:
|
| 478 |
+
print(f" Upload: {e}")
|
| 479 |
+
|
| 480 |
+
if __name__ == '__main__':
|
| 481 |
+
main()
|