Categories
Misc

Layer "triplet_snn" expects 3 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor: shape=(1, 224, 224, 3)

I just need a little help regarding my project. I’ve already written a code but I am facing an error in that. I am using a few-shot learning technique, triplet neural network. The triplet neural network(TNN) is a horizontal concatenation triplet consisting of three identical Convolutional Neural Networks (with common parameters) that are trained with triplets of inputs. An anchor instance, a positive instance (of the same class as the anchor), and a negative instance make up the input triplet (different class from the anchor). After that, the network is trained to learn a triplet loss embedding function. To compute triplet loss, three training examples are required. Each triplet is formed by intentionally selecting training examples such that each triplet has: • a reference image called anchor image • an image having the same label as anchor is called a positive image • an image has a different label than the anchor called a negative image.

The TNN learns to create k-dimensional feature vector representation of images in such a manner that similar images lie closer in an embedding space of k-dimensions.

embedding_dimension = 128

from tensorflow.keras.applications.vgg16 import VGG16

pre_trained_vgg16 = tf.keras.applications.VGG16( input_shape=(size, size,3), include_top=False, weights=”imagenet”, input_tensor=None, pooling=None, classes=1000, classifier_activation=None )

pre_trained_vgg16.save(‘vgg16.h5’)

pre_trained_vgg16 = tf.keras.models.load_model(‘vgg16.h5’)

def build_embedding_network(embedding_dimension):

embedding_network = pre_trained_vgg16

embedding_network.trainable = False

x = embedding_network.output

x = tf.keras.layers.GlobalAveragePooling2D()(x)

x = tf.keras.layers.Flatten()(x)

x = tf.keras.layers.Dropout(0.2)(x)

x = tf.keras.layers.Dense(2*embedding_dimension,
activation=’sigmoid'(x)
x = tf.keras.layers.Dense(embedding_dimension, activation=’sigmoid’)
(x)
embedding_network = tf.keras.Model(embedding_network.input, x,
name=”embedding_network”)

return embedding_network

def build_metric_network(single_embedding_dim):

input1 = tf.keras.layers.Input((single_embedding_dim), name=”input1″)
input2 = tf.keras.layers.Input((single_embedding_dim), name=”input2″)
embedded_distance =
tf.keras.layers.Subtract(name=’subtract_embeddings’) ([input1, input2])

embedded_distance = tf.keras.layers.Lambda(lambda
x:K.sqrt(K.sum(K.square(x), axis=-1, keepdims=True)),
name=’euclidean_distance’)(embedded_distance)

metric_network = tf.keras.Model(inputs=[input1, input2],
outputs=[embedded_distance],
name=”metric_network”)

return metric_network

class TripletLossLayer(tf.keras.layers.Layer):

def __init__(self, margin, **kwargs):

self.margin = margin

super(TripletLossLayer, self).__init__(**kwargs)

def triplet_loss(self, inputs):

ap_dist, an_dist = inputs

square

ap_dist2 = K.square(ap_dist)

an_dist2 = K.square(an_dist)

return K.sum(K.maximum(ap_dist2 – an_dist2 + self.margin, 0))

def call(self, inputs):

loss = self.triplet_loss(inputs)

self.add_loss(loss)

return loss

def get_config(self):

config = super().get_config().copy()

config.update({‘margin’: self.margin})

return config

def build_triplet_snn(input_shape, embedding_network, metric_network, margin=0.1):

Define the tensors for the three input images

anchor_input = tf.keras.layers.Input(input_shape, name=”anchor_input”)
positive_input = tf.keras.layers.Input(input_shape,
name=”positive_input”)

negative_input = tf.keras.layers.Input(input_shape,
name=”negative_input”)

Generate the embeddings (feature vectors) for the three images

embedding_a = embedding_network(anchor_input)

embedding_p = embedding_network(positive_input)

embedding_n = embedding_network(negative_input)

ap_dist = metric_network([embedding_a,embedding_p])

an_dist = metric_network([embedding_a,embedding_n])

Triplet loss layer

loss_layer = TripletLossLayer(margin=margin, name=’TripletLossLayer’)([ap_dist, an_dist])

Compute the concatenated pairs

all_concatenated = tf.keras.layers.Concatenate(axis=-1,name=”All-Embeddings”)([embedding_a,embedding_p,embedding_n])

Connect the inputs with the outputs

triplet_snn = tf.keras.Model(inputs=[anchor_input, positive_input,
negative_input],outputs=[loss_layer, all_concatenated],
name=”triplet_snn”)

Return the model

return triplet_snn

embedding_network = build_embedding_network(embedding_dimension) metric_network = build_metric_network(embedding_dimension)

triplet_snn = build_triplet_snn(input_shape=(size, size,3), embedding_network=embedding_network, metric_network=metric_network,margin=0.1)

learning_rate = 0.0001

epochs = 5

class TripletDataGenerator(tf.keras.utils.Sequence):

def __init__(self, triplet_dataset, shuffle=False):
self.triplet_dataset = triplet_dataset self.shuffle = shuffle
self.on_epoch_end() def __len__(self):

return len(self.triplet_dataset)

def __getitem__(self, index):

return triplet_dataset[index][0]

return (np.array(triplet_dataset[index][0]).reshape(1,224,224,3))

def on_epoch_end(self):

if self.shuffle == True:
random.shuffle(self.triplet_dataset)

data_gen = TripletDataGenerator(triplet_dataset)

filepath = ‘C:\Users\Y540\Desktop\Retinal Disease\TrainedSNN\temp\weights.{epoch}’

save_model_weights_at_every_epoch = tf.keras.callbacks.ModelCheckpoint( filepath,monitor=”loss”,verbose=1,save_best_only=False,save_weights_only=True,mode=”auto”,save_freq=”epoch” )

optimizer = tf.keras.optimizers.Adam(lr=learning_rate) triplet_snn.compile(loss=None, optimizer=optimizer, run_eagerly=True)

%%time

history = triplet_snn.fit(data_gen, epochs=epochs, verbose=1, callbacks=[save_model_weights_at_every_epoch])

submitted by /u/Intelligent_Term6689
[visit reddit] [comments]

Leave a Reply

Your email address will not be published. Required fields are marked *