我正在Keras上的Triplets网络上工作,以查找图像相似性。但是,将三胞胎喂入模型时出现错误。请您对此提供帮助。
基本上,我试图通过3个输入(锚,正,负)来感受模型
我正在研究Python3,并使用Keras将模型与fit_model拟合。这是我训练模型的功能:
def train(trainDB, testDB, n_iter, batch_size, evaluate_every, test_size,
loss_every):
print("Starting training process!")
print("-------------------------------------")
best = -1
t_start = time.time()
inputs=trainDB.getTripletTrainData(batch_size)
targets=np.ones([batch_size])
for i in range(0, n_iter):
loss=tripletNet.fit(inputs, targets)
#print("Loss: {0}".format(loss))
if i % evaluate_every == 0:
print("Time for {0} iterations: {1}".format(i, time.time()-t_start))
val_acc = self.test_oneshot(testDB, test_size)
if val_acc > best:
print("Current best: {0}, previous best: {1}".format(val_acc, best))
print("Saving weights to: {0} \n".format(weights_path))
self.tripletNet.save_weights(weights_path)
best=val_acc
if i % loss_every == 0:
print("iteration {}, training loss: {:.2f},".format(i,loss))
错误消息:
Starting training process!
-------------------------------------
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-163-b5442e61de2d> in <module>()
----> 1 train(trainDatabase, testDatabase, n_iter, batch_size, evaluate_every, test_size, loss_every)
5 frames
<ipython-input-161-f417f0ebcfc7> in train(trainDB, testDB, n_iter, batch_size, evaluate_every, test_size, loss_every)
10
11 for i in range(0, n_iter):
---> 12 loss=tripletNet.fit(inputs, targets)
13
14 #print("Loss: {0}".format(loss))
/usr/local/lib/python3.6/dist-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)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
749 feed_input_shapes,
750 check_batch_axis=False, # Don't enforce the batch size.
--> 751 exception_prefix='input')
752
753 if y is not None:
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
90 data = data.values if data.__class__.__name__ == 'DataFrame' else data
91 data = [data]
---> 92 data = [standardize_single_array(x) for x in data]
93
94 if len(data) != len(names):
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in <listcomp>(.0)
90 data = data.values if data.__class__.__name__ == 'DataFrame' else data
91 data = [data]
---> 92 data = [standardize_single_array(x) for x in data]
93
94 if len(data) != len(names):
/usr/local/lib/python3.6/dist-packages/keras/engine/training_utils.py in standardize_single_array(x)
25 'Got tensor with shape: %s' % str(shape))
26 return x
---> 27 elif x.ndim == 1:
28 x = np.expand_dims(x, 1)
29 return x
**AttributeError: 'generator' object has no attribute 'ndim'**
如果我使用fit_generator ..我收到以下错误..
Starting training process!
-------------------------------------
Epoch 1/1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-194-b5442e61de2d> in <module>()
----> 1 train(trainDatabase, testDatabase, n_iter, batch_size, evaluate_every, test_size, loss_every)
3 frames
<ipython-input-193-8ad964a9916f> in train(trainDB, testDB, n_iter, batch_size, evaluate_every, test_size, loss_every)
10
11 for i in range(0, n_iter):
---> 12 loss=tripletNet.fit_generator(inputs, targets)
13
14 #print("Loss: {0}".format(loss))
/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.6/dist-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
1416 use_multiprocessing=use_multiprocessing,
1417 shuffle=shuffle,
-> 1418 initial_epoch=initial_epoch)
1419
1420 @interfaces.legacy_generator_methods_support
/usr/local/lib/python3.6/dist-packages/keras/engine/training_generator.py in fit_generator(model, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_queue_size, workers, use_multiprocessing, shuffle, initial_epoch)
178 steps_done = 0
179 batch_index = 0
--> 180 while steps_done < steps_per_epoch:
181 generator_output = next(output_generator)
182
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()