# Pixelwise loss weights in Flux?

**URL:** <https://discourse.julialang.org/t/pixelwise-loss-weights-in-flux/49332>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [October 30, 2020, 9:09pm UTC](https://discourse.julialang.org/t/pixelwise-loss-weights-in-flux/49332 "2020-10-30T21:09:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [October 30, 2020, 9:09pm UTC](https://discourse.julialang.org/t/pixelwise-loss-weights-in-flux/49332/1 "2020-10-30T21:09:03Z")

</div>

Hi all,

I’m interested in implementing a pixel-wise loss function in Flux where I would weight up pixels that separate segments from each other to force the network to learn those pixels preferentially. That is, I want to segment an input grayscale image into a binary mask, but want to make sure that the edges of the segments are as correct as possible. I’m struggling to see how I would pass these weights (which aren’t learned, they are precomputed) to the loss functions.

An example is panel D below (reproduced from the original UNet paper):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/7/f75d6512d210d470704d24a98be2f26801642181.jpeg)

These weights are specific to the training data so I would assume that you would group them with the ground truth binary mask (as an additional dimension) and then design a custom loss function that knows the mask is the first index in that dimension and the weights are the second? (for example).

Am I on the right track with my thinking?

---

<div class="post-metadata">

**Author:** ![tlnagy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlnagy/32/5815_2.png) [@tlnagy](https://discourse.julialang.org/u/tlnagy)\
**Post date:** [November 14, 2020, 12:09am UTC](https://discourse.julialang.org/t/pixelwise-loss-weights-in-flux/49332/2 "2020-11-14T00:09:56Z")

</div>

bump

---

<div class="post-metadata">

**Author:** ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)\
**Post date:** [November 14, 2020, 7:48am UTC](https://discourse.julialang.org/t/pixelwise-loss-weights-in-flux/49332/3 "2020-11-14T07:48:49Z")

</div>

Hi,  
for the pixelwise binary classification task, the final output of the network before the softmax will be a WxHx1xN array, let’s call it `ypred`. The weights in eq. 1 and 2 of the [Unet paper](https://arxiv.org/pdf/1505.04597.pdf) have the same role as the usual labels in classification tasks. You will have to arrange them into a WxHx1xN array, let’s call it `y`, then yoour loss will be `logitbinarycrossentropy(ypred, y)` that you find in Flux.Losses module.

If you have pixelwise multiclass classification instead, you will work with WxHxCxN arrays, with C the number of classes, and loss `logitcrossentropy(ypred, y; dims=3)`

You will find an implementation of UNet here [https://github.com/DhairyaLGandhi/UNet.jl](https://github.com/DhairyaLGandhi/UNet.jl)
