【缺陷检测】AlexNet和SVM异常螺母检测【含Matlab源码 2147期】

⛄一、获取代码方式

获取代码方式1:
完整代码已上传我的资源: 【缺陷检测】基于matlab形态学水果蔬菜缺陷检测【含Matlab源码 820期】
点击上面蓝色字体,直接付费下载,即可。

获取代码方式2:
付费专栏Matlab图像处理(初级版)

备注:
点击上面蓝色字体付费专栏Matlab图像处理(初级版),扫描上面二维码,付费29.9元订阅海神之光博客付费专栏Matlab图像处理(初级版),凭支付凭证,私信博主,可免费获得1份本博客上传CSDN资源代码(有效期为订阅日起,三天内有效);
点击CSDN资源下载链接:1份本博客上传CSDN资源代码

⛄二、AlexNet网络简介

AlexNet网络模型由Hinton以及他的学生Alex Krizhevsky所设计,并在2012年的ImageNet竞赛中获得冠军,其物体分类错误率仅有16.4%,相比于传统的机器学习分类算法而言极其出色。该模型由11层组成,分别为5个卷积层、3个池化层以及3个全连接层,其中图像特征信息的提取工作主要由卷积层和池化层完成,而全连接层的作用则是整合局部特征信息,将特征信息扁平化处理,传递给Softmax层继续完成分类任务。

相比于传统的CNN,AlexNet网络采用了许多有效的改动,例如采用了ReLU函数进行激活,起到稀疏网络减少参数的作用。ReLU函数的表达式如下:
ReLU(x)=max(0,x)。 (1)
其次,重叠池化(Overlapping Pooling)、局部归一化处理(Local Response Normalization)、在全连接层采用Dropout处理等等,这些操作大大减少了网络复杂度以及参数数量,提高了网络的训练速度,减少了过拟合。这也是本文选用该网络的主要原因之一。

Alex Net模型如图2所示,该网络由5个卷积层和3个全连接层组成,深度总共8层。最后一个全连接层的输出被送到Softmax层,会产生一个覆盖多类标签的分布。与传统神经网络相比,Alex Net模型具有以下优势:(1)采用Re Lu激活函数;(2)增强数据集来抑制过拟合;(3)采用Drop Out方法抑制过拟合;(4)采用局部响应归一化(LRN)层增强泛化。
在这里插入图片描述
图2 Alex Net模型

⛄三、部分源代码

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 2272273.
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’)

⛄四、运行结果

在这里插入图片描述
在这里插入图片描述

⛄五、matlab版本及参考文献

1 matlab版本
2019版

2 参考文献
[1]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.

3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Matlab领域

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

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

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

打赏作者

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

抵扣说明:

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

余额充值