python – 我试图用预先训练的网络对花进行分类,但由于某种原因它不训练

我目前正在尝试使用Pytorch对
dataset中的鲜花进行分类.

首先,我开始从我的数据转换为培训,验证和测试集.

data_dir = 'flowers'
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'

train_transforms = transforms.Compose([transforms.RandomRotation(30),
                                       transforms.RandomResizedCrop(224),
                                       transforms.RandomHorizontalFlip(),
                                       transforms.ToTensor(),
                                       transforms.Normalize([0.485, 0.456, 0.406], 
                                                            [0.229, 0.224, 0.225])])

test_transforms = transforms.Compose([transforms.Resize(224),
                                      transforms.CenterCrop(224),
                                      transforms.ToTensor(),
                                      transforms.Normalize([0.485, 0.456, 0.406], 
                                                           [0.229, 0.224, 0.225])])

然后我用ImageFolder加载数据:

trainset = datasets.ImageFolder(train_dir, transform=train_transforms)
testset = datasets.ImageFolder(test_dir, transform=test_transforms)
validationset = datasets.ImageFolder(valid_dir, transform=test_transforms)

然后我定义了我的DataLoaders:

trainloader = torch.utils.data.DataLoader(trainset, batch_size = 64, shuffle = True)
testloader = torch.utils.data.DataLoader(testset, batch_size = 32)
validationloader = torch.utils.data.DataLoader(validationset, batch_size = 32)

我选择vgg作为我的预训练模型:

model = models.vgg16(pretrained = True)

并定义了一个新的分类器:

for param in model.parameters():
    param.requires_grad = False

classifier = nn.Sequential(OrderedDict([
    ('fc1', nn.Linear(25088, 4096)),
    ('relu', nn.ReLU()),
    ('fc2', nn.Linear(4096, 4096)),
    ('relu', nn.ReLU()),
    ('fc3', nn.Linear(4096, 102)),
    ('output', nn.Softmax(dim = 1))

]))

model.classifier = classifier 

这是实际训练我的NN(在GPU上)的代码:

criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr = 0.005)

epochs = 9
print_every = 10
steps = 0

model.to('cuda')

for e in range(epochs):
    running_loss = 0

    for ii, (inputs, labels) in enumerate(trainloader):
        steps += 1



        inputs, labels = inputs.to('cuda'), labels.to('cuda')

        optimizer.zero_grad()

        # Forward and backward 
        outputs = model.forward(inputs)
        loss = criterion(outputs, labels)



        loss.backward()
        optimizer.step()

        running_loss += loss.item()



        if steps % print_every == 0:
            print("Epoch: {}/{}... ".format(e+1, epochs),
                  "Loss: {:.4f}".format(running_loss/print_every))

            running_loss = 0

但是当我运行我的模型时,损失是随机的,我不知道为什么.

感谢您提前提供任何帮助和来自德国的问候!

最佳答案 以下是一些提示 – 按照我认为有用的顺序排列:

>尝试进行一些超参数优化. (即在1e-2到1e-6等域上尝试10个学习率)更多信息是什么:(http://cs231n.github.io/neural-networks-3/#hyper)
>编码并打印精度指标(打印出您的损失),因为您可能会对预先训练的模型精度有多高感到惊讶.
>尝试切换到model = models.vgg16_bn(pretrained = True)以及更大的网络,如vgg 19或resnet34

你可以包括你的准确性和每个时期丢失?

如果有任何提示有帮助,请告诉我!

(来自美国的你好)

点赞