tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

Why only the information of channel G is preserved in FromPixelsProgram() ?

Open ThreeSevenGO opened this issue 8 years ago • 3 comments

To get help from the community, check out our Google group.

TensorFlow.js version

0.11.6

Browser version

any

Describe the problem or feature request

I try to draw tensor3D type image data (shape[height, width, 3]), 3 channels are RGB) to screen by WebGL. The data are correct if I download them from GPU by dataSync(). But if I transfer the GPU data to texture unit, only the channel G of GPU data are send to the channel R of texture unit.

Code to reproduce the bug / link to feature request

The following is the relevant code in tfjs/tfjs-core/dist/kernels/webgl/from_pixel_gpu.js

Object.defineProperty(exports, "__esModule", { value: true });
var FromPixelsProgram = (function () {
    function FromPixelsProgram(outputShape) {
        this.variableNames = ['A'];
        var height = outputShape[0], width = outputShape[1];
        this.outputShape = outputShape;
        this.userCode = "\n\
            void main() {\n\
                ivec3 coords = getOutputCoords();\n\
                int texR = coords[0];\n\
                int texC = coords[1];\n\
                int depth = coords[2];\n\
                vec2 uv = (vec2(texC, texR) + halfCR) / vec2(" + width + ".0, " + height + ".0);\n\
                vec4 values = texture2D(A, uv);\n\
                float value;\n\
                    if (depth == 0) {\n\
                    value = values.r;\n\
                }\n\
                else if (depth == 1) {\n\
                    value = values.g;\n\
                }\n\
                else if (depth == 2) {\n\
                    value = values.b;\n\
                }\n\
                else if (depth == 3) {\n\
                    value = values.a;\n\
                }\n\
                setOutput(floor(value * 255.0 + 0.5));\n\
            }\n";
    }
    return FromPixelsProgram;
}());

In my code, depth == 1, value = values.g, so the image process after that only get the value of channel G. The other color channel information can get by dataSync() after image process,where are them stored? How can the fragment shader of the subsequent image process get the color information of other channels?

ThreeSevenGO avatar Jun 19 '18 08:06 ThreeSevenGO

How to render a texture that dimension is 2w * 2h with the data like [R_1, 0,0,0, G_1, 0,0,0, B_1,0,0,0, A_1,0,0,0, R_2,0,0,0, G_2, 0,0,0, B_2,0,0,0, A_2,0,0,0, ...] to a w * h dimension canvas in a fragment shader by sampling? I would really appreciate it if someone gave a example of the fragment shader.

chenjin3 avatar Jun 20 '18 08:06 chenjin3

We only use the red channel of our textures to store data, which is why when read values from a canvas we stick them into R channels. This means that images have a different representation than the standard image texture representation.

Just curious, are you trying to use the output in a 3D application or just trying to write a custom shader? If the latter, check out this guide: https://js.tensorflow.org/tutorials/custom-webgl-op.html

nsthorat avatar Oct 26 '18 18:10 nsthorat

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

rthadur avatar Nov 30 '18 21:11 rthadur