kohya-ss / sd-scripts

Apache License 2.0
4.61k stars 783 forks source link

Division By Zero Error #1365

Open rickbau5 opened 1 month ago

rickbau5 commented 1 month ago

Occasionally a dataset will result in a ZeroDivisionError, the dataset will reliably reproduce this error. The error is caused by reso[1] being 0 here.

In this case, no_upscale was set to True so we would be in the branch here: https://github.com/kohya-ss/sd-scripts/blob/main/library/train_util.py#L259-L280. There are no 0 height images, so it must be that image_width * image_height > self.max_area is true.

That branch causes bucket_height to be set to 0 with this logic.

bucket_height = resized_size[1] - resized_size[1] % self.reso_steps
Traceback (most recent call last):
  File "/sd-scripts/sdxl_train_network.py", line 184, in <module>
    trainer.train(args)
  File "/sd-scripts/train_network.py", line 199, in train
    train_dataset_group = config_util.generate_dataset_group_by_blueprint(blueprint.dataset_group)
  File "/sd-scripts/library/config_util.py", line 551, in generate_dataset_group_by_blueprint
    dataset.make_buckets()
  File "/sd-scripts/library/train_util.py", line 824, in make_buckets
    image_info.bucket_reso, image_info.resized_size, ar_error = self.bucket_manager.select_bucket(
  File "/sd-scripts/library/train_util.py", line 291, in select_bucket
    ar_error = (reso[0] / reso[1]) - aspect_ratio
ZeroDivisionError: division by zero
Cauldrath commented 1 month ago

One other thing to note is that you can get a width or height of zero if one of the dimensions is less than reso_steps. So, for the usual reso_steps of 64, a 63x63 or 1024x63 image will wind up with a height of zero and cause a divide by zero error later.