7
$\begingroup$

Reading YOLO method to detect object in the image(paper),I want to train this tiny-VOLO v1 on Mathematica,but the special loss function is very difficult to write.

Total loss is:

enter image description here

For brief,I use 1-dim vec to make a example.

This is ordinary MSE function,but it does't have Mask like the loss in the image.

MeanSquaredLossLayer[][<|"Input" -> {1, 2, 3, 4, 5, 6}, 
 "Target" -> {3, 2, 1, 4, 6, 2}|>](*4.16667*)

So I define a function:

meanSquareLoss = Mean[#Mask*(#Input - #Target)^2] &;
N@meanSquareLoss[<|"Mask" -> {1, 1, 1, 1, 1, 1}, 
 "Input" -> {1, 2, 3, 4, 5, 6}, 
 "Target" -> {3, 2, 1, 4, 6, 2}|>](*4.16667*)

N@meanSquareLoss[<|"Mask" -> {1, 0, 0, 1, 0, 1}, 
 "Input" -> {1, 2, 3, 4, 5, 6}, 
 "Target" -> {3, 2, 1, 4, 6, 2}|>](*3.33333*)

net = NetGraph[{ThreadingLayer[(#1 - #2)^2 &], 
    ThreadingLayer[Times]}, {{NetPort["Input"], NetPort["Target"]} -> 1 -> 2,
    NetPort["Mask"] -> 2}]
Mean@net[<|"Input" -> {1, 2, 3, 4, 5, 6}, 
  "Target" -> {3, 2, 1, 4, 6, 2}, 
  "Mask" -> {1, 0, 0, 1, 0, 1}|>](*3.33333*)
(*Because mma don't have MeanLayer[]*)

This way to define a loss net it throws error:

NetGraph[{ThreadingLayer[(#1 - #2)^2 &], 
     ThreadingLayer[Times], TotalLayer[]}, {{NetPort["Input"], 
     NetPort["Target"]} -> 1 -> 2, NetPort["Mask"] -> 2 -> 3}]

NetGraph::tyinc: Value for "Input" port of layer 3 (a list of tensors) is inconsistent with value for "Output" port of layer 2 (a tensor).

But Using the TotalLayer[] is just want to calculate the sum of all the elements...

And I want to use ConstantArrayLayer.

MeanSquaredLossMaskLayer = NetGraph[{ThreadingLayer[(#1 - #2)^2 &],ThreadingLayer[Times], 
   ConstantArrayLayer[], MeanSquaredLossLayer[]}, 
   {{NetPort["Input"], NetPort["Target"]} -> 1 -> 2 -> NetPort[4, "Input"], 
   3 -> NetPort[4, "Target"], NetPort["Mask"] -> 2}]

But it can not use NetInitialize@MeanSquaredLossMaskLayer...

So how to define a custom loss function like this using *Layer funtion? if use 2-dims or higer dimension? How to add Mask into it?

And I find it has no *Layer function can do Map[Max, #, {2}]& that not only reduce the dimension of input but also use the Max to spicific dimension.

I try the NetMapOperator[ElementwiseLayer[Max], "Input" -> {20, 7, 7}],but the dimension of output is also {20, 7, 7}.

By the way PartLayer is not support {All,1}...

So how to write the custom loss function more effectly?

enter image description here

$\endgroup$
  • 1
    $\begingroup$ NetChain[{AggregationLayer[Max]}, "Input" -> {20, 7, 7}] $\endgroup$ – Alexey Golyshev Jun 13 '17 at 10:25
  • 1
    $\begingroup$ meanLayer = {ReplicateLayer[1], AggregationLayer[Mean], PartLayer[1]} $\endgroup$ – Alexey Golyshev Jun 13 '17 at 10:25
  • 1
    $\begingroup$ Replace TotalLayer with SummationLayer. $\endgroup$ – Alexey Golyshev Jun 13 '17 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.