p2irc / deepplantphenomics

Deep learning for plant phenotyping.
GNU General Public License v2.0
133 stars 46 forks source link

Heatmap Loss Functions and L2/1 Loss Correction #45

Closed donovanlavoie closed 4 years ago

donovanlavoie commented 4 years ago

This changes the supported loss functions for heatmap object counting and changes the L2 loss calculations to not calculate norm, which would often make it no different from L1 loss.

Heatmap object counting had by default inherited the loss functions of semantic segmentation, which was just sigmoid cross entropy. This doesn't really apply to heatmap counting, however, as that treated pixel values as class probabilities rather than real values; it should therefore be using L2 and L1 loss on each pixel.

Implementing this change without duplicating the assemble_graph function required refactoring the loss function calculations. Every problem type now has an abstract method graph_problem_loss that takes the predictions and labels for a batch of samples and returns the appropriate loss for each sample, with the expectation of applying a mean to the samples losses.

While implementing this, a bug was found that affected the L2 loss for models with single-valued outputs like regression. The code for the L2 loss used tf.norm(), which calculates sqrt(sum(x2)), but with only 1 x value for a sample, this reduces to just |x|, the same as the L1 norm. The norm calls were replaced with explicit calculations of sum(x2) and sum(|x|), fixing this. Another bug with the smooth-L1 loss returning multiple values instead of just one value was also fixed.

All of the tests pass and all of the examples should run fine.

jubbens commented 4 years ago

Can we add smooth l1 to the possible loss functions for heatmaps too?