andy thé product marketing manager image processing...
TRANSCRIPT
![Page 1: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/1.jpg)
1 © 2014 The MathWorks, Inc.
GPU Computing with MATLAB
Andy Thé
Product Marketing Manager
Image Processing Applications
![Page 2: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/2.jpg)
2
GPU Computing with MATLAB
For MATLAB Programmers
– Acceleration MATLAB Code with GPUs
– Minimal code changes
For CUDA Programmers
– Create test harnesses for your kernels
– Quickly explore algorithm parameters
– Analyze and visualize kernel results
![Page 3: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/3.jpg)
3
Agenda
What is MATLAB?
Demo: Designing a Camera Pipeline in MATLAB
Demo: Brain Scan Demo – CPU vs GPU
Demo: White Balance Example using CUDA Code
Summary
![Page 4: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/4.jpg)
4
What is MATLAB?
High level language and development
environment for:
– Algorithm and application development
– Data analysis
– Mathematical modeling
– Multicore and GPU computing*
Extensive math, engineering, and
plotting functionality
Add-on products for image and video
processing, communications, signal
processing, financial modeling, and
more
Over 1.3 million COM & EDU users
* Requires Parallel Computing Toolbox
![Page 5: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/5.jpg)
5
Algorithm Development Process
Te
st &
Ve
rifica
tion
Implementation
Research & Design
Explore and discover
Gain insight into problem
Evaluate options, trade-offs
Requirements
Implementation
Migrate design to production
Optimize performance
Deploy / Integrate / Test
Design Test
Elaborate
.NET
Java
.dll
.C/C++
.exe CUDA
HDL
![Page 6: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/6.jpg)
6
Running MATLAB code on the GPU
200+ MATLAB functions that are GPU enabled
• Random number generation
• FFT
• Matrix multiplications
• Solvers
• Convolutions
• Min/max
• SVD
• Cholesky and LU
factorization
Additional support in toolboxes
Image Processing
• Morphological filtering,
2-D filtering, …
Communications
• Turbo, LDPC, and
Viterbi decoders, …
Signal Processing
• Cross correlation,
FIR filtering, …
Neural Networks
• Network training
and simulation
Ability to launch CUDA kernels
Ability to deploy MATLAB GPU applications
bwmorph
bwlookup
corr2
edge
histeq
imadjust
imbothat
imclose
imdilate
imerode
imfilter
imgradient
imhist
imnoise
imopen
imresize
imrotate
imshow
imtophat
imwarp
mean2
medfilt2
padarray
rgb2gray
![Page 7: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/7.jpg)
7
Agenda
What is MATLAB?
Demo: Designing a Camera Pipeline in MATLAB
Demo: Brain Scan Demo – CPU vs GPU
Demo: White Balance Example using CUDA Code
Summary
![Page 8: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/8.jpg)
8
Demo: Digital Camera Pipeline From Sensor Data to Image
Scene Image
![Page 9: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/9.jpg)
9
Stages of the Camera Pipeline
1. Noise Reduction
2. Demosaic
3. Tone Mapping
4. White Balance
5. Gamma Correction
1. Noise Reduction : Reduce noise in the raw data
2. Demosaic: Interpolate the raw image into an
RGB image
3. Tone Mapping: Convert sensor RGB values to
RGB values
4. White Balance: Adjust color of image to
compensate for different lighting conditions
5. Gamma Correction: Adjust color of image for
display
![Page 10: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/10.jpg)
10
Approaches to GPU Computing in MATLAB
GPU enabled functions
Simple programming constructs: gpuArray, gather
Advanced programming constructs: arrayfun
Interface for experts:
CUDAKernel, MEX support
Ease o
f U
se
G
rea
ter C
on
trol
http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html
http://www.mathworks.com/help/distcomp/run-cuda-or-ptx-code-on-gpu.htmll
![Page 11: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/11.jpg)
11
Agenda
What is MATLAB?
Demo: Designing a Camera Pipeline in MATLAB
Demo: Brain Scan Demo – CPU vs GPU
Demo: White Balance Example using CUDA Code
Summary
![Page 12: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/12.jpg)
12
Demo: Brain Scan
CPU vs GPU
![Page 13: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/13.jpg)
13
Agenda
What is MATLAB?
Demo: Designing a Camera Pipeline in MATLAB
Demo: Brain Scan Demo – CPU vs GPU
Demo: White Balance Example using CUDA Code
Summary
![Page 14: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/14.jpg)
14
Stages of the Camera Pipeline
1. Noise Reduction
2. Demosaic
3. Tone Mapping
4. White Balance
5. Gamma Correction
1. Noise Reduction : Reduce noise in the raw data
2. Demosaic: Interpolate the raw image into an
RGB image
3. Tone Mapping: Convert sensor RGB values to
RGB values
4. White Balance: Adjust color of image to
compensate for different lighting conditions
5. Gamma Correction: Adjust color of image for
display
![Page 15: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/15.jpg)
15
Demonstration: White balance algorithm
Goal: Deliver algorithm as CUDA C/C++
Before After
How can MATLAB support the CUDA development process?
![Page 16: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/16.jpg)
16
Programming Parallel Applications (GPU)
GPU enabled functions
Simple programming constructs: gpuArray, gather
Advanced programming constructs: arrayfun
Interface for experts:
CUDAKernel, MEX support
Ease o
f U
se
G
rea
ter C
on
trol
http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html
http://www.mathworks.com/help/distcomp/run-cuda-or-ptx-code-on-gpu.htmll
![Page 17: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/17.jpg)
17
Summary Part 1: Apply Scaling Factors on GPU
function imageData = whitebalance(imageData)
% WHITEBALANCE forces average image color to be gray.
% Find the average values for each channel.
avg_rgb = mean(mean(imageData));
% Find average gray value and compute scaling factor.
factors = max(mean(avg_rgb), 128)./avg_rgb;
% Adjust the image to the new gray value.
imageData(:,:,1) = uint8(imageData(:,:,1)*factors(1));
imageData(:,:,2) = uint8(imageData(:,:,2)*factors(2));
imageData(:,:,3) = uint8(imageData(:,:,3)*factors(3));
Code run entirely on CPU
Execution time on CPU: 120 ms
function adjustedImage = whitebalance_gpu(imageData)
...
% Load the Kernel
kernel =
parallel.gpu.CUDAKernel('applyScaleFactors.ptxw64',
'applyScaleFactors.cu' );
...
% Copy our Image to the GPU
imageDataGPU = gpuArray( imageData );
...
% Apply kernel to scale the color values
adjustedImageGPU = feval( kernel, adjustedImageGPU,
imageDataGPU, factors, nRows, nCols );
Applying Scaling Factors on GPU
Execution time on GPU: 2.2 ms
50x Faster
![Page 18: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/18.jpg)
18
Summary Part 2: Compute Scaling Factors on GPU
function imageData = whitebalance(imageData)
% WHITEBALANCE forces average image color to be gray.
% Find the average values for each channel.
avg_rgb = mean(mean(imageData));
% Find average gray value and compute scaling factor.
factors = max(mean(avg_rgb), 128)./avg_rgb;
% Adjust the image to the new gray value.
imageData(:,:,1) = uint8(imageData(:,:,1)*factors(1));
imageData(:,:,2) = uint8(imageData(:,:,2)*factors(2));
imageData(:,:,3) = uint8(imageData(:,:,3)*factors(3));
Code run entirely on CPU Computing Scaling Factors on GPU
function imageData = whitebalance(imageData)
% WHITEBALANCE forces average image color to be gray.
...
% Compute the factors from the mean.
computeFactorsKernel = parallel.gpu.CUDAKernel( ...
'computeScaleFactors.ptxw64','computeScaleFactors.cu'
);
% 3 doubles of shared memory
computeFactorsKernel.SharedMemorySize = 3*8;
computeFactorsKernel.ThreadBlockSize = [3 1 1];
factors = feval( computeFactorsKernel, avg_rgb
...
![Page 19: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/19.jpg)
19
Summary Part 3: Compute Mean on GPU with NPP
function imageData = whitebalance(imageData)
% WHITEBALANCE forces average image color to be gray.
% Find the average values for each channel.
avg_rgb = mean(mean(imageData));
% Find average gray value and compute scaling factor.
factors = max(mean(avg_rgb), 128)./avg_rgb;
% Adjust the image to the new gray value.
imageData(:,:,1) = uint8(imageData(:,:,1)*factors(1));
imageData(:,:,2) = uint8(imageData(:,:,2)*factors(2));
imageData(:,:,3) = uint8(imageData(:,:,3)*factors(3));
Code run entirely on CPU Implementing NPP through MEX
function adjustedImage = whitebalance_gpu(imageData)
...
%**************************************
% Find the average values for each channel.
avg_rgb = computeMeanMEX(imageDataGPU);
%**************************************
% Copy our Image to the GPU
imageDataGPU = gpuArray( imageData );
...
% Apply kernel to scale the color values
adjustedImageGPU = feval( kernel, adjustedImageGPU,
imageDataGPU, factors, nRows, nCols );
![Page 20: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/20.jpg)
20
// dllmain.cpp : Defines the entry point for the DLL
application.
#include <mex.h>
#include <gpu\mxGPUArray.h>
#include "whitebalance.h"
// A function to compute the mean of all the elements
of a uint8 matrix on the GPU.
// Returns a single scalar value on the GPU.
void mexFunction ( const int nlhs, mxArray * plhs[],
const int nrhs,
const mxArray * const prhs[] )
{
//Ensure the GPU system is initialized.
int mwGpuStat = 0;
mwGpuStat = mxInitGPU();
if ( mwGpuStat != 0 )
mexErrMsgTxt( "Error initializing MW GPU
system" );
...
Summary Part 4: Integrate C with CUDA
function imageData = whitebalance(imageData)
% WHITEBALANCE forces average image color to be gray.
% Find the average values for each channel.
avg_rgb = mean(mean(imageData));
% Find average gray value and compute scaling factor.
factors = max(mean(avg_rgb), 128)./avg_rgb;
% Adjust the image to the new gray value.
imageData(:,:,1) = uint8(imageData(:,:,1)*factors(1));
imageData(:,:,2) = uint8(imageData(:,:,2)*factors(2));
imageData(:,:,3) = uint8(imageData(:,:,3)*factors(3));
Code run entirely on CPU Implementing C++ with CUDA Kernels
![Page 21: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/21.jpg)
21
Agenda
What is MATLAB?
Demo: Designing a Camera Pipeline in MATLAB
Demo: Brain Scan Demo – CPU vs GPU
Demo: White Balance Example using CUDA Code
Summary
![Page 22: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/22.jpg)
22
Programming Parallel Applications (GPU)
GPU enabled functions
Simple programming constructs: gpuArray, gather
Advanced programming constructs: arrayfun
Interface for experts:
CUDAKernel, MEX support
Ease o
f U
se
G
rea
ter C
on
trol
http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html
http://www.mathworks.com/help/distcomp/run-cuda-or-ptx-code-on-gpu.htmll
![Page 23: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/23.jpg)
23
MATLAB Value-add to CUDA Programmers
Develop prototype code to explore
algorithms
Manage GPU data and launch kernels
using a simple interface
Incrementally develop and test kernels
Analyze and visualize kernel results
Te
st &
Ve
rifica
tion
Implementation
Research & Design
Explore and discover
Gain insight into problem
Evaluate options, trade-offs
Requirements
Implementation
Migrate design to production
Optimize performance
Deploy / Integrate / Test
Design
Test
Elaborate
.NET
Java
.dll
.C/C++
.exe CUDA
HDL
![Page 24: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/24.jpg)
24
Scaling to Run on Multiple GPUs
N = 1000; % Number of iterations
A = gpuArray(A); % transfer data
parfor ix = 1:M
% Do the GPU-based calculation
X = myGPUFunction(ix,A);
% Gather data
Xtotal(ix,:)= gather(X);
end
Single GPU Multiple GPUs
N = 1000; % Number of iterations
A = gpuArray(A); % transfer data to GPU
for ix = 1:M
% Do the GPU-based calculation
X = myGPUFunction(ix,A);
% Gather data
Xtotal(ix,:)= gather(X);
end
![Page 25: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/25.jpg)
25
Running MATLAB code on the GPU
200+ MATLAB functions that are GPU enabled
• Random number generation
• FFT
• Matrix multiplications
• Solvers
• Convolutions
• Min/max
• SVD
• Cholesky and LU
factorization
Additional support in toolboxes
Image Processing
• Morphological filtering,
2-D filtering, …
Communications
• Turbo, LDPC, and
Viterbi decoders, …
Signal Processing
• Cross correlation,
FIR filtering, …
Neural Networks
• Network training
and simulation
Ability to launch CUDA kernels
Ability to deploy MATLAB GPU applications
bwmorph
bwlookup
corr2
edge
histeq
imadjust
imbothat
imclose
imdilate
imerode
imfilter
imgradient
imhist
imnoise
imopen
imresize
imrotate
imshow
imtophat
imwarp
mean2
medfilt2
padarray
rgb2gray
![Page 26: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/26.jpg)
26
GPU Computing with MATLAB
For MATLAB Programmers
– Acceleration MATLAB Code with GPUs
– Minimal code changes
For CUDA Programmers
– Create test harnesses for your kernels
– Quickly explore algorithm parameters
– Analyze and visualize kernel results
![Page 27: Andy Thé Product Marketing Manager Image Processing ...on-demand.gputechconf.com/.../S4421-gpu-computing-with-matlab.pdf · Product Marketing Manager Image Processing Applications](https://reader031.vdocument.in/reader031/viewer/2022030415/5aa15b397f8b9a436d8b7bf0/html5/thumbnails/27.jpg)
27 © 2014 The MathWorks, Inc.
Questions & Answers