Keras模型在三重损失模型上编译

时间:2019-01-06 07:03:35

标签: python keras deep-learning

我正试图在Keras中实现三重态损失。 我有一个预先训练的vgg模型作为基本模型

def identity_loss(y_true, y_pred):
    return y_pred

from triplet_loss import triplet_loss,img_to_encoding, identity_loss
model = applications.VGG16(weights = "imagenet", include_top=False, 
input_shape = (256, 256, 3))
x = model.output
x = Flatten()(x)
x = Dense(128, activation="relu")(x)
# L2 normalization
X = Lambda(lambda  x: k.l2_normalize(x,axis=1))(x)
base_model = Model(input = model.input, output = X)

现在要创建实际模型,我使用的模型需要3个输入(锚,正和负)并输出三重态损失。

input_1 = Input((img_width,img_height,3))
input_2 = Input((img_width,img_height,3))
input_3 = Input((img_width,img_height,3))

r1 = base_model(input_1)
r2 = base_model(input_2)
r3 = base_model(input_3)
y_pred = [r1,r2,r3]
y_true = (None, None, None)
loss = Lambda(triplet_loss)(y_pred)
model = Model(inputs=[input_1, input_2, input_3], outputs=loss)
model.summary()
model.compile(optimizer = 'adam', loss=identity_loss)

model.fit给了我以下错误。不知道这意味着什么!有人可以帮忙吗?

from PIL import Image
import numpy as np
for index, row in df.iterrows():
    anchor_img = Image.open(row[0])
    anchor_img = anchor_img.resize((img_width,img_height))
    anchor_img_array = np.asarray(anchor_img)
    positive_img = Image.open(row[1])
    positive_img = positive_img.resize((img_width,img_height))
    positive_img_array = np.asarray(positive_img)
    negative_img = Image.open(row[2])
    negative_img = negative_img.resize((img_width,img_height))
    negative_img_array = np.asarray(negative_img)
    #x_train = np.array([anchor_img_array, positive_img_array, negative_img_array])
    anchor  = np.array([anchor_img_array])
    positive  = np.array([positive_img_array])
    negative = np.array([negative_img_array])
    #print(x_train.shape)
    model.fit(x=[anchor, positive, negative], verbose=1, callbacks=None, validation_steps=None)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-16-840f40c3e02e> in <module>()
     16     negative = np.array([negative_img_array])
     17     #print(x_train.shape)
---> 18     model.fit(x=[anchor, positive, negative], verbose=1, callbacks=None, validation_steps=None)

C:\Users\..\AppData\Local\Continuum\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
   1037                                         initial_epoch=initial_epoch,
   1038                                         steps_per_epoch=steps_per_epoch,
-> 1039                                         validation_steps=validation_steps)
   1040 
   1041     def evaluate(self, x=None, y=None,

C:\Users\..\AppData\Local\Continuum\Anaconda3\lib\site-packages\keras\engine\training_arrays.py in fit_loop(model, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch, steps_per_epoch, validation_steps)
    137     indices_for_conversion_to_dense = []
    138     for i in range(len(feed)):
--> 139         if issparse(ins[i]) and not K.is_sparse(feed[i]):
    140             indices_for_conversion_to_dense.append(i)
    141 

IndexError: list index out of range

0 个答案:

没有答案