1

我是 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:“张量”对象不可调用

我该如何解决这个错误?

我正在使用这个Jupyter notebook实现一个“连体网络” 。

4

1 回答 1

2

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

这应该可以解决问题:

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
于 2020-01-20T09:12:37.433 回答