这次的代码是我给人工智能发送以下内容后,人工智能所出的。现在请结合我给你的这些信息,对代码进行调整,解决准确率低和代码运行错误的问题。
我法的内容如下:
我在做计算智能课的结课大论文,请你结合深度学习、机器学习和计算智能的知识,及其相关知识,帮助我完成本次结课大论文。另,本次实验采用MATLAB R2024a的实验环境。
任务一:利用BP神经网络做车牌数字识别
[简述BP算法的原理]
[说明BP神经网络结构及重要参数设置]
[实验结果展示]
[实验结果分析及可改进方向]
[代码展示]
请先给我[代码展示]部分的内容,其他部分内容我之后再问你要。
我这有些关于任务一的相关数据如下,这些文件夹里装了各色车牌的照片。
black文件夹的地址为C:\Users\蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train
blue文件夹的地址为C:\Users\蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train
green文件夹的地址为C:\Users\蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train\green
lv_double文件夹的地址为C:\Users\蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train\green
white文件夹的地址为C:\Users\蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train
yellow_double文件夹的地址为C:\Users\蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train\yellow
yellow_single文件夹的地址为C:\Users\v蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train\yellow
你给的代码如下:
%% 最终优化版 - 车牌数字识别
clear; clc; close all;
rng(42);
%% 参数设置
targetSize = [32, 32];
numClasses = 10; % 固定为0-9十个类别
%% 数据加载与预处理
[images, labels] = robustDataLoader(targetSize, numClasses);
% 数据集划分 (60%训练, 20%验证, 20%测试)
numSamples = size(images,4);
[trainInd, valInd, testInd] = dividerand(numSamples, 0.6, 0.2, 0.2);
trainImages = images(:,:,:,trainInd);
trainLabels = labels(trainInd);
valImages = images(:,:,:,valInd);
valLabels = labels(valInd);
testImages = images(:,:,:,testInd);
testLabels = labels(testInd);
%% 增强的CNN结构
layers = [
imageInputLayer([targetSize 1])
convolution2dLayer(3, 32, 'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 64, 'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 128, 'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(256)
reluLayer
dropoutLayer(0.6) % 增加丢弃率
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer
];
%% 优化训练配置
options = trainingOptions('adam', ...
'MaxEpochs', 40, ... % 增加训练轮次
'MiniBatchSize', 128, ...
'InitialLearnRate', 0.001, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.5, ...
'LearnRateDropPeriod', 15, ... % 调整学习率衰减周期
'Shuffle', 'every-epoch', ...
'ValidationData', {valImages, categorical(valLabels)}, ...
'ValidationFrequency', 50, ... % 减少验证频率以加速
'Plots', 'training-progress', ...
'Verbose', true);
%% 训练模型
net = trainNetwork(trainImages, categorical(trainLabels), layers, options);
%% 评估与可视化
predicted = classify(net, testImages);
accuracy = mean(predicted == categorical(testLabels));
fprintf('测试准确率: %.2f%%\n', accuracy*100);
% 修复混淆矩阵错误
testLabelsCategorical = categorical(testLabels);
predictedCategorical = categorical(double(predicted)); % 转换为分类类型
figure;
cm = confusionmat(testLabelsCategorical, predictedCategorical);
confusionchart(cm, categories(testLabelsCategorical));
title(sprintf('混淆矩阵 (准确率: %.2f%%)', accuracy*100));
% 显示样本预测
figure;
numSamples = 25;
randIndices = randperm(numel(testLabels), numSamples);
for i = 1:numSamples
subplot(5, 5, i);
idx = randIndices(i);
imshow(testImages(:,:,:,idx));
predLabel = double(predicted(idx));
trueLabel = testLabels(idx);
if predLabel == trueLabel
color = 'g';
else
color = 'r';
end
title(sprintf('预测:%d\n真实:%d', predLabel, trueLabel), 'Color', color);
end
% 错误分析:显示错误预测样本
figure;
wrongIdx = find(predictedCategorical ~= testLabelsCategorical);
numSamples = min(25, numel(wrongIdx));
randWrong = randperm(numel(wrongIdx), numSamples);
for i = 1:numSamples
subplot(5, 5, i);
idx = wrongIdx(randWrong(i));
imshow(testImages(:,:,:,idx));
predLabel = double(predicted(idx));
trueLabel = testLabels(idx);
title(sprintf('预测:%d\n真实:%d', predLabel, trueLabel), 'Color', 'r');
end
%% 鲁棒的数据加载函数
function [images, labels] = robustDataLoader(targetSize, numClasses)
dataPath = 'C:\Users\蓝喲\Desktop\课\计算智能\结课大作业\CCPDCRPD\train';
colors = {'black', 'blue', 'green', 'white', 'yellow'};
% 初始化
images = single([]);
labels = [];
% 遍历颜色文件夹
for c = 1:length(colors)
colorPath = fullfile(dataPath, colors{c});
% 获取所有子文件夹
subDirs = getSubdirectories(colorPath);
% 遍历子文件夹
for s = 1:length(subDirs)
currentPath = fullfile(colorPath, subDirs{s});
jpgFiles = dir(fullfile(currentPath, '*.jpg'));
if isempty(jpgFiles)
fprintf('跳过空文件夹: %s\n', currentPath);
continue;
end
% 加载当前文件夹的图像
for i = 1:length(jpgFiles)
try
imgPath = fullfile(currentPath, jpgFiles(i).name);
img = imread(imgPath);
% 转换为灰度图
if size(img, 3) == 3
img = rgb2gray(img);
end
% 预处理 - 增强对比度
img = imadjust(img);
% 调整尺寸
img = imresize(img, targetSize);
img = im2single(img);
% 从文件名提取标签
label = extractLabelFromName(jpgFiles(i).name);
% 只接受0-9的有效标签
if label < 0 || label > 9
fprintf('跳过无效标签图像: %s (标签:%d)\n', jpgFiles(i).name, label);
continue;
end
% 添加到数据集
if isempty(images)
images = img;
else
images = cat(4, images, img);
end
labels = [labels; label];
catch ME
fprintf('加载图像错误: %s (%s)\n', imgPath, ME.message);
end
end
end
end
% 确保至少有一些数据
if isempty(images)
error('未找到有效图像数据,请检查路径: %s', dataPath);
end
fprintf('成功加载 %d 张图像,共 %d 个类别\n', numel(labels), numClasses);
end
%% 获取子目录列表
function subDirs = getSubdirectories(mainDir)
dirList = dir(mainDir);
isSubDir = [dirList.isdir] & ~ismember({dirList.name}, {'.', '..'});
subDirs = {dirList(isSubDir).name};
% 如果主文件夹下有图像文件,添加主文件夹
if ~isempty(dir(fullfile(mainDir, '*.jpg')))
subDirs = [subDirs, {''}];
end
end
%% 改进的标签提取函数
function label = extractLabelFromName(filename)
% 尝试匹配括号内的数字 (1)
[match] = regexp(filename, '\((\d+)\)', 'tokens');
if ~isempty(match)
num = str2double(match{1}{1});
if num >= 0 && num <= 9
label = num;
return;
end
end
% 尝试匹配文件名末尾的数字
[match] = regexp(filename, '(\d+)\.jpg$', 'tokens');
if ~isempty(match)
num = str2double(match{1}{1});
label = mod(num, 10); % 取个位数
return;
end
% 尝试匹配文件名中的数字字符
[match] = regexp(filename, '\d', 'match');
if ~isempty(match)
label = str2double(match{1});
return;
end
% 默认返回无效标签
label = -1;
end
运行结果如下:
成功加载 15483 张图像,共 10 个类别
在单 CPU 上训练。
正在初始化输入数据归一化。
|=============================================================================|
| 轮 | 迭代 | 经过的时间 | 小批量准确度 | 验证准确度 | 小批量损失 | 验证损失 | 基础学习率 |
| | | (hh:mm:ss) | | | | | |
|=============================================================================|
| 1 | 1 | 00:00:12 | 4.69% | 33.84% | 3.1265 | 6.1090 | 0.0010 |
| 1 | 50 | 00:01:00 | 48.44% | 50.66% | 1.4774 | 1.4475 | 0.0010 |
| 2 | 100 | 00:01:45 | 55.47% | 54.05% | 1.2905 | 1.3421 | 0.0010 |
| 3 | 150 | 00:02:30 | 51.56% | 55.67% | 1.3363 | 1.2663 | 0.0010 |
| 3 | 200 | 00:03:15 | 50.00% | 57.86% | 1.3424 | 1.2264 | 0.0010 |
| 4 | 250 | 00:03:59 | 55.47% | 57.80% | 1.1695 | 1.1962 | 0.0010 |
| 5 | 300 | 00:04:51 | 51.56% | 58.80% | 1.2479 | 1.1856 | 0.0010 |
| 5 | 350 | 00:05:31 | 59.38% | 60.57% | 1.2770 | 1.1191 | 0.0010 |
| 6 | 400 | 00:05:51 | 60.16% | 62.87% | 1.1318 | 1.0742 | 0.0010 |
| 7 | 450 | 00:06:33 | 60.16% | 62.16% | 1.1437 | 1.0831 | 0.0010 |
| 7 | 500 | 00:07:41 | 64.84% | 64.48% | 0.9498 | 1.0336 | 0.0010 |
| 8 | 550 | 00:08:37 | 68.75% | 65.55% | 1.0622 | 1.0383 | 0.0010 |
| 9 | 600 | 00:09:13 | 52.34% | 66.29% | 1.1652 | 1.0414 | 0.0010 |
| 10 | 650 | 00:09:38 | 70.31% | 66.52% | 0.8450 | 1.0268 | 0.0010 |
| 10 | 700 | 00:09:56 | 66.41% | 68.10% | 0.8517 | 0.9665 | 0.0010 |
| 11 | 750 | 00:10:15 | 57.03% | 67.68% | 1.1278 | 0.9506 | 0.0010 |
| 12 | 800 | 00:10:33 | 73.44% | 68.26% | 0.6809 | 0.9920 | 0.0010 |
| 12 | 850 | 00:10:54 | 66.41% | 69.42% | 0.8815 | 0.9742 | 0.0010 |
| 13 | 900 | 00:11:13 | 68.75% | 70.55% | 0.7691 | 0.9185 | 0.0010 |
| 14 | 950 | 00:12:03 | 81.25% | 70.58% | 0.6132 | 0.9381 | 0.0010 |
| 14 | 1000 | 00:12:28 | 64.84% | 70.91% | 0.9334 | 0.8932 | 0.0010 |
| 15 | 1050 | 00:12:47 | 81.25% | 71.13% | 0.5735 | 0.9524 | 0.0010 |
| 16 | 1100 | 00:13:07 | 78.91% | 71.71% | 0.6210 | 0.9344 | 0.0005 |
| 16 | 1150 | 00:13:31 | 75.78% | 71.81% | 0.6192 | 0.9230 | 0.0005 |
| 17 | 1200 | 00:14:33 | 77.34% | 72.33% | 0.5982 | 0.9175 | 0.0005 |
| 18 | 1250 | 00:15:45 | 74.22% | 72.01% | 0.6713 | 0.9694 | 0.0005 |
| 19 | 1300 | 00:16:55 | 82.03% | 72.68% | 0.4780 | 0.9674 | 0.0005 |
| 19 | 1350 | 00:18:02 | 86.72% | 71.91% | 0.3710 | 0.9776 | 0.0005 |
| 20 | 1400 | 00:19:12 | 77.34% | 73.36% | 0.5328 | 0.9105 | 0.0005 |
| 21 | 1450 | 00:20:17 | 78.91% | 72.33% | 0.5978 | 0.9863 | 0.0005 |
| 21 | 1500 | 00:21:23 | 73.44% | 72.97% | 0.5857 | 0.9991 | 0.0005 |
| 22 | 1550 | 00:22:33 | 89.84% | 73.46% | 0.3211 | 0.9644 | 0.0005 |
| 23 | 1600 | 00:23:57 | 82.81% | 72.78% | 0.4824 | 0.9861 | 0.0005 |
| 23 | 1650 | 00:25:28 | 82.03% | 73.78% | 0.4521 | 0.9510 | 0.0005 |
| 24 | 1700 | 00:27:28 | 86.72% | 72.33% | 0.3082 | 1.0560 | 0.0005 |
| 25 | 1750 | 00:28:53 | 80.47% | 72.72% | 0.4768 | 1.0563 | 0.0005 |
| 25 | 1800 | 00:30:09 | 85.94% | 72.39% | 0.3754 | 0.9933 | 0.0005 |
| 26 | 1850 | 00:31:34 | 84.38% | 72.42% | 0.3483 | 1.0455 | 0.0005 |
| 27 | 1900 | 00:32:56 | 84.38% | 72.62% | 0.4467 | 1.0685 | 0.0005 |
| 28 | 1950 | 00:34:34 | 85.16% | 72.81% | 0.3287 | 1.1146 | 0.0005 |
| 28 | 2000 | 00:36:04 | 83.59% | 73.01% | 0.4610 | 1.1104 | 0.0005 |
| 29 | 2050 | 00:37:22 | 84.38% | 73.10% | 0.4101 | 1.0793 | 0.0005 |
| 30 | 2100 | 00:38:24 | 87.50% | 73.26% | 0.3039 | 1.0915 | 0.0005 |
| 30 | 2150 | 00:39:33 | 89.06% | 73.39% | 0.3864 | 1.1083 | 0.0005 |
| 31 | 2200 | 00:40:52 | 90.62% | 73.97% | 0.2977 | 1.1225 | 0.0003 |
| 32 | 2250 | 00:41:35 | 89.06% | 73.52% | 0.2699 | 1.1896 | 0.0003 |
| 32 | 2300 | 00:42:37 | 92.19% | 74.27% | 0.1868 | 1.1225 | 0.0003 |
| 33 | 2350 | 00:43:58 | 89.06% | 74.20% | 0.3109 | 1.1430 | 0.0003 |
| 34 | 2400 | 00:45:19 | 89.84% | 72.84% | 0.3195 | 1.2183 | 0.0003 |
| 35 | 2450 | 00:46:47 | 90.62% | 73.52% | 0.2949 | 1.2095 | 0.0003 |
| 35 | 2500 | 00:48:17 | 90.62% | 73.85% | 0.2368 | 1.2068 | 0.0003 |
| 36 | 2550 | 00:49:47 | 90.62% | 73.68% | 0.2423 | 1.2037 | 0.0003 |
| 37 | 2600 | 00:50:31 | 92.19% | 73.75% | 0.2511 | 1.2154 | 0.0003 |
| 37 | 2650 | 00:51:37 | 96.88% | 73.49% | 0.1041 | 1.2017 | 0.0003 |
| 38 | 2700 | 00:53:10 | 86.72% | 73.88% | 0.3460 | 1.2474 | 0.0003 |
| 39 | 2750 | 00:54:31 | 91.41% | 73.78% | 0.2129 | 1.2496 | 0.0003 |
| 39 | 2800 | 00:55:55 | 92.19% | 73.78% | 0.2755 | 1.2247 | 0.0003 |
| 40 | 2850 | 00:56:50 | 91.41% | 73.52% | 0.1725 | 1.2715 | 0.0003 |
| 40 | 2880 | 00:57:33 | 91.41% | 74.17% | 0.2438 | 1.2292 | 0.0003 |
|=============================================================================|
训练结束: 已完成最大轮数。
测试准确率: 74.39%
不支持将脚本 confusionchart 作为函数执行:
C:\Program
Files\MATLAB\R2024a\toolbox\shared\mlearnlib\confusionchart.m
出错 untitled (第 78 行)
confusionchart(cm, categories(testLabelsCategorical));