Вход для изменения формы - это тензор со 128 значениями, но запрошенная форма имеет 32 [Op: Reshape] - PullRequest
1 голос
/ 07 января 2020

Это немного озадачило меня. Я пробовал несколько вариантов, но не могу заставить его работать правильно. Я использую функцию тройной потери TF2.0 с моими собственными данными, но она работает неправильно. Я использовал пример из TF для начального теста, и он работает.

ссылка на функцию для справки: https://www.tensorflow.org/addons/tutorials/losses_triplet

С одной небольшой разницей я на windows поэтому я не могу скачать и установить аддоны тензорного потока, поэтому я взял то, что мне нужно, чтобы код работал. Я использую ту же модель и компилирую ее тем же способом, но все еще есть проблемы. Вот код:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...