Skip to main content
Mudei a pergunta para Português
Fonte Link

How to create a custom Como criar uma função de perda no keras loss function withque utiliza opencv?

I'm developing aEstou desenvolvendo um modelo de machine learning model usingusando a biblioteca keras and I notice that the available losses functions are not giving the best results on my test sete percebo que as funções de perda disponíveis não estão dando os melhores resultados no meu conjunto de testes.

I am using anEstou usando uma arquitetura Unet architecture, where I input aonde eu entro com uma imagem (16,16,3) image and the net also outputse a rede também gera uma imagem (16,16,3) picture (auto-encoder). I notice that maybe one way to improve the model would be if I used a loss function that comparesPercebi que talvez uma maneira de melhorar o modelo seria se eu usasse uma função de perda que compara pixel toa pixel on the gradientsnos gradientes da imagem (laplacianlaplaciano) between the net output and theentre a saída da rede e o conjunto de ground truth. HoweverNo entanto, I did not found anyeu não encontrei nenhum tutorial that would handle this kind of applicationque pudesse lidar com esse tipo de aplicação, because it would need to use opencvporque ela precisaria usar a função laplacian function on each output image from the netdo opencv em cada imagem de saída da rede.

The loss function would be something like thisA função de perda seria algo assim:

def laplacian_loss(y_true, y_pred):
  
  # y_true already is the calculated gradients, only needs to compute on the y_pred
  # calculates the gradients for each predicted image
  y_pred_lap = []
  for img in y_pred:
    laplacian = cv2.Laplacian( np.float64(img), cv2.CV_64F )
    y_pred_lap.append( laplacian )
  
  y_pred_lap = np.array(y_pred_lap)
  
  # mean squared error, according to keras losses documentation
  return K.mean(K.square(y_pred_lap - y_true), axis=-1)

Has anyone done something like that for loss calculationAlguém já fez algo parecido para o cálculo de perda?

How to create a custom keras loss function with opencv?

I'm developing a machine learning model using keras and I notice that the available losses functions are not giving the best results on my test set.

I am using an Unet architecture, where I input a (16,16,3) image and the net also outputs a (16,16,3) picture (auto-encoder). I notice that maybe one way to improve the model would be if I used a loss function that compares pixel to pixel on the gradients (laplacian) between the net output and the ground truth. However, I did not found any tutorial that would handle this kind of application, because it would need to use opencv laplacian function on each output image from the net.

The loss function would be something like this:

def laplacian_loss(y_true, y_pred):
  
  # y_true already is the calculated gradients, only needs to compute on the y_pred
  # calculates the gradients for each predicted image
  y_pred_lap = []
  for img in y_pred:
    laplacian = cv2.Laplacian( np.float64(img), cv2.CV_64F )
    y_pred_lap.append( laplacian )
  
  y_pred_lap = np.array(y_pred_lap)
  
  # mean squared error, according to keras losses documentation
  return K.mean(K.square(y_pred_lap - y_true), axis=-1)

Has anyone done something like that for loss calculation?

Como criar uma função de perda no keras que utiliza opencv?

Estou desenvolvendo um modelo de machine learning usando a biblioteca keras e percebo que as funções de perda disponíveis não estão dando os melhores resultados no meu conjunto de testes.

Estou usando uma arquitetura Unet, onde eu entro com uma imagem (16,16,3) e a rede também gera uma imagem (16,16,3) (auto-encoder). Percebi que talvez uma maneira de melhorar o modelo seria se eu usasse uma função de perda que compara pixel a pixel nos gradientes da imagem (laplaciano) entre a saída da rede e o conjunto de ground truth. No entanto, eu não encontrei nenhum tutorial que pudesse lidar com esse tipo de aplicação, porque ela precisaria usar a função laplacian do opencv em cada imagem de saída da rede.

A função de perda seria algo assim:

def laplacian_loss(y_true, y_pred):
  
  # y_true already is the calculated gradients, only needs to compute on the y_pred
  # calculates the gradients for each predicted image
  y_pred_lap = []
  for img in y_pred:
    laplacian = cv2.Laplacian( np.float64(img), cv2.CV_64F )
    y_pred_lap.append( laplacian )
  
  y_pred_lap = np.array(y_pred_lap)
  
  # mean squared error, according to keras losses documentation
  return K.mean(K.square(y_pred_lap - y_true), axis=-1)

Alguém já fez algo parecido para o cálculo de perda?

Fonte Link

How to create a custom keras loss function with opencv?

I'm developing a machine learning model using keras and I notice that the available losses functions are not giving the best results on my test set.

I am using an Unet architecture, where I input a (16,16,3) image and the net also outputs a (16,16,3) picture (auto-encoder). I notice that maybe one way to improve the model would be if I used a loss function that compares pixel to pixel on the gradients (laplacian) between the net output and the ground truth. However, I did not found any tutorial that would handle this kind of application, because it would need to use opencv laplacian function on each output image from the net.

The loss function would be something like this:

def laplacian_loss(y_true, y_pred):
  
  # y_true already is the calculated gradients, only needs to compute on the y_pred
  # calculates the gradients for each predicted image
  y_pred_lap = []
  for img in y_pred:
    laplacian = cv2.Laplacian( np.float64(img), cv2.CV_64F )
    y_pred_lap.append( laplacian )
  
  y_pred_lap = np.array(y_pred_lap)
  
  # mean squared error, according to keras losses documentation
  return K.mean(K.square(y_pred_lap - y_true), axis=-1)

Has anyone done something like that for loss calculation?