changed sample 02_Convolution to use imageLayout to enable cudnn use;

and fixed a refactoring bug in ImageParameter NDL definition
This commit is contained in:
Frank Seide 2016-01-29 22:21:31 -08:00 коммит произвёл Philipp Kranen
Родитель 926a081e84
Коммит aa90af35ac
8 изменённых файлов: 25 добавлений и 41 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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)
@ -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
@ -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

Просмотреть файл

@ -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)
]

Просмотреть файл

@ -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"));
@ -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);

Просмотреть файл

@ -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);

Просмотреть файл

@ -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;

Просмотреть файл

@ -198,20 +198,6 @@ public:
#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);
@ -224,11 +210,11 @@ public:
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;

Просмотреть файл

@ -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 $?