softmax_layer.cpp 7.22 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 "softmax_layer.hpp"
45
#include <opencv2/core/ocl.hpp>
Alexander Alekhin's avatar
Alexander Alekhin committed
46
#include "opencl_kernels_dnn.hpp"
47 48 49 50 51 52 53 54
#include <algorithm>
#include <stdlib.h>
using std::max;

namespace cv
{
namespace dnn
{
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

SoftMaxLayerImpl::SoftMaxLayerImpl(int axis)
{
    axisRaw = axis;
}

void SoftMaxLayerImpl::allocate(const std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
{
    CV_Assert(inputs.size() == 1);
    axis = inputs[0]->canonicalAxis(axisRaw);

    useOpenCL = ocl::useOpenCL();

    BlobShape shape = inputs[0]->shape();
    outerSize = shape.total(0, axis);
    channels = shape[axis];
    innerSize = shape.total(axis + 1);

    int allocFlag = (useOpenCL) ? Blob::ALLOC_UMAT : Blob::ALLOC_MAT;
    shape[axis] = 1;
    buf.create(shape, inputs[0]->type(), allocFlag);

    outputs.resize(1);
    outputs[0].create(inputs[0]->shape(), inputs[0]->type(), allocFlag);
}

void SoftMaxLayerImpl::forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
{
    Blob &src = *inputs[0];
    Blob &dst = outputs[0];

    if (!useOpenCL)
        forward_cpu(src, dst);
    else
89
    {
90
        CV_Assert(forward_ocl(src, dst));
91
    }
92
}
93

94 95 96 97 98 99 100 101
#ifdef HAVE_OPENCL
bool SoftMaxLayerImpl::forward_ocl(Blob &src, Blob &dst)
{
    const UMat &srcMat = src.umatRefConst();
    UMat &dstMat = dst.umatRef();
    srcMat.copyTo(dstMat);
    UMat &bufMat = buf.umatRef();
    CV_Assert(dstMat.offset == 0);
102

103 104
    String buildOpts = String("-DT=") + ocl::typeToStr(src.type());
    ocl::Kernel kmax, ksub, ksum, kdiv;
105

106 107
    if (!kmax.create("kernel_channel_max", ocl::dnn::softmax_oclsrc, buildOpts))
        return false;
108

109 110
    if (!ksub.create("kernel_channel_subtract", ocl::dnn::softmax_oclsrc, buildOpts))
        return false;
111

112 113
    if (!ksum.create("kernel_channel_sum", ocl::dnn::softmax_oclsrc, buildOpts))
        return false;
114

115 116
    if (!kdiv.create("kernel_channel_div", ocl::dnn::softmax_oclsrc, buildOpts))
        return false;
117

118 119 120
    size_t wgSize = ocl::Device::getDefault().maxWorkGroupSize();
    size_t bufSize = buf.total();
    size_t totalSize = src.total();
121

122 123 124 125 126 127 128 129 130 131 132
    kmax.args((int)outerSize, (int)channels, (int)innerSize,
              ocl::KernelArg::PtrReadOnly(dstMat), ocl::KernelArg::PtrReadWrite(bufMat));
    if (!kmax.run(1, &bufSize, &wgSize, true))
        return false;

    ksub.args((int)totalSize, (int)outerSize, (int)channels, (int)innerSize,
              ocl::KernelArg::PtrReadOnly(bufMat), ocl::KernelArg::PtrReadWrite(dstMat));
    if (!ksub.run(1, &totalSize, &wgSize, true))
        return false;

    cv::exp(dstMat, dstMat);
133

134 135 136 137
    ksum.args((int)outerSize, (int)channels, (int)innerSize,
              ocl::KernelArg::PtrReadOnly(dstMat), ocl::KernelArg::PtrReadWrite(bufMat));
    if (!ksum.run(1, &bufSize, &wgSize, true))
        return false;
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    kdiv.args((int)totalSize, (int)outerSize, (int)channels, (int)innerSize,
              ocl::KernelArg::PtrReadOnly(bufMat), ocl::KernelArg::PtrReadWrite(dstMat));
    if (!kdiv.run(1, &totalSize, &wgSize, true))
        return false;

    return true;
}
#else
bool SoftMaxLayerImpl::forward_ocl(Blob&, Blob&)
{
    return false;
}
#endif

void SoftMaxLayerImpl::forward_cpu(Blob &src, Blob &dst)
{
    CV_Assert(src.type() == CV_32F);

    float *srcPtr = src.ptrf();
    float *dstPtr = dst.ptrf();
    float *bufPtr = buf.ptrf();

    size_t outerStep = src.total(axis);
    size_t cnStep = src.total(axis + 1);

    //compute max along axis
    for (size_t outerDim = 0; outerDim < outerSize; outerDim++)
    {
        size_t srcOffset = outerDim * outerStep;
        size_t bufOffset = outerDim * cnStep;

        memcpy(bufPtr + bufOffset, srcPtr + srcOffset, innerSize * sizeof(float));

        for (size_t cnDim = 1; cnDim < channels; cnDim++)
        {
            for (size_t i = 0; i < innerSize; i++)
                bufPtr[bufOffset + i] = std::max(bufPtr[bufOffset + i], srcPtr[srcOffset + cnDim * cnStep + i]);
176
        }
177 178 179 180 181 182 183
    }

    //subtract max
    for (size_t outerDim = 0; outerDim < outerSize; outerDim++)
    {
        size_t srcOffset = outerDim * outerStep;
        size_t bufOffset = outerDim * cnStep;
184

185
        for (size_t cnDim = 0; cnDim < channels; cnDim++)
186
        {
187 188
            for (size_t i = 0; i < innerSize; i++)
                dstPtr[srcOffset + cnDim * cnStep + i] = srcPtr[srcOffset + cnDim * cnStep + i] - bufPtr[bufOffset + i];
189
        }
190
    }
191

192
    cv::exp(dst.matRef(), dst.matRef());
193

194 195 196 197 198 199 200 201 202 203
    for (size_t outerDim = 0; outerDim < outerSize; outerDim++)
    {
        size_t srcOffset = outerDim * outerStep;
        size_t bufOffset = outerDim * cnStep;

        //sum exp along axis
        for (size_t i = 0; i < innerSize; i++)
            bufPtr[bufOffset + i] = 0.f;

        for (size_t cnDim = 0; cnDim < channels; cnDim++)
204
        {
205 206 207
            for (size_t i = 0; i < innerSize; i++)
                bufPtr[bufOffset + i] += dstPtr[srcOffset + cnDim * cnStep + i];
        }
208

209 210 211
        //divide by computed sum
        for (size_t cnDim = 0; cnDim < channels; cnDim++)
        {
212
            for (size_t i = 0; i < innerSize; i++)
213
                dstPtr[srcOffset + cnDim * cnStep + i] /= bufPtr[bufOffset + i];
214 215
        }
    }
216 217 218 219 220 221 222
}

Ptr<SoftmaxLayer> SoftmaxLayer::create(int axis)
{
    return Ptr<SoftmaxLayer>(new SoftMaxLayerImpl(axis));
}

223 224
}
}