void ConvolutionLayer::im2col(Blob &inpBlob, int imNum, int cnGroup)
    {
        uchar *srcPtr = inpBlob.ptr(imNum, cnGroup*inpGroupCn);

        if (is1x1())
        {
            colMat = Mat(ksize, inpBlob.rows()*inpBlob.cols(), inpBlob.type(), srcPtr);
            return;
        }

#ifdef HAVE_OPENCL
        if (useOpenCL && ocl::useOpenCL() && inpBlob.type() == CV_32F && !is1x1())
        {
            std::vector<Range> ranges(4, Range::all());
            ranges[0] = Range(imNum, imNum+1);
            ranges[1] = Range(cnGroup*inpGroupCn, (cnGroup + 1)*inpGroupCn);

            UMat src = inpBlob.matRef()(&ranges[0]).getUMat(ACCESS_READ);
            UMat dst(colMat.size(), colMat.type());
            im2col_ocl(src, inpGroupCn, inpH, inpW, kerH, kerW, padH, padW, strideH, strideW, dst);
            dst.copyTo(colMat);
            return;
        }
#endif // HAVE_OPENCL

        if (inpBlob.type() == CV_32F)
            im2col_cpu((float *)srcPtr, inpGroupCn, inpH, inpW, kerH, kerW, padH, padW, strideH, strideW, (float *)colMat.ptr());
        if (inpBlob.type() == CV_64F)
            im2col_cpu((double*)srcPtr, inpGroupCn, inpH, inpW, kerH, kerW, padH, padW, strideH, strideW, (double*)colMat.ptr());
    }
    void DeConvolutionLayer::computeInpOutShape(const Blob &inpBlob)
    {
        outH = inpBlob.rows();
        outW = inpBlob.cols();
        outCn = inpBlob.channels();

        inpH = strideH * (outH - 1) + kerH - 2 * padH;
        inpW = strideW * (outW - 1) + kerW - 2 * padW;
        inpCn = numOutput;

        topH = inpH; topW = inpW; topCn = inpCn;
    }
    void ConvolutionLayer::computeInpOutShape(const Blob &inpBlob)
    {
        inpH = inpBlob.rows();
        inpW = inpBlob.cols();
        inpCn = inpBlob.channels();

        outH = (inpH + 2 * padH - kerH) / strideH + 1;
        outW = (inpW + 2 * padW - kerW) / strideW + 1;
        outCn = numOutput;

        topH = outH; topW = outW; topCn = outCn;
    }
示例#4
0
static void colorizeSegmentation(Blob &score, Mat &segm, Mat &legend, vector<String> &classNames)
{
    const int rows = score.rows();
    const int cols = score.cols();
    const int chns = score.channels();

    vector<Vec3i> colors;
    RNG rng(12345678);

    cv::Mat maxCl(rows, cols, CV_8UC1);
    cv::Mat maxVal(rows, cols, CV_32FC1);
    for (int ch = 0; ch < chns; ch++)
    {
        colors.push_back(Vec3i(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)));
        for (int row = 0; row < rows; row++)
        {
            const float *ptrScore = score.ptrf(0, ch, row);
            uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
            float *ptrMaxVal = maxVal.ptr<float>(row);
            for (int col = 0; col < cols; col++)
            {
                if (ptrScore[col] > ptrMaxVal[col])
                {
                    ptrMaxVal[col] = ptrScore[col];
                    ptrMaxCl[col] = ch;
                }
            }
        }
    }

    segm.create(rows, cols, CV_8UC3);
    for (int row = 0; row < rows; row++)
    {
        const uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
        cv::Vec3b *ptrSegm = segm.ptr<cv::Vec3b>(row);
        for (int col = 0; col < cols; col++)
        {
            ptrSegm[col] = colors[ptrMaxCl[col]];
        }
    }

    if (classNames.size() == colors.size())
    {
        int blockHeight = 30;
        legend.create(blockHeight*classNames.size(), 200, CV_8UC3);
        for(int i = 0; i < classNames.size(); i++)
        {
            cv::Mat block = legend.rowRange(i*blockHeight, (i+1)*blockHeight);
            block = colors[i];
            putText(block, classNames[i], Point(0, blockHeight/2), FONT_HERSHEY_SIMPLEX, 0.5, Scalar());
        }
    }
}
void PoolingLayerImpl::maxPooling_cpu(Blob &src, Blob &dst)
{
    CV_DbgAssert(dst.rows() == out.height && dst.cols() == out.width);

    for (int n = 0; n < src.num(); ++n)
    {
        for (int c = 0; c < src.channels(); ++c)
        {
            const float *srcData = src.ptrf(n, c);
            float *dstData = dst.ptrf(n, c);

            for (int ph = 0; ph < out.height; ++ph)
            {
                for (int pw = 0; pw < out.width; ++pw)
                {
                    int hstart = ph * stride.height - pad.height;
                    int wstart = pw * stride.width - pad.width;
                    int hend = min(hstart + kernel.height, inp.height);
                    int wend = min(wstart + kernel.width, inp.width);
                    hstart = max(hstart, 0);
                    wstart = max(wstart, 0);
                    const int poolIndex = ph * out.width + pw;
                    float max_val = -FLT_MAX;

                    for (int h = hstart; h < hend; ++h)
                        for (int w = wstart; w < wend; ++w)
                        {
                            const int index = h * inp.width + w;
                            if (srcData[index] > max_val)
                                max_val = srcData[index];
                        }

                    dstData[poolIndex] = max_val;
                }
            }
        }
    }
}