这让我有些难过。我尝试了几种选择,但无法正常工作。我正在做的是对自己的数据使用TF2.0三重丢失功能,但是它无法正常工作。我将TF中的示例用于初始测试,并且可以正常工作。
参考功能链接:https://www.tensorflow.org/addons/tutorials/losses_triplet
与我有一个小区别,我在Windows上,所以我无法下载和安装tensorflow-addons,所以我抓住了使代码正常工作所需的东西。我正在使用相同的模型,并以相同的方式进行编译,但是仍然存在问题。这是代码:
augment = True
if augment:
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0,
rotation_range=20,
zoom_range=0.15,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
validation_split=0.20) # set validation split
else:
train_datagen = ImageDataGenerator(
horizontal_flip=True,
rescale=1. / 255,
fill_mode='nearest',
validation_split=0.20) # set validation split
train_generator = train_datagen.flow_from_directory(
DATA_PATH,
target_size=(28,28),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='training') # set as training data
validation_generator = train_datagen.flow_from_directory(
DATA_PATH, # same directory as training data
target_size=(28,28),
color_mode='grayscale',
batch_size=BATCH_SIZE,
class_mode='categorical',
subset='validation') # set as validation data
# Build the network
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=64, kernel_size=2, padding='valid', activation='relu', input_shape=(28,28, 1)))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=2, padding='valid', activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=2))
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256, activation=None)) # No activation on final dense layer
model.add(tf.keras.layers.Lambda(lambda x: tf.math.l2_normalize(x, axis=1))) # L2 normalize embeddings
# Compile the model
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss=triplet_semihard_loss)
都是一样的,只是没有数据。我什至可以调整数据的大小和灰度,使其与其他输入数据具有相同的形状。
这里是整个错误:
File "C:\Users\matthew.millar\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 128 values, but the requested shape has 32 [Op:Reshape]
编辑
def triplet_semihard_loss(y_true, y_pred, margin=1.0):
labels, embeddings = y_true, y_pred
# Reshape label tensor to [batch_size, 1].
lshape = tf.shape(labels)
labels = tf.reshape(labels, [lshape[0], 1])
# Build pairwise squared distance matrix.
pdist_matrix = pairwise_distance(embeddings, squared=True)
# Build pairwise binary adjacency matrix.
adjacency = tf.math.equal(labels, tf.transpose(labels))
# Invert so we can select negatives only.
adjacency_not = tf.math.logical_not(adjacency)
batch_size = tf.size(labels)
# Compute the mask.
pdist_matrix_tile = tf.tile(pdist_matrix, [batch_size, 1])
mask = tf.math.logical_and(
tf.tile(adjacency_not, [batch_size, 1]),
tf.math.greater(pdist_matrix_tile,
tf.reshape(tf.transpose(pdist_matrix), [-1, 1])))
mask_final = tf.reshape(
tf.math.greater(
tf.math.reduce_sum(
tf.cast(mask, dtype=tf.dtypes.float32), 1, keepdims=True),
0.0), [batch_size, batch_size])
mask_final = tf.transpose(mask_final)
adjacency_not = tf.cast(adjacency_not, dtype=tf.dtypes.float32)
mask = tf.cast(mask, dtype=tf.dtypes.float32)
# negatives_outside: smallest D_an where D_an > D_ap.
negatives_outside = tf.reshape(
_masked_minimum(pdist_matrix_tile, mask), [batch_size, batch_size])
negatives_outside = tf.transpose(negatives_outside)
# negatives_inside: largest D_an.
negatives_inside = tf.tile(
_masked_maximum(pdist_matrix, adjacency_not), [1, batch_size])
semi_hard_negatives = tf.where(mask_final, negatives_outside,
negatives_inside)
loss_mat = tf.math.add(margin, pdist_matrix - semi_hard_negatives)
mask_positives = tf.cast(
adjacency, dtype=tf.dtypes.float32) - tf.linalg.diag(
tf.ones([batch_size]))
# In lifted-struct, the authors multiply 0.5 for upper triangular
# in semihard, they take all positive pairs except the diagonal.
num_positives = tf.math.reduce_sum(mask_positives)
triplet_loss = tf.math.truediv(
tf.math.reduce_sum(
tf.math.maximum(tf.math.multiply(loss_mat, mask_positives), 0.0)),num_positives)
return triplet_loss
这是整个堆栈跟踪:
Traceback (most recent call last):
File "C:/Users/gus/Documents/ImageSimularity/FoodTrainer.py", line 79, in <module>
callbacks=callbacks_list)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 1297, in fit_generator
steps_name='steps_per_epoch')
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_generator.py", line 265, in model_iteration
batch_outs = batch_function(*batch_data)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 973, in train_on_batch
class_weight=class_weight, reset_metrics=reset_metrics)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_v2_utils.py", line 264, in train_on_batch
output_loss_metrics=model._output_loss_metrics)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 311, in train_on_batch
output_loss_metrics=output_loss_metrics))
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 252, in _process_single_batch
training=training))
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\engine\training_eager.py", line 166, in _model_loss
per_sample_losses = loss_fn.call(targets[i], outs[i])
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\keras\losses.py", line 221, in call
return self.fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\gus\Documents\ImageSimularity\Tripleloss.py", line 75, in triplet_semihard_loss
labels = tf.reshape(labels, [lshape[0], 1])
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\ops\array_ops.py", line 131, in reshape
result = gen_array_ops.reshape(tensor, shape, name)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 8105, in reshape
tensor, shape, name=name, ctx=_ctx)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py", line 8143, in reshape_eager_fallback
ctx=_ctx, name=name)
File "C:\Users\gus\Anaconda3\envs\TF2\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 128 values, but the requested shape has 32 [Op:Reshape]
Process finished with exit code 1
tf.reshape
不会更改或张量中的元素总数]的顺序。作为错误状态,您正在尝试将元素总数从128减少到32。您正在tf.reshape
函数中使用triplet_semihard_loss
。
在下面的示例中,我重新创建了您的方案,其中我给定了shape
的2
参数作为tf.reshape
的参数,该参数未容纳原始张量的所有元素,因此引发了错误-
代码-
%tensorflow_version 2.x import tensorflow as tf t1 = tf.Variable([1,2,2,4,5,6]) t2 = tf.reshape(t1, 2)
输出-
--------------------------------------------------------------------------- InvalidArgumentError Traceback (most recent call last) <ipython-input-3-0ff1d701ff22> in <module>() 3 t1 = tf.Variable([1,2,2,4,5,6]) 4 ----> 5 t2 = tf.reshape(t1, 2) 3 frames /usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value) InvalidArgumentError: Input to reshape is a tensor with 6 values, but the requested shape has 2 [Op:Reshape]
tf.reshape
应采用这样的方式:元素的排列可以更改,但元素总数必须保持不变。因此解决方法是将形状更改为[2,3]
-
代码-
%tensorflow_version 2.x import tensorflow as tf t1 = tf.Variable([1,2,2,4,5,6]) t2 = tf.reshape(t1, [2,3]) print(t2)
输出-
tf.Tensor( [[1 2 2] [4 5 6]], shape=(2, 3), dtype=int32)
希望这能回答您的问题。祝您学习愉快。