当前位置:   article > 正文

mnist数据集使用resnet50预训练网络进行微调_resnet50预训练 微调

resnet50预训练 微调
加载预训练网络模型并加载权重
resnet50=torchvision.models.resnet50(weights=torchvision.models.ResNet50_Weights.DEFAULT)

in_features=resnet50.fc.in_features

# 将原resnet50网络中的最后一个全连接层改成10分类的输出
resnet50.fc=nn.Linear(in_features,10)
resnet50=resnet50.to(device)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

因为resnet50网络需要输入224x224x3大小的图片
因此对网络接收的输入也要做相应的调整

tf=torchvision.transforms.Compose([

    torchvision.transforms.Resize(size=(224,224)),
    torchvision.transforms.Grayscale(num_output_channels=3),
    torchvision.transforms.ToTensor(),
    # torchvision.transforms.Normalize((0.1307,),(0.3081,))
    torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
固定卷积层参数
# 固定卷积层的参数
optim=torch.optim.Adam(resnet50.fc.parameters(),lr=0.001)
  • 1
  • 2

完整代码:

import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader

tf=torchvision.transforms.Compose([

    torchvision.transforms.Resize(size=(224,224)),
    torchvision.transforms.Grayscale(num_output_channels=3),
    torchvision.transforms.ToTensor(),
    # torchvision.transforms.Normalize((0.1307,),(0.3081,))
    torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])


transforms = torchvision.transforms.Compose([
                                # torchvision.transforms.Normalize((0.1307,),(0.3081,)
                                torchvision.transforms.RandomHorizontalFlip(),
                                torchvision.transforms.ColorJitter(brightness=0.5,contrast=0.5,saturation=0.5,hue=0.5),
                                torchvision.transforms.ToTensor()])
# 导入数据集

train_data=torchvision.datasets.MNIST(root='./dataset', train=True,transform=tf,download=True)
test_data=torchvision.datasets.MNIST(root='./dataset', train=False,transform=tf,download=True)
test_size=len(test_data)

device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
batch_size=128

trainloader=DataLoader(train_data,batch_size=batch_size)
testlooader=DataLoader(test_data,batch_size=batch_size)

# 定义LeNet网络
class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.model=nn.Sequential(
            # MNIST数据集大小为28x28,要先做padding=2的填充才满足32x32的输入大小
            nn.Conv2d(1,6,5,1,2),
            nn.ReLU(),
            nn.MaxPool2d(2,2),
            nn.Conv2d(6,16,5),
            nn.ReLU(),
            nn.MaxPool2d(2,2),
            nn.Flatten(),
            nn.Linear(16*5*5,120),
            nn.ReLU(),
            nn.Linear(120,84),
            nn.ReLU(),
            nn.Linear(84,10)
        )

    def forward(self, x):
        x=self.model(x)
        return x


resnet50=torchvision.models.resnet50(weights=torchvision.models.ResNet50_Weights.DEFAULT)
vgg16=torchvision.models.vgg16(weights=torchvision.models.VGG16_Weights.DEFAULT)

in_features=resnet50.fc.in_features

# 将原resnet50网络中的最后一个全连接层改成10分类的输出
resnet50.fc=nn.Linear(in_features,10)
resnet50=resnet50.to(device)

# in_features=vgg16.classifier[6].in_features
# vgg16.classifier[6]=nn.Linear(in_features,10)
# vgg16=vgg16.to(device)
print(resnet50)


# print(in_features)

epochs=30

model=LeNet().to(device)

loss_fn=nn.CrossEntropyLoss().to(device)

# 固定卷积层的参数
optim=torch.optim.Adam(resnet50.fc.parameters(),lr=0.001)

for epoch in range(epochs):
    resnet50.train()
    for data in trainloader:
        images,labels=data
        images,labels=images.to(device),labels.to(device)

        output=resnet50(images)
        loss=loss_fn(output,labels)

        optim.zero_grad()
        loss.backward()
        optim.step()

    resnet50.eval()
    with torch.no_grad():
        accuracy=0
        for data in testlooader:
            images,labels=data
            images,labels=images.to(device),labels.to(device)

            output=resnet50(images)
            accuracy+=((output.argmax(1)==labels).sum())
    print("第{}轮中,测试集上的准确率为:{}".format(epoch+1,accuracy/test_size))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/73110
推荐阅读
  

闽ICP备14008679号