Convert tensorflow tensor to Keras tensor

See original GitHub issue

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:

images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")

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…

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
fornaciaricommented, Oct 19, 2018
0reactions
HarborZengcommented, Mar 26, 2019

@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)  # 一定要在最后调用它
Read more comments on GitHub >

github_iconTop Results From Across the Web

tf.convert_to_tensor | TensorFlow v2.11.0
This function converts Python objects of various types to Tensor objects. It accepts Tensor objects, numpy arrays, Python lists, and Python scalars.
Read more >
TensorFlow to Keras Tensor - Stack Overflow
Is there a simple way to just convert? Or is there a Keras function that does bilinear resize?
Read more >
How to get or convert contents of Keras Tensor using tf.keras ...
on the web page of Keras, I want to get/see the contents of objects such as age_encoded or trestbps_encoded.
Read more >
Keras as a simplified interface to TensorFlow: tutorial
Keras layers and models are fully compatible with pure-TensorFlow ... Converting a Keras Sequential model for use in a TensorFlow workflow.
Read more >
Convert numpy array to tensor - ProjectPro
How to convert a numpy array to tensor? · To achieve this we have a function in tensorflow called "convert_to_tensor", this will convert...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found