ConvOperator2D
public class ConvOperator2D: ComputableOperator
2D Convolution operator.
Input tensors shapes
All tensors in inputTensors
should have same shapes
Shape specification
- inputTensors: each tensor:
[channel, height, width]
or[height, width, channel]
according tochannelPosition
- weight:
[num_filter,channel, kernelSize[0], kernelSize[1]]
, - bias:
[num_filter]
, - outputTensors: each tensor:
[out_height, out_width, num_filter]
, i.e.,TensorChannelOrder.Last
nil weight
At declaration, weight
could be nil
.
However, if you add this operator through a graph’s operation(_,inputs:,op:)
API (i.e. symbolic constructing graph).
You must indicate the inputShape
attribute so that the graph could estimate the input and output shape information.
Batch process
This operator does not directly support batch data.
However, user could use TensorSlice
to do the batch processing.
Details can be found in Slice tensor and Batch calculation with operators.
Dilation
Currently, calculation with dilation > 1
has not been implemented and supported.
-
Operator label. Conforms to
ComputableOperator
Declaration
Swift
public var operatorLabel: String
-
This operator does not operator on GPU. Conforms to
ComputableOperator
Declaration
Swift
public var metalKernelFuncLabel:String = "Conv2D"
-
Conforms to
ComputableOperator
Declaration
Swift
public var computationDelegate: OperatorCalculationDelegate?
-
Conforms to
ComputableOperator
Declaration
Swift
public var inputTensors: [Tensor]?
-
Conforms to
ComputableOperator
Declaration
Swift
public var outputTensors: [Tensor]?
-
If
true
, operator will not callinputOutputTensorsCheck()
before doing calculation. This is used inside framework to speed up in situation we know it will not be wrong.Declaration
Swift
public var disableInputOutputCheck: Bool
-
Indicate if this operator would do paramter update.
Declaration
Swift
public var trainable: Bool = true
-
The mapping type of this operator.
OneToOne
for this operator.Declaration
Swift
public var mapType: OperatorMappingType
-
conv operator cannot do in-place calculation
Declaration
Swift
public var inPlaceble: Bool = false
-
The number of fitlers
Declaration
Swift
public var numFilters: Int
-
The kernel size.
[height, width]
Declaration
Swift
public var kernelSize: [Int]
-
Stride.
[height, width]
Default is[1, 1]
.Declaration
Swift
public var stride: [Int]
-
Dilate values. 2D vecotr indicating the dilation value in height and width. Default is
[1, 1]
, i.e. without dilation. TODO: Support dilationDeclaration
Swift
public var dilation: [Int]
-
Padding mode. Default
PaddingMode.valid
Declaration
Swift
public var padMode: PaddingMode = PaddingMode.Valid
-
Channel position. Default is
ImageChannelOrder.First
Declaration
Swift
public var channelPosition: TensorChannelOrder = .First
-
The weight tensor.
Declaration
Swift
public var weight: Tensor?
-
If use
bias
. Default istrue
.Declaration
Swift
public var biasEnabled: Bool
-
The bias tensor.
Declaration
Swift
public var bias: Tensor?
-
The input shape Used to indicate the input tensors’ shape. Should not be
nil
construction from scratch.Declaration
Swift
public var inputShape: TensorShape?
-
Calculation method
Declaration
Swift
public var calMethod: ConvMethod
-
Padding value when using Same mode
Declaration
Swift
public var paddingValue: Float = 0.0
-
init(numFilters:kernelSize:stride:padMode:channelPosition:weight:bias:dilation:biasEnabled:computationDelegate:inputTensors:outputTensors:operatorLabel:inputShape:disableInputOutputCheck:calMethod:)
Designated init.
Declaration
Swift
public init(numFilters: Int, kernelSize: [Int], stride: [Int] = [1, 1], padMode: PaddingMode = .Valid, channelPosition: TensorChannelOrder = .First, weight: Tensor? = nil, bias: Tensor? = nil, dilation: [Int] = [1, 1], biasEnabled: Bool = true, computationDelegate: OperatorCalculationDelegate? = nil, inputTensors: [Tensor]? = nil, outputTensors: [Tensor]? = nil, operatorLabel: String = "Conv2DOp", inputShape: TensorShape? = nil, disableInputOutputCheck: Bool = false, calMethod: ConvMethod = ConvMethod.Img2Col)
Parameters
numFilters
kernelSize
stride
padMode
channelPosition
weight
bias
dilation
computationDelegate
inputTensors
outputTensors
operatorLabel
inputShape
disableInputOutputCheck
-
Compute output shape according
numFilters
,kernelSize
,stride
anddilation
.Declaration
Swift
public func outputShape(shapeArray shapes: [TensorShape]) -> [TensorShape]?
Parameters
shapes
shapes description
Return Value
return value description
-
Check input and output tensors.
Declaration
Swift
public func inputOutputTensorsCheck() -> (check: Bool, msg: String)
Return Value
return value description
-
Compute sync way.
Declaration
Swift
public func compute(_ computationMode: OperatorComputationMode)
Parameters
computationMode
mode
-
Compute async
Declaration
Swift
public func computeAsync(_ computationMode: OperatorComputationMode)
Parameters
computationMode
computationMode
-
Declaration
Swift
public func gradCompute(_ computationMode: OperatorComputationMode) -> [String: DataSymbolSupportedDataType]
-
Declaration
Swift
public func gradComputAsync(_ computationMode: OperatorComputationMode)
-
Undocumented
Declaration
Swift
public func updateParams(grads: [Tensor], LR: Float)
-
Bind according to labels.
-Note: if cannot bind all needed parameters.
fatalError
will be raised.Declaration
Swift
public func bindParamSymbols(_ symbols: [GraphSymbol])
-
Weight
asTensorSymbol
Declaration
Swift
public func paramSymbols() -> [GraphSymbol]
Return Value
-
Cpu calculation
Declaration
Swift
internal func cpu()
-
GPU calcualtion
Declaration
Swift
internal func gpu()
-
Calculate the convolution following naive algorithm
Declaration
Swift
internal func naive(_ mode: OperatorComputationMode)
Parameters
mode
mode
-
Naive gpu calcualtion. No intermediate tensor created
Declaration
Swift
internal func naive_gpu(input: Tensor, output: Tensor, weightBuffer: MTLBufferResource, biasBuffer: MTLBufferResource?)
Parameters
input
output
weightBuffer
biasBuffer
-
Naive CPU calculation
Parameters
input
input
output
output
-
Use Img2Col to calcualte result.
- Convert each input tensor via Img2Col to a 2D tensor
A
with shape[out_Height x out_Width, channel x kernelSize[0] x kernelSize[1]]
; - We view weight tensor as a 2D tensor
B
with shape[num_filter,channel x kernelSize[0] x kernelSize[1]]
; - Do matrix multiplication
AxB
withtransposeB
setting astrue
.
Declaration
Swift
internal func img2Col(_ mode: OperatorComputationMode)
Parameters
mode
computation mode
- Convert each input tensor via Img2Col to a 2D tensor
-
Undocumented
Declaration
Swift
internal func cpu_dilation()
-
Undocumented
Declaration
Swift
internal func gpu_dilation()