Train Network in the Cloud Using Automatic Parallel Support
This example shows how to train a convolutional neural network using MATLAB automatic support for parallel training. Deep learning training often takes hours or days. With parallel computing, you can speed up training using multiple graphical processing units (GPUs) locally or in a cluster in the cloud. If you have access to a machine with multiple GPUs, then you can complete this example on a local copy of the data. If you want to use more resources, then you can scale up deep learning training to the cloud. To learn more about your options for parallel training, see Scale Up Deep Learning in Parallel, on GPUs, and in the Cloud. This example guides you through the steps to train a deep learning network on a cluster in the cloud using MATLAB automatic parallel support.
Before you can run the example, you need to configure a cluster and upload data to the cloud. In MATLAB, you can create clusters in the cloud directly from the MATLAB Desktop. On the Home tab, in the Environment area, select Parallel > Create and Manage Clusters. In the Cluster Profile Manager, click Create Cloud Cluster. Alternatively, you can use MathWorks Cloud Center to create and access compute clusters. For more information, see Getting Started with Cloud Center. After that, upload your data to an Amazon S3 bucket and access it directly from MATLAB. This example uses a copy of the CIFAR-10 data set that is already stored in Amazon S3. For instructions, see Work with Deep Learning Data in AWS.
Set Up Cluster
Select your cloud cluster and start a parallel pool, set the number of workers to the number of GPUs in your cluster. If you specify more workers than GPUs, then the remaining workers are idle.
numberOfGPUs = 4; cluster = parcluster("MyClusterInTheCloud"); pool = parpool(cluster,numberOfGPUs);
Starting parallel pool (parpool) using the 'MyClusterInTheCloud' profile ... Connected to parallel pool with 4 workers.
If you do not specify a cluster, the default cluster profile is used. Check the default cluster profile on the MATLAB Home tab, in the Environment area, select Parallel > Create and Manage Cluster.
Load Data Set from the Cloud
Load the training and test data sets from the cloud using
imageDatastore. In this example, you use a copy of the CIFAR-10 data set stored in Amazon S3. To ensure that the workers have access to the datastore in the cloud, make sure that the environment variables for the AWS credentials are set correctly. See Work with Deep Learning Data in AWS.
imdsTrain = imageDatastore("s3://cifar10cloud/cifar10/train", ... IncludeSubfolders=true, ... LabelSource="foldernames"); imdsTest = imageDatastore("s3://cifar10cloud/cifar10/test", ... IncludeSubfolders=true, ... LabelSource="foldernames");
Train the network with augmented image data by creating an
augmentedImageDatastore object. Use random translations and horizontal reflections. Data augmentation helps prevent the network from overfitting and memorizing the exact details of the training images.
imageSize = [32 32 3]; pixelRange = [-4 4]; imageAugmenter = imageDataAugmenter( ... RandXReflection=true, ... RandXTranslation=pixelRange, ... RandYTranslation=pixelRange); augmentedImdsTrain = augmentedImageDatastore(imageSize,imdsTrain, ... DataAugmentation=imageAugmenter, ... OutputSizeMode="randcrop");
Define Network Architecture and Training Options
Define a network architecture for the CIFAR-10 data set. To simplify the code, use convolutional blocks that convolve the input. The supporting function
convolutionalBlock is provided at the end of this example and creates repeating blocks of layers, each containing a convolution layer, a batch normalization layer, and a ReLU layer. The pooling layers downsample the spatial dimensions.
blockDepth = 4; netWidth = 32; layers = [ imageInputLayer(imageSize) convolutionalBlock(netWidth,blockDepth) maxPooling2dLayer(2,Stride=2) convolutionalBlock(2*netWidth,blockDepth) maxPooling2dLayer(2,Stride=2) convolutionalBlock(4*netWidth,blockDepth) averagePooling2dLayer(8) fullyConnectedLayer(10) softmaxLayer classificationLayer ];
When you use multiple GPUs, you increase the available computational resources. Scale up the mini-batch size with the number of GPUs to keep the workload on each GPU constant and scale the learning rate according to the mini-batch size.
miniBatchSize = 256 * numberOfGPUs; initialLearnRate = 1e-1 * miniBatchSize/256;
Specify the training options:
Train a network using the SGDM solver for 50 epochs.
Train the network in parallel using the current cluster by setting the execution environment to
Use a learning rate schedule to drop the learning rate as the training progresses.
Use L2 regularization to prevent overfitting.
Set the mini-batch size and shuffle the data every epoch.
Validate the network using the validation data.
Turn on the training progress plot to obtain visual feedback during training.
Disable the verbose output.
options = trainingOptions("sgdm", ... MaxEpochs=50, ... ExecutionEnvironment="parallel", ... InitialLearnRate=initialLearnRate, ... LearnRateSchedule="piecewise", ... LearnRateDropFactor=0.1, ... LearnRateDropPeriod=45, ... L2Regularization=1e-10, ... MiniBatchSize=miniBatchSize, ... Shuffle="every-epoch", ... ValidationData=imdsTest, ... ValidationFrequency=floor(numel(imdsTrain.Files)/miniBatchSize), ... Plots="training-progress", ... Verbose=false);
Train Network and Use for Classification
Train the network in the cluster. During training, the plot displays the progress.
net = trainNetwork(augmentedImdsTrain,layers,options);
Use the trained network to classify the test images on your local machine, then compare the predicted labels to the actual labels.
YPredicted = classify(net,imdsTest); accuracy = sum(YPredicted == imdsTest.Labels)/numel(imdsTest.Labels)
accuracy = 0.8968
Close the parallel pool if you do not intend to use it again.
Convolutional Block Function
convolutionalBlock function creates
numConvBlocks convolutional blocks, each containing a 2-D convolutional layer, a batch normmalization layer, and a ReLU layer. Each 2-D convolutional layer has
numFilters 3-by-3 filters.
function layers = convolutionalBlock(numFilters,numConvBlocks) layers = [ convolution2dLayer(3,numFilters,Padding="same") batchNormalizationLayer reluLayer ]; layers = repmat(layers,numConvBlocks,1); end