Deep learning UNet模型精度保持在精确的0.5(不大于或小于)(没有班级不平衡,尝试调整学习率)

Deep learning UNet模型精度保持在精确的0.5(不大于或小于)(没有班级不平衡,尝试调整学习率),deep-learning,computer-vision,pytorch,conv-neural-network,image-segmentation,Deep Learning,Computer Vision,Pytorch,Conv Neural Network,Image Segmentation,这是在使用PyTorch 我一直在尝试在我的图像上实现UNet模型,但是,我的模型精度始终精确到0.5。损失确实减少了 我还检查了班级不平衡。我也尝试过学习速度。学习率影响损失,但不影响准确性 我的架构如下(从这里开始) “”“`UNet`类基于https://arxiv.org/abs/1505.04597 U网络是一种卷积编码-解码神经网络。 上下文空间信息(来自解码, 关于输入张量的扩展路径)与 表示细节本地化的信息 (从编码、压缩途径)。 对原始文件的修改: (1) 在3x3卷积中使用填

这是在使用PyTorch

我一直在尝试在我的图像上实现UNet模型,但是,我的模型精度始终精确到0.5。损失确实减少了

我还检查了班级不平衡。我也尝试过学习速度。学习率影响损失,但不影响准确性

我的架构如下(从这里开始)

“”“`UNet`类基于https://arxiv.org/abs/1505.04597
U网络是一种卷积编码-解码神经网络。
上下文空间信息(来自解码,
关于输入张量的扩展路径)与
表示细节本地化的信息
(从编码、压缩途径)。
对原始文件的修改:
(1) 在3x3卷积中使用填充以防止丢失
边界像素
(2) 由于(1),合并输出不需要裁剪
(3) 剩余连接可通过指定
UNet(合并模式='add')
(4) 如果解码器中使用了非参数上采样
路径(由upmode='upsample'指定),然后
上采样后会出现额外的1x1 2d卷积
将通道维度减少2倍。
这种通道减半是在卷积的情况下发生的
变换卷积(由upmode='transpose'指定)
论据:
in_channels:int,输入张量中的通道数。
RGB图像的默认值为3。我们的SPARCS数据集为13通道。
深度:int,U-Net中的最大池数。在训练期间,输入大小需要
(深度-1)可被2整除的次数
start_filtes:int,第一次转换的卷积滤波器数。
向上模式:字符串,向上卷积的类型。选择:“转置”用于转置卷积
"""
类UNet(nn.模块):
def(self,num(类),depth,in(通道),start(过滤器)16,up(模式)转置,merge(模式)concat):
super(UNet,self)。\uuuu init\uuuuu()
如果“上采样”模式处于(“转置”、“上采样”):
self.up\u mode=up\u mode
其他:
raise VALUERROR(“\“{}\”不是有效的上采样模式。只允许使用“转置”和“上采样”。.format(上采样模式))
如果在('concat','add')中使用合并模式:
self.merge\u mode=合并\u mode
其他:
raise VALUERROR(“\”{}\”不是合并上行和下行路径的有效模式。只允许使用\“concat\”和\“add\”。.format(上行模式))
#注意:向上模式“向上采样”与合并模式“添加”不兼容
如果self.up_模式=='upsample'和self.merge_模式=='add':
raise VALUE ERROR(“向上模式\“向上采样\”与当前的合并模式\“添加\”不兼容”
“因为使用最近邻来减少深度通道(减少一半)是没有意义的。”)
self.num\u classes=num\u classes
self.in_channels=in_channels
self.start\u filtes=启动\u filtes
self.depth=深度
self.down_convs=[]
self.up_convs=[]
#创建编码器路径并添加到列表中
对于范围内的i(深度):
如果i==0,则ins=self.in_通道
outs=自启动过滤器*(2**i)
池=如果i
参数包括:

device=torch.device('cuda'如果torch.cuda.is_可用(),则为'cpu')
x、 y=列车_序列[0];批次大小=x.shape[0]
model=UNet(num_classes=2,depth=5,in_channels=5,merge_mode='concat')。到(设备)
optim=torch.optim.Adam(model.parameters(),lr=0.01,weight\u decay=1e-3)
criteria=nn.BCEWithLogitsLoss()#内部有乙状结肠
纪元=1000
培训的功能是:

import torch.nn.functional as f


def train_model(epoch,train_sequence):
    """Train the model and report validation error with training error
    Args:
        model: the model to be trained
        criterion: loss function
        data_train (DataLoader): training dataset
    """
    model.train()
    for idx in range(len(train_sequence)):        
        X, y = train_sequence[idx]             
        images = Variable(torch.from_numpy(X)).to(device) # [batch, channel, H, W]
        masks = Variable(torch.from_numpy(y)).to(device) 

        outputs = model(images)
        print(masks.shape, outputs.shape)
        loss = criterion(outputs, masks)
        optim.zero_grad()
        loss.backward()
        # Update weights
        optim.step()
    # total_loss = get_loss_train(model, data_train, criterion)
我的计算损耗和精度的函数如下所示:

def get_loss_列车(型号、列车顺序):
"""
计算列车组的损耗
"""
model.eval()
总成本=0
总损失=0
对于范围内的idx(len(序列号顺序)):
使用手电筒。无梯度()
十、 y=列车顺序[idx]
图像=变量(torch.from
for epoch in range(epochs):
    train_model(epoch, train_sequence)
    train_acc, train_loss = get_loss_train(model,train_sequence)
    print("Train Acc:", train_acc)
    print("Train loss:", train_loss)