get Embeddings by reloading tensorflow model - python

I saved a tensorflow model, .pb file, trained using transfer learning taking this as reference with following code added at the end:
tf.train.write_graph(graph_name, saving_dir_path, output.pb, as_text=False)
and it successfully saved. But now after training I want to get Embedding's output. Following is the last layer defined to train in the graph under layer name final_training_ops:
with tf.name_scope('Wx_plus_b'):
logits = tf.add(tf.matmul(bottleneck_input, layer_weights), layer_biases, name='logits')
After reloading saved model I am using tf.get_default_graph().get_tensor_by_name('Wx_plus_b/logits') to access layer so as to pass image to get embeddings but getting error as invalid operation name.

I gave more time to it and found correct syntax is of the form <op_name>:<output_index> -
tf.get_default_graph().get_tensor_by_name('Wx_plus_b/logits:0')

Related

Error when trying to predict audio: Could not compute output Tensor ("ctc/ExpandDims_22:0"

So i tried to create a speech recognition neural network using the librispeech dataset dev-clean. I tried to convert the code from https://github.com/soheil-mpg/Speech-Recognition into a jupyter notebook.
Everything appears to be working. The model can be trained and doesn't give any errors.
But when using model.predict() i get the following error:
AssertionError: Could not compute output Tensor("ctc/ExpandDims_22:0", shape=(None, 1), dtype=float32)
I uploaded the Jupyter Notebook to https://github.com/jake-salmone/ASR
The code is almost identical, the only thing i have change is, that i don't use the json, but use a pandas DataFrame.
I found the answer!: The model has the wrong output-dimensions.
Of course the ctc loss should only be added to the model during training.
when adding the ctc loss, it should only happen within the scope of a function:
model = add_ctc_loss(model)
and creating a training function that only adds the loss within the scope of the function will not change the model.

AttributeError: Layer tf_bert_model has no inbound nodes

I have a deep learning model that uses BERT layer from Huggingface library (TF=2.0, Transformers=2.8.0).
The model consists of: BERT embeddings -> Attention -> Softmax. Also I am using tf.keras.callbacks.ModelCheckpoint to save the best model during training.
I'm trying to slice the model to get the attention weights.
My problem is that, if I try to access the output of any layer after loading the saved model using output1 = model.layers[3].output, I get this error:
AttributeError: Layer tf_bert_model has no inbound nodes.
but if I do the same on the original model without saving it (instantly after finishing model.fit()), I have no problem.
This is how I load the saved model:
model = tf.keras.models.load_model(model_dir)
Is there a problem with this way?, giving that it works for prediction.
Answering here for the benefit of the community since the user has already found the solution.
Issue was resolved by following steps
Loading the saved model
Creating another new model with the same structure
Copying the weights from the saved model to the newly created one
Then access the needed layer without any problem

Feeding input to resnet18 onnx model (Resnet18 deployment for object detction in video file)

I have built and saved a trained resnet18 model using the code in github in this link
the code can be run by specifying the training directory and type of network model.
the model resnet18.onnx is chosen and trained to classify 4 types of cells.
I am using Nvidia jetson (ubuntu) for this project.
now i need to use the generated trained model from the code above (resnet18.onnx)to classify objects in video using the following snippet code where a small box and the value of prediction is displayed on the detected cell in the video.
the error message i get when i run the above code using the resnet18.onnx is
confidences, boxes = ort_session.run(None, {input_name: img})
ort_session.run expected 2 get 1
what is the second input that is expected by the onnx model(i know that the model needs only the image to classify it so what is the second input that is required)
The error means the model expects 2 inputs and you only provided one.

keras problems loading custom model from yolov2

I've searched around for a couple of answers regarding the load_model from keras but I still have a question.
I am following this model really closely (https://github.com/experiencor/keras-yolo2), and am training on a custom dataset.
I have done the training which gives me a yolov2.h5 file, basically model weights to fit into the keras model. But I am encountering some problems with the loading of the model.
After loading the model (in a separate.py file)
model = load_model('file_dir/yolov2.h5')
First I encounter the issue
NameError: name 'tf' is not defined
Which I then searched up to modify my code to add custom objects as such:
model = load_model('file_dir/yolov2.h5', custom_objects={'tf':tf})
This clears the first error but results in another
ValueError: Unknown loss function : custom_loss
I used the custom_loss function from the yolov2 (https://github.com/experiencor/keras-yolo2/blob/master/frontend.py), so i tried to solve it by
from frontend import YOLO
model = load_model('file_dir/yolov2.h5' custom_objects={'tf':tf, 'custom_loss':YOLO.custom_loss)
But ran into another error:
TypeError: custom_loss() missing 1 required positional argument
I got rather stuck here because I have no idea how to fit in the parameters for custom_loss. Seek some help regarding this (Don't particularly understand this part since I'm loading my model in a different python script separate.py). Thank you so much!
(Edit: This fix doesn't work for me either)
model = load_model('file_dir/yolov2.h5', compile = False)
To resolve this problem, as you already have the network at hand, only save trained weights (like what keras trainer does in callback).
For testing, make model, no need to compile, and then load trained weights using model.load_weights(path/to/saved/weights).
You also can use "by_name=True" if you make the network in a different way, this time you should keep layer names.
Another option id to manually set weights; for this you will load .h5 file bu "h5py" (h5py.File(path/to/weights, mode='r')) for example (have look how keras do that), then try to correspond layer names of the model and loaded weights.

Can't convert Keras model to tflite

I have a Keras model saved with the following line:
tf.keras.models.save_model(model, "path/to/model.h5")
Later, I try to convert it to a tflite file as follows:
converter = tf.contrib.lite.TFLiteConverter.from_keras_model_file('path/to/model.h5')
tflite_model = converter.convert()
open("path/to/model.tflite", "wb").write(tflite_model)
But I get a weird error:
You are trying to load a weight file containing 35 layers into a model with 0 layers.
I know that my model is working fine. I am able to load it and draw inferences. This error only shows up when trying to save it as a tflite model.
TensorFlow version: tensorflow-gpu 1.12.0
I'm using tf.keras.
Turns out, the issue is due to explicitly defining an InputLayer with some input_shape.
My model was of the form:
InputLayer(input_shape=(...))
BatchNormalization()
.... Remaining layers
I changed it to:
BatchNormalization(input_shape=(...))
.... Remaining layers
and transferred the weights from the previous model here. Now it works perfectly.

Categories

Resources