Open zbrasseaux opened 4 years ago
Hi!
Thanks for letting me know about this issue! I'll investigate this and get back to you as soon as possible.
anybody found something ? I have encountered the same. Am i missing something ?
Ok, now I know what is wrong, it has been right in front of me :)
The classifier in the example of @zbrasseaux is
classifier = KerasClassifier(load_model('./0.7917.h5'))
This is unfortunately not how the scikit-learn wrapper of Keras works. You need to pass a build function to the KerasClassifier
upon initialization, see the proper usage here.
In the example for the modAL library (https://github.com/modAL-python/modAL/blob/master/examples/keras_integration.py), I do this the following way:
# build function for the Keras' scikit-learn API
def create_keras_model():
"""
This function compiles and returns a Keras model.
Should be passed to KerasClassifier in the Keras scikit-learn API.
"""
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
return model
# create the classifier
classifier = KerasClassifier(create_keras_model)
Notice that I pass the build function instead of the model. So, you need to implement a function which loads the weights and returns a Keras model. In your example, you pass the model directly instead of the build function.
This might work in your case:
build_fn = lambda : load_model('./0.7917.h5')
classifier = KerasClassifier(build_fn)
I haven't tested this however.
All of my relevant code:
Messages, Warnings, and Errors:
I honestly don't even know where to begin to solve this, my code is based on your example here: [https://modal-python.readthedocs.io/en/latest/content/examples/Keras_integration.html] https://modal-python.readthedocs.io/en/latest/content/examples/Keras_integration.html)
And I've read the docs here: [https://modal-python.readthedocs.io/en/latest/content/apireference/models.html] https://modal-python.readthedocs.io/en/latest/content/apireference/models.html
Any input is appreciated.