nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/convolution/Convolution.java
public static INDArray im2col(INDArray img, int kh, int kw, int sy, int sx, int ph, int pw, int pval,
                                  boolean isSameMode) {
        INDArray output = null;

        if(isSameMode) {
            int oH = (int) Math.ceil(img.size(2) * 1.f / sy);
            int oW = (int) Math.ceil(img.size(3) * 1.f / sx);

            output = Nd4j.createUninitialized(new long[] {img.size(0), img.size(1), kh, kw, oH, oW}, 'c');
        }
        else {
            // FIXME: int cast
            int oH = ((int) img.size(2) - (kh + (kh-1)*(1-1)) + 2* ph)/sy + 1;
            int oW = ((int) img.size(3) - (kw + (kw-1)*(1-1)) + 2*pw)/sx + 1;

            output = Nd4j.createUninitialized(new long[] {img.size(0), img.size(1), kh, kw, oH, oW}, 'c');
        }

        Im2col im2col =  Im2col.builder()
                .inputArrays(new INDArray[]{img})
                .outputs(new INDArray[]{output})
                .conv2DConfig(Conv2DConfig.builder()
                        .kh(kh)
                        .pw(pw)
                        .ph(ph)
                        .sy(sy)
                        .sx(sx)
                        .kw(kw)
                        .kh(kh)
                        .dw(1)
                        .dh(1)
                        .isSameMode(isSameMode)
                        .build()).build();

        Nd4j.getExecutioner().exec(im2col);
        return im2col.outputArguments()[0];
    }
public static INDArray im2col(INDArray img, int kh, int kw, int sy, int sx, int ph, int pw, int pval,
                                  boolean isSameMode) {
        INDArray output = null;

        if(isSameMode) {
            int oH = (int) Math.ceil(img.size(2) * 1.f / sy);
            int oW = (int) Math.ceil(img.size(3) * 1.f / sx);

            output = Nd4j.createUninitialized(new long[] {img.size(0), img.size(1), kh, kw, oH, oW}, 'c');
        }
        else {
            // FIXME: int cast
            int oH = ((int) img.size(2) - (kh + (kh-1)*(1-1)) + 2* ph)/sy + 1;
            int oW = ((int) img.size(3) - (kw + (kw-1)*(1-1)) + 2*pw)/sx + 1;

            output = Nd4j.createUninitialized(new long[] {img.size(0), img.size(1), kh, kw, oH, oW}, 'c');
        }

        Im2col im2col =  Im2col.builder()
                .inputArrays(new INDArray[]{img})
                .outputs(new INDArray[]{output})
                .conv2DConfig(Conv2DConfig.builder()
                        .kh(kh)
                        .pw(pw)
                        .ph(ph)
                        .sy(sy)
                        .sx(sx)
                        .kw(kw)
                        .kh(kh)
                        .dw(1)
                        .dh(1)
                        .isSameMode(isSameMode)
                        .build()).build();

        Nd4j.getExecutioner().exec(im2col);
        return im2col.outputArguments()[0];
    }
Implement column formatted images
@param img the image to process
@param kh the kernel height
@param kw the kernel width
@param sy the stride along y
@param sx the stride along x
@param ph the padding width
@param pw the padding height
@param pval the padding value (not used)
@param isSameMode whether padding mode is 'same'
@return the column formatted image