Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripping keras_learning #6775

Closed
Lakedaemon opened this issue May 28, 2017 · 2 comments
Closed

Stripping keras_learning #6775

Lakedaemon opened this issue May 28, 2017 · 2 comments

Comments

@Lakedaemon
Copy link

I am building an app for android with a keras model exported (through tenserflow).

Yet, it crashes at runtime because there are DT_BOOL node in the graph and the tensorflow library I use doesn't have KernelOp for DT_BOOL registered out of the box. So, I would like to try getting read of all DT_BOOL noes (if it is possible) from the model that I only use for inference on the device.

Is it possible to strip a keras model from all DT_BOOL nodes that depens on keras_learning_phase ?
How can it be done ?

I tried this :

K.set_learning_phase(0) # all new operations will be in test mode from now on

serialize the model and get its weights, for quick re-building

config = model.get_config()
weights = model.get_weights()

re-build a model where the learning phase is now hard-coded to 0

new_model = Sequential.from_config(config)
new_model.set_weights(weights)

And I also tried a few things on the tensorflow side (optimize_for_inference, freeze_graph, (wrongly) turning Switch node into Identity (I corrupted my graph), ...)

@Lakedaemon
Copy link
Author

I worked around this issue by exporting the model to pbtxt (protocol buffer text mode)
and then replacing
node {
name: "dropout_1/keras_learning_phase"
op: "Placeholder"
attr {
key: "dtype"
value {
type: DT_BOOL
}
}
attr {
key: "shape"
value {
shape {
unknown_rank: true
}
}
}
}

with a Const op
node {
name: "dropout_1/keras_learning_phase"
op: "Const"
attr {
key: "dtype"
value {
type: DT_BOOL
}
}
attr {
key: "value"
value {
tensor {
dtype: DT_BOOL
tensor_shape {
}
bool_val: false
}
}
}
}

and then I was able to freeze/transform the model and use it in an android application

Sadly, the nodes that depend from this boolean values aren't stripped though. So this isn't the best solution

@Lakedaemon
Copy link
Author

I finally found the right way to do this:

In a completely new python process (ie. not in a process that I previously used for training), I use
K.set_learning_phase(0) model = buildMyModel() model.load_weights('myWeights.hdf5') saver = tf.train.Saver() saver.save(K.get_session(), modelName)
and the model is saved without the nodes for learning/training

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant