目录
一、项目界面
二、代码实现
1、网络代码
2、训练代码
3、评估代码
4、结果显示
三、项目代码
一、项目界面
二、代码实现
1、网络代码
该网络基于残差模型修改
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)
2、训练代码
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)
3、评估代码
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)
4、结果显示
上述仅仅是简单演示,结果没有参考意义。
三、项目代码
本项目的代码通过以下链接下载:基于深度学习的图像分类或识别系统(含全套项目+PyQt5界面)