CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

ND convolution and pooling

Open Madgeeno opened this issue 10 years ago • 31 comments

Hi all, thanks again for releasing this very powerful toolkit. I read the doc and run the MNIST example. In this example the "mnist_convert.py" resizes the 2d digit images in a feature vector of length 28x28. How can I implement something similar for 3d datasets? Would it work out of the box if I transform my data in a feature vector of length NX x NY x NZ? Is 3d convolution already supported?

Madgeeno avatar Feb 09 '16 16:02 Madgeeno

Hello, Have you looked at CIFAR-10 and ImageNet examples? For instance, take a look at 01_Conv.cntk sample here. Refer to readme.txt in that folder to get more information and instructions. Hope this helps.

Alexey-Kamenev avatar Feb 09 '16 17:02 Alexey-Kamenev

Note that Convolution currently relies on the cuDNN library's cudnnConvolutionForward() function, i.e. it only supports tensor shapes of 2D planes with multiple channels (such as 2D color images, or speech spectrograms with derivatives), i.e. convolving over the first 2 dimensions, reducing/removing the third dimension, and creating a new third dimension (feature map).

Tensor support in CNTK evolved in stages, starting from no tensor support (vectors only) via special-cased 3D tensors (2D-images with color planes) to general tensor support (TensorShape). Convolution was implemented for the special-cased 3D tensor version, and has not yet been fully adapted to arbitrary TensorShapes.

I'd love to see the next step taken towards allowing allowing at least all TensorShapes that are supported by cuDNN, including convolving over 3 dimensions plus reducing over 1.

frankseide avatar Feb 09 '16 17:02 frankseide

@frankseide thanks for your answer, I'd already looked into the Cifar10, but there the 3rd dimension is to support the color channel. I see that in ConvolutionEngine.h there is a ConvolutionTensor4D, which is I think what you mentioned already. Is there already a plan for releasing support of convolution on 5D (cudnn supported) tensor shapes?

Madgeeno avatar Feb 10 '16 10:02 Madgeeno

Yes (…since yesterday, triggered by your feedback--thanks!). We will generalize how users define the Convolution operation, and initially implement all constellations that are available in cuDNN, while deprioritizing other configurations unless we have a use case (like sparse convolution for NLP). This will solve the most use cases, including yours. It will be a few days though until we will get to it.

From: Madgeeno [mailto:[email protected]] Sent: Wednesday, February 10, 2016 2:40 To: Microsoft/CNTK [email protected] Cc: Frank Seide [email protected] Subject: Re: [CNTK] ND convolution and pooling (#104)

@frankseidehttps://github.com/frankseide thanks for your answer, I'd already looked into the Cifar10, but there the 3rd dimension is to support the color channel. I see that in ConvolutionEngine.h there is a ConvolutionTensor4D, which is I think what you mentioned already. Is there already a plan for releasing support of convolution on 5D (cudnn supported) tensor shapes?

— Reply to this email directly or view it on GitHubhttps://github.com/Microsoft/CNTK/issues/104#issuecomment-182305952.

frankseide avatar Feb 10 '16 16:02 frankseide

Wow awesome, thanks again for the quick response and the re-prioritizing! I'll stay tuned.

Madgeeno avatar Feb 10 '16 16:02 Madgeeno

Hi @frankseide, is there any news about ND Convolution & Pooling? Is there a dev branch that I could check out in the meanwhile? Thanks again!

Madgeeno avatar Feb 22 '16 15:02 Madgeeno

Hello, I've started working on it but there is no branch that you can look at yet. If all goes well then I will have something by the end of this week. It won't be the fastest/prettiest code, of course, but it should give you an idea on how to use it/provide feedback. Thanks.

Alexey-Kamenev avatar Feb 22 '16 17:02 Alexey-Kamenev

Thanks a lot @Alexey-Kamenev!

Madgeeno avatar Feb 23 '16 08:02 Madgeeno

@Alexey-Kamenev @frankseide

Hi, I am wondering if 3D convolution is supported by CNTK now? For example, if I have a 3D input with the size of 10 by 10 by 10, can I indicate that the kernel size should be 5 by 5 by 5 (the kernel slides along x, y and z direction during the convolution)?

Thank you!

pinzhang10 avatar Jul 23 '16 23:07 pinzhang10

Hello, Yes, 3D (and ND in general) convolutions are supported. You should be able to use N-dimensional tensors in your BrainScript code when specifying convolution parameters. Note that in case of 1, 2 and 3D convolutions CNTK will use cuDNN implementation (fast), in all other cases it will use reference engine (slow). You can verify it by looking in the logs.

Alexey-Kamenev avatar Jul 24 '16 03:07 Alexey-Kamenev

@Alexey-Kamenev

I tried the 3D convolution. But it says "EXCEPTION occurred: cuDNN could not find suitable algorithm for the current convolution configuration."

I paste the output below. Should I call the reference engine by myself? If it is the case, how can I do it? Thank you very much!

##############################################################################

Action "train"

##############################################################################

CNTKCommandTrainBegin: train LockDevice: Locked GPU 0 to test availability. LockDevice: Unlocked GPU 0 after testing. LockDevice: Locked GPU 0 for exclusive use. NDLBuilder Using GPU 0

Creating virgin network.

Post-processing network...

6 roots: deconv1.act = RectifiedLinear() deconv1.b = LearnableParameter() deconv1.isd = LearnableParameter() deconv1.m = LearnableParameter() deconv1.sc = LearnableParameter() mse = SquareError()

Validating network. 20 nodes to process in pass 1.

Validating --> conv1.W = LearnableParameter() : -> [16 x 125] Validating --> featScale = LearnableParameter() : -> [1 x 1] Validating --> features = InputValue() : -> [32 x 36 x 11 x *] Validating --> featScaled = ElementTimes (featScale, features) : [1 x 1], [32 x 36 x 11 x *] -> [32 x 36 x 11 x *] Validating --> conv1.c = Convolution (conv1.W, featScaled) : [16 x 125], [32 x 36 x 11 x *] -> [32 x 36 x 176 x *] Validating --> conv1.sc = LearnableParameter() : -> [16 x 1] Validating --> conv1.b = LearnableParameter() : -> [16 x 1] Validating --> conv1.m = LearnableParameter() : -> [16 x 1] Validating --> conv1.isd = LearnableParameter() : -> [16 x 1] Validating --> conv1.BN = BatchNormalization (conv1.c, conv1.sc, conv1.b, conv1.m, conv1.isd) : [32 x 36 x 176 x *], [16 x 1], [16 x 1], [16 x 1], [16 x 1] -> [32 x 36 x 176 x *] Validating --> conv1.y = RectifiedLinear (conv1.BN) : [32 x 36 x 176 x *] -> [32 x 36 x 176 x *] Validating --> deconv1.act = RectifiedLinear (conv1.y) : [32 x 36 x 176 x *] -> [32 x 36 x 176 x *] Validating --> deconv1.b = LearnableParameter() : -> [16 x 1] Validating --> deconv1.isd = LearnableParameter() : -> [16 x 1] Validating --> deconv1.m = LearnableParameter() : -> [16 x 1] Validating --> deconv1.sc = LearnableParameter() : -> [16 x 1] Validating --> labels = InputValue() : -> [32 x 36 x 11 x *] Validating --> deconv1.w.W = LearnableParameter() : -> [16 x 125] Validating --> deconv1.out = Convolution (deconv1.w.W, conv1.y) : [16 x 125], [32 x 36 x 176 x *] -> [32 x 36 x 11 x *] Validating --> mse = SquareError (labels, deconv1.out) : [32 x 36 x 11 x *], [32 x 36 x 11 x *] -> [1]

Validating network. 7 nodes to process in pass 2.

Validating network, final pass.

conv1.c: using cuDNN convolution engine for geometry: Input: 32 x 36 x 11, Output: 32 x 36 x 176, Kernel: 5 x 5 x 5, Map: 16, Stride: 1 x 1 x 1, Sharing: (1, 1, 1), AutoPad: (1), LowerPad: 0, UpperPad: 0.

Using CNTK batch normalization engine.

deconv1.out: using cuDNN convolution engine for geometry: Input: 32 x 36 x 11, Output: 32 x 36 x 176, Kernel: 5 x 5 x 5, Map: 16, Stride: 1 x 1 x 1, Sharing: (1, 1, 1), AutoPad: (1), LowerPad: 0, UpperPad: 0.

12 out of 20 nodes do not share the minibatch layout with the input data.

Post-processing network complete.

Created model with 20 nodes on GPU 0.

Training criterion node(s): mse = SquareError

Allocating matrices for forward and/or backward propagation.

Memory Sharing Structure:

0000000000000000: {[conv1.isd Gradient[16 x 1]] [conv1.m Gradient[16 x 1]] [deconv1.act Gradient[32 x 36 x 176 x *]] [deconv1.act Value[32 x 36 x 176 x *]] [deconv1.b Gradient[16 x 1]] [deconv1.isd Gradient[16 x 1]] [deconv1.m Gradient[16 x 1]] [deconv1.sc Gradient[16 x 1]] [featScale Gradient[1 x 1]] [featScaled Gradient[32 x 36 x 11 x *]] [features Gradient[32 x 36 x 11 x *]] [labels Gradient[32 x 36 x 11 x *]] } 000000001C18F150: {[labels Value[32 x 36 x 11 x *]] } 000000001C18F290: {[featScale Value[1 x 1]] } 000000001C18F470: {[conv1.W Value[16 x 125]] } 000000001C18F5B0: {[conv1.b Value[16 x 1]] } 000000001C18F6F0: {[conv1.sc Value[16 x 1]] } 000000001C18F830: {[conv1.m Value[16 x 1]] } 000000001C18F970: {[conv1.isd Value[16 x 1]] } 000000001C18FC90: {[deconv1.b Value[16 x 1]] } 000000001C18FDD0: {[deconv1.sc Value[16 x 1]] } 000000001C18FF10: {[deconv1.m Value[16 x 1]] } 000000001C190050: {[deconv1.isd Value[16 x 1]] } 000000001C190190: {[deconv1.w.W Value[16 x 125]] } 000000001E7839A0: {[features Value[32 x 36 x 11 x *]] } 000000002E30CE80: {[mse Value[1]] } 000000002E30CF20: {[featScaled Value[32 x 36 x 11 x *]] } 000000002E30CFC0: {[conv1.c Value[32 x 36 x 176 x *]] } 000000002E30D100: {[conv1.BN Value[32 x 36 x 176 x *]] } 000000002E30D2E0: {[conv1.c Gradient[32 x 36 x 176 x *]] [conv1.y Value[32 x 36 x 176 x *]] } 000000002E30D380: {[deconv1.out Value[32 x 36 x 11 x *]] } 000000002E30D420: {[conv1.BN Gradient[32 x 36 x 176 x *]] } 000000002E30D560: {[mse Gradient[1]] } 000000002E30D600: {[conv1.W Gradient[16 x 125]] [deconv1.out Gradient[32 x 36 x 11 x *]] } 000000002E30D6A0: {[deconv1.w.W Gradient[16 x 125]] } 000000002E30D740: {[conv1.sc Gradient[16 x 1]] [conv1.y Gradient[32 x 36 x 176 x *]] } 000000002E30D7E0: {[conv1.b Gradient[16 x 1]] }

No PreCompute nodes found, skipping PreCompute step.

Starting Epoch 1: learning rate per sample = 0.001000 effective momentum = 0.900000 momentum as time constant = 9.5 samples BlockRandomizer::StartEpoch: epoch 0: frames [0..1](first sequence at sample 0), data subset 0 of 1

Starting minibatch loop.

[CALL STACK] > Microsoft::MSR::CNTK::CuDnnConvolutionEngine:: ForwardCore - Microsoft::MSR::CNTK::ConvolutionNode:: ForwardProp - Microsoft::MSR::CNTK::ComputationNetwork::PARTraversalFlowControlNode:: ForwardProp - Microsoft::MSR::CNTK::ComputationNetwork:: ForwardProp - Microsoft::MSR::CNTK::SGD:: TrainOneEpoch - Microsoft::MSR::CNTK::SGD:: TrainOrAdaptModel - Microsoft::MSR::CNTK::SGD:: Train - DoTrainMicrosoft::MSR::CNTK::ConfigParameters,float
- DoCommands
- wmainOldCNTKConfig - wmain1 - wmain - __tmainCRTStartup - BaseThreadInitThunk - RtlUserThreadStart

EXCEPTION occurred: cuDNN could not find suitable algorithm for the current convolution configuration.

pinzhang10 avatar Jul 25 '16 20:07 pinzhang10

cuDNN does not support all combinations of tensor dimensions and layouts. Unfortunately, the error message does not tell you which node is the problematic one.

Are you compiling yourself from source? Then you could try to set a breakpoint into this error message. By going up the call stack, we should be able to find the name of the node. We can then look up its specific dimensions in the Validation output to understand what the problem is.

The solution would then be to either try to reinterpret the problem to fit into cuDNN's constraints, or to switch to the reference engine, but for this specific node only (keep using cuDNN where it works).

On our side, we should add some code to catch more of the lower-level exceptions and augment then with information on which node they occured.

frankseide avatar Jul 25 '16 21:07 frankseide

Actually, there are only two, should have checked first:

conv1.c: using cuDNN convolution engine for geometry: Input: 32 x 36 x 11, Output: 32 x 36 x 176, Kernel: 5 x 5 x 5, Map: 16, Stride: 1 x 1 x 1, Sharing: (1, 1, 1), AutoPad: (1), LowerPad: 0, UpperPad: 0.
deconv1.out: using cuDNN convolution engine for geometry: Input: 32 x 36 x 11, Output: 32 x 36 x 176, Kernel: 5 x 5 x 5, Map: 16, Stride: 1 x 1 x 1, Sharing: (1, 1, 1), AutoPad: (1), LowerPad: 0, UpperPad: 0.

Let me loop in @Alexey-Kamenev. Alexey, can you see what combination is not supported?

frankseide avatar Jul 25 '16 21:07 frankseide

AFAIR, you would need to use 4D tensors if you want to use 3D convolutions otherwise cuDNN tensor objects will be created in a format that is not supported by cuDNN. Basically, the problem with cuDNN is that it requires that the last (e.g. "input map") dimension of the input should be equal to the corresponding dimension of the kernel. For example, for 2D convos if input is 5x5x3 (WHC notation) then last dimension of the kernel must be equal to 3. Same for 3D convos. Try experimenting first with just one layer to understand whether this works.

Edit: for 4D tensors, just set the last dimension to 1 to make it work with cuDNN.

Alexey-Kamenev avatar Jul 25 '16 22:07 Alexey-Kamenev

@Alexey-Kamenev @frankseide

Thank you for your quick reply. I tried to use 4D tensors and a 3D kernel with the size of 5 by 5 by 5. I used convolution function in the way below: (kW = kH = kD = 5, hStride = vStride = dStride =1)

c = Convolution (W, inp, {kW, kH, kD, 1}, mapCount=outMap, stride={hStride,vStride,dStride,1}, sharing={true,true,true,true}, autoPadding={true,true,true,true}, lowerPad={0, 0, 0, 0}, upperPad={0, 0, 0, 0}, imageLayout="cudnn")

The output shows: "Convolution operation currently only supports 1D or 2D convolution on 3D tensors." . I pasted the output below. The version I compiled was cloned in July.

<<<<<<<<<<<<<<<<<<<< PROCESSED CONFIG WITH ALL VARIABLES RESOLVED <<<<<<<<<<<<<<<<<<<< Commands: train output test Precision = "float" CNTKModelPath: ../Output/Models/04_DeConv CNTKCommandTrainInfo: train : 100 CNTKCommandTrainInfo: CNTKNoMoreCommands_Total : 100

##############################################################################

Action "train"

##############################################################################

CNTKCommandTrainBegin: train LockDevice: Locked GPU 0 to test availability. LockDevice: Unlocked GPU 0 after testing. LockDevice: Locked GPU 0 for exclusive use. NDLBuilder Using GPU 0

Creating virgin network.

Post-processing network...

2 roots: labels = InputValue() mse = SquareError()

Validating network. 13 nodes to process in pass 1.

Validating --> labels = InputValue() : -> [32 x 36 x 11 x 1 x *] Validating --> conv1.W = LearnableParameter() : -> [16 x 125] Validating --> featScale = LearnableParameter() : -> [1 x 1] Validating --> features = InputValue() : -> [32 x 36 x 11 x 1 x *] Validating --> featScaled = ElementTimes (featScale, features) : [1 x 1], [32 x 36 x 11 x 1 x *] -> [32 x 36 x 11 x 1 x *] Validating --> conv1.c = Convolution (conv1.W, featScaled) : [16 x 125], [32 x 36 x 11 x 1 x *] -> [] FAILED

[CALL STACK] > Microsoft::MSR::CNTK::ComputationNetwork:: ValidateNode - Microsoft::MSR::CNTK::ComputationNetwork:: ValidateNodes - Microsoft::MSR::CNTK::ComputationNetwork:: ValidateNetwork - Microsoft::MSR::CNTK::ComputationNetwork:: CompileNetwork - Microsoft::MSR::CNTK::NDLBuilder:: LoadFromConfig - Microsoft::MSR::CNTK::NDLBuilder:: LoadNetworkFromConfig - Microsoft::MSR::CNTK::NDLBuilder:: BuildNetworkFromDescription - <lambda_561694359eb1004edfa06a8ac23f2690>:: operator () - std::_Callable_obj<<lambda_c0a316cc5e512cb27e98243bce3eb140>,0>::_ApplyXstd::shared_ptrMicrosoft::MSR::CNTK::ComputationNetwork,int
- std::_Func_implstd::_Callable_obj<<lambda_dc2d47120f78fb62d722aabb9c05d39f,0>,std::allocatorstd::_Func_class<std::shared_ptrMicrosoft::MSR::CNTK::ComputationNetwork,int>,std::shared_ptrMicrosoft::MSR::CNTK::ComputationNetwork,int>:: _Do_call - std::_Func_classstd::shared_ptrMicrosoft::MSR::CNTK::ComputationNetwork,int:: operator () - Microsoft::MSR::CNTK::SGD:: Train - DoTrainMicrosoft::MSR::CNTK::ConfigParameters,float
- DoCommands
- wmainOldCNTKConfig - wmain1

EXCEPTION occurred: Convolution operation currently only supports 1D or 2D convolution on 3D tensors.

pinzhang10 avatar Jul 26 '16 14:07 pinzhang10

Anybody can help with this?

pinzhang10 avatar Jul 29 '16 19:07 pinzhang10

Hi there, I used the trained weights of CNN in the same structure network in MATLAB, but the result is different from what CNTK eval shows. The Matlab code is ok because i trained same network with eval error near to my CNTK program. Is there any tip to tell me for emigrating learned weights from CNTK to MATLAB?

navidnadery avatar Aug 16 '16 14:08 navidnadery

Hi again, i found more details. For just one convolution neuron the output of convolution mean conv1.c.c is the same as what i calculate by myself. But when i use 3 convolution neurons , the result is not what i calculate by myself.

Here is my values with zero padding convolution. input Value: 1 0.5 2 -0.5 Weight of 3 neurons (conv1.w.W): 0.0007 -0.0018 0.0003 0.0015 0.0010 0.0024 Convolution Result of CNTK (mean: conv1.c.c): -0.0010 0.0021 -0.0043 0.0028 0.0027 0.0044 0.0008 But if you calculate convolution of [1 0.5 2 -0.5] and [0.0007 -0.0018] with zero padding you will find see such a result like: -0.0014 0.0006 -0.0039

Does CNTK calculate differently? I've read the PDF file but i could not find my answer.

navidnadery avatar Aug 17 '16 21:08 navidnadery

I'm really sorry for calling you @frankseide , i was wondering if you could help me please?

navidnadery avatar Aug 18 '16 06:08 navidnadery

Is it possible that the weights are arranged differently? CNTK currently does something confusing:

The weight dimensions are specified as a matrix:

Validating --> conv1.W = LearnableParameter() : -> [16 x 125]

but the tensor dimensions are actually different. Say the convolution has spatial extent (3:5), an input feature-map depth of 7. and an output feature-map depth of '16'. Then the weight would be specified as [16 x 105] (105=3 * 5 * 7), but the actual layout of kernel parameters is [3 x 5 x 7 x 16]. Notice the 16 is at the other end.

This confusing configuration had to do with the transition from our older implementation of convolution to the new one based on NVidia's design and memory layout.

frankseide avatar Aug 18 '16 17:08 frankseide