question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
johncorringcommented, Apr 26, 2019

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.

0reactions
shamazharikhcommented, Nov 28, 2019

I used the original code and met the same problem

Did you solve it?

No, this problem randomly occurs and only occurred once, when I resume training and train for another backbone, seems that this problem is gone, sorry that I also have no idea why this happened.

I have encountered similar problem before that the randomly cropped path does not have any boxes so it cannot perform training normally. I have implemented a new version and it works good now.

`class RandomCrop(object):

Random crop data augmentation for training.

This is motivated from papers of face detection on WiderFace.

The min_scale and max_scale are relative to the smaller side

of the input image. First, we crop a square region by randomly

sample a float scale within [min_scale, max_scale], and then

crop the image. Second, the boxes are cropped accordingly with

boxes kept only if the IoU with original box is larger than

the given iou_thresh. If it ends up without any remaining boxes,

it will randomly select a box to re-crop to make sure the box is

within the cropped region (to guard the process to not crush

due to no target for an image).

def __init__(self, min_scale, max_scale, iou_thresh=0.5):
    # scale realtive to the smaller size of the image
    self.min_scale = min_scale
    self.max_scale = max_scale
    self.iou_thresh = iou_thresh

def get_params(self, image_size):
    w, h = image_size
    min_s = self.min_scale
    max_s = self.max_scale
    # randomly sample a scale within the range
    s = random.random() * (max_s - min_s) + min_s
    crop_size = int(min(w, h) * s)
    x1 = random.randint(0, w - crop_size)
    y1 = random.randint(0, h - crop_size)
    return x1, y1, crop_size

def get_safe_params(self, image_size, crop_size, target):
    w, h = image_size
    b = random.randint(0, len(target)-1) 
    x1,y1,x2,y2 = target[[b]]._split_into_xyxy()
    cx = int((x1 + x2) / 2)
    cy = int((y1 + y2) / 2)
    x1 = max(int(cx - crop_size/2), 0)
    y1 = max(int(cy - crop_size/2), 0)
    x2 = min(int(cx + crop_size/2), w)
    y2 = min(int(cy + crop_size/2), h)
    new_crop_size = min(x2-x1+1, y2-y1+1)
    return x1, y1, new_crop_size

def __call__(self, image, target):
    image_size = image.size
    x1, y1, crop_size = self.get_params(image_size)

    original_target = target.copy_with_fields(list(target.extra_fields.keys()))
    box = (x1, y1, x1+crop_size-1, y1+crop_size-1)
    target = target.crop(box)
    ious = target.area() / original_target.area()
    target = target[ious >= self.iou_thresh]

    if len(target) > 0:
        image = F.crop(image, y1, x1, crop_size, crop_size)
        return image, target

    # guard against no box available after crop
    x1, y1, crop_size = self.get_safe_params(image_size, crop_size, original_target)
    box = (x1, y1, x1+crop_size-1, y1+crop_size-1)
    target = original_target.crop(box) # re-crop original target
    image = F.crop(image, y1, x1, crop_size, crop_size)
    return image, target        

`

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found