当前位置: 首页 > news >正文

【图像检测】基于 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代码问题可私信交流。

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

相关文章:

  • vue开发-从源码开始解读一个智慧园区项目
  • 接入Twitter和Facebook分享踩坑记录
  • 这次主要的配置
  • 工作5年,没接触过高并发编程,这正常吗?
  • 【微信小程序】带你进入小程序的世界
  • 机器学习-线性回归 二维问题
  • 分享从零开始学习网络设备配置--2.1 交换机基本配置
  • 大数据ClickHouse进阶(九):ClickHouse的From和Sample子句
  • vue3 | HighCharts实战自定义封装之径向条形图
  • Web前端系列技术之Web APIs基础(从基础开始)③
  • 线段树基本操作——建树+单点修改+区间查询
  • python/php/java/nodejs通讯录管理系统vue+elementui
  • 【老生谈算法】matlab实现蒙特卡罗定积分源码——蒙特卡罗定积分
  • 卷积神经网络 - 从全连接层到卷积
  • selenium爬虫如何绕过反爬,看这一篇文章就足够了
  • 【mysql】环境安装、服务启动、密码设置
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Angular 响应式表单之下拉框
  • C++类的相互关联
  • Docker入门(二) - Dockerfile
  • JS进阶 - JS 、JS-Web-API与DOM、BOM
  • js数组之filter
  • Linux中的硬链接与软链接
  • mongodb--安装和初步使用教程
  • Redash本地开发环境搭建
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • supervisor 永不挂掉的进程 安装以及使用
  • unity如何实现一个固定宽度的orthagraphic相机
  • 读懂package.json -- 依赖管理
  • 近期前端发展计划
  • 实习面试笔记
  • 实现菜单下拉伸展折叠效果demo
  • 详解NodeJs流之一
  • 译自由幺半群
  • 最近的计划
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • puppet连载22:define用法
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • #NOIP 2014# day.2 T2 寻找道路
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (3)(3.5) 遥测无线电区域条例
  • (BFS)hdoj2377-Bus Pass
  • (day6) 319. 灯泡开关
  • (DenseNet)Densely Connected Convolutional Networks--Gao Huang
  • (Pytorch框架)神经网络输出维度调试,做出我们自己的网络来!!(详细教程~)
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (超详细)语音信号处理之特征提取
  • (独孤九剑)--文件系统
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)