2

This is the code I have written to implement the model (see image at The model architecture) in keras. I want to get the ouput tensors (A & B in the image) of the last layer in the shared network. I see many examples in other stackoverflow answers. But my model is a siamese network and I want to get the output of the layer which becomes input to siamese similarity function.

from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import os

from keras.preprocessing import sequence
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional, Input, Lambda
from keras.datasets import imdb
from keras.layers.embeddings import Embedding
from keras.optimizers import RMSprop
from keras import backend as K

# custom module to read activations of layers
from read_activations import get_activations

# ignore TensorFlow messages and warnings
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
# os.system('clear')

# fix random seed for reproducibility
np.random.seed(7)

# importing custom module for data preprocessing
import preprocess_data

def euclidean_distance(vects):
    x, y = vects
    return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
    # return K.abs(x-y)

def eucl_dist_output_shape(shapes):
    shape1, shape2 = shapes
    return (shape1[0], 1)

def contrastive_loss(y_true, y_pred):
    '''Contrastive loss from Hadsell-et-al.'06
    http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
    '''
    margin = 1
    return K.mean(y_true * K.square(y_pred) +
                  (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))

def compute_accuracy(predictions, labels):
    '''Compute classification accuracy with a fixed threshold on distances.
    '''
    return labels[predictions.ravel() < 0.5].mean()

def create_base_network(input_dim):
    '''Base network to be shared.
    '''
    seq = Sequential()
    seq.add(Dense(128, input_shape=(input_dim,), activation='relu'))
    seq.add(Dropout(0.1))
    seq.add(Dense(128, activation='relu'))
    seq.add(Dropout(0.1))
    seq.add(Dense(128, activation='linear'))
    return seq

x_data, y_data = preprocess_data.dataset()
input_dim = 1000
epochs = 25

tr_pairs = x_data[:263]     # 263000
tr_y = y_data[:263]         
te_pairs = x_data[263:]     # 113000
te_y = y_data[263:]
# print(tr_pairs[:, 1])

base_network = create_base_network(input_dim)
print(base_network.summary())

input_a = Input(shape=(input_dim,))
input_b = Input(shape=(input_dim,))

# because we re-use the same instance `base_network`,
# the weights of the network
# will be shared across the two branches
processed_a = base_network(input_a)
processed_b = base_network(input_b)

distance = Lambda(euclidean_distance,
                  output_shape=eucl_dist_output_shape)([processed_a, processed_b])

model = Model([input_a, input_b], distance)

# train
rms = RMSprop()
model.compile(loss=contrastive_loss, optimizer=rms)
model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y,
          batch_size=128,
          epochs=epochs,
          validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y))

# compute final accuracy on training and test sets
pred = model.predict([tr_pairs[:, 0], tr_pairs[:, 1]])
tr_acc = compute_accuracy(pred, tr_y)
pred = model.predict([te_pairs[:, 0], te_pairs[:, 1]])
te_acc = compute_accuracy(pred, te_y)

print(model.summary())

print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc))
print('* Accuracy on test set: %0.2f%%' % (100 * te_acc))
Sanchit
  • 29
  • 9

1 Answers1

1

processed_a and processed_b are the inputs to the siamese similarity function.

As stated here, you can use to create a tensor function to get the outputs.

functor = K.function([input_a, input_b]+ [K.learning_phase()], [processed_a, processed_b])

And use the same on new inputs.

indraforyou
  • 8,969
  • 3
  • 49
  • 40
  • in this: `layer_outs = [func([test, 1.]) for func in functors]` what does `1.` signify if I have to use in my implementation? – Vijay Prakash Dwivedi Jun 15 '17 at 11:22
  • `1` signifies that its in training phase. In your case it should probably be `0` to indicate that its testing phase. The distinction is because few layers like `Dropout` and `BatchNormalization` behaves differently in training and test phase. – indraforyou Jun 15 '17 at 12:19
  • 1
    @indraforyou I got struck at this for a long time. Could you please explain after loading the trained model how to use to extract features from last but one conv layer. – Balaji Mar 02 '20 at 12:08