UNet进行裂缝分割
项目描述
裂缝分割的任务是从道路图像或视频中识别并分割出裂缝。在本项目中,我们使用UNet模型来检测裂缝。我们评估了不同损失函数的性能,并比较了它们的结果。以下是实现的损失函数:
• 交叉熵损失(Cross Entropy Loss)
• Dice损失(Dice Loss)
• Dice交叉熵损失(Dice Cross Entropy Loss)
• 焦点损失(Focal Loss)
我们使用上述损失函数训练模型,并通过Dice系数(Dice Score = 1 - Dice Loss)评估其性能。需要注意的是,Dice Score可能不是比较这些模型的唯一正确指标。
安装
下载项目:
安装依赖:
pipinstall-rrequirements.txt下载模型权重:将使用Dice Loss训练的模型权重
model.pt放入weights文件夹中。
使用
数据集
下载数据集并将train和test文件夹放入data文件夹中,结构如下:data-| |-train-| |-images |-masks |-test -| |-images |-masks训练
运行以下命令进行训练:python train.py训练参数:
python train.py-husage: train.py[-h][--data DATA][--image_size IMAGE_SIZE][--save-dir SAVE_DIR][--epochs EPOCHS][--batch-size BATCH_SIZE][--lr LR][--weights WEIGHTS][--amp][--num-classes NUM_CLASSES]optional arguments: -h,--helpshow thishelpmessage andexit--dataDATA Path to root folder of data--image_sizeIMAGE_SIZE Input image size, default:512--save-dir SAVE_DIR Directory to save weights--epochsEPOCHS Number of epochs, default:5--batch-size BATCH_SIZE Batch size, default:12--lrLR Learning rate, default: 1e-5--weightsWEIGHTS Pretrained model, default: None--ampUse mixed precision --num-classes NUM_CLASSES Number of classes
- 推理
运行以下命令进行推理:
推理参数:python inference.py--weightsweights/model.pt--inputassets/CFD_001_image.jpgpython inference.py-husage: inference.py[-h][--weights WEIGHTS][--input INPUT][--output OUTPUT][--image-size IMAGE_SIZE][--view][--no-save][--conf-thresh CONF_THRESH]optional arguments: -h,--helpshow thishelpmessage andexit--weightsWEIGHTS Path to weightfile(default: last.pt)--inputINPUT Path to input image--outputOUTPUT Path to save mask image --image-size IMAGE_SIZE Input image size--viewVisualize image and mask --no-save Do not save the output masks --conf-thresh CONF_THRESH Confidence thresholdformask
结果
以下是不同损失函数的Dice Score结果:
| 损失函数 | Dice Score |
|---|---|
| 交叉熵损失 | 0.9719 |
| Dice损失 | 0.9804 |
| Dice交叉熵损失 | 0.9754 |
| 焦点损失 | 0.9679 |