After removing the Sequential from a convolutional network I get: "TypeError: 'Tensor' object is not callable"

After removing the Sequential from a convolutional network I get: "TypeError: 'Tensor' object is not callable"

我是 Python 的新手。

我有这个代码:

def build_base_network(input_shape):

    seq = Sequential()

    nb_filter = [6, 12]
    kernel_size = 3


    #convolutional layer 1
    seq.add(Convolution2D(nb_filter[0], kernel_size, kernel_size, input_shape=input_shape,
                          border_mode='valid', dim_ordering='th'))
    seq.add(Activation('relu'))
    seq.add(MaxPooling2D(pool_size=(2, 2))) 
    seq.add(Dropout(.25))

    #convolutional layer 2
    seq.add(Convolution2D(nb_filter[1], kernel_size, kernel_size, border_mode='valid', dim_ordering='th'))
    seq.add(Activation('relu'))
    seq.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th')) 
    seq.add(Dropout(.25))

    #flatten 
    seq.add(Flatten())
    seq.add(Dense(128, activation='relu'))
    seq.add(Dropout(0.1))
    seq.add(Dense(50, activation='relu'))
    return seq


"""Next, we feed the image pair, to the base network, which will return the embeddings that is, feature vectors:"""

input_dim = x_train.shape[2:]
img_a = Input(shape=input_dim)
img_b = Input(shape=input_dim)
base_network = build_base_network(input_dim)
feat_vecs_a = base_network(img_a)
feat_vecs_b = base_network(img_b)

然后我将网络更新到最新的 Keras API 并将其删除 Sequential:

def build_base_network(input_shape):

    inputs = Input(shape = input_shape)

    nb_filter = [6, 12]
    kernel_size = 3
    conv1 = Conv2D(nb_filter[0], (kernel_size, kernel_size), activation='relu', padding="valid", data_format='channels_first')(inputs)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    drop1 = Dropout(.25)(pool1)

    #convolutional layer 2
    conv2 = Conv2D(nb_filter[1], (kernel_size, kernel_size), activation='relu', padding="valid", data_format="channels_first")(drop1)
    pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_first")(conv2)
    drop2 = Dropout(.25)(pool2)

    #flatten 
    dense1 = Dense(128, activation='relu')(drop2)
    drop3 = Dropout(0.1)(dense1)
    dense2 = Dense(50, activation='relu')(drop3)

    return dense2

"""Next, we feed the image pair, to the base network, which will return the embeddings that is, feature vectors:"""

input_dim = x_train.shape[2:]
img_a = Input(shape=input_dim)
img_b = Input(shape=input_dim)
base_network = build_base_network(input_dim)
feat_vecs_a = base_network(img_a)
feat_vecs_b = base_network(img_b)

现在我在这一行收到以下错误 feat_vecs_a = base_network(img_a):

TypeError: 'Tensor' object is not callable

我该如何解决这个错误?

我正在使用这个 Jupyter notebook.

实现 `Siamese 网络

你得到这个错误是因为你面对返回的是张量而不是模型。 而不是顺序使用模型来包装你的张量。

这应该可以解决问题:

def build_base_network(input_shape):

    inputs = Input(shape = input_shape)

    nb_filter = [6, 12]
    kernel_size = 3
    conv1 = Conv2D(nb_filter[0], (kernel_size, kernel_size), activation='relu', padding="valid", data_format='channels_first')(inputs)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    drop1 = Dropout(.25)(pool1)

    #convolutional layer 2
    conv2 = Conv2D(nb_filter[1], (kernel_size, kernel_size), activation='relu', padding="valid", data_format="channels_first")(drop1)
    pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_first")(conv2)
    drop2 = Dropout(.25)(pool2)

    #flatten 
    dense1 = Dense(128, activation='relu')(drop2)
    drop3 = Dropout(0.1)(dense1)
    dense2 = Dense(50, activation='relu')(drop3)
    model = Model(inputs=inputs, outputs=dense2)
    return model