Commit 942e9205 authored by Vitaliy Lyudvichenko's avatar Vitaliy Lyudvichenko

Fixing of reshape layer to pass Torch test

parent 231546b7
...@@ -364,9 +364,15 @@ BlobShape computeShapeByReshapeMask(const BlobShape &srcShape, const BlobShape & ...@@ -364,9 +364,15 @@ BlobShape computeShapeByReshapeMask(const BlobShape &srcShape, const BlobShape &
{ {
if (srcRange == Range::all()) if (srcRange == Range::all())
srcRange = Range(0, srcShape.dims()); srcRange = Range(0, srcShape.dims());
else
{
int sz = srcRange.size();
srcRange.start = srcShape.canonicalAxis(srcRange.start);
srcRange.end = (srcRange.end == INT_MAX) ? srcShape.dims() : srcRange.start + sz;
}
CV_Assert(0 <= srcRange.start && srcRange.start <= srcRange.end && srcRange.end <= srcShape.dims()); CV_Assert(0 <= srcRange.start && srcRange.start <= srcRange.end && srcRange.end <= srcShape.dims());
Shape dstShape(srcShape.dims() - srcRange.size() + maskShape.dims(), nullptr); BlobShape dstShape(srcShape.dims() - srcRange.size() + maskShape.dims(), (const int*)NULL);
std::copy(srcShape.ptr(), srcShape.ptr() + srcRange.start, dstShape.ptr()); std::copy(srcShape.ptr(), srcShape.ptr() + srcRange.start, dstShape.ptr());
std::copy(srcShape.ptr() + srcRange.end, srcShape.ptr() + srcShape.dims(), dstShape.ptr() + srcRange.start + maskShape.dims()); std::copy(srcShape.ptr() + srcRange.end, srcShape.ptr() + srcShape.dims(), dstShape.ptr() + srcRange.start + maskShape.dims());
......
#include "../precomp.hpp" #include "../precomp.hpp"
#include "layer_loaders.hpp" #include "layer_loaders.hpp"
#include <opencv2/dnn/shape_utils.hpp> #include <opencv2/dnn/shape_utils.hpp>
#include <climits>
namespace cv namespace cv
{ {
...@@ -180,13 +181,13 @@ Ptr<Layer> createLayerFromCaffe<ReshapeLayer>(LayerParams &params) ...@@ -180,13 +181,13 @@ Ptr<Layer> createLayerFromCaffe<ReshapeLayer>(LayerParams &params)
int axis = params.get<int>("axis", 0); int axis = params.get<int>("axis", 0);
int numAxes = params.get<int>("num_axes", -1); int numAxes = params.get<int>("num_axes", -1);
CV_Assert(numAxes >= -1); CV_Assert(numAxes >= -1);
Range applyingRange = (numAxes == -1) ? Range::all() : Range(axis, axis + numAxes); Range applyingRange = (numAxes == -1) ? Range(axis, INT_MAX) : Range(axis, axis + numAxes);
Shape newShape; Shape newShape;
if (params.has("dim")) if (params.has("dim"))
{ {
const DictValue &paramShape = params.get("dim"); const DictValue &paramShape = params.get("dim");
newShape = Shape(paramShape.size(), nullptr); newShape = Shape::all(paramShape.size());
for (int i = 0; i < paramShape.size(); i++) for (int i = 0; i < paramShape.size(); i++)
newShape[i] = paramShape.get<int>(i); newShape[i] = paramShape.get<int>(i);
} }
......
...@@ -89,10 +89,12 @@ void FullyConnectedLayerImpl::allocate(const std::vector<Blob*> &input, std::vec ...@@ -89,10 +89,12 @@ void FullyConnectedLayerImpl::allocate(const std::vector<Blob*> &input, std::vec
void FullyConnectedLayerImpl::forward(std::vector<Blob*> &input, std::vector<Blob> &output) void FullyConnectedLayerImpl::forward(std::vector<Blob*> &input, std::vector<Blob> &output)
{ {
if (!useOpenCL) #ifdef HAVE_OPENCL
forward_<Mat>(input, output); if (useOpenCL)
else
forward_<UMat>(input, output); forward_<UMat>(input, output);
else
#endif
forward_<Mat>(input, output);
} }
template<typename XMat> template<typename XMat>
......
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
#include <opencv2/core/ocl.hpp> #include <opencv2/core/ocl.hpp>
#include <opencv2/dnn/shape_utils.hpp> #include <opencv2/dnn/shape_utils.hpp>
#include <algorithm> #include <algorithm>
#include <type_traits>
namespace cv namespace cv
{ {
...@@ -205,6 +204,21 @@ void LRNLayerImpl::spatialNormalization(Blob &src, Blob &dst) ...@@ -205,6 +204,21 @@ void LRNLayerImpl::spatialNormalization(Blob &src, Blob &dst)
spatialNormalization_<UMat>(src, dst); spatialNormalization_<UMat>(src, dst);
} }
//TODO: fix cv::boxFilter with BORDER_ISOLATED flag in CPU mode
template<>
void LRNLayerImpl::sqrBoxFilter_<Mat>(const Mat &src, Mat &dst)
{
Mat bufMat = buf.getRef<Mat>();
src.copyTo(bufMat);
cv::sqrBoxFilter(bufMat, dst, dst.depth(), Size(size, size), Point(-1, -1), false, BORDER_CONSTANT);
}
template<>
void LRNLayerImpl::sqrBoxFilter_<UMat>(const UMat &src, UMat &dst)
{
cv::sqrBoxFilter(src, dst, dst.depth(), Size(size, size), Point(-1, -1), false, BORDER_CONSTANT | BORDER_ISOLATED);
}
template<typename XMat> template<typename XMat>
void LRNLayerImpl::spatialNormalization_(Blob &srcBlob, Blob &dstBlob) void LRNLayerImpl::spatialNormalization_(Blob &srcBlob, Blob &dstBlob)
{ {
...@@ -221,17 +235,7 @@ void LRNLayerImpl::spatialNormalization_(Blob &srcBlob, Blob &dstBlob) ...@@ -221,17 +235,7 @@ void LRNLayerImpl::spatialNormalization_(Blob &srcBlob, Blob &dstBlob)
XMat src = getPlane(srcMat, n, cn); XMat src = getPlane(srcMat, n, cn);
XMat dst = getPlane(dstMat, n, cn); XMat dst = getPlane(dstMat, n, cn);
if (std::is_same<XMat, UMat>::value) sqrBoxFilter_(src, dst);
{
cv::sqrBoxFilter(src, dst, dst.depth(), Size(size, size), Point(-1, -1), false, BORDER_CONSTANT | BORDER_ISOLATED);
}
else
{
//TODO: fix cv::boxFilter with BORDER_ISOLATED flag in CPU mode
Mat bufMat = buf.getRef<Mat>();
src.copyTo(bufMat);
cv::sqrBoxFilter(bufMat, dst, dst.depth(), Size(size, size), Point(-1, -1), false, BORDER_CONSTANT);
}
dst.convertTo(dst, dst.type(), alpha/(size*size), 1); dst.convertTo(dst, dst.type(), alpha/(size*size), 1);
cv::pow(dst, beta, dst); cv::pow(dst, beta, dst);
......
...@@ -62,6 +62,8 @@ class LRNLayerImpl : public LRNLayer ...@@ -62,6 +62,8 @@ class LRNLayerImpl : public LRNLayer
void spatialNormalization(Blob &src, Blob &dst); void spatialNormalization(Blob &src, Blob &dst);
template<typename XMat> template<typename XMat>
void spatialNormalization_(Blob &src, Blob &dst); void spatialNormalization_(Blob &src, Blob &dst);
template<typename XMat>
void sqrBoxFilter_(const XMat &src, XMat &dst);
public: public:
......
...@@ -52,6 +52,12 @@ namespace dnn { ...@@ -52,6 +52,12 @@ namespace dnn {
#if defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER #if defined(ENABLE_TORCH_IMPORTER) && ENABLE_TORCH_IMPORTER
#include "THDiskFile.h" #include "THDiskFile.h"
#ifdef NDEBUG
static bool dbgPrint = false;
#else
static bool dbgPrint = true;
#endif
enum LuaType enum LuaType
{ {
TYPE_NIL = 0, TYPE_NIL = 0,
...@@ -290,6 +296,7 @@ struct TorchImporter : public ::cv::dnn::Importer ...@@ -290,6 +296,7 @@ struct TorchImporter : public ::cv::dnn::Importer
} }
String key = readString(); String key = readString();
if (dbgPrint)
std::cout << i << "th key: " << key << "\n"; std::cout << i << "th key: " << key << "\n";
fpos = THFile_position(file); fpos = THFile_position(file);
...@@ -334,6 +341,8 @@ struct TorchImporter : public ::cv::dnn::Importer ...@@ -334,6 +341,8 @@ struct TorchImporter : public ::cv::dnn::Importer
} }
//Debug output //Debug output
if (dbgPrint)
{
std::cout << "scalarParams:\n"; std::cout << "scalarParams:\n";
std::cout << scalarParams; std::cout << scalarParams;
...@@ -342,6 +351,7 @@ struct TorchImporter : public ::cv::dnn::Importer ...@@ -342,6 +351,7 @@ struct TorchImporter : public ::cv::dnn::Importer
for (it = tensorParams.begin(); it != tensorParams.end(); it++) for (it = tensorParams.begin(); it != tensorParams.end(); it++)
std::cout << it->first << ": Tensor " << it->second.shape() << "\n"; std::cout << it->first << ": Tensor " << it->second.shape() << "\n";
} }
}
void readTorchTensor(int indexTensor, int typeTensor) void readTorchTensor(int indexTensor, int typeTensor)
{ {
...@@ -435,6 +445,8 @@ struct TorchImporter : public ::cv::dnn::Importer ...@@ -435,6 +445,8 @@ struct TorchImporter : public ::cv::dnn::Importer
String className = readTorchClassName(); String className = readTorchClassName();
String nnName; String nnName;
if (dbgPrint)
std::cout << "Class: " << className << std::endl; std::cout << "Class: " << className << std::endl;
int type; int type;
......
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