我正在建立一个暹罗网络来接收2个图像输入,通过同一个卷积网络以提取特征,然后计算图像的距离。
为了获得更高的准确性,我正在尝试为卷积层加载具有图像网络权重的预训练Xception模型,但是仅第一层,因为我需要提取的功能比图像网络的图像更简单(我需要检查手写文字之间的距离。
这是我的模型架构:
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))
但是当我致电siameseNet
时,我得到了错误消息:
TypeError跟踪(最近的呼叫 最后) 38 39 ---> 40个模型= siameseNet((299,299,3))
siameseNet中的(input_shape) 20 21#基于相同的卷积网络创建两个模型 ---> 22 input_model_1 = x(input1) 23 input_model_2 = x(input2) 24
TypeError:“张量”对象不可调用
我以前没有预先训练的模型就完成了相同的任务,不同之处在于,我没有使用custo张量(在这种情况下为x
),而是使用了从头开始构建的Sequential
模型。
要使体系结构正常工作,我应该如何更改模型?
答案 0 :(得分:1)
您只能在模型或层上传递张量,而不能直接传递给另一个张量。对于您的情况,您需要为暹罗分支建立一个临时模型:
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)