PrepareInput::PrepareInput(Buffer* dataMean, bool useCenterOnly, bool needsFlip, bool doRandomSample, int imageSize, int rescaledSize, bool isMeanChanneled) : _useCenterOnly(useCenterOnly), _needsFlip(needsFlip), _doRandomSample(doRandomSample), _imageSize(imageSize), _rescaledSize(rescaledSize) { assert(dataMean != NULL); Dimensions expectedDims(_rescaledSize, _rescaledSize, kOutputChannels); dataMean->reshape(expectedDims); Dimensions outputDims(_imageSize, _imageSize, kOutputChannels); _dataMean = new Buffer(outputDims); const int deltaX = (_rescaledSize - _imageSize); const int deltaY = (_rescaledSize - _imageSize); const int marginX = (deltaX / 2); const int marginY = (deltaY / 2); if (isMeanChanneled) { Buffer* fromChanneled = convert_from_channeled_rgb_image(dataMean); crop_and_flip_image(_dataMean, fromChanneled, marginX, marginY, false); delete fromChanneled; } else { crop_and_flip_image(_dataMean, dataMean, marginX, marginY, false); } _dataMean->setName("_dataMean"); setClassName("PrepareInput"); }
void Foam::equationReader::checkFinalDimensions ( const label& equationIndex, dimensionSet& expectedDimensions, const word& outputName ) const { const equation& eqn(operator[](equationIndex)); dimensionSet outputDims(evaluateDimensions(equationIndex)); if ((outputDims != expectedDimensions) && (dimensionSet::debug)) { WarningIn("equationReader::checkFinalDimenions") << "Dimension error thrown for equation " << eqn.name() << ". Output dimensions: " << outputDims << "do not match " << "dimensions of destination field " << outputName << ", " << expectedDimensions << ". You can initialize " << outputName << "'s dimensions with:" << token::NL << token::TAB << outputName << ".dimensions().reset" << token::NL << token::TAB << "(" << token::NL << token::TAB << token::TAB << "equationReaderObject.evaluateDimensions(" << equationIndex << "))" << endl; } expectedDimensions = outputDims; }
Buffer* PrepareInput::run(Buffer* input) { if (_output != NULL) { delete _output; } Dimensions rescaledDims(_rescaledSize, _rescaledSize, kOutputChannels); Buffer* rescaled = new Buffer(rescaledDims); rescaled->setName("rescaled"); input->setName("input"); rescale_image_to_fit(input, rescaled, _needsFlip); const int deltaX = (_rescaledSize - _imageSize); const int deltaY = (_rescaledSize - _imageSize); const int marginX = (deltaX / 2); const int marginY = (deltaY / 2); if (_useCenterOnly) { Dimensions outputDims(1, _imageSize, _imageSize, kOutputChannels); _output = new Buffer(outputDims); _output->setName("prepareInput_output"); int sourceX; int sourceY; if (_doRandomSample) { sourceX = (int)(rand() * (deltaX / (float)(RAND_MAX))); sourceY = (int)(rand() * (deltaY / (float)(RAND_MAX))); } else { sourceX = marginX; sourceY = marginY; } Buffer* blitDestination = buffer_view_at_top_index(_output, 0); crop_and_flip_image(blitDestination, rescaled, sourceX, sourceY, false); matrix_add_inplace(blitDestination, _dataMean, -1.0f); } else { Dimensions outputDims(10, _imageSize, _imageSize, kOutputChannels); _output = new Buffer(outputDims); _output->setName("prepareInput_output"); for (int flipPass = 0; flipPass < 2; flipPass += 1) { const bool doFlip = (flipPass == 1); Buffer* blitDestination = buffer_view_at_top_index(_output, (flipPass * 5)); crop_and_flip_image(blitDestination, rescaled, marginX, marginY, doFlip); for (int yIndex = 0; yIndex < 2; yIndex += 1) { for (int xIndex = 0; xIndex < 2; xIndex += 1) { const int viewIndex = ((flipPass * 5) + (yIndex * 2) + xIndex + 1); Buffer* blitDestination = buffer_view_at_top_index(_output, viewIndex); const int sourceX = (xIndex * deltaX); const int sourceY = (yIndex * deltaY); crop_and_flip_image(blitDestination, rescaled, sourceX, sourceY, doFlip); } } } } delete rescaled; return _output; }