Changed Blob::ptr and Blob::offset methods

pull/265/head
Vitaliy Lyudvichenko 10 years ago
parent 519167ec9e
commit e644b5a358
  1. 14
      modules/dnn/include/opencv2/dnn/blob.hpp
  2. 27
      modules/dnn/include/opencv2/dnn/blob.inl.hpp
  3. 2
      modules/dnn/include/opencv2/dnn/dnn.hpp
  4. 2
      modules/dnn/src/blob.cpp
  5. 12
      modules/dnn/src/layers/convolution_layer.cpp
  6. 2
      modules/dnn/src/layers/elementwise_layers.hpp
  7. 2
      modules/dnn/src/layers/lrn_layer.cpp

@ -67,6 +67,7 @@ namespace dnn
Mat& getMatRef(); Mat& getMatRef();
const Mat& getMatRef() const; const Mat& getMatRef() const;
//TODO: add UMat get methods
Mat getMat(int n, int cn); Mat getMat(int n, int cn);
//shape getters //shape getters
@ -98,13 +99,13 @@ namespace dnn
*/ */
int canonicalAxis(int axis) const; int canonicalAxis(int axis) const;
/** @brief returns real shape of the blob /** @brief returns shape of the blob
*/ */
BlobShape shape() const; BlobShape shape() const;
bool equalShape(const Blob &other) const; bool equalShape(const Blob &other) const;
//shape getters, oriented for 4-dim Blobs processing //shape getters for 4-dim Blobs processing
int cols() const; int cols() const;
int rows() const; int rows() const;
int channels() const; int channels() const;
@ -113,11 +114,14 @@ namespace dnn
Vec4i shape4() const; Vec4i shape4() const;
//CPU data pointer functions //CPU data pointer functions
int offset(int n = 0, int cn = 0, int row = 0, int col = 0) const; template<int n>
uchar *ptrRaw(int n = 0, int cn = 0, int row = 0, int col = 0); size_t offset(const Vec<int, n> &pos) const;
float *ptrf(int n = 0, int cn = 0, int row = 0, int col = 0); size_t offset(int n = 0, int cn = 0, int row = 0, int col = 0) const;
uchar *ptr(int n = 0, int cn = 0, int row = 0, int col = 0);
template<typename TFloat> template<typename TFloat>
TFloat *ptr(int n = 0, int cn = 0, int row = 0, int col = 0); TFloat *ptr(int n = 0, int cn = 0, int row = 0, int col = 0);
float *ptrf(int n = 0, int cn = 0, int row = 0, int col = 0);
//TODO: add const ptr methods
/** @brief share data with other blob and returns *this /** @brief share data with other blob and returns *this
@returns *this @returns *this

@ -158,10 +158,25 @@ inline size_t Blob::total(int startAxis, int endAxis) const
return size; return size;
} }
inline int Blob::offset(int n, int cn, int row, int col) const
template<int n>
inline size_t Blob::offset(const Vec<int, n> &pos) const
{
size_t ofs = 0;
int i;
for (i = 0; i < std::min(n, dims()); i++)
{
CV_DbgAssert(pos[i] >= 0 && pos[i] < size(i));
ofs = ofs * (size_t)size(i) + pos[i];
}
for (; i < dims(); i++)
ofs *= (size_t)size(i);
return ofs;
}
inline size_t Blob::offset(int n, int cn, int row, int col) const
{ {
CV_DbgAssert(0 <= n && n < num() && 0 <= cn && cn < channels() && 0 <= row && row < rows() && 0 <= col && col < cols()); return offset(Vec4i(n, cn, row, col));
return ((n*channels() + cn)*rows() + row)*cols() + col;
} }
inline float *Blob::ptrf(int n, int cn, int row, int col) inline float *Blob::ptrf(int n, int cn, int row, int col)
@ -170,7 +185,7 @@ inline float *Blob::ptrf(int n, int cn, int row, int col)
return (float*)m.data + offset(n, cn, row, col); return (float*)m.data + offset(n, cn, row, col);
} }
inline uchar *Blob::ptrRaw(int n, int cn, int row, int col) inline uchar *Blob::ptr(int n, int cn, int row, int col)
{ {
return m.data + m.elemSize() * offset(n, cn, row, col); return m.data + m.elemSize() * offset(n, cn, row, col);
} }
@ -179,7 +194,7 @@ template<typename TFloat>
inline TFloat* Blob::ptr(int n, int cn, int row, int col) inline TFloat* Blob::ptr(int n, int cn, int row, int col)
{ {
CV_Assert(type() == cv::DataDepth<TFloat>::value); CV_Assert(type() == cv::DataDepth<TFloat>::value);
return (TFloat*) ptrRaw(n, cn, row, col); return (TFloat*) ptr(n, cn, row, col);
} }
inline BlobShape Blob::shape() const inline BlobShape Blob::shape() const
@ -212,7 +227,7 @@ inline const Mat& Blob::getMatRef() const
inline Mat Blob::getMat(int n, int cn) inline Mat Blob::getMat(int n, int cn)
{ {
return Mat(rows(), cols(), m.type(), this->ptrRaw(n, cn)); return Mat(rows(), cols(), m.type(), this->ptr(n, cn));
} }
inline int Blob::cols() const inline int Blob::cols() const

@ -79,7 +79,7 @@ namespace dnn
Ptr<Impl> impl; Ptr<Impl> impl;
}; };
class CV_EXPORTS Importer class Importer
{ {
public: public:

@ -88,7 +88,7 @@ namespace dnn
std::vector<Mat> wrapBuf(dstShape[-3]); std::vector<Mat> wrapBuf(dstShape[-3]);
int elemSize = (int)m.elemSize(); int elemSize = (int)m.elemSize();
uchar *ptr = this->ptrRaw(); uchar *ptr = this->ptr();
for (size_t i = 0; i < inMats.size(); i++) for (size_t i = 0; i < inMats.size(); i++)
{ {
Mat inMat = inMats[i]; Mat inMat = inMats[i];

@ -86,8 +86,8 @@ namespace dnn
{ {
im2col(inpBlob, n, g); im2col(inpBlob, n, g);
Mat kerMat(outGroupCn, ksize, wgtBlob.type(), wgtBlob.ptrRaw(g*outGroupCn)); Mat kerMat(outGroupCn, ksize, wgtBlob.type(), wgtBlob.ptr(g*outGroupCn));
Mat dstMat(outGroupCn, outH*outW, outBlob.type(), outBlob.ptrRaw(n, g*outGroupCn)); Mat dstMat(outGroupCn, outH*outW, outBlob.type(), outBlob.ptr(n, g*outGroupCn));
cv::gemm(kerMat, colMat, 1, noArray(), 0, dstMat); cv::gemm(kerMat, colMat, 1, noArray(), 0, dstMat);
@ -104,7 +104,7 @@ namespace dnn
void ConvolutionLayer::im2col(Blob &inpBlob, int imNum, int cnGroup) void ConvolutionLayer::im2col(Blob &inpBlob, int imNum, int cnGroup)
{ {
uchar *srcPtr = inpBlob.ptrRaw(imNum, cnGroup*inpGroupCn); uchar *srcPtr = inpBlob.ptr(imNum, cnGroup*inpGroupCn);
if (is1x1()) if (is1x1())
{ {
@ -170,13 +170,13 @@ namespace dnn
{ {
for (int g = 0; g < group; g++) for (int g = 0; g < group; g++)
{ {
Mat dstMat(inpGroupCn, inpH*inpW, decnBlob.type(), decnBlob.ptrRaw(n, g*inpGroupCn)); Mat dstMat(inpGroupCn, inpH*inpW, decnBlob.type(), decnBlob.ptr(n, g*inpGroupCn));
if (is1x1()) if (is1x1())
colMat = dstMat; colMat = dstMat;
Mat convMat(outGroupCn, outH*outW, convBlob.type(), convBlob.ptrRaw(n, g*outGroupCn)); Mat convMat(outGroupCn, outH*outW, convBlob.type(), convBlob.ptr(n, g*outGroupCn));
Mat wghtMat(outGroupCn, ksize, wghtBlob.type(), wghtBlob.ptrRaw(g*outGroupCn)); Mat wghtMat(outGroupCn, ksize, wghtBlob.type(), wghtBlob.ptr(g*outGroupCn));
cv::gemm(wghtMat, convMat, 1, noArray(), 0, colMat, GEMM_1_T); cv::gemm(wghtMat, convMat, 1, noArray(), 0, colMat, GEMM_1_T);
col2im(dstMat); col2im(dstMat);

@ -33,7 +33,7 @@ using std::pow;
{ {
for (size_t i = 0; i < inputs.size(); i++) for (size_t i = 0; i < inputs.size(); i++)
{ {
CV_Assert(inputs[i]->ptrRaw() == outputs[i].ptrRaw() && inputs[i]->type() == outputs[i].type()); CV_Assert(inputs[i]->ptr() == outputs[i].ptr() && inputs[i]->type() == outputs[i].type());
size_t size = outputs[i].total(); size_t size = outputs[i].total();

@ -59,7 +59,7 @@ namespace dnn
void LRNLayer::channelNoramlization(Blob &srcBlob, Blob &dstBlob) void LRNLayer::channelNoramlization(Blob &srcBlob, Blob &dstBlob)
{ {
CV_DbgAssert(srcBlob.ptrRaw() != dstBlob.ptrRaw()); CV_DbgAssert(srcBlob.ptr() != dstBlob.ptr());
int num = srcBlob.num(); int num = srcBlob.num();
int channels = srcBlob.channels(); int channels = srcBlob.channels();

Loading…
Cancel
Save