如何使用官方的vgg-16模型作为具有三重态损失的喀拉拉邦的特征extarcor?

时间:2018-09-16 04:50:12

标签: python keras

from keras.layers import Input
from keras.applications import VGG16
from keras.models import Model
from keras import backend as K


def triplet_loss_2(y_true, y_pred):
    alpha = 0.2
    anchor = y_pred[0]
    positive = y_pred[1]
    negative = y_pred[2]
    # Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1
    pos_dist = K.sum(K.square(anchor - positive), axis=-1)
    # Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1
    neg_dist = K.sum(K.square(anchor - negative), axis=-1)
    # Step 3: subtract the two previous distances and add alpha.
    basic_loss = pos_dist - neg_dist + alpha
    # Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
    loss = K.sum(K.maximum(basic_loss, 0))

    return loss


def create_vgg_3_input(input_shape=(224, 224, 3)):
    # model = create_vgg_16(input_shape)
    input_x = Input(shape=input_shape)
    input_p = Input(shape=input_shape)
    input_n = Input(shape=input_shape)
    vgg = VGG16(include_top=False, input_shape=input_shape, weights='vgg_pretrained_model/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')
    encoded_x = vgg(input_x)
    encoded_p = vgg(input_p)
    encoded_n = vgg(input_n)
    the_net = Model(inputs=[input_x, input_p, input_n], outputs=[encoded_x, encoded_p, encoded_n])

    return the_net

def load_img():
    ````
    img_a = load_img(pic_a, target_size=input_shape)
    img_b = load_img(pic_b, target_size=input_shape)
    img_c = load_img(pic_c, target_size=input_shape)
    data_a = img_to_array(img_a)
    data_b = img_to_array(img_b)
    data_c = img_to_array(img_c)
    data_a = data_a.astype('float32')
    data_b = data_b.astype('float32')
    data_c = data_c.astype('float32')
    data_a /= 255.
    data_b /= 255.
    data_c /= 255.
    ````
new_net = model.the_vgg_16(INPUT_SHAPE)

rms = RMSprop()

new_net.compile(optimizer='Adam', loss=model.triplet_loss_2)

loss = new_net.train_on_batch([pair_x[:, 0], pair_x[:, 1], pair_x[:, 2]], [label_y1, label_y2, label_y3])
print(loss)

vgg-16

我定义模型时使用vgg作为特征提取器。

3个输入

我使模型具有3个输入,因此它可以使用三重态损失并学习如何识别诸如面孔之类的东西。

亏损没有变化

无论我如何改变学习速度。损失仍然没有改变。 像这样:

  1. Epoch 1/100
  2. [73.5,24.5,24.5,24.5]
  3. [73.5,24.5,24.5,24.5]
  4. [73.5,24.5,24.5,24.5]
  5. [73.5,24.5,24.5,24.5]

为什么损失有4个条件,为什么损失不变?

0 个答案:

没有答案