Random crop error: ZeroDivisionError: float division by zero
See original GitHub issue❓ Questions and Help
I’m implementing random cropping method and my functions is the following.
class RandomCrop(object):
def __init__(self, size):
self.size = size
@staticmethod
def get_params(img, output_size):
"""Get parameters for ``crop`` for a random crop.
Args:
img (PIL Image): Image to be cropped.
output_size (tuple): Expected output size of the crop.
Returns:
tuple: params (i, j, h, w) to be passed to ``crop`` for random crop.
"""
w, h = img.size
th, tw = output_size
if w == tw and h == th:
return 0, 0, h, w
i = random.randint(0, h - th) #height
j = random.randint(0, w - tw) #width
return i, j, th, tw
def __call__(self, image, target):
i, j, h, w = self.get_params(image, self.size)
image = F.crop(image, i, j ,h, w)
target = target.crop([j, i, j+w, i+h])
return image, target
However, I’m getting the error message as follows:
2019-04-26 15:27:16,323 maskrcnn_benchmark.trainer INFO: Start training
Traceback (most recent call last):
File "tools/train_net.py", line 186, in <module>
main()
File "tools/train_net.py", line 179, in main
model = train(cfg, args.local_rank, args.distributed)
File "tools/train_net.py", line 85, in train
arguments,
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/engine/trainer.py", line 67, in do_train
loss_dict = model(images, targets)
File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/apex-0.1-py3.6-linux-x86_64.egg/apex/amp/_initialize.py", line 194, in new_fwd
**applier(kwargs, input_caster))
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/modeling/detector/generalized_rcnn.py", line 52, in forward
x, result, detector_losses = self.roi_heads(features, proposals, targets)
File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/roi_heads.py", line 39, in forward
x, detections, loss_mask = self.mask(mask_features, detections, targets)
File "/home/ubuntu/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__
result = self.forward(*input, **kwargs)
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/mask_head/mask_head.py", line 77, in forward
loss_mask = self.loss_evaluator(proposals, mask_logits, targets)
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/mask_head/loss.py", line 112, in __call__
labels, mask_targets = self.prepare_targets(proposals, targets)
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/mask_head/loss.py", line 94, in prepare_targets
segmentation_masks, positive_proposals, self.discretization_size
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/modeling/roi_heads/mask_head/loss.py", line 37, in project_masks_on_boxes
scaled_mask = cropped_mask.resize((M, M))
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/structures/segmentation_mask.py", line 486, in resize
resized_instances = self.instances.resize(size)
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/structures/segmentation_mask.py", line 393, in resize
resized_polygons.append(polygon.resize(size))
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/structures/segmentation_mask.py", line 277, in resize
ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(size, self.size))
File "/home/ubuntu/github/maskrcnn-benchmark/maskrcnn_benchmark/structures/segmentation_mask.py", line 277, in <genexpr>
ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(size, self.size))
ZeroDivisionError: float division by zero
Does anyone have any insight on this issue? Many thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
ZeroDivisionError: float division by zero in Python | bobbyhadz
To solve the error, use an if statement to check if the number you are dividing by is not zero, or handle the...
Read more >How to fix "ZeroDivisionError: float division by zero"
The ZeroDivisionError happens when you try to divide a number by 0, which as you know is a mathematical impossibility, just change the...
Read more >Python error ZeroDivisionError float division by zero - Edureka
A float number cannot be devided by zero. In this case the express is divided by zero, so the ZeroDivisionError: float division by...
Read more >Representing Rational Numbers With Python Fractions
In this tutorial, you'll learn about the Fraction data type in Python, which can represent rational numbers precisely without the rounding errors in...
Read more >Jython Scripting Examples - ImageJ Wiki
will create the correct float number in python, but will throw an ... e1 except ZeroDivisionError, e2: print "Dividing by zero doesn't make...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Looks like cropping is leaving some boxes empty and so the masks/boxes have 0 area. Pretty sure you need to filter out empty/severely cropped boxes when you call bbox.crop.
Hi, On using the code, I am getting the following assertion error. Can you help me out on this?
assert all(bbox.size == size for bbox in bboxes) AssertionError