AgentSkillsCN

matlab-deep-learning

MATLAB 深度学习工具箱,助您训练神经网络、卷积神经网络以及各类深度学习模型。无论您是在构建分类器(trainNetwork、trainnet)、进行语义分割(unetLayers、deeplabv3plusLayers、semanticseg)、开展目标检测(yolov4ObjectDetector、fasterRCNNObjectDetector)、实施迁移学习(resnet50、vgg16),还是自定义训练循环(dlarray、dlfeval、dlgradient、adamupdate),乃至进行 GPU 加速训练,MATLAB 深度学习工具箱都能为您提供强大助力。此外,它还覆盖了 MRI、CT、X 光片以及病理切片等医学影像分析的工作流。

SKILL.md
--- frontmatter
name: matlab-deep-learning
description: MATLAB Deep Learning Toolbox for training neural networks, CNNs, and deep learning models. Use when building classifiers (trainNetwork, trainnet), semantic segmentation (unetLayers, deeplabv3plusLayers, semanticseg), object detection (yolov4ObjectDetector, fasterRCNNObjectDetector), transfer learning (resnet50, vgg16), custom training loops (dlarray, dlfeval, dlgradient, adamupdate), or GPU-accelerated training. Covers medical image analysis workflows for MRI, CT, X-ray, and histopathology.

MATLAB Deep Learning Toolbox

Expert skill for deep learning in MATLAB, focused on medical image analysis workflows.

Cross-Toolbox Note: For image preprocessing (filtering, morphology), see matlab-image-processing-toolbox. For medical I/O (DICOM, NIfTI), see matlab-medical-imaging-toolbox. For wavelet features, see matlab-wavelet-toolbox.

When to Use This Skill

  • Building CNN classifiers for medical diagnosis (X-ray, dermatology, histopathology)
  • Semantic segmentation (U-Net, DeepLabv3+ for organ/lesion segmentation)
  • Object detection (YOLO, Faster R-CNN for nodule/cell detection)
  • Transfer learning with pretrained networks (ResNet, VGG, EfficientNet)
  • Custom training loops with dlarray and automatic differentiation
  • 3D volumetric deep learning for CT/MRI
  • GPU-accelerated training and multi-GPU workflows
  • Model deployment (ONNX export, GPU Coder)

Read Before Coding

TaskKnowledge CardKey Functions
Classify imagescards/classification.mdtrainNetwork, classify, pretrained nets
Transfer learningcards/classification.mdresnet50, layerGraph, replace layers
Semantic segmentationcards/segmentation-semantic.mdunetLayers, deeplabv3plusLayers, semanticseg
Instance segmentationcards/segmentation-instance.mdmaskrcnn, Mask R-CNN
Object detectioncards/object-detection.mdyolov4ObjectDetector, fasterRCNNObjectDetector
Custom trainingcards/custom-training.mddlarray, dlfeval, dlgradient, adamupdate
Data pipelinescards/data-pipeline.mdimageDatastore, augmentedImageDatastore, minibatchqueue
Network layerscards/network-architecture.mddlnetwork, layerGraph, custom layers
GPU/parallelcards/gpu-parallel.mdgpuArray, 'ExecutionEnvironment', multi-GPU
Deploy modelscards/deployment.mdexportONNXNetwork, GPU Coder
Medical workflowscards/medical-imaging.md3D networks, DICOM pipelines

⚠️ IMPORTANT: API Changes in R2024b+

Several functions shown in this skill are deprecated in R2024b and later. Use the modern equivalents for new projects:

Deprecated FunctionModern ReplacementNotes
trainNetworktrainnetReturns dlnetwork instead of DAGNetwork
unetLayersunetReturns dlnetwork directly
unet3dLayersunet3dReturns dlnetwork directly
deeplabv3plusLayersdeeplabv3plusReturns dlnetwork directly
classificationLayerUse trainnet with "crossentropy" lossLoss specified at training time
pixelClassificationLayerUse trainnet with "crossentropy" lossFor segmentation

Modern pattern:

matlab
net = dlnetwork(lgraph);           % Create dlnetwork
net = trainnet(ds, net, "crossentropy", opts);  % Train with loss function

The examples below use the legacy API for compatibility with pre-R2024b code.


Critical Rules

Rule 1: Data Type and Normalization

matlab
% WRONG: Using uint8 directly
net = trainNetwork(uint8Images, labels, layers, options);  % Poor convergence

% CORRECT: Normalize to [0,1] or [-1,1]
images = im2single(uint8Images);  % Now [0,1]
% Or for pretrained networks expecting specific preprocessing:
images = (im2single(uint8Images) - 0.485) / 0.229;  % ImageNet normalization

Rule 2: Match Input Sizes

matlab
% Check network input size
inputSize = net.Layers(1).InputSize;  % e.g., [224 224 3]

% Resize images to match
augDs = augmentedImageDatastore(inputSize(1:2), imds);
% Or
images = imresize(images, inputSize(1:2));

Rule 3: dlarray Format Strings

matlab
% Format: S=Spatial, C=Channel, B=Batch, T=Time, U=Unspecified
x = dlarray(data, 'SSCB');    % H×W×C×B (standard image batch)
x = dlarray(data, 'SSSCB');   % H×W×D×C×B (3D volume batch)
x = dlarray(data, 'CBT');     % C×B×T (sequence)

% Common error: wrong format causes dimension mismatch
% WRONG: x = dlarray(data, 'BCSS');  % Batch and Channel swapped

Rule 4: Gradient Computation

matlab
% Gradients only flow through dlarray operations
% WRONG: Using non-differentiable ops
mask = Y > 0.5;  % Thresholding breaks gradient flow

% CORRECT: Use soft operations
mask = sigmoid((Y - 0.5) * 10);  % Differentiable approximation

Rule 5: GPU Memory Management

matlab
% Check available GPU memory before training
gpu = gpuDevice;
fprintf('Available: %.2f GB\n', gpu.AvailableMemory/1e9);

% Reduce batch size if OOM
options = trainingOptions('adam', 'MiniBatchSize', 8);  % Start small

% Clear GPU memory between experiments
reset(gpuDevice);

Transform Selection

code
Task?
├── Image Classification
│   ├── Binary/Multiclass → trainNetwork + softmax + crossentropy
│   └── Multi-label → trainNetwork + sigmoid + binary crossentropy
├── Segmentation
│   ├── Semantic (pixel labels) → unetLayers/deeplabv3plusLayers + semanticseg
│   ├── Instance (object masks) → Mask R-CNN
│   └── 3D volumetric → unet3dLayers
├── Object Detection
│   ├── Real-time → YOLO v4 (yolov4ObjectDetector)
│   ├── High accuracy → Faster R-CNN (fasterRCNNObjectDetector)
│   └── Small objects → RetinaNet
├── Custom Architecture
│   └── dlnetwork + custom training loop + dlfeval/dlgradient
└── Generative
    ├── Image synthesis → GAN (generator + discriminator)
    └── Variational → VAE with reparameterization

Quick Patterns

Transfer Learning (Classification)

matlab
% Load pretrained network
net = resnet50;
lgraph = layerGraph(net);

% Replace final layers for your classes
numClasses = 4;  % e.g., Normal, Pneumonia, COVID, TB
lgraph = removeLayers(lgraph, {'fc1000', 'fc1000_softmax', 'ClassificationLayer_fc1000'});
newLayers = [
    fullyConnectedLayer(numClasses, 'Name', 'fc_new')
    softmaxLayer('Name', 'softmax_new')
    classificationLayer('Name', 'output')];
lgraph = addLayers(lgraph, newLayers);
lgraph = connectLayers(lgraph, 'avg_pool', 'fc_new');

% Train
options = trainingOptions('adam', ...
    'MaxEpochs', 10, ...
    'MiniBatchSize', 32, ...
    'InitialLearnRate', 1e-4, ...
    'ValidationData', valDs, ...
    'Plots', 'training-progress');
trainedNet = trainNetwork(trainDs, lgraph, options);  % Legacy API
% Modern: net = trainnet(trainDs, dlnetwork(lgraph), "crossentropy", options);

U-Net Segmentation

matlab
% Create U-Net for medical image segmentation
imageSize = [256 256 1];
numClasses = 2;  % Background + Lesion

% Legacy API (deprecated in R2024b+, use unet() instead):
lgraph = unetLayers(imageSize, numClasses, ...
    'EncoderDepth', 4, ...
    'NumFirstEncoderFilters', 64);  % Default is 64, not 32

% Modern API (R2024b+):
% net = unet(imageSize, numClasses, EncoderDepth=4);

% Prepare pixel label datastore
classNames = ["Background", "Lesion"];
pixelLabelIDs = [0, 1];
pxds = pixelLabelDatastore('masks/', classNames, pixelLabelIDs);
ds = combine(imageDatastore('images/'), pxds);

% Train with dice loss
options = trainingOptions('adam', ...
    'MaxEpochs', 50, ...
    'MiniBatchSize', 8, ...
    'InitialLearnRate', 1e-3);
net = trainNetwork(ds, lgraph, options);  % Legacy API
% Modern: net = trainnet(ds, dlnetwork(lgraph), "crossentropy", options);

% Inference
mask = semanticseg(testImage, net);

Custom Training Loop

matlab
% For advanced control: custom loss, metrics, logging
net = dlnetwork(lgraph);
numEpochs = 50;
learnRate = 1e-3;
[avgGrad, avgSqGrad] = deal([]);

for epoch = 1:numEpochs
    shuffle(mbq);
    while hasdata(mbq)
        [X, Y] = next(mbq);

        % Forward + backward
        [loss, gradients, state] = dlfeval(@modelLoss, net, X, Y);
        net.State = state;

        % Update with Adam
        [net, avgGrad, avgSqGrad] = adamupdate(net, gradients, ...
            avgGrad, avgSqGrad, iteration, learnRate);
    end
end

function [loss, gradients, state] = modelLoss(net, X, Y)
    [Ypred, state] = forward(net, X);
    loss = crossentropy(Ypred, Y);
    gradients = dlgradient(loss, net.Learnables);
end

Object Detection (YOLO)

matlab
% Create YOLO v4 detector
detector = yolov4ObjectDetector('csp-darknet53-coco');

% For custom classes, create from scratch
name = "nodule_detector";
classes = ["nodule"];
anchorBoxes = {[32 32; 64 64]; [128 128; 256 256]};  % Multi-scale
detector = yolov4ObjectDetector(name, classes, anchorBoxes, ...
    'InputSize', [416 416 3]);

% Train
options = trainingOptions('adam', ...
    'MaxEpochs', 80, ...
    'MiniBatchSize', 8, ...
    'InitialLearnRate', 1e-3);
[detector, info] = trainYOLOv4ObjectDetector(trainData, detector, options);

% Detect
[bboxes, scores, labels] = detect(detector, testImage);

Function Quick Reference

Network Training

FunctionPurposeExample
trainNetworkTrain from layers/lgraphnet = trainNetwork(ds, layers, opts)
trainnetTrain dlnetwork with lossnet = trainnet(ds, net, lossFcn, opts)
trainingOptionsConfigure trainingopts = trainingOptions('adam', ...)

Network Architecture

FunctionPurposeExample
dlnetworkCreate trainable networknet = dlnetwork(lgraph)
layerGraphNetwork with brancheslgraph = layerGraph(net)
addLayersAdd layers to graphlgraph = addLayers(lgraph, newLayers)
connectLayersConnect layer outputslgraph = connectLayers(lgraph, src, dst)
removeLayersRemove layerslgraph = removeLayers(lgraph, names)

Segmentation

FunctionPurposeExample
unetLayersCreate U-Netlgraph = unetLayers([256 256 1], 2)
unet3dLayersCreate 3D U-Netlgraph = unet3dLayers([128 128 128 1], 2)
deeplabv3plusLayersCreate DeepLabv3+lgraph = deeplabv3plusLayers(...)
semanticsegPixel classificationmask = semanticseg(img, net)
pixelLabelDatastoreLabel datastorepxds = pixelLabelDatastore(...)

Object Detection

FunctionPurposeExample
yolov4ObjectDetectorYOLO v4 detectordet = yolov4ObjectDetector(...)
fasterRCNNObjectDetectorFaster R-CNNdet = fasterRCNNObjectDetector(...)
ssdObjectDetectorSSD detectordet = ssdObjectDetector(...)
detectRun detection[bboxes, scores] = detect(det, img)
trainYOLOv4ObjectDetectorTrain YOLOdet = trainYOLOv4ObjectDetector(...)

Custom Training

FunctionPurposeExample
dlarrayDifferentiable arrayx = dlarray(data, 'SSCB')
dlfevalEvaluate with gradients[loss, grad] = dlfeval(@fn, net, x)
dlgradientCompute gradientsgrad = dlgradient(loss, params)
adamupdateAdam optimizer step[net, ag, asg] = adamupdate(...)
sgdmupdateSGD with momentum[net, vel] = sgdmupdate(...)
forwardForward pass[y, state] = forward(net, x)
predictInference (no gradients)y = predict(net, x)

Data Handling

FunctionPurposeExample
imageDatastoreImage folder datastoreimds = imageDatastore(folder)
augmentedImageDatastoreWith augmentationaugDs = augmentedImageDatastore(...)
imageDataAugmenterDefine augmentationsaug = imageDataAugmenter(...)
minibatchqueueCustom batchingmbq = minibatchqueue(ds, ...)
transformApply functiontds = transform(ds, @myFcn)
combineCombine datastorescds = combine(imds, labelds)

Pretrained Networks

NetworkFunctionInput SizeUse Case
ResNet-50resnet50224×224General classification
ResNet-101resnet101224×224Higher capacity
VGG-16vgg16224×224Feature extraction
EfficientNet-B0efficientnetb0224×224Efficient inference
Inception-v3inceptionv3299×299Fine details
DenseNet-201densenet201224×224Feature reuse

Knowledge Cards Summary

~2,400 lines of curated content:

CardLinesFocus
classification.md~300Transfer learning, pretrained networks
segmentation-semantic.md~350U-Net, DeepLabv3+, loss functions
segmentation-instance.md~200Mask R-CNN
object-detection.md~300YOLO, Faster R-CNN, anchor boxes
custom-training.md~350dlarray, gradients, optimizers
data-pipeline.md~250Datastores, augmentation
network-architecture.md~200Layers, custom layers
gpu-parallel.md~150GPU, multi-GPU training
deployment.md~150ONNX, code generation
medical-imaging.md~300Medical-specific workflows

See knowledge/INDEX.md for full navigation.

Cross-Toolbox Integration

For preprocessing → matlab-image-processing-toolbox

matlab
% Before DL: enhance, denoise, normalize
img = adapthisteq(img);                    % CLAHE
img = imgaussfilt(img, 1.5);               % Denoise
img = im2single(img);                       % Normalize

For wavelet features → matlab-wavelet-toolbox

matlab
% Multi-scale features as network input
[C, S] = wavedec2(img, 3, 'db4');
features = cat(3, appcoef2(C,S,'db4'), ...
    detcoef2('h',C,S,1), detcoef2('v',C,S,1), detcoef2('d',C,S,1));

For medical I/O → matlab-medical-imaging-toolbox

matlab
% Load with spatial referencing
V = medicalVolume('brain.nii');
% Process slices for DL
for k = 1:V.NumTransverseSlices
    slice = extractSlice(V, k, 'transverse');
    prediction = semanticseg(slice, net);
end

Source: MathWorks Deep Learning Toolbox Documentation (R2025b) Note: Legacy API examples (trainNetwork, unetLayers) shown for compatibility. Use trainnet and unet for new projects.