OCT训练阶段性总结)

Posted 今晚看星星

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OCT训练阶段性总结)相关的知识,希望对你有一定的参考价值。

文章目录

变量与不变的量

1.训练过程中不变的量

1.1 数据

  • 图像大小: (512 * 512)
  • 不采用任何的图像处理

1.2 模型

  • in_channels = 1, out_channels = 1
  • 下采样为: MaxPooling, 上采样为: TransposeConv

1.3 损失函数 - BCEDiceLoss

实现:

class BCEDiceLoss(torch.nn.Module):

    def __init__(self):
        super().__init__()

    def forward(self, x, target):

        bce = F.binary_cross_entropy_with_logits(x, target)

        x = torch.sigmoid(x)
        num = target.size(dim=0)   #求所在的行数
        x = x.view(num, -1)
        target = target.view(num, -1)
		
		# 设置smooth变量,防止除零出错
        smooth = 1e-5
        intersection = (x * target).sum(dim=1)  # 按照列求和
        dice = (2. * intersection + smooth) / (x.sum(dim=1) + target.sum(dim=1) + smooth)
        dice = 1 - dice.sum() / num
        return 0.5 * bce + dice

1.4 优化器 - Adam

  • 固定的学习率: 3e-4

1.5 评价指标

  • MeanIOU

Code:

def get_single_image_IOU(x, y, is_threshed=True):
    device = torch.device('cpu')

    if x.shape != y.shape:
        assert "图片维数不一致!"
    # 对tensor进行深拷贝
    x = x.detach().clone().to(device)
    y = y.detach().clone().to(device)

    if not is_threshed:

        x[x >= 0.5] = 1
        x[x < 0.5] = 0

    intersection = (x * y).sum()  # 二分类时 mask为1, 采用矩阵逐个元素相乘的办法获取intersection

    union = x.sum() + y.sum() - intersection

    rate = intersection / union
    return rate

def get_mean_IOU(image1, image2, is_threshed=True):
    batch = image1.shape[0]

    total_rate = 0
    for i in range(batch):
        rate = get_single_image_IOU(image1[i][0], image2[i][0], is_threshed)
        total_rate += rate

    return total_rate / batch

  • 平均像素准确度 pixel-accuracy
def pixel_acc(x, target, image_size, is_threshed=True):
    x = x.detach().clone()
    target = target.detach().clone()

    if not is_threshed:
        x[x >= 0.5] = 1
        x[x < 0.5] = 0

    batch = x.shape[0]
    total_acc = 0
    for i in range(batch):
        channels = x[i].shape[0]
        for j in range(channels):
            pixel_a = (x[i][j] == target[i][j]).sum()
            acc = pixel_a / (image_size[0] * image_size[1])
            total_acc += acc

    return total_acc / batch

2. 训练过程中变化的量

2.1 按照人划分 训练集与验证集

从5开始增加,增加到25

epoch = 5

  • mean_loss

每100个batch

每个epoch

epoch = 10

  • batch

  • epoch

epoch = 15

  • batch

  • epoch

epoch = 20

  • batch

  • epoch

epoch = 25

  • batch

  • epoch

epoch = 30

  • batch

  • epoch

2.2 随机划分训练集与验证集

训练时只划分一次 : epoch = 20

  • batch

  • epoch

每个epoch划分一次 (不建议: val也参与到了训练中)

epoch = 36

  • batch

  • epoch

以上是关于OCT训练阶段性总结)的主要内容,如果未能解决你的问题,请参考以下文章

训练指南DP阶段训练1

两阶段目标检测R-CNN论文精读与学习总结

两阶段目标检测R-CNN论文精读与学习总结

李新海:师徒聊“口才训练三阶段”

《LeetCode零基础指南》(第十二讲) 如何系统的训练

OOP前三次训练总结