我正在尝试建立一个暹罗网络以进行重新标识,该网络的最终输出应该是经过比较的特征向量,以便确定输入图像是否来自同一类别。我的模型的精度停留在50%。我在损失函数,准确性和模型各层上尝试了多种变体,但似乎没有任何效果,而我在该领域的知识使我无法尝试另一种方法。有什么可能解决这个问题?
def cnn(in_dims):
model = Sequential()
model.add(Conv2D(8,(3,3),padding='same',input_shape=(in_dims[0],in_dims[1],in_dims[2]),activation='relu',name='conv1'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool1'))
model.add(Conv2D(16,(3,3),padding='same',activation='relu',name='conv2'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool2'))
model.add(Conv2D(32,(3,3),padding='same',activation='relu',name='conv3'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool3'))
model.add(Conv2D(64,(3,3),padding='same',activation='relu',name='conv4'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D((2,2),(2,2),padding='same',name='pool4'))
model.add(Flatten(name='flatten'))
model.add(Dense(64,name='embeddings'))
return model
def double_loss(y_true, y_pred):
total_length = y_pred.shape.as_list()[-1]
image1 = y_pred[:,0:int(total_length*1/2)]
image2 = y_pred[:,int(total_length*1/2):int(total_length*2/2)]
dist = K.sum(K.square(image1 - image2), axis=1)
return dist
def accuracy(y_true, y_pred):
return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))
input_1 = Input(shape=(128,64,3,), name='input_1')
input_2 = Input(shape=(128,64,3,), name = 'input_2')
shared = cnn([128,64,3])
encoded_image1 = shared(input_1)
encoded_image2 = shared(input_2)
merged_vector = concatenate([encoded_image1, encoded_image2], axis=-1, name='merged_layer')
model = Model(inputs=[input_1, input_2], outputs=merged_vector)
model.compile(loss=double_loss, optimizer=Adam(lr = 0.0001, beta_1 = 0.9, beta_2 = 0.999), metrics=[accuracy])
history = model.fit([X_a_train, X_b_train], y=y_train,
batch_size=128, epochs=100, validation_data=[[X_a_val, X_b_val], y_val])