主要内容

使用U-NET的Intel CPU上的语义分割应用程序代码生成

This example demonstrates code generation for an image segmentation application that uses deep learning. It uses the代码根命令生成一个MEX函数,该功能通过使用深度学习网络U-NET进行图像分割来执行预测。

对于类似的示例,该示例通过使用u-net来证明图像的分割但不使用代码根命令,请参阅使用深度学习对多光谱图像的语义分割(图像处理工具箱)

第三方先决条件

  • Xeon处理器支持Intel Advan金宝appced Vector扩展2(Intel AVX2)指令

该示例在Linux®,Windo金宝appws®和MacOS平台上支持。

此示例使用Intel MKL-DNN库,该库带有MATLAB并生成用于语义分割的MEX函数。

MATLAB在线不支持此示例。金宝app

Overview of U-Net

U-Net[1]是一种卷积神经network (CNN)that is designed for semantic image segmentation. In U-Net, the initial series of convolutional layers are interspersed with max pooling layers, successively decreasing the resolution of the input image. These layers are followed by a series of convolutional layers interspersed with upsampling operators, successively increasing the resolution of the input image. The combination of these two series paths forms a U-shaped graph. The network was originally trained to perform prediction for biomedical image segmentation applications. This example demonstrates the ability of the network to track changes in forest cover over time. Environmental agencies track deforestation to assess and qualify the environmental and ecological health of a region.

Deep-learning-based semantic segmentation can yield a precise measurement of vegetation cover from high-resolution aerial photographs. One of the challenges is differentiating classes that have similar visual characteristics, such as trying to classify a green pixel as grass, shrubbery, or tree. To increase classification accuracy, some data sets contain multispectral images that provide additional information about each pixel. For example, the Hamlin Beach State Park data set supplements the color images with near-infrared channels that provide a clearer separation of the classes.

该示例使用Hamlin Beach State Park数据[2]以及预验证的U-NET网络,以正确地对每个像素进行分类。

u-net此示例的训练可以训练以分段属于18个类的像素,其中包括:

0.其他类/图像边框7.野餐表14.草1.道路标记8.黑色木板15.沙子2.树9.白色木板16.水(湖)3。建筑物10.建筑物10.橙色着陆垫17。水(池塘)4。车辆(汽车,卡车或巴士)11。水浮标18.沥青(停车场/人行道)5。人12.岩石6.救生员椅子13.其他植被

获得验证的U-NET DAG网络对象

Trainedunet_url ='//www.tatmou.com/金宝appsupportfiles/vision/data/multispectralunet.mat';Download Trainedunet(Trainedunet_url,PWD);
为Hamlin Beach数据集下载预培训的U-NET ...这将需要几分钟才能下载。
ld =负载(“ Trainedunet/Multispectralunet.mat”);net = ld.net;

DAG网络包含58层,包括卷积,最大池,深度串联和像素分类输出层。要显示深度学习网络体系结构的互动可视化,请使用分析功能。

%分析(NET);

段落入口点功能

Sementimageunet.m入口点函数通过使用包含在multispectralUnet.mat文件。此功能从multispectralUnet.mat归档到持久变量mynet。这function reuses this persistent variable in subsequent prediction calls.

类型('segmentImageUnet.m'
% OUT = segmentImageUnet(IM, PATCHSIZE) returns a semantically segmented % image, segmented using the network multispectralUnet. The segmentation % is performed over each patch of size PATCHSIZE. % % Copyright 2019-2020 The MathWorks, Inc. function out = segmentImageUnet(im, patchSize) %#codegen persistent mynet; if isempty(mynet) mynet = coder.loadDeepLearningNetwork('trainedUnet/multispectralUnet.mat'); end [height, width, nChannel] = size(im); patch = coder.nullcopy(zeros([patchSize, nChannel-1])); % pad image to have dimensions as multiples of patchSize padSize = zeros(1,2); padSize(1) = patchSize(1) - mod(height, patchSize(1)); padSize(2) = patchSize(2) - mod(width, patchSize(2)); im_pad = padarray (im, padSize, 0, 'post'); [height_pad, width_pad, ~] = size(im_pad); out = zeros([size(im_pad,1), size(im_pad,2)], 'uint8'); for i = 1:patchSize(1):height_pad for j =1:patchSize(2):width_pad for p = 1:nChannel-1 patch(:,:,p) = squeeze( im_pad( i:i+patchSize(1)-1,... j:j+patchSize(2)-1,... p)); end % pass in input segmentedLabels = activations(mynet, patch, 'Segmentation-Layer'); % Takes the max of each channel (6 total at this point) [~,L] = max(segmentedLabels,[],3); patch_seg = uint8(L); % populate section of output out(i:i+patchSize(1)-1, j:j+patchSize(2)-1) = patch_seg; end end % Remove the padding out = out(1:height, 1:width);

准备数据

下载Hamlin海滩州立公园数据。

如果~exist(fullfile(pwd,'data'),'dir')url ='http://www.cis.rit.edu/~rmk6217/rit18_data.mat';downloadHamlinBeachMSIData(url,pwd+"/data/");结尾
下载Hamlin Beach数据集...这将需要几分钟才能下载...完成。

加载并检查MATLAB中的数据。

负载(fullfile(PWD,'data',,,,'rit18_data',,,,'rit18_data.mat');%检查数据谁是测试数据
名称大小字节类属性test_data 7x12446x7654 13333663576 uint16

该图像有七个通道。RGB颜色通道是第四,第五和第六图像通道。前三个通道对应于近红外带,并根据其热标志突出显示图像的不同组件。频道7是指示有效分割区域的掩模。

多光谱图像数据被排列为逐宽阵列的数字。在MATLAB中,多通道图像被排列为逐个宽度驱动器阵列。要重塑数据,以使通道处于第三维,请使用助手函数,开关渠道脚本平面

test_data = switchChannelstothirdplane(test_data);

确认数据具有正确的结构(通道最后)。

谁是测试数据
名称大小字节类属性test_data 12446x7654x7 13336663576 UINT16

此示例使用了完整的Hamlin Beach State Park数据集的裁剪版本测试数据变量包含。裁剪高度和宽度测试数据to create the variable输入数据该示例使用。

test_datacroprgb = imcrop(test_data(:,::,1:3),[2600,3000,2000,2000]);test_datacropinfrared = imcrop(test_data(:,::,4:6),[2600,3000,2000,2000]);test_datacropmask = imcrop(test_data(:,::,7),[2600,3000,2000,2000]);input_data(:,::,1:3)= test_datacroprgb;input_data(:,::,4:6)= test_datacropinfrared;input_data(::,:,7)= test_datacropmask;

检查input_data变量。

谁是('input_data');
名称大小字节类属性input_data 2001x2001x7 56056014 uint16

产生MEX

为生成MEX函数Sementimageunet.m入口点功能,创建一个代码配置对象CFG对于MEX代码生成。将目标语言设置为C ++。使用coder.deeplearningconfig(GPU编码器)function to create an MKL-DNN deep learning configuration object and assign it to the深度学习财产的CFG。跑过代码根命令指定输入大小为[12446,7654,7]和[1024,1024]的补丁大小。这些值对应于整个的大小输入数据多变的。较小的补丁大小加快推理。要查看如何计算补丁,请参阅段落entry-point function.

cfg = coder.config('Mex');cfg.targetlang ='C ++';cfg.deeplearningconfig = coder.deeplearningconfig('mkldnn');代码根-configCFG段落-args{-报告
代码生成成功:要查看报告,请打开('CodeGen \ Mex \ Sementimageunet \ HTML \ report.mldatx')。

运行生成的MEX预测结果输入数据

段落功能接受输入数据以及包含斑块大小作为输入的尺寸的矢量。该函数将图像划分为贴片,预测特定贴片中的像素,最后结合了所有贴片。因为很大输入数据(12446x7654x7),在补丁中处理图像更容易。

sementedImage = segmentimageunet_mex(input_data,[1024 1024]);

要仅提取分割的有效部分,请将分段图像乘以测试数据的掩码通道。

segmentedImage = uint8(input_data(:,:,7)~=0) .* segmentedImage;

通过使用Medfilt2功能。

分段图= medfilt2(分段图,[5,5]);

显示u-net分割input_data

这条代码线创建了类名的向量:

classNames = net.Layers(end).Classes;

覆盖分段的RGB测试图像上的标签,并在分割图像中添加颜色条。

%显示输入数据图1);iMshow(histeq(input_data(:,::,:,1:3)));标题('Input Image');cmap = jet(numel(classNames));sementedImageOut = labeloverlay(imadjust(input_data(:,:::,4:6),[0 0.6],[0.1 0.9],0.55),分段图,'透明度',0,'colormap',cmap);%显示分段数据图(2);imshow(分割图);标题(“分段图像输出”);n = numel(classNames);tick = 1/(n*2):1/n:1;配色栏('ticklabels',,,,cellstr(classNames),'ticks',tick,“ ticklength”,0,'ticklabelinterterpreter',,,,'没有任何');colormap(cmap) title(“使用mkldnn分割图像”');SemengedImageOverlay = labeloverlay(imadjust(input_data(:,:::,4:6),[0 0.6],[0.1 0.9],0.55),分段图,'透明度',,,,0.7,'colormap',cmap);图(3);imshow(segengedimageOverlay);标题(“分段覆盖图像”);

References

[1] Ronneberger,Olaf,Philipp Fischer和Thomas Brox。“ U-NET:生物医学图像分割的卷积网络。”arXiv preprint arXiv:1505.04597,2015。

[2] Kemker,R。,C。Salvaggio和C. Kanan。“用于语义分割的高分辨率多光谱数据集。”Corr,ABS/1703.01918,2017。

也可以看看

(MATLAB编码器)|(MATLAB编码器)|

Related Topics