-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Convert tensorflow tensor to Keras tensor #5325
Comments
perhaps this tutorial can help? https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html |
No I haven't been able to solve my issue. I face the problem when I apply keras based operations directly on tensorflow tensors, I don't face it when I use pure tensorflow without keras. Also for your case, I think just |
I'm dealing with this as detailed in these releated issues:
So far it seems you have to either convert the tf tensors to numpy arrays or write/use TF only training code as supplied in tf itself, or potentially making use of https://github.com/ppwwyyxx/tensorpack or https://github.com/zsdonghao/tensorlayer. My code where I'm trying out some of these things is here: I'd be happy to collaborate and/or interested in your solutions to these holes in the API as well. |
Oh this stackoverflow question also looks like it might be very helpful: |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed. |
I'm having this issue also. Has anyone found a solution? |
ah whoops! I thought this was about tfrecords. You can extract numpy arrays from a tensorflow model, and you can set keras weights from a numpy array. You can use the layer name strings to access specific layers. Therefore:
That's the way to do the conversion. extracting weights: Inspecting tfrecord model weight checkpoints (get the names): python ~/src/tensorflow/tensorflow/python/tools/inspect_checkpoint.py --file_name=/path/to/tf_image_segmentation_checkpoints/resnet_v1_101.ckpt > ~/tf_image_segmentation_checkpoints/resnet_v1_101_chkpt_tensors.txt |
@ahundt So there's no way to do this without rewriting the whole model in keras? (It's kind of a complex model, so this wouldn't be trivial) I was hoping there would be some way to either convert the |
Maybe someone wrote something that can convert models from tf to keras, try googling around? To my knowledge it isn't built into in the Keras API. |
@mddrill This has an example of how to convert models from TF to keras, you could implement something similar: |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed. |
Has anybody found an answer to this one?
|
@bottydim I stopped working on this problem a year ago. But you may want to check out https://stackoverflow.com/questions/44466066/how-can-i-convert-a-trained-tensorflow-model-to-keras |
Make your own layer: https://keras.io/layers/writing-your-own-keras-layers/ |
please explain more specificly, how and way to use a custom layer can solve this problem. I am stuck in this for a long time. Thanks |
The core is the build function, where you create the trainable weights. class MyLayer(Layer):
to: class MyLayer(Layer):
I hope this helps. |
@fornaciari It helped me a lot! I wonder how to call this |
@fornaciari I raised a question at https://stackoverflow.com/questions/55354612/how-to-turn-a-tf-tensor-to-some-form-that-keras-can-fit, and someone metioned your custom layer way. You can see my code shown below, I tried as you explained to me just now, and still got some error: _AttributeError: 'NoneType' object has no attribute 'inbound_nodes' def pointnet2(nb_classes):
input_points = tf.placeholder(tf.float32, shape=(16, 1024, 3))
model_input = Input(tensor=input_points)
sa1_xyz, sa1_points = set_abstraction_msg(model_input,
None,
512,
[0.1, 0.2, 0.4],
[16, 32, 128],
[[32, 32, 64], [64, 64, 128], [64, 96, 128]])
sa2_xyz, sa2_points = set_abstraction_msg(sa1_xyz,
sa1_points,
128,
[0.2, 0.4, 0.8],
[32, 64, 128],
[[64, 64, 128], [128, 128, 256], [128, 128, 256]])
sa3_xyz, sa3_points = set_abstraction(sa2_xyz,
sa2_points,
[256, 512, 1024])
# point_net_cls
c = Dense(512, activation='relu')(sa3_points)
c = BatchNormalization()(c)
c = Dropout(0.5)(c)
c = Dense(256, activation='relu')(c)
c = BatchNormalization()(c)
c = Dropout(0.5)(c)
c = Dense(nb_classes, activation='softmax')(c)
prediction = Flatten()(c)
output_keras = TfToKerasLayer()(prediction)
model = Model(inputs=model_input, outputs=output_keras)
# turn tf tensor to keras
return model
class TfToKerasLayer(Layer):
def __init__(self, **kwargs):
super(TfToKerasLayer, self).__init__(**kwargs)
def build(self, input_shape):
# 为该层创建一个可训练的权重
super(TfToKerasLayer, self).build(input_shape) # 一定要在最后调用它 |
I have a trained tensorflow model that I've loaded using the checkpoint and meta files. The model loads properly. But now I'd like to work with the same model using Keras, instead of direct tensorflow.
I got the input and output tensor of the loaded model:
Now I want to create a Keras model with these, so I tried this:
model = Model(images_placeholder,embeddings)
This however, gives me this error:
TypeError: Input tensors to a Model must be Keras tensors. Found: Tensor("input:0", shape=(?, 160, 160, 3), dtype=float32) (missing Keras metadata).
Any ideas on how to fix this? I also don't know the architecture of this model, so I wanted to do model.summary() after the keras model is loaded. I thought Keras worked seamlessly with Tensorflow and Theano graphs..
The text was updated successfully, but these errors were encountered: