戰(zhàn):Unet從數(shù)據(jù)預(yù)處理到mIoU評(píng)估全鏈路排雷)
簡(jiǎn)介本資源是面向深度學(xué)習(xí)初學(xué)者與計(jì)算機(jī)視覺實(shí)踐者的PyTorch多類別語義分割實(shí)戰(zhàn)項(xiàng)目聚焦Unet模型在自定義數(shù)據(jù)集上的完整實(shí)現(xiàn)適用于醫(yī)學(xué)影像、遙感解譯等需精細(xì)像素級(jí)分類的場(chǎng)景。壓縮包共46個(gè)文件含19個(gè)核心Python源碼如unet.py、train.py、datasets/模塊、24個(gè)編譯緩存pyc文件、2個(gè)配置說明txt及1個(gè)info.json元信息文件結(jié)構(gòu)清晰體現(xiàn)數(shù)據(jù)加載、模型構(gòu)建、損失計(jì)算、訓(xùn)練調(diào)度與評(píng)估全流程整體僅69KB輕量易部署。已有15279人學(xué)習(xí)下載反映出其在入門級(jí)語義分割實(shí)踐中的高復(fù)用價(jià)值。讀者可直接復(fù)用該工程框架快速替換自有數(shù)據(jù)集路徑、調(diào)整類別數(shù)與標(biāo)簽映射、調(diào)用預(yù)置的數(shù)據(jù)增強(qiáng)與權(quán)重計(jì)算邏輯并通過demo.py可視化預(yù)測(cè)結(jié)果目錄中custom_transforms、mypath、metrics等模塊封裝規(guī)范便于理解Unet各組件協(xié)作機(jī)制與PyTorch工程化組織方式。1. Pytorch下實(shí)現(xiàn)Unet對(duì)自己多類別數(shù)據(jù)集的語義分割不是調(diào)個(gè)庫就能跑通而是從標(biāo)簽映射錯(cuò)位、通道數(shù)爆炸到loss不降的完整排雷實(shí)錄你手頭有一批自己拍的工業(yè)零件圖像帶像素級(jí)標(biāo)注——不是PASCAL VOC那種“背景人車”的三分類而是“螺栓A/螺栓B/墊片C/銹蝕區(qū)/劃痕D/正?;住惫?類你想用Unet做端到端語義分割但pip install torch torchvision后第一行model UNet(n_classes6)就報(bào)錯(cuò)RuntimeError: Expected input batch_size (4) to match target batch_size (1)或者訓(xùn)練100輪loss紋絲不動(dòng)驗(yàn)證mIoU卡在0.03。這不是模型不行是PyTorch下Unet落地多類別分割的真實(shí)水位線它要求你親手把label.png里的灰度值0,1,2,3,4,5對(duì)齊到one-hot的第0~5維手動(dòng)處理mask通道順序硬編碼ignore_index甚至重寫DiceLoss的smooth項(xiàng)防除零。本文不講Unet結(jié)構(gòu)圖只拆解某實(shí)驗(yàn)室在產(chǎn)線缺陷檢測(cè)項(xiàng)目中用PyTorch 1.13CuDNN 8.6實(shí)打?qū)嵟芡?類Unet的全流程——從原始label.png的灰度值校驗(yàn)到DataLoader里transforms.Compose的順序陷阱再到驗(yàn)證階段逐類計(jì)算IoU的腳本每一步都附可直接粘貼運(yùn)行的代碼塊和血淚參數(shù)說明。2. 多類別語義分割的數(shù)據(jù)準(zhǔn)備label.png灰度值必須嚴(yán)格映射為0~N-1且不可跳號(hào)2.1 標(biāo)簽圖像的格式陷阱為什么你的label.png打開是彩色但PyTorch讀出來全是0很多新手用LabelMe或CVAT導(dǎo)出標(biāo)注時(shí)誤選了“colormap PNG”而非“grayscale PNG”。結(jié)果label.png在Windows畫圖里顯示五顏六色但用cv2.imread()讀取后實(shí)際像素值是[0, 127, 255]這種非連續(xù)整數(shù)——PyTorch的CrossEntropyLoss會(huì)直接報(bào)錯(cuò)Target size must be the same as input size。正確做法是強(qiáng)制轉(zhuǎn)為單通道灰度圖并重映射為連續(xù)整數(shù)。import cv2 import numpy as np from pathlib import Path def convert_label_to_continuous(label_path: Path, class_map: dict): class_map: {background:0, bolt_A:1, bolt_B:2, washer_C:3, rust:4, scratch:5} 將label.png中原始RGB值或離散灰度值統(tǒng)一映射為0~len(class_map)-1的連續(xù)整數(shù) label cv2.imread(str(label_path), cv2.IMREAD_UNCHANGED) # 若是3通道彩色圖先轉(zhuǎn)灰度注意不能用cv2.COLOR_BGR2GRAY因LabelMe colormap非標(biāo)準(zhǔn) if len(label.shape) 3: # LabelMe colormap固定映射(0,0,0)-0, (128,0,0)-1, (0,128,0)-2, (128,128,0)-3, (0,0,128)-4, (128,0,128)-5 # 手動(dòng)查表轉(zhuǎn)換避免顏色抖動(dòng) label_gray np.zeros(label.shape[:2], dtypenp.uint8) for name, idx in class_map.items(): # 定義該類別的RGB閾值根據(jù)你實(shí)際導(dǎo)出的colormap調(diào)整 if name background: mask (label[:,:,0] 0) (label[:,:,1] 0) (label[:,:,2] 0) elif name bolt_A: mask (label[:,:,0] 128) (label[:,:,1] 0) (label[:,:,2] 0) elif name bolt_B: mask (label[:,:,0] 0) (label[:,:,1] 128) (label[:,:,2] 0) elif name washer_C: mask (label[:,:,0] 128) (label[:,:,1] 128) (label[:,:,2] 0) elif name rust: mask (label[:,:,0] 0) (label[:,:,1] 0) (label[:,:,2] 128) elif name scratch: mask (label[:,:,0] 128) (label[:,:,1] 0) (label[:,:,2] 128) else: continue label_gray[mask] idx label label_gray else: # 已是單通道但可能含非連續(xù)值如[0,2,4,6]需重映射 unique_vals np.unique(label) mapping {v: i for i, v in enumerate(sorted(unique_vals))} label np.vectorize(mapping.get)(label) # 強(qiáng)制保存為單通道uint8灰度圖 cv2.imwrite(str(label_path).replace(.png, _clean.png), label) print(f? {label_path.name} - clean: unique values {np.unique(label)}) # 使用示例定義你的6類映射 class_map { background: 0, bolt_A: 1, bolt_B: 2, washer_C: 3, rust: 4, scratch: 5 } for p in Path(data/labels).glob(*.png): convert_label_to_continuous(p, class_map)提示這段代碼的核心邏輯是——不依賴任何第三方標(biāo)注工具的“導(dǎo)出設(shè)置”而是用OpenCV原生操作把label.png的物理像素值無論RGB還是灰度硬解為邏輯類別ID。class_map字典必須與你后續(xù)模型輸出的n_classes完全一致且鍵名順序無關(guān)值必須為0~N-1連續(xù)整數(shù)。執(zhí)行后檢查_clean.png的unique值必須是[0 1 2 3 4 5]缺一不可。2.2 圖像與標(biāo)簽尺寸必須嚴(yán)格一致resize時(shí)的雙三次插值陷阱Unet對(duì)輸入尺寸敏感但更致命的是image和label的H×W不一致。常見錯(cuò)誤用PIL.Image.open().resize()處理image卻用cv2.resize()處理label導(dǎo)致插值方式不同PIL默認(rèn)LANCZOScv2默認(rèn)INTER_LINEAR同一縮放比例下像素偏移1~2像素。解決方案統(tǒng)一用torchvision.transforms.functional且對(duì)image和label使用完全相同的transform對(duì)象。import torch from torchvision import transforms from torchvision.transforms import functional as F from PIL import Image class SegmentationTransform: def __init__(self, size(512, 512)): self.size size def __call__(self, image: Image.Image, target: Image.Image): # 統(tǒng)一使用F.resize且interpolation參數(shù)必須相同 # image用BILINEAR保留紋理細(xì)節(jié)target用NEAREST保持label整數(shù)性 image F.resize(image, self.size, interpolationF.InterpolationMode.BILINEAR) target F.resize(target, self.size, interpolationF.InterpolationMode.NEAREST) # 轉(zhuǎn)tensor前確保target是單通道 if target.mode ! L: target target.convert(L) image F.to_tensor(image) # 歸一化到[0,1]shape [3,H,W] target torch.as_tensor(np.array(target), dtypetorch.long) # 不歸一化shape [H,W] return image, target # 在Dataset.__getitem__中使用 transform SegmentationTransform(size(512, 512)) image Image.open(data/images/001.jpg) label Image.open(data/labels/001_clean.png) # 注意必須是_convert_label_to_continuous生成的_clean.png image_t, label_t transform(image, label) print(f? image shape: {image_t.shape}, label shape: {label_t.shape}, label unique: {torch.unique(label_t)}) # 輸出應(yīng)為image shape: torch.Size([3, 512, 512]), label shape: torch.Size([512, 512]), label unique: tensor([0, 1, 2, 3, 4, 5])參數(shù)說明F.InterpolationMode.BILINEAR用于圖像保證邊緣平滑F(xiàn).InterpolationMode.NEAREST用于label這是鐵律——任何其他插值BILINEAR/AREA都會(huì)把整數(shù)label變成浮點(diǎn)導(dǎo)致CrossEntropyLoss崩潰。torch.as_tensor(..., dtypetorch.long)顯式指定dtype避免默認(rèn)float32引發(fā)的loss計(jì)算錯(cuò)誤。2.3 DataLoader的batch維度對(duì)齊為什么target的shape是[4,512,512]而input是[4,3,512,512]PyTorch的CrossEntropyLoss要求target的shape為[N, H, W]N為batch size而input為[N, C, H, W]。若你在Dataset中返回了[H, W, 1]的label常見于cv2.imread讀取后未squeezeDataLoader會(huì)自動(dòng)堆疊為[N, H, W, 1]觸發(fā)Target size must be the same as input size。必須在__getitem__末尾確保label是二維張量。class CustomSegmentationDataset(torch.utils.data.Dataset): def __init__(self, image_dir, label_dir, transformNone): self.image_paths sorted(list(Path(image_dir).glob(*.jpg))) self.label_paths sorted(list(Path(label_dir).glob(*_clean.png))) # 關(guān)鍵只認(rèn)_clean.png self.transform transform def __getitem__(self, idx): image Image.open(self.image_paths[idx]).convert(RGB) label Image.open(self.label_paths[idx]).convert(L) # 強(qiáng)制單通道 if self.transform: image, label self.transform(image, label) # ?? 死亡陷阱確保label是二維 if label.ndim 3 and label.shape[0] 1: label label.squeeze(0) # [1,H,W] - [H,W] elif label.ndim 3 and label.shape[2] 1: label label.squeeze(2) # [H,W,1] - [H,W] # 再次校驗(yàn)label必須是long型二維張量 assert label.ndim 2, flabel must be 2D, got {label.ndim}D assert label.dtype torch.long, flabel dtype must be torch.long, got {label.dtype} return image, label def __len__(self): return len(self.image_paths) # 實(shí)例化DataLoader dataset CustomSegmentationDataset( image_dirdata/images, label_dirdata/labels, transformSegmentationTransform(size(512, 512)) ) dataloader torch.utils.data.DataLoader( dataset, batch_size4, shuffleTrue, num_workers4, pin_memoryTrue # 關(guān)鍵加速GPU傳輸 )邏輯說明squeeze()操作是防御性編程。即使transform已保證label為二維但在某些PIL版本或異常圖像下仍可能殘留單通道維度。assert語句在debug階段必加上線后可注釋。pin_memoryTrue能將CPU tensor預(yù)加載到page-locked memory使GPU數(shù)據(jù)搬運(yùn)提速20%以上尤其在batch_size2時(shí)效果顯著。3. Unet模型構(gòu)建與PyTorch適配通道數(shù)、初始化、輸出頭必須與多類別嚴(yán)格匹配3.1 自定義Unet為什么不能直接用timm或segmentation_models_pytorch的預(yù)訓(xùn)練Unettimm.unet默認(rèn)輸出通道數(shù)為1二值分割segmentation_models_pytorch.Unet雖支持classesn但其decoder最后一層是Conv2d(64, n, 1)而你的6類分割需要輸出[N,6,H,W]且loss函數(shù)要求logits未softmax。更重要的是這些庫的預(yù)訓(xùn)練權(quán)重如encoder部分來自ImageNet在工業(yè)小樣本場(chǎng)景下易過擬合某實(shí)驗(yàn)室實(shí)測(cè)直接加載imagenet權(quán)重的Unet在6類產(chǎn)線數(shù)據(jù)上val loss震蕩劇烈而從零初始化encoderdecoder配合強(qiáng)augmentation收斂更穩(wěn)。因此我們手寫輕量Unet控制總參數(shù)3M。import torch import torch.nn as nn import torch.nn.functional as F class DoubleConv(nn.Module): (conv BN ReLU) * 2 def __init__(self, in_ch, out_ch): super().__init__() self.conv nn.Sequential( nn.Conv2d(in_ch, out_ch, 3, padding1, biasFalse), nn.BatchNorm2d(out_ch), nn.ReLU(inplaceTrue), nn.Conv2d(out_ch, out_ch, 3, padding1, biasFalse), nn.BatchNorm2d(out_ch), nn.ReLU(inplaceTrue) ) def forward(self, x): return self.conv(x) class Down(nn.Module): def __init__(self, in_ch, out_ch): super().__init__() self.mpconv nn.Sequential( nn.MaxPool2d(2), DoubleConv(in_ch, out_ch) ) def forward(self, x): return self.mpconv(x) class Up(nn.Module): def __init__(self, in_ch, out_ch, bilinearTrue): super().__init__() if bilinear: self.up nn.Upsample(scale_factor2, modebilinear, align_cornersTrue) else: self.up nn.ConvTranspose2d(in_ch // 2, in_ch // 2, 2, stride2) self.conv DoubleConv(in_ch, out_ch) def forward(self, x1, x2): x1 self.up(x1) # input is CHW diffY x2.size()[2] - x1.size()[2] diffX x2.size()[3] - x1.size()[3] x1 F.pad(x1, [diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2]) x torch.cat([x2, x1], dim1) return self.conv(x) class OutConv(nn.Module): def __init__(self, in_ch, out_ch): super().__init__() self.conv nn.Conv2d(in_ch, out_ch, 1) # 關(guān)鍵out_ch n_classes def forward(self, x): return self.conv(x) class UNet(nn.Module): def __init__(self, n_channels3, n_classes6, bilinearTrue): super().__init__() self.n_channels n_channels self.n_classes n_classes self.bilinear bilinear self.inc DoubleConv(n_channels, 64) self.down1 Down(64, 128) self.down2 Down(128, 256) self.down3 Down(256, 512) self.down4 Down(512, 512) self.up1 Up(1024, 256, bilinear) self.up2 Up(512, 128, bilinear) self.up3 Up(256, 64, bilinear) self.up4 Up(128, 64, bilinear) self.outc OutConv(64, n_classes) # ?? 這里必須是n_classes不是1 def forward(self, x): x1 self.inc(x) x2 self.down1(x1) x3 self.down2(x2) x4 self.down3(x3) x5 self.down4(x4) x self.up1(x5, x4) x self.up2(x, x3) x self.up3(x, x2) x self.up4(x, x1) logits self.outc(x) # shape: [N, n_classes, H, W] return logits # 初始化模型并校驗(yàn)輸出 model UNet(n_channels3, n_classes6, bilinearTrue).cuda() dummy_input torch.randn(2, 3, 512, 512).cuda() output model(dummy_input) print(f? Model output shape: {output.shape}) # 應(yīng)為 torch.Size([2, 6, 512, 512]) print(f? Output min/max: {output.min().item():.3f}, {output.max().item():.3f})參數(shù)說明n_classes6直接決定OutConv的輸出通道數(shù)這是整個(gè)流程的錨點(diǎn)。bilinearTrue啟用雙線性上采樣比轉(zhuǎn)置卷積更穩(wěn)定align_cornersTrue解決PyTorch 1.13中upsample的坐標(biāo)對(duì)齊bug避免分割邊界偏移。dummy_input測(cè)試必須在GPU上運(yùn)行因?yàn)楹罄m(xù)loss計(jì)算依賴CUDA kernel。3.2 權(quán)重初始化策略為什么He初始化比Xavier更適合Unet的ReLUUnet大量使用ReLU激活其輸出存在“死亡神經(jīng)元”風(fēng)險(xiǎn)。Xavier初始化適用于tanh/sigmoid會(huì)使ReLU前向傳播方差衰減而He初始化專為ReLU設(shè)計(jì)能保持各層輸出方差穩(wěn)定。我們?cè)赺_init__后立即調(diào)用init_weights()。def init_weights(m): if isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d): # He初始化std sqrt(2 / fan_in) nn.init.kaiming_normal_(m.weight, modefan_in, nonlinearityrelu) if m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0) # 應(yīng)用初始化 model UNet(n_channels3, n_classes6).cuda() model.apply(init_weights) # 驗(yàn)證檢查第一層卷積權(quán)重標(biāo)準(zhǔn)差 print(f? conv1 weight std: {model.inc.conv[0].weight.std().item():.4f}) # 應(yīng)接近0.05邏輯說明nn.init.kaiming_normal_的modefan_in表示以輸入通道數(shù)fan_in為歸一化基準(zhǔn)這符合Unet encoder中特征圖通道數(shù)遞增、空間尺寸遞減的特點(diǎn)。nonlinearityrelu告知初始化器激活函數(shù)類型自動(dòng)計(jì)算最優(yōu)std。若跳過此步某實(shí)驗(yàn)室實(shí)測(cè)未初始化的Unet在epoch5前l(fā)oss下降極慢且梯度norm波動(dòng)劇烈。3.3 輸出頭與loss的耦合logits必須直接喂給CrossEntropyLoss禁止softmaxCrossEntropyLoss LogSoftmax NLLLoss它內(nèi)部已包含softmax若你手動(dòng)F.softmax(logits, dim1)再傳入會(huì)導(dǎo)致雙重softmax輸出概率全趨近于1loss爆炸。這是新手最高頻翻車點(diǎn)。# ? 錯(cuò)誤示范手動(dòng)softmax logits model(image_batch) # shape [N,6,H,W] pred_prob F.softmax(logits, dim1) # 錯(cuò)dim1是channel維度 loss criterion(pred_prob, target_batch) # 報(bào)錯(cuò)或lossnan # ? 正確示范logits直連 logits model(image_batch) # shape [N,6,H,W] loss criterion(logits, target_batch) # target_batch shape [N,H,W]dtype long關(guān)鍵參數(shù)criterion nn.CrossEntropyLoss(ignore_index255)。ignore_index用于跳過無效像素如標(biāo)注中的ignore區(qū)域但你的數(shù)據(jù)若無ignore區(qū)域必須設(shè)為-100PyTorch默認(rèn)值不可設(shè)為0背景類會(huì)被忽略。若你強(qiáng)行設(shè)ignore_index0則背景類像素不參與loss計(jì)算模型永遠(yuǎn)學(xué)不會(huì)區(qū)分背景和其他類。4. 多類別分割的損失函數(shù)與評(píng)估DiceLossCE混合、逐類IoU計(jì)算不可少4.1 混合損失函數(shù)為什么單用CrossEntropyLoss在類別不平衡時(shí)失效你的6類數(shù)據(jù)中“background”占85%像素“rust”僅占0.3%。單用CrossEntropyLoss會(huì)導(dǎo)致模型偏向預(yù)測(cè)background驗(yàn)證集上rust類IoU恒為0。解決方案DiceLoss懲罰前景類召回率CE Loss保證全局分類能力按0.5:0.5加權(quán)。class DiceLoss(nn.Module): def __init__(self, smooth1.0, ignore_index-100): super().__init__() self.smooth smooth self.ignore_index ignore_index def forward(self, logits, target): # logits: [N,C,H,W], target: [N,H,W] probs F.softmax(logits, dim1) # [N,C,H,W] # 將target轉(zhuǎn)為one-hot: [N,H,W] - [N,C,H,W] target_one_hot F.one_hot(target, num_classeslogits.shape[1]).permute(0,3,1,2).float() # 忽略ignore_index位置 if self.ignore_index 0: valid_mask (target ! self.ignore_index).unsqueeze(1).float() # [N,1,H,W] probs probs * valid_mask target_one_hot target_one_hot * valid_mask intersection (probs * target_one_hot).sum(dim(2,3)) # [N,C] cardinality (probs target_one_hot).sum(dim(2,3)) # [N,C] dice_coeff (2. * intersection self.smooth) / (cardinality self.smooth 1e-8) # 對(duì)每個(gè)類別求平均再對(duì)batch求平均 dice_loss 1 - dice_coeff.mean() return dice_loss class CombinedLoss(nn.Module): def __init__(self, ce_weight0.5, dice_weight0.5, ignore_index-100): super().__init__() self.ce_weight ce_weight self.dice_weight dice_weight self.ce_loss nn.CrossEntropyLoss(ignore_indexignore_index) self.dice_loss DiceLoss(ignore_indexignore_index) def forward(self, logits, target): ce self.ce_loss(logits, target) dice self.dice_loss(logits, target) return self.ce_weight * ce self.dice_weight * dice # 實(shí)例化損失函數(shù) criterion CombinedLoss(ce_weight0.5, dice_weight0.5, ignore_index-100).cuda() # 測(cè)試構(gòu)造模擬數(shù)據(jù) logits_test torch.randn(2, 6, 128, 128).cuda() target_test torch.randint(0, 6, (2, 128, 128)).cuda() loss_test criterion(logits_test, target_test) print(f? Combined loss: {loss_test.item():.4f})參數(shù)說明smooth1.0防止分母為01e-8是額外保險(xiǎn)ignore_index-100與CrossEntropyLoss默認(rèn)值對(duì)齊確保一致性。ce_weight和dice_weight需根據(jù)數(shù)據(jù)平衡性調(diào)整若前景類極少1%可提高dice_weight至0.7若各類均衡0.5:0.5即可。F.one_hot是PyTorch 1.8特性低于此版本需用torch.eye手動(dòng)構(gòu)造。4.2 逐類IoU計(jì)算mIoU不是唯一指標(biāo)必須看每一類的召回與精確率CrossEntropyLoss優(yōu)化的是像素級(jí)分類準(zhǔn)確率但語義分割核心指標(biāo)是IoU交并比。mIoU是各類IoU的平均值但若某類IoU0.95另一類0.01mIoU0.48看似尚可實(shí)則模型完全失效。必須輸出每類IoU表格。def compute_iou_per_class(pred_mask, target_mask, num_classes6, ignore_index-100): pred_mask: [N,H,W] int64, 每個(gè)像素的預(yù)測(cè)類別ID target_mask: [N,H,W] int64, 真實(shí)類別ID 返回: list of IoU per class, lengthnum_classes ious [] for cls in range(num_classes): # 獲取該類別的預(yù)測(cè)和真實(shí)mask pred_cls (pred_mask cls) target_cls (target_mask cls) # 忽略ignore_index位置 if ignore_index 0: valid (target_mask ! ignore_index) pred_cls pred_cls valid target_cls target_cls valid intersection (pred_cls target_cls).sum().item() union (pred_cls | target_cls).sum().item() if union 0: ious.append(float(nan)) # 該類無真值IoU無定義 else: ious.append(intersection / union) return ious # 在驗(yàn)證循環(huán)中調(diào)用 model.eval() all_ious [] with torch.no_grad(): for images, targets in val_dataloader: images, targets images.cuda(), targets.cuda() logits model(images) preds logits.argmax(dim1) # [N,H,W] ious compute_iou_per_class(preds, targets, num_classes6) all_ious.append(ious) # 匯總對(duì)所有batch取平均忽略nan ious_array np.array(all_ious) # shape [num_batches, 6] mean_ious np.nanmean(ious_array, axis0) class_names [background, bolt_A, bolt_B, washer_C, rust, scratch] print(\n Per-class IoU:) for name, iou in zip(class_names, mean_ious): print(f{name:12s}: {iou:.4f}) print(fmIoU: {np.nanmean(mean_ious):.4f})邏輯說明preds logits.argmax(dim1)是獲取預(yù)測(cè)類別ID的標(biāo)準(zhǔn)操作dim1對(duì)應(yīng)channel維度。compute_iou_per_class中union 0的判斷至關(guān)重要——當(dāng)某類在當(dāng)前batch中完全未出現(xiàn)如rust類樣本極少union為0此時(shí)IoU無意義返回nan最終np.nanmean會(huì)自動(dòng)忽略。這樣得到的mIoU才真實(shí)反映模型能力。4.3 避坑多類別分割的5個(gè)高頻翻車現(xiàn)場(chǎng)與血淚解法現(xiàn)象1訓(xùn)練loss下降但驗(yàn)證mIoU始終為0且所有預(yù)測(cè)都是background原因label.png的灰度值未重映射為0~5而是[0,10,20,30,40,50]導(dǎo)致CrossEntropyLoss將所有非0像素視為ignore_index默認(rèn)-100只學(xué)習(xí)background。解決嚴(yán)格執(zhí)行2.1節(jié)的convert_label_to_continuous()并用np.unique()打印clean label的值確認(rèn)為[0 1 2 3 4 5]?,F(xiàn)象2loss在0.3附近震蕩不下降梯度norm極小1e-5原因模型權(quán)重未初始化或init_weights()未應(yīng)用到所有子模塊如忘記model.apply()。解決在模型實(shí)例化后立即調(diào)用model.apply(init_weights)并用print(model.inc.conv[0].weight.std())驗(yàn)證首層權(quán)重標(biāo)準(zhǔn)差是否≈0.05?,F(xiàn)象3驗(yàn)證時(shí)preds logits.argmax(dim1)報(bào)錯(cuò)RuntimeError: argmax(): Expected dtype int64 or fp32原因logits是fp16混合精度訓(xùn)練argmax不支持fp16輸入。解決preds logits.float().argmax(dim1)或在AMP上下文中用with torch.cuda.amp.autocast(enabledFalse):包裹預(yù)測(cè)代碼。現(xiàn)象4mIoU計(jì)算中某類IoU為nan且該類在驗(yàn)證集出現(xiàn)頻率高原因ignore_index設(shè)為0而0是background類導(dǎo)致background被忽略u(píng)nion恒為0。解決ignore_index必須設(shè)為-100PyTorch默認(rèn)或一個(gè)不在類別ID中的值如255絕不可設(shè)為任一有效類別ID。現(xiàn)象5訓(xùn)練時(shí)GPU顯存爆滿batch_size1就OOM原因Unet的skip connection中torch.cat([x2, x1], dim1)將encoder特征與upsampled特征拼接若encoder輸出通道為512upsampled為256則拼接后為768通道顯存激增。解決在Up模塊中將self.up nn.ConvTranspose2d(in_ch // 2, in_ch // 2, 2, stride2)改為self.up nn.Upsample(scale_factor2, modebilinear, align_cornersTrue)并確保bilinearTrue可減少30%顯存占用。5. 訓(xùn)練循環(huán)與早停策略動(dòng)態(tài)學(xué)習(xí)率、梯度裁剪、模型保存的工業(yè)級(jí)實(shí)踐5.1 帶warmup的余弦退火學(xué)習(xí)率為什么固定lr1e-3在第10輪就過擬合Unet對(duì)學(xué)習(xí)率敏感。某實(shí)驗(yàn)室對(duì)比實(shí)驗(yàn)顯示固定lr1e-3時(shí)loss在epoch5驟降但val mIoU在epoch8達(dá)峰后持續(xù)下跌而采用warmupcosinelr從1e-5線性升至1e-3warmup 5 epoch再余弦退火至1e-6val mIoU穩(wěn)定提升至epoch30。代碼實(shí)現(xiàn)如下from torch.optim.lr_scheduler import CosineAnnealingLR, LinearLR, SequentialLR def get_scheduler(optimizer, warmup_epochs5, total_epochs50, min_lr1e-6): warmup_scheduler LinearLR( optimizer, start_factor1e-3, # 從lr*1e-3開始 end_factor1.0, # 到lr*1.0結(jié)束 total_iterswarmup_epochs ) cosine_scheduler CosineAnnealingLR( optimizer, T_maxtotal_epochs - warmup_epochs, eta_minmin_lr ) scheduler SequentialLR( optimizer, schedulers[warmup_scheduler, cosine_scheduler], milestones[warmup_epochs] ) return scheduler # 使用示例 optimizer torch.optim.AdamW(model.parameters(), lr1e-3, weight_decay1e-4) scheduler get_scheduler(optimizer, warmup_epochs5, total_epochs50) # 在訓(xùn)練循環(huán)中 for epoch in range(50): model.train() for images, targets in train_dataloader: images, targets images.cuda(), targets.cuda() optimizer.zero_grad() logits model(images) loss criterion(logits, targets) loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm1.0) # 梯度裁剪 optimizer.step() scheduler.step() # 更新學(xué)習(xí)率 print(fEpoch {epoch1}, LR: {scheduler.get_last_lr()[0]:.2e})參數(shù)說明LinearLR的start_factor1e-3表示warmup起始lr為1e-3 * 1e-3 1e-6end_factor1.0表示結(jié)束lr為1e-3 * 1.0 1e-3CosineAnnealingLR的eta_min1e-6是最終最小lr。torch.nn.utils.clip_grad_norm_將梯度norm限制在1.0內(nèi)防止梯度爆炸這是訓(xùn)練穩(wěn)定性的后悔藥。5.2 早停與最佳模型保存監(jiān)控val mIoU而非lossloss下降不代表分割質(zhì)量提升必須以val mIoU為早停依據(jù)。保存最佳模型時(shí)不僅存state_dict還要存optimizer和scheduler狀態(tài)便于斷點(diǎn)續(xù)訓(xùn)。class EarlyStopping: def __init__(self, patience7, delta0.001, save_pathbest_model.pth): self.patience patience self.delta delta self.save_path save_path self.best_score None self.epochs_no_improve 0 self.improved False def __call__(self, val_iou, model, optimizer, scheduler): score val_iou if self.best_score is None: self.best_score score self.save_checkpoint(model, optimizer, scheduler) self.improved True elif score self.best_score self.delta: self.epochs_no_improve 1 self.improved False if self.epochs_no_improve self.patience: return True # 觸發(fā)早停 else: self.best_score score self.epochs_no_improve 0 self.save_checkpoint(model, optimizer, scheduler) self.improved True return False def save_checkpoint(self, model, optimizer, scheduler): torch.save({ epoch: epoch, model_state_dict: model.state_dict(), optimizer_state_dict: optimizer.state_dict(), scheduler_state_dict: scheduler.state_dict(), best_val_iou: self.best_score, }, self.save_path) print(f? Saved best model to {self.save_path}) # 在驗(yàn)證循環(huán)后調(diào)用 early_stopping EarlyStopping(patience10, delta0.002, save_pathunet_best.pth) for epoch in range(50): # ... 訓(xùn) p a hrefhttps://download.csdn.net/download/brf_UCAS/14109453 stylecolor:#ec7500;font-size:14px; 本文還有配套的精品資源點(diǎn)擊獲取 /a img altmenu-r.4af5f7ec.gif srchttps://csdnimg.cn/release/wenkucmsfe/public/img/menu-r.4af5f7ec.gif stylewidth:16px;margin-left:4px;vertical-align:text-bottom;cursor:text; /p