Skip to content

Commit

Permalink
changed sample 02_Convolution to use imageLayout to enable cudnn use;
Browse files Browse the repository at this point in the history
and fixed a refactoring bug in ImageParameter NDL definition
  • Loading branch information
frankseide authored and pkranen committed Jan 30, 2016
1 parent 24411c2 commit a4f5b7a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 41 deletions.
10 changes: 7 additions & 3 deletions Examples/Image/MNIST/Config/02_Convolution.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
RootDir = ".."

ConfigDir = "$RootDir$/Config"
DataDir = "$RootDir$/Data"
DataDir = "$RootDir$/Data"
OutputDir = "$RootDir$/Output"
ModelDir = "$OutputDir$/Models"
ModelDir = "$OutputDir$/Models"

deviceId = "auto"
deviceId = 0
imageLayout = "cudnn"
# override the above as follows when running from CPU:
#deviceId = -1
#imageLayout = "legacy"

command = train:test

Expand Down
6 changes: 3 additions & 3 deletions Examples/Image/MNIST/Config/02_Convolution.ndl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ndlMnistMacros = [
imageH = 28
labelDim = 10

features = ImageInput(imageW, imageH, 1)
features = ImageInput(imageW, imageH, 1, imageLayout=$imageLayout$)
featScale = Const(0.00390625)
featScaled = Scale(featScale, features)
labels = Input(labelDim)
Expand All @@ -31,7 +31,7 @@ DNN=[
pool1H = 2
pool1hStride = 2
pool1vStride = 2
pool1 = MaxPooling(conv1_act, pool1W, pool1H, pool1hStride, pool1vStride)
pool1 = MaxPooling(conv1_act, pool1W, pool1H, pool1hStride, pool1vStride, imageLayout=$imageLayout$)

# conv2
kW2 = 5
Expand All @@ -48,7 +48,7 @@ DNN=[
pool2H = 2
pool2hStride = 2
pool2vStride = 2
pool2 = MaxPooling(conv2_act, pool2W, pool2H, pool2hStride, pool2vStride)
pool2 = MaxPooling(conv2_act, pool2W, pool2H, pool2hStride, pool2vStride, imageLayout=$imageLayout$)

h1Dim = 128
# DNNSigmoidLayer and DNNLayer are defined in Macros.ndl
Expand Down
13 changes: 2 additions & 11 deletions Examples/Image/MNIST/Config/Macros.ndl
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@ DNNLayer(inDim, outDim, x, parmScale) = [

ConvReLULayer(inp, outMap, inWCount, kW, kH, hStride, vStride, wScale, bValue) = [
convW = Parameter(outMap, inWCount, init="uniform", initValueScale=wScale)
convB = Parameter(outMap, 1, init="fixedValue", value=bValue)
conv = Convolution(convW, inp, kW, kH, outMap, hStride, vStride, zeroPadding=false)
convB = ImageParameter(1, 1, outMap, init="fixedValue", value=bValue, imageLayout=$imageLayout$)
conv = Convolution(convW, inp, kW, kH, outMap, hStride, vStride, zeroPadding=false, imageLayout=$imageLayout$)
convPlusB = Plus(conv, convB);
act = RectifiedLinear(convPlusB);
]

BatchNorm(dim, x, scaleInit, biasInit) = [
m = Mean(x)
isd = InvStdDev(x)
norm = ColumnElementTimes(Minus(x, m), isd)
sc = Parameter(dim, 1, init="uniform", initValueScale=scaleInit)
b = Parameter(dim, 1, init="uniform", initValueScale=biasInit)
bn_norm = Plus(ColumnElementTimes(norm, sc), b)
]
6 changes: 2 additions & 4 deletions Source/CNTK/SynchronousExecutionEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ void SynchronousNodeEvaluator<ElemType>::Evaluate(NDLNode<ElemType>* node, const
{
// evaluate only scalar parameters
vector<void*> params = EvaluateParameters(node, baseName, 0, parameter.size(), pass);
size_t imageWidth = ((NDLNode<ElemType>*) params[0])->GetScalar();
size_t imageHeight = ((NDLNode<ElemType>*) params[1])->GetScalar();
size_t imageWidth = ((NDLNode<ElemType>*) params[0])->GetScalar();
size_t imageHeight = ((NDLNode<ElemType>*) params[1])->GetScalar();
size_t imageChannels = ((NDLNode<ElemType>*) params[2])->GetScalar();
ImageLayoutKind imageLayoutKind = ImageLayoutKindFrom(node->GetOptionalParameter("imageLayout", "HWC"));

Expand Down Expand Up @@ -123,8 +123,6 @@ void SynchronousNodeEvaluator<ElemType>::Evaluate(NDLNode<ElemType>* node, const
vector<void*> params = EvaluateParameters(node, baseName, 0, parameter.size(), pass);
size_t i = 0;
auto tensorShape = ProcessTensorShapeParameters(node, params, i, isImage, cnNodeType);
if (isImage)
tensorShape.AppendInPlace(3, 1); // this goes into the column dimension
bool needGradient = node->GetOptionalParameter("needGradient", "true");

nodePtr = builder.CreateLearnableParameter(name, tensorShape);
Expand Down
2 changes: 1 addition & 1 deletion Source/Common/Include/Basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ __declspec_noreturn static inline void ThrowFormatted(const char* format, ...)
va_start(args, format);
vsprintf(buffer, format, args);
#ifdef _DEBUG // print this to log before throwing, so we can see what the error is
fprintf(stderr, "About to throw exception '%s'\n", buffer);
fprintf(stderr, "\nAbout to throw exception '%s'\n", buffer);
#endif
Microsoft::MSR::CNTK::DebugUtil::PrintCallStack();
throw E(buffer);
Expand Down
2 changes: 1 addition & 1 deletion Source/Common/Include/TensorShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ static inline ImageLayoutKind ImageLayoutKindFrom(const wstring& s)
}

// interpret TensorShape as an image descriptor
// considering that we support two ways of storingimages
// considering that we support two ways of storing images
struct ImageDimensions
{
size_t m_width, m_height, m_numChannels;
Expand Down
20 changes: 3 additions & 17 deletions Source/ComputationNetworkLib/ConvolutionalNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,6 @@ class ConvolutionNode : public ComputationNode<ElemType>, public NumInputs<2>
#endif
}

// BUGBUG: Should not be here. Use PlusNode and m_sampleLayout. TODO: Bad naming:'output' is actually an 'input'
void AddBias(const Matrix<ElemType>& output, const Matrix<ElemType>& bias, Matrix<ElemType>& dst)
{
assert(m_convEng != nullptr);
m_convEng->AddBias(*m_outT, output, *m_biasT, bias, dst);
}

void BackwardBias(const Matrix<ElemType>& srcGrad, Matrix<ElemType>& biasGrad)
{
assert(m_convEng != nullptr);
m_convEng->BackwardBias(*m_outT, srcGrad, *m_biasT, biasGrad);
}

// note: this also infers dimensions from chilren
void /*ComputationNodeBase::*/ Validate(bool isFinalValidationPass) override
{
Base::Validate(isFinalValidationPass);
Expand All @@ -224,11 +210,11 @@ class ConvolutionNode : public ComputationNode<ElemType>, public NumInputs<2>
InvalidArgument("%ls %ls operation requires that input width be >= kernelWidth and input height >= kernelHeight.", NodeName().c_str(), OperationName().c_str());

// determine output tensor shape
const int kernelWidthCenter = m_zeroPadding ? m_kernelWidth % 2 : m_kernelWidth;
const int kernelWidthCenter = m_zeroPadding ? m_kernelWidth % 2 : m_kernelWidth;
const int kernelHeightCenter = m_zeroPadding ? m_kernelHeight % 2 : m_kernelHeight;
auto outDims = ImageDimensions(
(inDims.m_width - kernelWidthCenter) / m_horizontalSubsample + 1,
(inDims.m_height - kernelHeightCenter) / m_verticalSubsample + 1,
(inDims.m_width - kernelWidthCenter) / m_horizontalSubsample + 1,
(inDims.m_height - kernelHeightCenter) / m_verticalSubsample + 1,
m_outputChannels);

size_t weightCols = m_kernelWidth * m_kernelHeight * inDims.m_numChannels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ else
fi

# cntkrun <CNTK config file name> <additional CNTK args>
cntkrun 02_Convolution.config "train=[reader=[file=$CleanDataDir/Train.txt]] test=[reader=[file=$CleanDataDir/Test.txt]] train=[SGD=[maxEpochs=1]] train=[SGD=[epochSize=128]] train=[reader=[randomize=none]]" || exit $?
if [ "$TEST_DEVICE" == "cpu" ]; then
imageLayout='"legacy"'
else
imageLayout='"cudnn"'
fi

cntkrun 02_Convolution.config "train=[reader=[file=$CleanDataDir/Train.txt]] test=[reader=[file=$CleanDataDir/Test.txt]] train=[SGD=[maxEpochs=1]] train=[SGD=[epochSize=128]] train=[reader=[randomize=none]] imageLayout=$imageLayout" || exit $?

0 comments on commit a4f5b7a

Please sign in to comment.