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

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. 

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 运行结果

4 参考文献

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

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

博主简介:擅长智能优化算法神经网络预测信号处理元胞自动机图像处理路径规划无人机雷达通信无线传感器等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值