• 百度飞浆ResNet50大模型微调实现十二种猫图像分类


    12种猫分类比赛传送门

    要求很简单,给train和test集,训练模型实现图像分类

    这里使用的是残差连接模型,这个平台有预训练好的模型,可以直接拿来主义。

    训练十几个迭代,每个批次60左右,准确率达到90%以上

    一、导入库,解压文件

    1. import os
    2. import zipfile
    3. import random
    4. import json
    5. import cv2
    6. import numpy as np
    7. from PIL import Image
    8. import matplotlib.pyplot as plt
    9. from sklearn.model_selection import train_test_split
    10. import paddle
    11. import paddle.nn as nn
    12. from paddle.io import Dataset,DataLoader
    13. from paddle.nn import \
    14. Layer, \
    15. Conv2D, Linear, \
    16. Embedding, MaxPool2D, \
    17. BatchNorm2D, ReLU
    18. import paddle.vision.transforms as transforms
    19. from paddle.vision.models import resnet50
    20. from paddle.metric import Accuracy
    21. train_parameters = {
    22. "input_size": [3, 224, 224], # 输入图片的shape
    23. "class_dim": 12, # 分类数
    24. "src_path":"data/data10954/cat_12_train.zip", # 原始数据集路径
    25. "src_test_path":"data/data10954/cat_12_test.zip", # 原始数据集路径
    26. "target_path":"/home/aistudio/data/dataset", # 要解压的路径
    27. "train_list_path": "./train.txt", # train_data.txt路径
    28. "eval_list_path": "./eval.txt", # eval_data.txt路径
    29. "label_dict":{}, # 标签字典
    30. "readme_path": "/home/aistudio/data/readme.json",# readme.json路径
    31. "num_epochs":6, # 训练轮数
    32. "train_batch_size": 16, # 批次的大小
    33. "learning_strategy": { # 优化函数相关的配置
    34. "lr": 0.0005 # 超参数学习率
    35. }
    36. }
    37. scr_path=train_parameters['src_path']
    38. target_path=train_parameters['target_path']
    39. src_test_path=train_parameters["src_test_path"]
    40. z = zipfile.ZipFile(scr_path, 'r')
    41. z.extractall(path=target_path)
    42. z = zipfile.ZipFile(src_test_path, 'r')
    43. z.extractall(path=target_path)
    44. z.close()
    45. for imgpath in os.listdir(target_path + '/cat_12_train'):
    46. src = os.path.join(target_path + '/cat_12_train/', imgpath)
    47. img = Image.open(src)
    48. if img.mode != 'RGB':
    49. img = img.convert('RGB')
    50. img.save(src)
    51. for imgpath in os.listdir(target_path + '/cat_12_test'):
    52. src = os.path.join(target_path + '/cat_12_test/', imgpath)
    53. img = Image.open(src)
    54. if img.mode != 'RGB':
    55. img = img.convert('RGB')
    56. img.save(src)

     解压后将所有图像变为RGB图像

    二、加载训练集,进行预处理、数据增强、格式变换

    1. transform = transforms.Compose([
    2. transforms.Resize(size=224),
    3. transforms.ColorJitter(0.2, 0.2, 0.2, 0.2),
    4. transforms.RandomHorizontalFlip(),
    5. transforms.RandomRotation(15),
    6. transforms.RandomResizedCrop(size=224, scale=(0.8, 1.0)),
    7. transforms.ToTensor(),
    8. transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    9. ])
    10. x_train,x_eval,y_train=[],[],[]#获取训练图像和标签、测试图像和标签
    11. contents=[]
    12. with open('data/data10954/train_list.txt')as f:
    13. contents=f.read().split('\n')
    14. for item in contents:
    15. if item=='':
    16. continue
    17. path='data/dataset/'+item.split('\t')[0]
    18. data=np.array(Image.open(path).convert('RGB'))
    19. data=np.array(transform(data))
    20. x_train.append(data)
    21. y_train.append(int(item.split('\t')[-1]))
    22. contetns=os.listdir('data/dataset/cat_12_test')
    23. for item in contetns:
    24. path='data/dataset/cat_12_test/'+item
    25. data=np.array(Image.open(path).convert('RGB'))
    26. data=np.array(transform(data))
    27. x_eval.append(data)

    重点是transforms变换的预处理

    三、划分训练集和测试集

    1. x_train=np.array(x_train)
    2. y_train=np.array(y_train)
    3. x_eval=np.array(x_eval)
    4. x_train,x_test,y_train,y_test=train_test_split(x_train,y_train,test_size=0.2,random_state=42,stratify=y_train)
    5. x_train=paddle.to_tensor(x_train,dtype='float32')
    6. y_train=paddle.to_tensor(y_train,dtype='int64')
    7. x_test=paddle.to_tensor(x_test,dtype='float32')
    8. y_test=paddle.to_tensor(y_test,dtype='int64')
    9. x_eval=paddle.to_tensor(x_eval,dtype='float32')

     这是必要的,可以随时利用测试集查看准确率

    四、加载预训练模型,选择损失函数和优化器

    1. learning_rate=0.001
    2. epochs =5 # 迭代轮数
    3. batch_size = 50 # 批次大小
    4. weight_decay=1e-5
    5. num_class=12
    6. cnn=resnet50(pretrained=True)
    7. checkpoint=paddle.load('checkpoint.pdparams')
    8. for param in cnn.parameters():
    9. param.requires_grad=False
    10. cnn.fc = nn.Linear(2048, num_class)
    11. cnn.set_dict(checkpoint['cnn_state_dict'])
    12. criterion=nn.CrossEntropyLoss()
    13. optimizer = paddle.optimizer.Adam(learning_rate=learning_rate, parameters=cnn.fc.parameters(),weight_decay=weight_decay)

    第一次训练把加载模型注释掉即可,优化器包含最后一层全连接的参数

    五、模型训练 

    1. if x_train.shape[3]==3:
    2. x_train=paddle.transpose(x_train,perm=(0,3,1,2))
    3. dataset = paddle.io.TensorDataset([x_train, y_train])
    4. data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
    5. for epoch in range(epochs):
    6. for batch_data, batch_labels in data_loader:
    7. outputs = cnn(batch_data)
    8. loss = criterion(outputs, batch_labels)
    9. print(epoch)
    10. loss.backward()
    11. optimizer.step()
    12. optimizer.clear_grad()
    13. print(f"Epoch [{epoch+1}/{epochs}], Loss: {loss.numpy()[0]}")#保存参数
    14. paddle.save({
    15. 'cnn_state_dict': cnn.state_dict(),
    16. }, 'checkpoint.pdparams')

     使用批处理,这个很重要,不然平台分分钟炸了

    六、测试集准确率

    1. num_class=12
    2. batch_size=64
    3. cnn=resnet50(pretrained=True)
    4. checkpoint=paddle.load('checkpoint.pdparams')
    5. for param in cnn.parameters():
    6. param.requires_grad=False
    7. cnn.fc = nn.Linear(2048, num_class)
    8. cnn.set_dict(checkpoint['cnn_state_dict'])
    9. cnn.eval()
    10. if x_test.shape[3]==3:
    11. x_test=paddle.transpose(x_test,perm=(0,3,1,2))
    12. dataset = paddle.io.TensorDataset([x_test, y_test])
    13. data_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
    14. with paddle.no_grad():
    15. score=0
    16. for batch_data, batch_labels in data_loader:
    17. predictions = cnn(batch_data)
    18. predicted_probabilities = paddle.nn.functional.softmax(predictions, axis=1)
    19. predicted_labels = paddle.argmax(predicted_probabilities, axis=1)
    20. print(predicted_labels)
    21. for i in range(len(predicted_labels)):
    22. if predicted_labels[i].numpy()==batch_labels[i]:
    23. score+=1
    24. print(score/len(y_test))

    设置eval模式,使用批处理测试准确率 

  • 相关阅读:
    Dapr在Java中的实践 之 状态管理
    VMware使用vim遇到的一系列问题
    自己动手写数据库:并发管理器的实现,以及并发交易流程的分析
    内网渗透之Linux反弹shell(一)
    (WebFlux)002、如何打印日志与链路ID
    Ansible-palybook学习
    什么是Ipython
    AI在商业逻辑应用中的革命性作用
    独家 | 放弃Jupyter Notebooks吧,教你如何用仪表板展示研究成果
    Laravel笔记-自定义登录中新增登录5次失败锁账户功能(提高系统安全性)
  • 原文地址:https://blog.csdn.net/weixin_61067952/article/details/133715047