transcranial / keras-js

Run Keras models in the browser, with GPU support using WebGL
https://transcranial.github.io/keras-js
MIT License
4.96k stars 503 forks source link

Unable to load InceptionV3 model (npm keras.js@1.0.3, keras@2.1.12, tf@1.6, python@3.5.2) #117

Open rychuelektryk opened 6 years ago

rychuelektryk commented 6 years ago

Hi,

When I try to load inceptionV3 model in keras-js I get following error:

Error: cwise: Arrays do not all have the same shape!
at assign_cwise_thunk (eval at createThunk (backgroundscript.js:113190), <anonymous>:8:71)
at Object.assign_ndarrayops [as assign] (eval at makeOp (backgroundscript.js:2705), <anonymous>:3:43)
at Conv2D._padInput (backgroundscript.js:31435)
at Conv2D._callCPU (backgroundscript.js:31502)
at Conv2D.call (backgroundscript.js:31393)
at Model._traverseDAG (backgroundscript.js:102501)
at Model._traverseDAG (backgroundscript.js:102515)
at <anonymous>

The model is prepared in a following manner:

1: The model is loaded and then exported in keras using following code:

keras.applications import InceptionV3
inceptionModel = InceptionV3(weights="imagenet", include_top=False)
model.save(myPath)

2: The model is than encoded using latest(see the commit below) keras-js encoder.py

commit df999159284abc5de0b92bc0f801375c08bf9008 (HEAD -> master, origin/master, origin/HEAD)
Author: Leon Chen <leon@md.ai>
Date:   Fri Feb 9 10:15:28 2018 -0500
update demos build

The aforementioned error is generated when the model is loaded by keras-js using following code:

var KerasJS = require('keras-js');
let model = new KerasJS.Model({
    filepath: modelPath,
    gpu: false
})

What I find peculiar is that the inceptionv3 model loaded in keras-js example differs than the one that I generate.

small part of configuration part of model generated by me:

$b7dfac6a-dd00-4849-bb31-769a6397f93dmodel2.1.2"
tensorflow*˙ö{"class_name": "Model", "config": {"layers": [{"class_name": "InputLayer", "config": {"sparse": false, "dtype": "float32", "batch_input_shape": [null, null, null, 3], "name": "input_1"}

full model configuration here -> https://www.dropbox.com/s/o42oc243huqqlbk/myModel.txt?dl=0

small part of configuration part of model used in example:

$7db1fe5f-c96e-4804-b1bc-f399146eaa0cinception_v32.0.9"
tensorflow*ýö{"class_name": "Model", "config": {"name": "inception_v3", "layers": [{"name": "input_1", "class_name": "InputLayer", "config": {"batch_input_shape": [null, 299, 299, 3], "dtype": "float32", "sparse": false, "name": "input_1"}

full model configuration here -> https://www.dropbox.com/s/7ld1pmbjsj5g8fo/exampleModel.txt?dl=0

Issue happens in following environment npm keras.js@1.0.3, keras@2.1.12, tf@1.6, python@3.5.2

I've spent whole day on this issue and couldnt find the solution. Any help would by greatly appreciated.

MingwangLin commented 6 years ago

Maybe you should specify your model input to be (None, 299, 299, 3).

seranus commented 6 years ago

KerasJS doesn't support None, internally model with shape lets say (None, 299, 299, 3) would be represented as (299, 299, 3) or model with shape (None, None, None, 3) would be just (3)

rychuelektryk commented 6 years ago

Thanks,

Loading model in a following way did the trick:

inceptionModel = InceptionV3(weights="imagenet", include_top=False, input_shape=(299, 299, 3))