意见箱
恒创运营部门将仔细参阅您的意见和建议,必要时将通过预留邮箱与您保持联络。感谢您的支持!
意见/建议
提交建议

【图像检测】基于 AlexNet 和 SVM 实现异常螺母检测附matlab代码

来源:恒创科技 编辑:恒创科技编辑部
2024-01-01 13:58:59
1 内容介绍

考虑到异常检测问题中正负样本严重失衡,难以满足卷积神经网络训练对样本的要求,提出了基于AlexNet模型的异常检测模型.在数据预处理阶段,通过隔帧采样的方式生成3组训练数据,并利用预训练的AlexNet模型提取相应的3组图像特征,最后通过并联的形式训练3组一类支持向量机模型1SVM,在测试阶段对3个1SVM的结果进行投票,获得最终的检测结果.以UMN数据集作为实验数据进行实验,算法的等错误率为1.8,优于其他算法,充分说明了算法的有效性.

2 部分代码

%% Applying Deeplearning to Anomaly Detection for manufacturing product

% This is the way to detect feature outlier with AlexNet and 1-class SVM kernel method.


【图像检测】基于 AlexNet 和 SVM 实现异常螺母检测附matlab代码

clear; close all; imtool close all; clc;rng('default')

% unzip('data.zip')

% winopen('testimage')

%% Read Pre-trained Convolutional Neural Network (CNN) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

convnet = alexnet() %


%% show layers

convnet.Layers % show layer


%% open folder including training images

rootFolder = pwd;

categ = {fullfile('data','trainingimage')};

winopen(fullfile('data','trainingimage'))


%% use imageDatastore object for dealing with huge amount of image.

imds = imageDatastore(fullfile(rootFolder, categ), 'LabelSource', 'foldernames')

imds.ReadFcn = @(filename) readAndPreproc(filename); % set function to resize image to 227*227*3.

tbl = countEachLabel(imds) % Show the number of training image


%% Run AlexNet to get the feature data at the fc7 layer

fLayer = 'fc7';

trainingFeatures = activations(convnet, imds, fLayer, ...

'MiniBatchSize', 32, 'OutputAs', 'columns'); % run the network with images and get the feature data at the defined layer


%% train a 1-class SVM with the feature data

W = ones(size(trainingFeatures', 1), 1);

d = fitcsvm(trainingFeatures', W, 'KernelScale', 'auto', 'Standardize', false, 'OutlierFraction', 0.04,'KernelFunction','gaussian');


%% Detect 4 abnormal images from test image set

categ2 = {fullfile('data','testimage')};

% Read 100 images as a test set

imds2 = imageDatastore(fullfile(rootFolder, categ2), 'LabelSource', 'foldernames','IncludeSubfolders',true)

imds2.ReadFcn = @(filename) readAndPreproc(filename);

tic % start timer

testFeatures = activations(convnet, imds2, fLayer, ...

'MiniBatchSize', 32, 'OutputAs', 'columns'); % Execute Alexnet and get data at the fc7 layer

[~, score] = predict(d, testFeatures'); % predict score with trained SVM

[score_sorted, idx] = sort(score); % sort by score (is score is small (like negative), the image can be abnormal)

idx(1:25) % the indices of Top 25 abnormal images

toc % Stop time and show the calculation time

%% show the sorted images side-by-side

im = readall(imds2);

im = im(idx); % sort images by score in ascending order

sz = size(im{1});

% Insert rectangle on images people defined as anomaly

for i=1:numel(idx)

if idx(i) <5

im{i} = insertShape(uint8(im{i}),'rectangle',[1 1 sz(1) sz(2)],'LineWidth' ,10);

end

end

I = cat(4, im{1:100});

figure,montage(I, 'Size', [10 10]) % show 10*10 images in a figure

% The score of images in the first row are low. (anomalousness is high)

% the 1-4 lowest score images have rectangle yellow frame.

% This means that prediction by classifier is same as the correct answer people define.

score(idx) %


%% Use t-SNE for visualization

rng default %

testLabels = imds2.Labels; % Use label for visualization

% Use t-SNE to visualize 4096 dimension data bidimensionally

Y = tsne(testFeatures','Algorithm','exact','NumPCAComponents',50,'Perplexity',45);

figure

gscatter(Y(:,1),Y(:,2),testLabels)

title('Default Figure')

% feature plots of abnormal image are located far from center of whole distribution

% classifier detects these outliers

3 运行结果

【图像检测】基于 AlexNet 和 SVM 实现异常螺母检测附matlab代码_ide

【图像检测】基于 AlexNet 和 SVM 实现异常螺母检测附matlab代码_数据_02

4 参考文献

[1]付青、罗文浪、吕敬祥. 基于AlexNet和支持向量机相结合的卫星遥感影像土地利用变化检测[J]. 激光与光电子学进展, 2020, 57(17):9.

[2]雷丽莹, 陈华华. 基于AlexNet的视频异常检测技术[J]. 杭州电子科技大学学报(自然科学版), 2018.

部分理论引用网络文献,若有侵权联系博主删除。


上一篇: 智能优化算法Matlab源码大礼包领取(智能优化算法免疫算法的应用) 下一篇: nginx反向代理ftp服务器(nginx反向代理和正向代理的区别)