Reshape dimension format
I want to reshape a stacked vector (581-by-1 vector, a row stack of seven 83-by-1 matrix) into 83-by-7 matrix (and only one channel). Is Reshape(inputVector, 581, imageWidth=7, imageHeight=83) the right way to do so?
When I read the log, it's a little bit confusing:
LegacyReshape(featNorm.xNorm[581 x *], NumOfRows=581, imageWidth=7, imageHeight=83, imageChannels=1) -> [1 x 7 x 83 x *]
By convention, should't the resulting matrix be [1 x 83 x 7 x *] (#row-by-#column)?
Thank you
It's a bug. Like all image-related operations, Reshape() should interpret the 'imageLayout' parameter, but it doesn't. It just defaults to a "legacy" mapping of W,H,C that is different from the usual definition:
if (imageLayoutKind == ImageLayoutKind::CHW)
return TensorShape(width, height, numChannels); // common layout, e.g. cudnn
else if (imageLayoutKind == ImageLayoutKind::HWC)
return TensorShape(numChannels, width, height); // legacy layout
Please try to use
..., numChannels=83, imageWidth=7, imageHeight=1, imageLayout="legacy") # order is a workaround; "legacy" layout maps this to [83 x 7 x 1]
That additional imageLayout parameter is the one that is currently ignored, but I suggest to add it anyway, so that your script continues to work once this gets fixed.
Please let me know if this works, and sorry for the bug.
Thanks for replying. Unfortunately it doesn't work. I think I still need to set imageChannels = 1. What I'm not sure is that if I should set imageWidth=7 or 83 in my case. If CNTK is column-first I supposed to reshape my vector into 83-by-7 matrix, which I suppose to have imageChannels=1, imageHeight=83, imageWidth=7, and has dimension [1 x 83 x 7 x *], while CNTK shows something else.
Here's the error information of setting imageChannels to 83:
Validating --> featReshape = LegacyReshape(featNorm.xNorm[581 x *], NumOfRows=581, imageWidth=7, imageHeight=1, imageChannels=83) -> [83 x 7 x 1 x *]
Validating --> convFeat.conv = Convolution(convFeat.convW[256 x 40], featReshape[83 x 7 x 1 x * {W=7, H=1, C=83}]) -> [256 x 7 x 2 x *]
Validating --> convFeat.convB = LearnableParameter -> [256 x 1]
Validating --> convFeat.convPlusB = Plus(convFeat.conv[256 x 7 x 2 x * {W=7, H=2, C=256}], convFeat.convB[256 x 1]) -> [256 x 7 x 2 x *]
Validating --> convFeat.act = RectifiedLinear(convFeat.convPlusB[256 x 7 x 2 x * {W=7, H=2, C=256}]) -> [256 x 7 x 2 x *]
Validating --> pool1output = MaxPooling(convFeat.act[256 x 7 x 2 x * {W=7, H=2, C=256}]) -> [256 x 1 x 6148914691236517206 x *]
Somehow my e-mail reply does not show on Github, so let me paste it here:
Validating --> featReshape = LegacyReshape(featNorm.xNorm[581 x *], NumOfRows=581, imageWidth=7, imageHeight=1, imageChannels=83) -> [83 x 7 x 1 x *]
“[83 x 7 x 1 x *]” says that you have successfully reshaped your vector to 83 x 7 (x 1). In memory, that is 7 vectors of 83 components concatenated. CNTK is column-major.
The imageWidth etc. parameters in LegacyReshape are incorrectly named if you are using the cuDNN convolution, which is what is recommended. Please ignore the names, what matters is the numeric tensor shape. That is [83 x 7…] which is correct if I understand you right.
To make sure you use cuDNN, please verify that in all convolution, pooling, and image parameter/input definitions, you say imageLayout=”cudnn”.
While I am able to reshape that to 83 x 7 (x 1), some error still occurs which I think is due to incorrect naming of arguments...
The imageWidth etc. parameters in LegacyReshape are incorrectly named
So I guess in Convolution(w, image, kernelWidth, kernelHeight, ...), kernelWidth actually refers imageChannels dimension, while kernelHeight refers to imageWidth dimension.
Likewise in MaxPooling(m, windowWidth, windowHeight, stepW, stepH) windowWidth/stepW refers to imageChannels dimension
Is that correct?
Code:
convFeat = Convolution(convW, featReshape, 5, 8, 256, ..., imageLayout = "cudnn")
pool1output = MaxPooling(convFeat, 7, 3, 7, 3, imageLayout = "cudnn")
Log:
convFeat = Convolution(convFeat.convW[256 x 40], featReshape[83 x 7 x 1 x * {W=7, H=1, C=83}]) -> [83 x 8 x 256 x *]
pool1output = MaxPooling(convFeat[83 x 8 x 256 x * {W=8, H=256, C=83}]) -> [11 x 2 x 256 x *]
convFeat is supposed to be [83+(kernelHeight+1)%2 x 7+(kernelWidth+1)%2 x 256 x *], which is [84 x 7 x 256 x *]
Thanks!
Hi, I also met a problem with Reshape(), but it was different from mhng1580's problem.
I want to reshape a 1599x1 vector into a 39x41 matrix. I noticed the sequence of arguments in Reshape() discussed above and I wrote
features=Input(1599, tag=feature) features_img = Reshape(features,1599,41,1,39)
However, the log showed
Validating --> features = InputValue -> [1599 x *] Validating --> features_img = LegacyReshape(features[1599 x *], NumOfRows=1599, imageWidth=0, imageHeight=0, imageChannels=0) -> [1 x 1599 x 1 x *]
I am surprised because imageWidth, imageHeight, imageChannels are all zeros in the log. I don't know where is wrong.
Could you give me some advice? Thanks.
These are optional parameters, you must specify them with their names, e.g
features=Input(1599, tag=feature) features_img = Reshape(features,1599,imageChannels=41,imageWidth=39,imageHeight=1, imageLayout="legacy)
(I am fixing this bug now, so soon there should be no confusion.)
Thanks, I have tried that, but there is still a bug.
`Validating --> features = InputValue -> [1599 x *]
Validating --> features_img = LegacyReshape(features[1599 x ], NumOfRows=1599, imageWidth=39, imageHeight=1, imageChannels=41) -> [41 x 39 x 1 x *]
It seemed that the network built up correctly, but an exception raised as followed.
[CALL STACK]
/home/zhaoyue/CNTK/build/release/bin/../lib/libcntkmath.so ( Microsoft::MSR::CNTK::ExceptionWithCallStackstd::logic_error::CollectCallStack(std::function<void (std::string)> const&, std::function<void ()> const&) + 0x101 ) [0x7f7b21df9cc1]
/home/zhaoyue/CNTK/build/release/bin/../lib/libcntkmath.so ( Microsoft::MSR::CNTK::ExceptionWithCallStackstd::logic_error::PrintCallStack() + 0x88 ) [0x7f7b21dfa158]
/home/zhaoyue/CNTK/build/release/bin/cntk ( void Microsoft::MSR::CNTK::ThrowFormattedstd::logic_error(char const, ...) + 0xe2 ) [0x5437a2]
/home/zhaoyue/CNTK/build/release/bin/cntk ( Microsoft::MSR::CNTK::ComputationNode
EXCEPTION occurred: VerifySize: expected matrix size 41 x 39, but it is 1599 x 1 ` Is there any solution to this problem?
This sounds like Issue #181, which has been fixed in current master branch. How old is your version?
BTW I think you don't even need the Reshape. You could use ImageInput instead of InputValue, which takes three dimension arguments: ImageInput (41, 39, 1, imageLayout="cudnn").
@frankseide
Hi, I am doing semantic segmentation and reshaping the labels into the same size as the input image. reshaping. I am using the following code. I think there is something wrong, because the mse in all iteration are all 0. Do you have any idea what might be the reason?
labels = InputValue(labelDim) ground = Reshape(labels, labelDim, imageWidth = imageW, imageHeight = imageH, imageChannels = imageC, imageLayout="CHW") . . . . mse = SquareError(ground , deconv1)
Thank you!
There have been lots of improvements in this area over the last 12 month. If you still run into problems with the most recent version, please open a new issue with a detailed repro case