【问题标题】:I try to implement vggnet, but it does not be trained well我尝试实现vggnet,但没有很好地训练
【发布时间】:2021-04-05 08:47:44
【问题描述】:

我是 CNN 的新人。 我尝试训练 vggnet。

类网络(nn.Module):

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

    self.conv = nn.Sequential ( 
        #1
        #####
        nn.Conv2d(3,64,3, padding=1),  nn.ReLU(inplace=True), 
        nn.Conv2d(64,64,3, padding=1),nn.ReLU(inplace=True),
        nn.MaxPool2d(2,2),
        #2
        #####
        nn.Conv2d(64,128,3, padding=1), nn.ReLU(inplace=True),
        nn.Conv2d(128,128,3, padding=1),nn.ReLU(inplace=True),
        nn.MaxPool2d(2,2),       
        #####
        #3
        #####
        nn.Conv2d(128,256,3, padding=1),nn.ReLU(inplace=True),
        nn.Conv2d(256,256,3, padding=1),nn.ReLU(inplace=True),
        nn.Conv2d(256,256,3, padding=1),nn.ReLU(inplace=True),
        nn.MaxPool2d(2,2),      
        #4
        #####
        nn.Conv2d(256,512,3, padding=1), nn.ReLU(inplace=True),
        nn.Conv2d(512,512,3, padding=1),nn.ReLU(inplace=True),
        nn.Conv2d(512,512,3, padding=1), nn.ReLU(inplace=True),
        nn.MaxPool2d(2,2),  
        #5
        #####
        nn.Conv2d(512,512,3, padding=1),nn.ReLU(inplace=True),
        nn.Conv2d(512,512,3, padding=1),nn.ReLU(inplace=True),
        nn.Conv2d(512,512,3, padding=1),nn.ReLU(inplace=True),
        nn.MaxPool2d(2,2), 
        ##### 
    )
    self.fc = nn.Sequential(
       nn.Linear(512 * 7 * 7, 4096), nn.ReLU(inplace=True), nn.Dropout(0.5),
       nn.Linear(4096, 4096), nn.ReLU(inplace=True), nn.Dropout(0.5),
       nn.Linear(4096, 1000)
    )

    
   


def forward(self, x):

    # diffrent depending on the model
    x = self.conv(x)
    # change dimension 
    x = torch.flatten(x, 1)
    # same 
    x = self.fc(x)

    return x

但损失在6.9077附近。

在 epoch 0 之后,它很少改变。

即使我将权重衰减更改为 0(不使用 L2 归一化),损失也会略有变化。

我的优化器和调度器是

优化器 = torch.optim.SGD(net.parameters(), lr=0.1, weight_decay=5e-4)

scheduler = lr_scheduler.ReduceLROnPlateau(优化器, 因子 = 0.1, 耐心= 2,mode='min')

有什么问题。 有时,它打印字节但只得到 0。 警告.warn(str(msg)) 这和我的问题有关吗?

【问题讨论】:

  • 我使用 transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5 , 0.5)), ])
  • 和 trainset = torchvision.datasets.ImageNet('./data', split='train', download=None, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size =64, shuffle=True, num_workers = 4, pin_memory = True) testset = torchvision.datasets.ImageNet('./data', split='val', download=None, transform=transform) testloader = torch.utils.data .DataLoader(testset, batch_size=64, shuffle=False, num_workers = 4, pin_memory = True)

标签: python pytorch conv-neural-network training-data vgg-net


【解决方案1】:

您的损失值6.9077 等于-log(1/1000),这基本上意味着您的网络从所有可能的 1000 个类别中产生随机输出。

从头开始训练 VGG 网络有点棘手,尤其是在不包含批规范层的情况下。

尝试将学习率降低到 0.01,并为您的 SGD 增加动力。

添加更多输入增强(例如,翻转颜色抖动等)。

【讨论】:

  • 对不起,我设置为 0.01
  • Lr = 0.1 是我的错误,据我所知,默认值是 0.9
  • 当我添加 batchnorm 效果很好。但是我想尝试没有BN。因为听说vgg的论文不使用BN
  • @Jjh 确实,batchnorm 在第一篇 vgg 论文之后问世。你可能想去原始论文看看他们使用了哪些元参数
  • 我在论文中设置了权重衰减(l2 惩罚)学习率...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2018-12-24
相关资源
最近更新 更多