Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroDivisionError: float division by zero in loss function #203

Open
ZhangZhaofeng opened this issue Aug 2, 2019 · 5 comments
Open

ZeroDivisionError: float division by zero in loss function #203

ZhangZhaofeng opened this issue Aug 2, 2019 · 5 comments

Comments

@ZhangZhaofeng
Copy link

ZhangZhaofeng commented Aug 2, 2019

Run script in

dense_correspondence/experiments/shoes/training_shoes.ipynb

meet a ZeroDivisionError

Dataset is

shoe_train_4_shoes.yaml

It seems that in some case, the num_matches == 0 cause the error. How to fix it?

Error msg is as follows:

ZeroDivisionError Traceback (most recent call last)
in ()
5 print "training descriptor of dimension %d" %(d)
6 train = DenseCorrespondenceTraining(dataset=dataset, config=train_config)
----> 7 train.run()
8 print "finished training descriptor of dimension %d" %(d)

/home/zhang/code/dense_correspondence/training/training.pyc in run(self, loss_current_iteration, use_pretrained)
340 masked_non_matches_a, masked_non_matches_b,
341 background_non_matches_a, background_non_matches_b,
--> 342 blind_non_matches_a, blind_non_matches_b)
343
344

/home/zhang/code/dense_correspondence/loss_functions/loss_composer.pyc in get_loss(pixelwise_contrastive_loss, match_type, image_a_pred, image_b_pred, matches_a, matches_b, masked_non_matches_a, masked_non_matches_b, background_non_matches_a, background_non_matches_b, blind_non_matches_a, blind_non_matches_b)
31 masked_non_matches_a, masked_non_matches_b,
32 background_non_matches_a, background_non_matches_b,
---> 33 blind_non_matches_a, blind_non_matches_b)
34
35 if (match_type == SpartanDatasetDataType.SINGLE_OBJECT_ACROSS_SCENE).all():

/home/zhang/code/dense_correspondence/loss_functions/loss_composer.pyc in get_within_scene_loss(pixelwise_contrastive_loss, image_a_pred, image_b_pred, matches_a, matches_b, masked_non_matches_a, masked_non_matches_b, background_non_matches_a, background_non_matches_b, blind_non_matches_a, blind_non_matches_b)
82 matches_a, matches_b,
83 masked_non_matches_a, masked_non_matches_b,
---> 84 M_descriptor=pcl._config["M_masked"])
85
86 if pcl._config["use_l2_pixel_loss_on_background_non_matches"]:

/home/zhang/code/dense_correspondence/loss_functions/pixelwise_contrastive_loss.py in get_loss_matched_and_non_matched_with_l2(self, image_a_pred, image_b_pred, matches_a, matches_b, non_matches_a, non_matches_b, M_descriptor, M_pixel, non_match_loss_weight, use_l2_pixel_loss)
84
85
---> 86 match_loss, _, _ = PCL.match_loss(image_a_pred, image_b_pred, matches_a, matches_b)
87
88

/home/zhang/code/dense_correspondence/loss_functions/pixelwise_contrastive_loss.py in match_loss(image_a_pred, image_b_pred, matches_a, matches_b)
169 #print(match_loss)
170 #else:
--> 171 match_loss = 1.0 / num_matches * (matches_a_descriptors - matches_b_descriptors).pow(2).sum()
172
173 return match_loss, matches_a_descriptors, matches_b_descriptors

ZeroDivisionError: float division by zero

@ZhangZhaofeng ZhangZhaofeng changed the title ZeroDivisionError: float division by zero in loss ZeroDivisionError: float division by zero in loss function Aug 2, 2019
@manuelli
Copy link
Collaborator

I think this occurs when the dataloader returns an empty set of matches (which happens very rarely and is probably why we didn't catch it earlier). Should be a simple fix to add a check on whether the size is zero and skip it if so. Will PR a fix as soon as we have time.

@peteflorence
Copy link
Collaborator

peteflorence commented Oct 24, 2019 via email

@manuelli
Copy link
Collaborator

Yes that fixes it. So that's a good short term solution. @peteflorence if you PR I am happy to review and merge.

@bhazza
Copy link

bhazza commented Nov 18, 2019

After applying @peteflorence's fix dense_correspondence/experiments/shoes/training_shoes.ipynb script now runs fine. However, I still get exactly the same "ZeroDivisionError: float division by zero" error as per the OP when running the mug training script:
dense_correspondence/experiments/mugs/training_mugs.ipynb

@lix4
Copy link

lix4 commented Oct 12, 2020

I also got the same error when training on mugs_all.yaml. Here is the entire message. It looks like one of the mask is empty.
warning, empty mask b Traceback (most recent call last): File "training_tutorial.py", line 64, in <module> train.run() File "/home/yixuan/pytorch-dense-correspondence/dense_correspondence/training/training.py", line 347, in run gebm_a, gebm_b) File "/home/yixuan/pytorch-dense-correspondence/dense_correspondence/loss_functions/loss_composer.py", line 37, in get_loss graph_embedding_a, graph_embedding_b) File "/home/yixuan/pytorch-dense-correspondence/dense_correspondence/loss_functions/loss_composer.py", line 95, in get_within_scene_loss M_descriptor=0.5) File "/home/yixuan/pytorch-dense-correspondence/dense_correspondence/loss_functions/pixelwise_contrastive_loss.py", line 85, in get_loss_matched_and_non_matched_with_l2 match_loss, _, _ = PCL.match_loss(image_a_pred, image_b_pred, matches_a, matches_b) File "/home/yixuan/pytorch-dense-correspondence/dense_correspondence/loss_functions/pixelwise_contrastive_loss.py", line 165, in match_loss match_loss = 1.0 / num_matches * (matches_a_descriptors - matches_b_descriptors).pow(2).sum() ZeroDivisionError: float division by zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants