Implementation of the user loss function in kerams of different sizes for y_true and y_pred

I am new to Keras. I need help writing a custom loss function in keras with TensorFlow support for the following loss equation.

Loss function

The parameters passed to the loss function are as follows:

  • y_truewill take the form (batch_size, N, 2). Here we skip the N (x, y)coordinates in each sample in the batch.
  • y_predwill take the form (batch_size, 256, 256, N). Here we skip N predicted 256 x 256pixel heatmaps in each sample in a batch.

i & isin; [0, 255]

j & isin; [0, 255]

Mn(i, j)represents the value at the pixel location (i, j)for the predicted heat map n th .

Mn(i, j) = Guassian2D((i, j), y_truen, std) Where

std = standard deviation, same standard deviation for both sizes (5 px).

y_true n - n th (x, y) . .

, , l 2, .

. batch_size y_true y_pred. , Keras , . , .

def l2_loss(y_true, y_pred):
     loss = 0
     n = y_true.shape[0]
     for j in range(n):
        for i in range(num_joints):
            yv, xv = tf.meshgrid(tf.arange(0, im_height), tf.arange(0, im_width))
            z = np.array([xv, yv]).transpose(1, 2, 0)
            ground = np.exp(-0.5*(((z - y_true[j, i, :])**2).sum(axis=2))/(sigma**2))
            loss = loss + np.sum((ground - y_pred[j,:, :, i])**2)
     return loss/num_joints

, . , , ndarrays numpy keras. , !

+5
3

numpy Keras. , , .

def l2_loss_keras(y_true, y_pred):
    # set up meshgrid: (height, width, 2)
    meshgrid = K.tf.meshgrid(K.arange(im_height), K.arange(im_width))
    meshgrid = K.cast(K.transpose(K.stack(meshgrid)), K.floatx())

    # set up broadcast shape: (batch_size, height, width, num_joints, 2)
    meshgrid_broadcast = K.expand_dims(K.expand_dims(meshgrid, 0), -2)
    y_true_broadcast = K.expand_dims(K.expand_dims(y_true, 1), 2)
    diff = meshgrid_broadcast - y_true_broadcast

    # compute loss: first sum over (height, width), then take average over num_joints
    ground = K.exp(-0.5 * K.sum(K.square(diff), axis=-1) / sigma ** 2)
    loss = K.sum(K.square(ground - y_pred), axis=[1, 2])
    return K.mean(loss, axis=-1)

:

def l2_loss_numpy(y_true, y_pred):
     loss = 0
     n = y_true.shape[0]
     for j in range(n):
        for i in range(num_joints):
            yv, xv = np.meshgrid(np.arange(0, im_height), np.arange(0, im_width))
            z = np.stack([xv, yv]).transpose(1, 2, 0)
            ground = np.exp(-0.5*(((z - y_true[j, i, :])**2).sum(axis=2))/(sigma**2))
            loss = loss + np.sum((ground - y_pred[j,:, :, i])**2)
     return loss/num_joints

batch_size = 32
num_joints = 10
sigma = 5
im_width = 256
im_height = 256

y_true = 255 * np.random.rand(batch_size, num_joints, 2)
y_pred = 255 * np.random.rand(batch_size, im_height, im_width, num_joints)

print(l2_loss_numpy(y_true, y_pred))
45448272129.0

print(K.eval(l2_loss_keras(K.variable(y_true), K.variable(y_pred))).sum())
4.5448e+10

dtype float32. dtype, float64:

y_true = 255 * np.random.rand(batch_size, num_joints, 2)
y_pred = 255 * np.random.rand(batch_size, im_height, im_width, num_joints)

print(l2_loss_numpy(y_true, y_pred))
45460126940.6

print(K.eval(l2_loss_keras(K.variable(y_true), K.variable(y_pred))).sum())
45460126940.6

EDIT:

, Keras , y_true y_pred . , :

X = np.random.rand(batch_size, 256, 256, 3)
model = Sequential([Dense(10, input_shape=(256, 256, 3))])
model.compile(loss=l2_loss_keras, optimizer='adam')
model.fit(X, y_true, batch_size=8)

ValueError: Cannot feed value of shape (8, 10, 2) for Tensor 'dense_2_target:0', which has shape '(?, ?, ?, ?)'

, expand_dims y_true :

def l2_loss_keras(y_true, y_pred):
    ...

    y_true_broadcast = K.expand_dims(y_true, 1)  # change this line

    ...

model.fit(X, np.expand_dims(y_true, axis=1), batch_size=8)
+9

Keras y_pred y_true. sparse_categorical_crossentropy sparse_categorical_crossentropy . TensorFlow : https://github.com/keras-team/keras/blob/0fc33feb5f4efe3bb823c57a8390f52932a966ab/keras/backend/tensorflow_backend.py#L3570

, target: An integer tensor. target: A tensor of the same shape as 'output'. . , , , , .

Keras 2.2.4.

0

, . , , :

  1. Keras . , y_pred, , , y_true . , , .. fit, , , . , sigmoid_crossentropy_with_logits, . , 3-D np.expand_dims.
  2. , , y_true y_pred ( - , , , ).
0

Source: https://habr.com/ru/post/1690455/


All Articles