%% Generate Digit Images Using Variational Autoencoder on Intel CPUs
% This example shows how to generate a MEX function for a trained variational autoencoder (VAE) network that runs on Intel® CPUs. The example illustrates:
% Generation of hand-drawn digit images in the style of the MNIST data set.
% Code generation for a dlnetwork object representing a deep learning network using the Intel MKL-DNN library.
% Use of dlarray objects in code generation.
% This example uses a pretrained decoder network based on the Train Variational Autoencoder (VAE) to Generate Images example from the Deep Learning Toolbox™. For more information, see Train Variational Autoencoder (VAE) to Generate Images.
%
% Table of Contents
% Third-Party Prerequisites
%     Required
%     Optional
% Pretrained Variational Autoencoder Network
% The generateVAE Entry-Point Function
% Evaluate the Entry-Point Function
% Generate MEX Function
% Run the Generated MEX
%
%% Third-Party Prerequisites
%% Required
% Intel processor with support for Intel Advanced Vector Extensions 2 (Intel AVX2) instructions.
%% Optional
% For non-MEX builds such as static, dynamic libraries or executables, this example has the following additional requirements.
% Intel Math Kernel Library for Deep Neural Networks (MKL-DNN)
% For information on the supported versions of the compilers and libraries, see docid:coder_ug#mw_99a4acec-aced-453c-aadd-a18e83133574
% This example is not supported in MATLAB® Online.
%
%% Pretrained Variational Autoencoder Network
% Autoencoders have two parts: the encoder and the decoder. The encoder takes an image input and outputs a compressed representation (the encoding), which is a vector of size latent_dim, equal to 20 in this example. The decoder takes the compressed representation, decodes it, and recreates the original image.
% VAEs differ from regular autoencoders in that they do not use the encoding-decoding process to reconstruct an input. Instead, they impose a probability distribution on the latent space, and learn the distribution so that the distribution of outputs from the decoder matches that of the observed data. Then, they sample from this distribution to generate new data.
% This example uses the decoder network trained in the Train Variational Autoencoder (VAE) to Generate Images example. To train the network yourself, see Train Variational Autoencoder (VAE) to Generate Images.
%
%
%% The generateVAE Entry-Point Function
% The generateVAE entry-point function loads the dlnetwork object from the trainedDecoderVAENet MAT-file into a persistent variable and reuses the persistent object for subsequent prediction calls. It initializes a dlarray object containing 25 randomly generated encodings, passes them through the decoder network, and extracts the numeric data of the generated image from the deep learning array object.

type('generateVAE.m')

%
%% Evaluate the Entry-Point Function
% Evaluate the generateVAE entry-point function to generate digit images and plot the results.

latentDim = 20;
matfile = 'trainedDecoderVAENet.mat';
Env = '';

figure()
title("Generated samples of digits - MATLAB")

generatedImageML = generateVAE(matfile, latentDim, Env);
imshow(imtile(generatedImageML, "ThumbnailSize", [100,100]))

%
%% Generate MEX Function
% To generate a MEX function for the generateVAE entry-point function, create a code configuration object for a MEX target and set the target language to C++. Use the coder.DeepLearningConfig function to create a MKL-DNN deep learning configuration object and assign it to the DeepLearningConfig property of the code configuration object.

cfg = coder.config('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('mkldnn');

args = {coder.Constant(matfile), coder.Constant(latentDim), coder.Constant(Env)};

codegen -config cfg -args args generateVAE -report

%
%
%% Run the Generated MEX
% Call the generated MEX and display the results.

figure()
title("Generated samples of digits using MKL-DNN")

generatedImage = generateVAE_mex(matfile, latentDim, Env);
imshow(imtile(generatedImage, "ThumbnailSize", [100,100]))

%
% Copyright 2020 The MathWorks, Inc.
%