Rodolfo Donã Hosp
Rodolfo Donã Hosp

Reputation: 1057

'Tensor' object is not callable when setting model's input layer

I'm building a siamese network to receive 2 image inputs, go trough the same convolutional network to extract features and then calculate the image's distance.

For a better accuracy, i'm trying to load a pre-trained Xception model with imagenet weights for the convolutional layers, but only the first layers, as the features I need to extract are way simpler than imagenet's images (I need to check the distance between handwritten texts).

Here's what my model architecture looks like:

def siameseNet(input_shape):
    # Input tensors
    input1 = Input(input_shape)
    input2 = Input(input_shape)

    # Load the Xception model and freeze the layers
    xc = Xception(weights='imagenet', include_top=False, input_tensor=Input(shape=INPUT_SHAPE))
    for l in xc.layers:
        l.trainable = False

    # Create layer dict
    layers = dict([(l.name, l) for l in xc.layers])

    # I only want to use the first 3 conv blocks
    x = layers['block3_pool'].output

    # Add my custom top layer
    x = Flatten()(x)
    x = Dense(500, activation='sigmoid')(x)

    # Create two models, based on the same conv nets
    input_model_1 = x(input1)
    input_model_2 = x(input2)

    # Distance function tensor
    distance_func = Lambda(lambda t: K.abs(t[0]-t[1]))

    # Feed the distance function with the outputs
    distance_layer = distance_func([input_model_1, input_model_2])

    # Final prediction layer
    prediction = Dense(1,activation='sigmoid')(distance_layer)

    #Create the full siamese model
    network = Model(inputs=[input1,input2],outputs=prediction)

    return network

model = siameseNet((299,299,3))

But when I call siameseNet I get the error:

TypeError Traceback (most recent call last) in 38 39 ---> 40 model = siameseNet((299,299,3))

in siameseNet(input_shape) 20 21 # Create two models, based on the same conv nets ---> 22 input_model_1 = x(input1) 23 input_model_2 = x(input2) 24

TypeError: 'Tensor' object is not callable

I did the same task previously without the pre-trained model, the difference beeing that instead of building a custo tensor (x in this case), I used a Sequential model built from scratch.

What should I change in my model for my achitecture to work?

Upvotes: 0

Views: 276

Answers (1)

Dr. Snoopy
Dr. Snoopy

Reputation: 56377

You can only pass a tensor on a model or a layer, not directly to another tensor. For your case you need to build a temporary Model for the siamese branch:

xc_input = Input(shape=INPUT_SHAPE)
xc = Xception(weights='imagenet', include_top=False, input_tensor=xc_input)
for l in xc.layers:
    l.trainable = False

# Create layer dict
layers = dict([(l.name, l) for l in xc.layers])

# I only want to use the first 3 conv blocks
x = layers['block3_pool'].output

# Add my custom top layer
x = Flatten()(x)
x = Dense(500, activation='sigmoid')(x)

xc_branch = Model(xc_input, x)

# Create two models, based on the same conv nets
input_model_1 = xc_branch(input1)
input_model_2 = xc_branch(input2)

Upvotes: 1

Related Questions