---
title: MPSCNNBinaryConvolution
framework: metalperformanceshaders
role: symbol
role_heading: Class
path: metalperformanceshaders/mpscnnbinaryconvolution
---

# MPSCNNBinaryConvolution

A convolution kernel with binary weights and an input image using binary approximations.

## Declaration

```swift
class MPSCNNBinaryConvolution
```

## Overview

Overview The MPSCNNBinaryConvolution optionally first binarizes the input image and then convolves the result with a set of binary-valued filters, each producing one feature map in the output image (which is a normal image). The output is computed as follows:

where the sum over dx,dy is over the spatial filter kernel window defined by kernelWidth and kernelHeight, sum over f is over the input feature channel indices within group, B contains the binary weights, interpreted as {-1, 1} or {0, 1} and scale[c] is the outputScaleTerms array and bias is the outputBiasTerms array. Above i is the image index in batch the sum over input channels f runs through the group indices. The convolution operator ⊗ is defined by MPSCNNBinaryConvolutionType passed in at initialization time of the filter: !(x ^ y) = delta_xy = { (x == y) ? 1 : 0 } and scaled according to the optional scaling operations. Note that we output the values of the bitwise convolutions to interval {-1, 1}, which means that the output of the XNOR-operator is scaled implicitly as follows: r = 2 * ( !(x ^ y) ) - 1 = { -1, 1 } This means that for a dot-product of two 32-bit words the result is: r = 2 * popcount(!(x ^ y) ) - 32 = 32 - 2 * popcount( x ^ y ) = { -32, -30, ..., 30, 32 } (x & y) = delta_xy * delta_x1 = { (x == y == 1) ? 1 : 0 } and scaled according to the optional scaling operations. Note that we output the values of the AND-operation is assumed to lie in {0, 1} interval and hence no more implicit scaling takes place. This means that for a dot-product of two 32-bit words the result is: r = popcount(x & y) = { 0, ..., 31, 32 } The input data can be pre-offset and scaled by providing the inputBiasTerms and inputScaleTerms parameters for the initialization functions and this can be used for example to accomplish batch normalization of the data. The scaling of input values happens before possible beta-image computation. The parameter beta above is an optional image which is used to compute scaling factors for each spatial position and image index. For the XNOR-Net based networks this is computed as follows:

where (dx,dy) are summed over the convolution filter window.

where in is the original input image (in full precision) and Nc is the number of input channels in the input image. Parameter beta is not passed as input and to enable beta-scaling the user can provide MPSCNNBinaryConvolutionFlags.useBetaScaling in the flags parameter in the initialization functions. Finally the normal activation neuron is applied and the result is written to the output image. note: MPSCNNBinaryConvolution does not currently support groups greater than 1.

## Topics

### Initializers

- [init(coder:device:)](metalperformanceshaders/mpscnnbinaryconvolution/init(coder:device:).md)
- [init(device:convolutionData:outputBiasTerms:outputScaleTerms:inputBiasTerms:inputScaleTerms:type:flags:)](metalperformanceshaders/mpscnnbinaryconvolution/init(device:convolutiondata:outputbiasterms:outputscaleterms:inputbiasterms:inputscaleterms:type:flags:).md)
- [init(device:convolutionData:scaleValue:type:flags:)](metalperformanceshaders/mpscnnbinaryconvolution/init(device:convolutiondata:scalevalue:type:flags:).md)
- [MPSCNNConvolutionDataSource](metalperformanceshaders/mpscnnconvolutiondatasource.md)
- [MPSCNNBinaryConvolutionType](metalperformanceshaders/mpscnnbinaryconvolutiontype.md)
- [MPSCNNBinaryConvolutionFlags](metalperformanceshaders/mpscnnbinaryconvolutionflags.md)

### Instance Properties

- [inputFeatureChannels](metalperformanceshaders/mpscnnbinaryconvolution/inputfeaturechannels.md)
- [outputFeatureChannels](metalperformanceshaders/mpscnnbinaryconvolution/outputfeaturechannels.md)

## Relationships

### Inherits From

- [MPSCNNKernel](metalperformanceshaders/mpscnnkernel.md)

### Inherited By

- [MPSCNNBinaryFullyConnected](metalperformanceshaders/mpscnnbinaryfullyconnected.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)

## See Also

### Convolution Layers

- [MPSCNNConvolution](metalperformanceshaders/mpscnnconvolution.md)
- [MPSCNNDepthWiseConvolutionDescriptor](metalperformanceshaders/mpscnndepthwiseconvolutiondescriptor.md)
- [MPSCNNSubPixelConvolutionDescriptor](metalperformanceshaders/mpscnnsubpixelconvolutiondescriptor.md)
- [MPSCNNConvolutionTranspose](metalperformanceshaders/mpscnnconvolutiontranspose.md)
- [MPSCNNConvolutionGradient](metalperformanceshaders/mpscnnconvolutiongradient.md)
- [MPSCNNConvolutionGradientState](metalperformanceshaders/mpscnnconvolutiongradientstate.md)
- [MPSImageSizeEncodingState](metalperformanceshaders/mpsimagesizeencodingstate.md)
- [MPSCNNConvolutionWeightsAndBiasesState](metalperformanceshaders/mpscnnconvolutionweightsandbiasesstate.md)
