目录
一、项目界面
二、代码实现
1、数据集结构
每一个文件夹对应一个类别的数据
2、设置需要模型的训练参数和指定数据集路径
python
# 数据名字标签
label_names = {0:"daisy",
1:"dandelion",
2:"rose",
3:"sunflower",
4:"tulip",
}
# 类别数量,根据label_names标签名自动得出
num_classes = len(label_names)
# 重采样大小。如果无则填None
re_size = (28,28)
# 训练集地址,默认即可
train_path = r"./data/train"
# 验证集地址,默认即可
val_path = r"./data/val"
# 测试集地址,默认即可
test_path = r"./data/test"
# 图像后缀
img_ = "jpg"
# 批量大小
batch_size = 64
# 结果保存地址
save_results = r"./results"
# 学习率
lr = 0.001
# 迭代次数
epochs = 20
# ----------划分数据集参数-----------
# 确定将数据集划分为训练集,验证集,测试集的比例
train_pct = 0.5
valid_pct = 0.1
test_pct = 0.4
# 确定原图像数据集路径。默认即可
dataset_dir = r"./data/data" # 原始数据集路径
# 确定数据集划分后保存的路径
split_dir = r"./data" # 划分后保存路径
3、网络代码
该网络基于残差模型修改
python
import torch
import torch.nn as nn
import torchvision.models as models
class resnet18(nn.Module):
def __init__(self, num_classes=5, pretrained=False):
super(resnet18, self).__init__()
# 加载ResNet-18模型
self.model = models.resnet18(pretrained=pretrained)
# print(self.model)
# 更改全连接层以输出自定义类别数量
self.model.fc = nn.Linear(self.model.fc.in_features, num_classes)
def forward(self, x):
return self.model(x)
if __name__ == '__main__':
# 示例用法
num_classes = 10
model = resnet18(num_classes=num_classes)
# 打印模型以确认更改
print(model)
4、训练代码
python
import os
import torch
import torch.nn as nn
from models.resnet18 import resnet18
from utils.utils import train_and_val,plot_acc,plot_loss,plot_lr,MyDataset
import numpy as np
from torch.utils.data import DataLoader
import glob
import pandas as pd
import config
def main(epochs,model):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if not os.path.exists(config.save_results):
os.makedirs(config.save_results)
# ----------------------------模型加载-------------------------
model = model.to(device)
loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=config.lr)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
step_size=5,
gamma=0.9) # 每经过5个epoch,学习率乘以0.9
# ------------------------------------------------------------
# ---------------------------加载数据--------------------------
im_train_list = glob.glob(config.train_path + "/*/*." + config.img_)
im_val_list = glob.glob(config.val_path + "/*/*." + config.img_)
train_dataset = MyDataset(im_train_list, config.label_names)
val_dataset = MyDataset(im_val_list, config.label_names)
train_loader = DataLoader(train_dataset,
batch_size=config.batch_size,
shuffle=True)
val_loader = DataLoader(val_dataset,
batch_size=config.batch_size,
shuffle=False)
print("num of train", len(train_dataset))
print("num of val", len(val_loader))
# ------------------------------------------------------------
# ---------------------------网络训练--------------------------
history = train_and_val(epochs, model, train_loader,val_loader,loss_function, optimizer,scheduler,config.save_results,device)
df = pd.DataFrame(history) # 转换为DataFrame
df.to_excel(os.path.join(config.save_results,'history.xlsx'), index=False) # 保存为 Excel 文件
plot_loss(np.arange(0,epochs),config.save_results, history)
plot_acc(np.arange(0,epochs),config.save_results, history)
plot_lr(np.arange(0,epochs),config.save_results, history)
if __name__ == '__main__':
model = resnet18(num_classes=config.num_classes)
main(config.epochs,model)
5、评估代码
python
from sklearn.metrics import classification_report
import torch
import os
import torch.nn as nn
from tqdm import tqdm
import pandas as pd
from models.resnet18 import resnet18
import matplotlib.pyplot as plt
from utils.utils import MyDataset,reports
from torch.utils.data import DataLoader
import seaborn as sns
import glob
import config
def main(model):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# ----------------------------模型加载-------------------------
model = model.to(device)
checkpoint = torch.load(os.path.join(config.save_results,"best.pth"))
model.load_state_dict(checkpoint, strict=True)
model.eval()
# ------------------------------------------------------------
# ---------------------------加载数据--------------------------
im_test_list = glob.glob(config.test_path + "/*/*." + config.img_)
test_dataset = MyDataset(im_test_list, config.label_names)
test_loader = DataLoader(test_dataset,
batch_size=config.batch_size,
shuffle=False)
print("num of test", len(test_loader))
# ------------------------------------------------------------
act = nn.Softmax(dim=-1)
y_true, y_pred = [], []
with torch.no_grad():
with tqdm(total=len(test_loader)) as pbar:
for images, labels in test_loader:
outputs = act(model(images.to(device)))
_, predicted = torch.max(outputs, 1)
predicted = predicted.cpu()
y_pred.extend(predicted.numpy())
y_true.extend(labels.cpu().numpy())
pbar.update(1)
oa,aa,kappa,cls,cm = reports(y_true, y_pred)
cr = classification_report(y_true, y_pred, target_names=config.label_names.values(), output_dict=True)
df = pd.DataFrame(cr).transpose()
df.to_csv(os.path.join(config.save_results,"classification_report.csv"), index=True)
print("Accuracy is :", oa)
with open(os.path.join(config.save_results,"results.txt"), "a") as file:
file.write('OA:{:.4f} AA:{:.4f} kappa:{:.4f}\ncls:{}\n混淆矩阵:\n{}\n'.format(oa, aa, kappa,cls,cm))
plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, xticklabels=config.label_names.values(), yticklabels=config.label_names.values(), cmap='Blues', fmt="d")
plt.xlabel('Predicted')
plt.ylabel('True')
plt.savefig(os.path.join(config.save_results,'test_confusion_matrix.png'))
plt.clf()
if __name__ == '__main__':
model = resnet18()
main(model)
6、结果显示
上述仅仅是简单演示,结果没有参考意义。
三、项目代码
本项目的代码通过以下链接下载:基于深度学习的图像分类或识别系统(含全套项目+PyQt5界面)