Commit abd998d1 authored by Anna Petrovicheva's avatar Anna Petrovicheva

Added NormalizeBBox layer implementation

parent 55fae8d8
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "../precomp.hpp"
#include "layers_common.hpp"
#include "normalize_bbox_layer.hpp"
#include "op_blas.hpp"
#include <float.h>
#include <algorithm>
namespace cv
{
namespace dnn
{
void NormalizeBBoxLayer::checkParameter(const LayerParams &params, const string &parameterName)
{
if (!params.has(parameterName))
{
CV_Error(Error::StsBadArg, "NormalizeBBox layer parameter does not contain " + parameterName + " index.");
}
}
NormalizeBBoxLayer::NormalizeBBoxLayer(LayerParams &params) : Layer(params)
{
checkParameter(params, "eps");
checkParameter(params, "across_spatial");
checkParameter(params, "channel_shared");
_eps = params.eps();
_across_spatial = params.across_spatial();
_channel_shared = params.channel_shared();
}
void NormalizeBBoxLayer::checkInputs(const std::vector<Blob*> &inputs)
{
CV_Assert(inputs.size() > 0);
for (size_t i = 0; i < inputs.size(); i++)
{
for (size_t j = 0; j < _num_axes; j++)
{
CV_Assert(inputs[i]->shape[j] == inputs[0]->shape[j]);
}
}
CV_Assert(inputs[0]->dims() > 2);
}
void NormalizeBBoxLayer::allocate(const std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
{
checkInputs(inputs);
_num = inputs[0]->num();
_channels = inputs[0]->shape[1];
_rows = inputs[0]->shape[2];
_cols = inputs[0]->shape[3];
_channelSize = _rows * _cols;
_imageSize = _channelSize * _channels;
_buffer = Blob(BlobShape(1, _channels, _rows, _cols));
_buffer_channel = Blob(BlobShape(1, _channels, 1, 1));
_buffer_spatial = Blob(BlobShape(1, 1, _rows, _cols));
if (_across_spatial)
{
_norm = Blob(BlobShape(_num, 1, 1, 1));
}
else
{
_norm = Blob(BlobShape(_num, 1, _rows, _cols));
}
// add eps to avoid overflow
_norm.fill(Scalar(eps));
_sumChannelMultiplier = Blob(BlobShape(1, _channels, 1, 1));
_sumChannelMultiplier.fill(Scalar(1.0));
_sumSpatialMultiplier = Blob(BlobShape(1, 1, _rows, _cols));
_sumSpatialMultiplier.fill(Scalar(1.0));
if (_channel_shared)
{
_scale = Blob(BlobShape(1, 1, 1, 1));
}
else
{
_scale = Blob(BlobShape(1, 1, 1, _channels));
}
for(size_t i = 0; i < inputs.size(); i++)
{
outputs[i].create(BlobShape(inputs[0]->shape()));
}
}
void NormalizeBBoxLayer::forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
{
Mat zeroBuffer = Mat(_buffer.matRef().size, _buffer.matRef().type(), Scalar(0));
Mat sumAbs;
for (size_t j = 0; j < inputs.size(); j++)
{
for (int n = 0; n < _num; ++n)
{
Blob src(BlobShape(1, _channels, _rows, _cols));
src.ptrf() = inputs[j]->ptrf(n);
Blob dst(BlobShape(1, _channels, _rows, _cols));
dst.ptrf() = outputs[j]->ptrf(n);
Mat normCurrent = _norm.ptrf(n);
cv::sqrt(src.matRefConst(), _buffer.matRef());
if (_across_spatial)
{
absdiff(_buffer.matRef(), zeroBuffer, sumAbs);
// add eps to avoid overflow
sumAbs += _eps;
pow(sumAbs, 0.5f, normCurrent);
dst.matRef() = src.matRef() / normCurrent;
}
else
{
gemmCPU(_buffer.matRef(), _sumChannelMultiplier.matRef(), 1, normCurrent, GEMM_1_T & GEMM_2_T);
// compute norm
pow(normCurrent, 0.5f, normCurrent);
// scale the layer
gemmCPU(_sumChannelMultiplier.matRef(), normCurrent, 1, _buffer.matRef(), 0);
dst.matRef() = src.matRef() / _buffer.matRef().at<float>(0, 0);
}
// scale the output
if (_channel_shared)
{
dst.matRef() *= _scale.matRef();
}
else
{
gemmCPU(_scale.matRef(), _sumSpatialMultiplier.matRef(), 1, _buffer.matRef(), 0);
dst.matRef() *= _buffer.matRef();
}
}
}
}
}
}
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_DNN_LAYERS_NORMALIZEBBOX_LAYER_HPP__
#define __OPENCV_DNN_LAYERS_NORMALIZEBBOX_LAYER_HPP__
#include "../precomp.hpp"
namespace cv
{
namespace dnn
{
class NormalizeBBoxLayer : public Layer
{
Blob _buffer;
Blob _buffer_channel;
Blob _buffer_spatial;
Blob _norm;
Blob _sumChannelMultiplier;
Blob _sumSpatialMultiplier;
Blob _scale;
bool _across_spatial;
double _eps;
bool _channel_shared;
size_t _num;
size_t _channels;
size_t _rows;
size_t _cols;
size_t _channelSize;
size_t _imageSize;
static const size_t _num_axes = 4;
public:
NormalizeBBoxLayer(LayerParams &params);
void allocate(const std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
void forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
void checkParameter(const LayerParams &params, const string &parameterName);
void checkInputs(const std::vector<Blob*> &inputs);
};
}
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment