Output vector given an image for Siamese model - tensorflow

This page (https://keras.io/examples/mnist_siamese/) highlights how we train a Siamese model. The model will output a score given two images input. What I want to do is that, during inference, given an image, I want it to return a 128-dimension vector that represents the image, how should I achieve that?

If you run model.summary() you will see a summary of all model layers. In your case 'model' appears to be the layer of interest. Then you can select the layer that contains the 128D output using the get_layer() method. Finally you can extract the output as below.
model.get_layer('model').output

Related

How can i concatenate two CNN models which each have a different input

I want to do a concatenated model for binary classification,
the data_set is the same, but in the first model I did some preprocessing to the image,
the input is xtrain and ytrain for the training, and xval, yval for validation
the input shape is 224,224,1.
in the second model, I used the image as it was with the function
train_set= tf.keras.utils.image_dataset_from_directory
val_set= tf.keras.utils.image_dataset_from_directory
the input shape is 224,224,3
I want to merge or concatenate them both, but I don't know how to fit the model for the training and validation with the different inputs
I want to merge or concatenate them both, but I don't know how to fit the model for the training and validation with the different inputs.
if anyone can help me I would be grateful
Thanks

Obtaining Logits of the output from deeplab model

I'm using a pre-trained deeplab model (from here) to obtain segmentations for an input image. I'm able to obtain the sematic labels (i.e. SemanticPredictions) which is argmax applied to logits (link).
I was wondering if there is an easy way to obtain the logits before argmax? I was hoping to find the output tensor name and simply pass it into my tfsession
as in the following:
tf_session.run(
self.OUTPUT_TENSOR_NAME,
feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(input_image)]})
But I have not been able to locate such tensor name in the code that reveals the logits, or softmax outputs.
For a model trained from MobileNet_V2 setting self.OUTPUT_TENSOR_NAME = 'ResizeBilinear_2:0' retrieves the logits before the argmax is performed.
I suspect this is the same for xception, but have not verified it.
I arrived at this answer by loading my model in tensorflow. Then, printing the name of all layers in the loaded graph. Finally, I took the name of the final output layer before the last 'ArgMax' layer and ran some inferencing using that.
Here is a link to a stackoverflow question on printing the names of the layers in a graph. I found the answer by Ted to be most helpful.
By the way, the output layers of DeeplabV3 models does not apply SoftMax. So you cannot simply take the raw value of the elements of output vectors as a confidence.

Extract the output of the embedding layer

I am trying to build a regression model, for which I have a nominal variable with very high cardinality. I am trying to get the categorical embedding of the column.
Input:
df["nominal_column"]
Output:
the embeddings of the column.
I want to use the op of the embedding column alone since I would require that as a input to my traditional regression model. Is there a way to extract that output alone.
P.S I am not asking for code, any suggestion on the approach would be great.
If the embedding is part of the model and you train it, then you can use functional API of keras to get output of any intermediate operation in your graph:
x=Input((number_of_categories,))
y=Embedding(parameters_of_your_embeddings)(x)
output=Rest_of_your_model()(y)
model=Model(inputs=[x],outputs=[output,y])
if you do it before you train the model, you'll have to define custom loss function, that deals only with part of the output. The other way is to train the model with just one output, then create identical model with two outputs and set the weights of the second model from the trained one.
If you want to get the embedding matrix from your model, you can just use method get_weights of the embedding layer which returns the weights in numpy array.

How to feed input into one layer in a tensorflow pre-trained model?

The pretrained model has many layers, I want to feed my input directly into one intermediate layer (and discard the result of the previous layers).
I only got the .pb file and the ckpt files of that model, so how to modify the computation flow without the source code?
This is the only code file that I got, but I dont know how to use it. Is the graph generate by this file?(much different from the normal tensorflow files)https://github.com/tensorflow/models/blob/master/research/object_detection/models/ssd_mobilenet_v2_feature_extractor.py
Here is what you need to do :
Load the model
Find the name of the layer or retrieve the tensor of the layer you want to feed values to (let's name it 'Z' for the sake of the explanation)
Find the name of the layer or retrieve the tensor of the layer you want to get results from ('Y')
Run this code snippet :results = sess.run('Y:0', {'Z:0': your_value})

making a pre-trained mxnet network fully convolutional

I wish to convert one of the existing pre-trained mxnet models available here to a fully convolutional one.
This means being able to input an image of any size, specifying the stride, and getting a full output.
For instance, assume the model was trained on 224x224x3 images. I want to input an image which is 226x226x3 and specify stride=1, in order to get a 3x3xnum-classes output.
I'm not asking "theoretically", but rather for an example code :-)
Thanks!
According to this example: https://github.com/dmlc/mxnet-notebooks/blob/master/python/tutorials/predict_imagenet.ipynb
You can change the data shape when binding the model:
mod.bind(for_training=False, data_shapes=[('data', (1,3,226,226))])
Then you can input an 3 * 226 * 226 image.
Another example:http://mxnet.io/how_to/finetune.html
This example replaces the last layer of pre-trained model with a fc layer.