这让我有点为难。我已经尝试了几种选择,但无法使其正常工作。 我正在对自己的数据使用TF2.0 triple loss函数,但该函数无法正常工作。 我使用TF中的示例进行初始测试,这很有效
供参考的函数链接:https://www.tensorflow.org/addons/tutorials/losses_triplet
有一点不同,我在windows上,因此无法下载和安装tensorflow插件,因此我获取了使代码正常工作所需的内容。 我使用相同的模型,并以相同的方式编译它,但仍然存在问题。 代码如下:
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。您正在triplet_semihard_loss
函数中使用tf.reshape
在下面的示例中,我重新创建了您的场景,其中我将
shape
参数作为2
的tf.reshape
参数,该参数不能容纳原始张量的所有元素,因此抛出错误-代码-
输出-
tf.reshape
应确保元素的排列可以改变,但元素的总数必须保持不变。因此,修复方法是将形状更改为[2,3]
-代码-
输出-
希望这能回答你的问题。快乐学习
相关问题 更多 >
编程相关推荐