public interface MPSNNPadding extends NSSecureCoding
A method to describe how MPSCNNKernels should pad images when data outside the image is needed
Different (non-Apple) CNN frameworks have different policies for how to size the result of a CNN filter and what padding to add around the edges. Some filters such as pooling and convolution read from neighboring feature channel (pixel) values. Four predefined MPSPaddingMethods are available: MPSNNPaddingMethodValidOnly, MPSNNPaddingMethodFull, MPSNNPaddingMethodSameTL, MPSNNPaddingMethodSameBR. You may also implement your own padding definition with a block that conforms to this prototype.
| Modifier and Type | Method and Description |
|---|---|
default MPSImageDescriptor |
destinationImageDescriptorForSourceImagesSourceStatesForKernelSuggestedDescriptor(NSArray<? extends MPSImage> sourceImages,
NSArray<? extends MPSState> sourceStates,
MPSKernel kernel,
MPSImageDescriptor inDescriptor)
Determine padding and sizing of result images
|
default MPSNNPadding |
inverse()
Make a "inverted" padding policy suitable for a training gradient pass.
|
default java.lang.String |
label()
FIXME: to be made @required
|
long |
paddingMethod()
Get the preferred padding method for the node
|
_supportsSecureCodingencodeWithCoder, initWithCoderdefault MPSImageDescriptor destinationImageDescriptorForSourceImagesSourceStatesForKernelSuggestedDescriptor(NSArray<? extends MPSImage> sourceImages, NSArray<? extends MPSState> sourceStates, MPSKernel kernel, MPSImageDescriptor inDescriptor)
A MPSNNPaddingMethod must both return a valid MPSImageDescriptor and set the MPSKernel.offset to the correct value. This is a required feature if the MPSNNPaddingMethodCustom bit is set in the paddingMethod.
Some code that may prove helpful:
[@code] const int centeringPolicy = 0; // When kernelSize is even: 0 pad bottom right. 1 pad top left. Centers the kernel for even sized kernels.
typedef enum Style{ StyleValidOnly = -1, StyleSame = 0, StyleFull = 1 }Style;
// Typical destination size in one dimension for forward filters (most filters) static int DestSize( int sourceSize, int stride, int filterWindowSize, Style style ){ sourceSize += style * (filterWindowSize - 1); // adjust how many pixels we are allowed to read return (sourceSize + stride - 1) / stride; // sourceSize / stride, round up }
// Typical destination size in one dimension for reverse filters (e.g. convolution transpose) static int DestSizeReverse( int sourceSize, int stride, int filterWindowSize, Style style ){ return (sourceSize-1) * stride + // center tap for the last N-1 results. Take stride into account 1 + // center tap for the first result style * (filterWindowSize-1); // add or subtract (or ignore) the filter extent }
// Find the MPSOffset in one dimension static int Offset( int sourceSize, int stride, int filterWindowSize, Style style ){ // The correction needed to adjust from position of left edge to center per MPSOffset definition int correction = filterWindowSize / 2;
// exit if all we want is to start consuming pixels at the left edge of the image. if( 0 ) return correction;
// Center the area consumed in the source image: // Calculate the size of the destination image int destSize = DestSize( sourceSize, stride, filterWindowSize, style ); // use DestSizeReverse here instead as appropriate
// calculate extent of pixels we need to read in source to populate the destination int readSize = (destSize-1) * stride + filterWindowSize;
// calculate number of missing pixels in source int extraSize = readSize - sourceSize;
// number of missing pixels on left side int leftExtraPixels = (extraSize + centeringPolicy) / 2;
// account for the fact that the offset is based on the center pixel, not the left edge return correction - leftExtraPixels; } [@endcode]
sourceImages - The list of source images to be usedsourceStates - The list of source states to be usedkernel - The MPSKernel the padding method will be applied to. Set the kernel.offsetinDescriptor - MPS will prepare a starting guess based on the padding policy (exclusive of
MPSNNPaddingMethodCustom) set for the object. You should adjust the offset
and image size accordingly. It is on an autoreleasepool.default java.lang.String label()
long paddingMethod()
default MPSNNPadding inverse()