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

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

  • 相关阅读:
    CSDN 云IDE产品初步体验优劣势(JAVA语言方向)
    LocalDateTime、LocalDate、LocalTime相关使用记录(未完)
    了解4-20mA信号
    堆排序详解
    Java泛型
    Java学习 --- 设计模式中的UML类图
    MySQL—MySQL架构
    Jmeter 使用正则表达式提取器将返回值全部保存到一个文件中
    工具篇之Axure RP 10的使用
    .NET 缓存类
  • 原文地址:https://blog.csdn.net/matlab_dingdang/article/details/126910033