CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

Can I just interpolate image instead of using MaxUnpool function?

Open pinzhang10 opened this issue 10 years ago • 10 comments

For my understanding, the MaxUnpool() depends on the values from the input of pooling to build the unpool result. I want to do just an interpolation operation. Is there any function that I can use?

Thank you!

pinzhang10 avatar Jul 23 '16 00:07 pinzhang10

Hello,

On this topic I am curious as well. What is the recommended way in CNTK to do an unpooling operation? In Keras, there is "UpSample" to do upsampling of a layer. Is MaxUnpool the way to go or do you provide some other form of unsampling operation for autoencoders?

Thanks

kestrelm avatar Aug 04 '16 00:08 kestrelm

@kestrelm The maxunpooling operation in CNTK is not Upsampling. It is mainly used in autoencoder network. It finds the max values in the input tensor for maxPooling function, and copies the values in the corresponding positions from the input tensor for maxUnPooling function into the result.

But if you want to have some simple upsampling funtion, you can modify the function MaxUnpooling() in CPUMatrix.cpp and kMaxUnpooling() in Convolution.cuh

The followings are the examples for CPU and GPU to do upsampling without interpolation.

`template void CPUMatrix::MaxUnpooling(const CPUMatrix& mpRowCol, const CPUMatrix& mpRowIndices, const CPUMatrix& indices, const CPUMatrix& poolInput, CPUMatrix& input) const { #pragma omp parallel for for (int64_t sample = 0; sample < (int64_t)GetNumCols(); sample++) { for (size_t row = 0; row < GetNumRows(); row++) { int colBase = mpRowCol(row, 0); assert(0 <= colBase && colBase < input.GetNumRows());

        int i0 = mpRowIndices(row, 0);
        int size = indices(i0++, 0);
        assert(size > 0);

        ElemType curMax = poolInput(colBase + indices(i0, 0), sample);
        ElemType prevMax = curMax;
        prevMax++;
        //int imax = 0;
        for (int i = 0; i < size; i++)
        {
            /*int dcol = indices(i0 + i, 0);
            assert(0 <= colBase + dcol && colBase + dcol < poolInput.GetNumRows());
            curMax = std::max(curMax, poolInput(colBase + dcol, sample));
            if (curMax > prevMax)
            {
                prevMax = curMax;
                imax = i;
            }*/
            int dcol = indices(i0 + i, 0);
            assert(0 <= colBase + dcol && colBase + dcol < input.GetNumRows());
            input(colBase + dcol, sample) = (*this)(row, sample);
        }

        /*int dcol = indices(i0 + imax, 0);
        assert(0 <= colBase + dcol && colBase + dcol < input.GetNumRows());
        input(colBase + dcol, sample) = (*this)(row, sample);*/
    }
}

} `

`template global void kMaxUnpooling(int batchSize, const int* mpRowCol, const int* mpRowIndices, const int* indices, const ElemType* restrict src, const ElemType* poolIn, int srcVecSize, ElemType* dst, int dstVecSize) { int row = blockIdx.x * blockDim.x + threadIdx.x; if (row >= srcVecSize) return;

src    += blockIdx.y * srcVecSize;
poolIn += blockIdx.y * dstVecSize;
dst    += blockIdx.y * dstVecSize;

for (int sample = blockIdx.y; sample < batchSize; sample += gridDim.y)
{
    int colBase = mpRowCol[row];
    assert(0 <= colBase && colBase < dstVecSize);

    int i0 = mpRowIndices[row];
    int size = indices[i0++];
    ElemType curMax = poolIn[colBase + indices[i0]];
    ElemType prevMax = curMax;
    prevMax++;
    //int imax = 0;
    for (int i = 0; i < size; i++)
    {
        /*int dcol = indices[i0 + i];
        assert(0 <= colBase + dcol && colBase + dcol < dstVecSize);
        curMax = max(curMax, poolIn[colBase + dcol]);
        if (curMax > prevMax)
        {
            prevMax = curMax;
            imax = i;
        }*/
        int dcol = indices[i0 + i];
        assert(0 <= colBase + dcol && colBase + dcol < dstVecSize);

        dst[colBase + dcol] = src[row];


    }
    dst += blockIdx.y * dstVecSize;
    src += blockIdx.y * srcVecSize;

    /*int dcol = indices[i0 + imax];
    assert(0 <= colBase + dcol && colBase + dcol < dstVecSize);

    dst[colBase + dcol] = src[row];

    src    += blockIdx.y * srcVecSize;
    poolIn += blockIdx.y * dstVecSize;
    dst    += blockIdx.y * dstVecSize;*/
}

}`

pinzhang10 avatar Aug 04 '16 15:08 pinzhang10

If this is a feature people want, would you mind adding a new node and submit a pull request?

dongyu888 avatar Aug 04 '16 16:08 dongyu888

Before you start implementing C++ code, would you mind posting the design here? I want to keep the code to contain the minimal orthogonal building blocks that are needed. I do not understand yet what the minimal building block is here. E.g. whether there is a common operation that could be extracted from unpooling.

frankseide avatar Aug 04 '16 18:08 frankseide

What is the exact upsampling operation you need?

Upsampling can be described as a two-step process:

  • insert zeroes; e.g. (x, y, z) --> (x, 0, y, 0, z, 0)
  • a reconstruction filter to fill in the gaps

The second step is just a convolution.

The first can be done with a somewhat convolved (no pun intended) trick using reshaping/transposition. Say you want to up-sample 3-fold, that is in this case you need to replace every pixel by a [3 x 3] field where one pixel contains the original value while the rest contains zeroes.

  • Let us for now assume a B&W image (1 channel). Your image would then be a [W x H] tensor.
  • Turn it into s [1 x W x 1 x H] using ReshapeDimension (x, /*axis*/1, 1:0/*0 gets inferred*/), then ReshapeDimension (x, /*axis*/3, 1:0/*0 gets inferred*/).
  • Splice a column vector (0; 0) (2 zeroes): Splice ((ConstantTensor (0, (2:1:1:1)), x)). Through broadcasting magic, this should get interpreted as an expanded [3 x W x H] tensor. (If this does not work, you need to give it the full dimension: Splice ((ConstantTensor (0, (2, W, H)), x)); or generate the zeroes through a small matrix product.) Now you have a [3 x W x 1 x H] tensor, where elements[i, *, *] = 0 for i>0 and the original pixel for i=0.
  • Repeat that in axis 3: Splice ((ConstantTensor (0, (1:1:3:1)), x, axis=3)). Now you have a [3 x W x 3 x H] tensor.
  • Merge dimensions into a [3W x 3H] tensor: FlattenDimension (x, 1), then FlattenDimension (x,2).

I think this should work, and not be too inefficient, since all that is done is copying the data a few times. The same idea can be extended into images with channels (or feature maps), it's just a bit more... convolved.

Want to give it a try?

If this works, would you mind wrapping it up in a BrainScript macro? We could include it in the core BrainScript library.

frankseide avatar Aug 04 '16 19:08 frankseide

Hello,

Thanks for the very helpful replies. My initial question was actually just a request for some guidance regarding a way to write my autoencoder.

Currently, I do

  1. Encoder Step: Conv -> MaxPool etc..

  2. Decoder Step: Conv -> UpSample

in a different framework. My question was thus how do I replicate that network in CNTK. I don't actually require an UpSample if the MaxUnpooling allows me to get back a tensor of the original size for the autoencoder to work. Is this correct?

I know the MaxUnpool isn't Upsampling (2 different operations) but as long as my training converges with a reasonable result, I am ok with it. So in other words, should I try MaxUnpool first?

Thanks

kestrelm avatar Aug 04 '16 19:08 kestrelm

@dongyu888 @frankseide

Sure, I can add a new node and submit a pull request.

I was following the original design.

Example: A[1 2] -> B[1 0 0 2]: original design finds the max value position (1 and 4) from the input of MaxPooling() and set the value from the input of MaxUnpooling() to the max positions.

I just set the value from the input of MaxUnpooling() to all new positions without choosing the max one. Example: A[1 2] -> B[1 1 2 2]:

pinzhang10 avatar Aug 04 '16 19:08 pinzhang10

@kestrelm

If your goal is an AutoEncoder, the current MaxUnpooling function in CNTK will be more appropriate for you since it adds back useful location information that you lost during Maxpooling.

pinzhang10 avatar Aug 04 '16 19:08 pinzhang10

@pinzhang10 Thanks for the very useful tips! I will give that a go.

kestrelm avatar Aug 04 '16 19:08 kestrelm

Is there any news on the progress of the pull request? I'm also very interested in the average unpooling node.

@frankseide Your suggestion requires several copies of the data and I'm constantly working on the edge of the available memory, so I would highly appreciate a memory-lean solution like a dedicated AverageUnpooling operation.

e-thereal avatar Oct 06 '16 09:10 e-thereal