slice_layer.cpp 5.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*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 "slice_layer.hpp"
45 46
#include <opencv2/core/ocl.hpp>
#include <opencv2/dnn/shape_utils.hpp>
47 48 49 50 51 52

namespace cv
{
namespace dnn
{

53
SliceLayerImpl::SliceLayerImpl(int axis_ /*= 1*/)
54
{
55 56
    axis = axis_;
}
57

58 59 60 61
SliceLayerImpl::SliceLayerImpl(int axis_, const std::vector<int> &sliceIndices_)
{
    axis = axis_;
    sliceIndices = sliceIndices_;
62 63
}

64
void SliceLayerImpl::allocate(const std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
65 66 67
{
    CV_Assert(inputs.size() == 1);

68 69 70 71 72
    const Blob &inpBlob = *inputs[0];
    useOpenCL = ocl::useOpenCL() && inpBlob.getState() == Blob::HEAD_AT_UMAT;

    axisIdx = inpBlob.canonicalAxis(axis);
    int axisSize = inpBlob.size(axisIdx);
73
    BlobShape inpShape = inpBlob.shape();
74
    int allocFlags = useOpenCL ? Blob::ALLOC_UMAT : Blob::ALLOC_MAT;
75

76
    if (sliceIndices.size()) //divide blob with respect to passed parameters
77 78 79 80
    {
        std::vector<int> outAxisSize;
        int prevSlice = 0;

81
        for (size_t i = 0; i < sliceIndices.size(); i++)
82
        {
83 84 85 86 87
            if (!(prevSlice < sliceIndices[i] && sliceIndices[i] < axisSize))
                CV_Error(Error::StsBadArg, "Slice indices should be positive, increased and don't exceed size of sliced dimension");

            outAxisSize.push_back(sliceIndices[i] - prevSlice);
            prevSlice = sliceIndices[i];
88 89 90 91 92 93
        }
        outAxisSize.push_back(axisSize - prevSlice);

        outputs.resize(outAxisSize.size());
        for (size_t i = 0; i < outAxisSize.size(); i++)
        {
94 95
            inpShape[axisIdx] = outAxisSize[i];
            outputs[i].create(inpShape, inpBlob.type(), allocFlags);
96 97 98 99 100 101 102 103 104
        }
    }
    else //divide blob with respect to count of output blobs
    {
        CV_Assert(outputs.size() > 0 && axisSize % outputs.size() == 0);
        int outAxisSize = axisSize / (int)outputs.size();

        for (size_t i = 0; i < outputs.size(); i++)
        {
105 106
            inpShape[axisIdx] = outAxisSize;
            outputs[i].create(inpShape, inpBlob.type(), allocFlags);
107 108 109 110
        }
    }
}

111 112 113 114 115 116 117 118 119 120 121 122
void SliceLayerImpl::forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
{
    #ifdef HAVE_OPENCL
    if (useOpenCL)
        forward_<UMat>(inputs, outputs);
    else
    #endif
        forward_<Mat>(inputs, outputs);
}

template<typename XMat>
void SliceLayerImpl::forward_(std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
123
{
124 125
    const XMat& inpMat = inputs[0]->getRefConst<XMat>();
    std::vector<Range> ranges(inputs[0]->dims(), Range::all());
126

127
    ranges[axisIdx].start = 0;
128 129
    for (size_t i = 0; i < outputs.size(); i++)
    {
130 131 132 133 134
        ranges[axisIdx].end = ranges[axisIdx].start + outputs[i].size(axisIdx);
        inpMat(&ranges[0]).copyTo(outputs[i].getRef<XMat>());
        ranges[axisIdx].start = ranges[axisIdx].end;
    }
}
135

136 137 138 139
Ptr<SliceLayer> SliceLayer::create(int axis)
{
    return Ptr<SliceLayer>(new SliceLayerImpl(axis));
}
140

141 142 143
Ptr<SliceLayer> SliceLayer::create(int axis, const std::vector<int> &sliceIndices)
{
    return Ptr<SliceLayer>(new SliceLayerImpl(axis, sliceIndices));
144 145 146 147
}

}
}