Error: Failed to compile fragment shader.

782 views
Skip to first unread message

adgelbfish

unread,
Sep 5, 2018, 5:14:18 AM9/5/18
to TensorFlow.js Discussion
Hi,

Tensorflow/DL/ML noob here.
 
I downloaded a DeeplabV3 pretrained model from here: http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz , used the tfjs converter to convert it and am now getting the following error:

tf-core.esm.js:17 1   
2      precision highp float;
3      precision highp int;
4      varying vec2 resultUV;
5      const vec2 halfCR = vec2(0.5, 0.5);
6   
7      struct ivec5
8      {
9        int x;
10       int y;
11       int z;
12       int w;
13       int u;
14     };
15  
16     struct ivec6
17     {
18       int x;
19       int y;
20       int z;
21       int w;
22       int u;
23       int v;
24     };
25  
26     bool isNaN(float val) {
27       return (val < 0.0 || 0.0 < val || val == 0.0) ? false : true;
28     }
29  
30     bool hasNaN(vec4 values) {
31       vec4 v1 = values * values;
32       vec4 v2 = values * values;
33       return any(notEqual(v1, v2));
34     }
35  
36     float getNaN(vec4 values) {
37       return dot(vec4(1), values);
38     }
39  
40     int round(float value) {
41       return int(floor(value + 0.5));
42     }
43  
44     int imod(int x, int y) {
45       return x - y * (x / y);
46     }
47  
48     //Based on the work of Dave Hoskins
49     //https://www.shadertoy.com/view/4djSRW
50     #define HASHSCALE1 443.8975
51     float random(float seed){
52       vec2 p = resultUV * seed;
53       vec3 p3  = fract(vec3(p.xyx) * HASHSCALE1);
54       p3 += dot(p3, p3.yzx + 19.19);
55       return fract((p3.x + p3.y) * p3.z);
56     }
57  
58    
59   vec2 UVfrom1D(int texNumR, int texNumC, int index) {
60     int texR = index / texNumC;
61     int texC = index - texR * texNumC;
62     return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
63   }
64  
65    
66   vec2 UVfrom2D(int texNumR, int texNumC, int numC, int row, int col) {
67     int index = row * numC + col;
68     int texR = index / texNumC;
69     int texC = index - texR * texNumC;
70     return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
71   }
72  
73    
74   vec2 UVfrom3D(int texNumR, int texNumC, int stride0,
75       int stride1, int row, int col, int depth) {
76     // Explicitly use integer operations as dot() only works on floats.
77     int index = row * stride0 + col * stride1 + depth;
78     int texR = index / texNumC;
79     int texC = index - texR * texNumC;
80     return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
81   }
82  
83    
84   vec2 UVfrom4D(int texNumR, int texNumC, int stride0,
85       int stride1, int stride2, int row, int col, int depth,
86       int depth2) {
87     // Explicitly use integer operations as dot() only works on floats.
88     int index = row * stride0 + col * stride1 + depth * stride2 + depth2;
89     int texR = index / texNumC;
90     int texC = index - texR * texNumC;
91     return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
92   }
93  
94    
95   vec2 UVfrom5D(int texNumR, int texNumC, int stride0,
96       int stride1, int stride2, int stride3, int row, int col, int depth,
97       int depth2, int depth3) {
98     // Explicitly use integer operations as dot() only works on floats.
99     int index = row * stride0 + col * stride1 +
100                depth * stride2 + depth2 * stride3 + depth3;
101    int texR = index / texNumC;
102    int texC = index - texR * texNumC;
103    return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
104  }
105 
106   
107  vec2 UVfrom6D(int texNumR, int texNumC, int stride0,
108      int stride1, int stride2, int stride3, int stride4,
109      int row, int col, int depth, int depth2, int depth3, int depth4) {
110    // Explicitly use integer operations as dot() only works on floats.
111    int index = row * stride0 + col * stride1 + depth * stride2 + depth2 *
112      stride3 + depth3 * stride4 + depth4;
113    int texR = index / texNumC;
114    int texC = index - texR * texNumC;
115    return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
116  }
117 
118 
119 
120    float sampleTexture(sampler2D textureSampler, vec2 uv) {
121      return texture2D(textureSampler, uv).r;
122    }
123 
124 
125    void setOutput(float val) {
126      gl_FragColor = vec4(val, 0, 0, 0);
127    }
128 
129  uniform sampler2D A;
130 
131      ivec4 getOutputCoords() {
132        ivec2 resTexRC = ivec2(resultUV.yx *
tf-core.esm.js:17 ERROR: 0:133: 'NaN' : undeclared identifier
tf-core.esm.js:17  133          vec2(1, NaN));                                                      
tf-core.esm.js:17 134        int index = resTexRC.x * NaN + resTexRC.y;
135 
136        int r = index / NaN;
137        index -= r * NaN;
138 
139        int c = index / NaN;
140        index -= c * NaN;
141 
142        int d = index / 3;
143        int d2 = index - d * 3;
144 
145        return ivec4(r, c, d, d2);
146      }
147   
148 
149      float getAFlat(int index) {
150        vec2 uv = UVfrom1D(88, 720, index);
151        return sampleTexture(A, uv);
152      }
153   
154       
155          float getA(int row, int col, int depth) {
156            int texR = row;
157            int texC = col * 3 + depth;
158            vec2 uv = (vec2(texC, texR) + halfCR) /
159                       vec2(720.0, 88.0);
160            return sampleTexture(A, uv);
161          }
162       
163        float getA(int row, int col, int depth, int depth2) {
164          return getA(col, depth, depth2);
165        }
166     
167 
168        const vec2 effectiveInputOverOutputRatioRC = vec2(
169            NaN,
170            NaN);
171        const vec2 inputShapeRC = vec2(88.0, 240.0);
172 
173        void main() {
174          ivec4 coords = getOutputCoords();
175          int b = coords[0];
176          int d = coords[3];
177          ivec2 yRC = coords.yz;
178 
179          // Fractional source index.
180          vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;
181 
182          // Compute the four integer indices.
183          ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);
184          ivec2 sourceCeilRC = ivec2(
185            min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
186 
187          float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);
188          float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);
189          float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);
190          float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);
191 
192          vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);
193 
194          float top = topLeft + (topRight - topLeft) * fracRC.y;
195          float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
196          float newValue = top + (bottom - top) * fracRC.x;
197 
198          setOutput(newValue);
199        }
200     
tf-core.esm.js:17 Uncaught (in promise) Error: Failed to compile fragment shader.
    at hn (tf-core.esm.js:17)
    at e.createProgram (tf-core.esm.js:17)
    at or (tf-core.esm.js:17)
    at tf-core.esm.js:17
    at e.getAndSaveBinary (tf-core.esm.js:17)
    at e.compileAndRun (tf-core.esm.js:17)
    at e.resizeBilinear (tf-core.esm.js:17)
    at Xo.lu.engine.runKernel.batchImages (tf-core.esm.js:17)
    at tf-core.esm.js:17
    at e.scopedRun (tf-core.esm.js:17)
hn @ tf-core.esm.js:17
e.createProgram @ tf-core.esm.js:17
or @ tf-core.esm.js:17
(anonymous) @ tf-core.esm.js:17
e.getAndSaveBinary @ tf-core.esm.js:17
e.compileAndRun @ tf-core.esm.js:17
e.resizeBilinear @ tf-core.esm.js:17
Xo.lu.engine.runKernel.batchImages @ tf-core.esm.js:17
(anonymous) @ tf-core.esm.js:17
e.scopedRun @ tf-core.esm.js:17
e.runKernel @ tf-core.esm.js:17
Xo @ tf-core.esm.js:17
resizeBilinear @ tf-core.esm.js:17
(anonymous) @ index.js:32
r @ runtime.js:62
(anonymous) @ runtime.js:296
e.(anonymous function) @ runtime.js:114
r @ main.50ebeee2.js:14682
(anonymous) @ main.50ebeee2.js:14682
Promise.then (async)
(anonymous) @ index.js:52
setTimeout (async)
(anonymous) @ index.js:51
EventImpl.dispatchToListener @ extensions::event_bindings:403
publicClassPrototype.(anonymous function) @ extensions::utils:138
EventImpl.dispatch_ @ extensions::event_bindings:387
EventImpl.dispatch @ extensions::event_bindings:409
publicClassPrototype.(anonymous function) @ extensions::utils:138
messageListener @ extensions::messaging:240
EventImpl.dispatchToListener @ extensions::event_bindings:403
publicClassPrototype.(anonymous function) @ extensions::utils:138
EventImpl.dispatch_ @ extensions::event_bindings:387
EventImpl.dispatch @ extensions::event_bindings:409
publicClassPrototype.(anonymous function) @ extensions::utils:138
dispatchOnMessage @ extensions::messaging:392

Nikhil Thorat

unread,
Sep 5, 2018, 10:10:14 AM9/5/18
to a...@adgelb.fish, TensorFlow.js Discussion
Is this when you try to make a prediction? Can you send us the JavaScript code you are using to load the model and make a prediction (it looks like there's a NaN in the shape somewhere).

--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/tfjs/50b70f7b-7721-41cc-890b-bff6372f92a3%40tensorflow.org.

yass...@google.com

unread,
Sep 5, 2018, 10:17:22 AM9/5/18
to TensorFlow.js Discussion, a...@adgelb.fish
To add to Nikhil's message we would be particularly interested in the shape of the input that you are passing to predict and how you construct that. I'd suspect the rank of the input tensor may be different from what the model is expecting.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+unsubscribe@tensorflow.org.

adgelbfish

unread,
Sep 5, 2018, 4:42:35 PM9/5/18
to TensorFlow.js Discussion, a...@adgelb.fish
const tensor =  tf.image.resizeBilinear(tf.fromPixels(image), [targetWidth, targetHeight]).expandDims()
model.predict({ImageTensor: tensor})


On Wednesday, September 5, 2018 at 10:17:22 AM UTC-4, yass...@google.com wrote:
To add to Nikhil's message we would be particularly interested in the shape of the input that you are passing to predict and how you construct that. I'd suspect the rank of the input tensor may be different from what the model is expecting.

On Wednesday, September 5, 2018 at 10:10:14 AM UTC-4, Nikhil Thorat wrote:
Is this when you try to make a prediction? Can you send us the JavaScript code you are using to load the model and make a prediction (it looks like there's a NaN in the shape somewhere).

To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.

Nikhil Thorat

unread,
Sep 5, 2018, 6:04:33 PM9/5/18
to a...@adgelb.fish, TensorFlow.js Discussion
Can you print the shape of "tensor"?

adgelbfish

unread,
Sep 5, 2018, 6:45:15 PM9/5/18
to TensorFlow.js Discussion, a...@adgelb.fish

sorry guys, it was a basic javascript mistake, targetWidth and targetHeight were NaN because the value they were based off of was declared later in the function so Math.round and Math.max had returned NaN.

adgelbfish

unread,
Sep 5, 2018, 6:52:41 PM9/5/18
to TensorFlow.js Discussion
Thanks for your time and help though.

Daniel Smilkov

unread,
Sep 5, 2018, 7:35:06 PM9/5/18
to a...@adgelb.fish, TensorFlow.js Discussion
Thanks! We can improve on detecting invalid inputs/NaNs as early as possible and throw an error that's meaninful. Filed an issue with the "ux" tag to track this.

Daniel


--
You received this message because you are subscribed to the Google Groups "TensorFlow.js Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tfjs+uns...@tensorflow.org.
Visit this group at https://groups.google.com/a/tensorflow.org/group/tfjs/.
Reply all
Reply to author
Forward
0 new messages