Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keras-js "Error: [Model] Model configuration does not contain any layers."

Im trying to load a simple example network created with keras in the browser using keras-js. After saving the model as .h5 file and converting it to a .bin file I get following error while loading it:

  *Error: [Model] Model configuration does not contain any layers.*

The model is simply created by:

from keras.models import Sequential
from keras.layers import Dense, Activation

model= Sequential()
model.add(Dense(10,input_shape=(1,)))
model.add(Activation('relu'))
model.add(Dense(1))

model.compile(optimizer='rmsprop',loss='mse',metrics=['accuracy'])

inputs=[[5],[3],[4],[5]]
targets=[[5],[3],[4],[5]]

model.fit(inputs,targets,epochs=100)
model.save("example.h5")

Then I convert it with:

python encoder.py -q example.h5

and load it in javascript with:

const model = new KerasJS.Model({
filepath: '/keras/example.bin',
filesystem: true,
gpu: false
})

I have tried it with keras version 2.0.9, 2.1.2 and 2.1.5. What could be the problem here?

like image 854
BGraf Avatar asked Mar 09 '18 19:03

BGraf


1 Answers

Well, I knew nothing about this JS library but I tried to reproduce the problem and I really get that error you mentioned. HOWEVER, a careful programmer would notice that an error previous to that mentioned error had shown up. And it was the following:

Access to XMLHttpRequest at 'file:///< your_local_path_to_keras-js >/keras-js-master/example.bin' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

You can read more about this issue in this question. Basically, a webapp is not allowed to access your local files due to security measures. Then, you need to serve these files, which can be easily done with the following python command:

python -m http.server

This will serve files from the current directory at localhost under port 8000:

http://localhost:8000/

There are more alternatives to do this in this link.

I tried this and get rid of both errors.

like image 116
Hemerson Tacon Avatar answered Oct 01 '22 09:10

Hemerson Tacon