Base class for neural network layers.
SDKs
- iOS 10.0+
- macOS 10.13+
- Mac Catalyst 13.0+
- tvOS 10.0+
Framework
- Metal Performance Shaders
Declaration
class MPSCNNKernel : MPSKernel
Overview
An MPSCNNKernel
object consumes one MPSImage
object and produces one MPSImage
object.
The region overwritten in the destination image is described by the clip
property. The top left corner of the region consumed (ignoring adjustments for filter size—for example, convolution filter size) is given by the offset
property. The size of the region consumed is a function of the size of the clip
property and any subsampling caused by pixel strides at work (for example, strideInPixelsX/
strideInPixelsY in the MPSCNNPooling
class). Wherever the offset
and clip
properties would cause an {x,y}
pixel address not in the image to be read, the edge
property is used to determine what value to read there.
The z
or depth
component of the offset
, origin
and size
properties indexes which images to use.
If the
MPSImage
object contains only a single image, then these values should beoffset
,.z = 0 clip
, andRect .origin .z = 0 clip
.Rect .size .depth = 1 If the
MPSImage
object contains multiple images, then the value ofclip
determines the number of images to process. Both the source and destinationRect .size .depth MPSImage
objects must have at least this many images. The value ofoffset
refers to the starting image index of the source. Thus, the value of.z offset
must be.z + clip Rect .size .depth <=source
. Similarly, the value of.number Of Images clip
determines the starting image index of the destination. Thus, the value ofRect .origin .z clip
must beRect .origin .z + clip Rect .size .depth <=destination
..number Of Images
The destination
property can be used to control where the kernel will start writing in terms of feature channel dimension. For example, if the destination has 64 channels and thdestination
e kernel outputs 32 channels, channels 0-31 of the destination will be populated by the kernel (by default). But if you want the kernel to populate channels 32-63 of the destination, you can set the value of destination
to 32. Suppose you have a source of dimensions w x h x Ni
, where N
is the number of channels, which goes through a convolution filter C0
which produces the output O0 = w x h x N0
and C
1 which produces the output O1 = w x h x N1
followed by concatenation which produces O = w x h x (N0 + N1)
. You can achieve this by creating an MPSImage
object with dimensions w x h x (N0 + N1)
and using this as the destination of both convolutions as follows:
C0: destination
, this will outputFeature Channel Offset = 0 N0
channels starting at channel0
of destination thus populating[0,N0-1]
channels.C1: destination
, this will outputFeature Channel Offset = N0 N1
channels starting at channelN0
of destination thus populating[N0,N0+N1-1]
channels.