Skip to content

UnboundLocalError occurred when not using onnx and with --max_data_loader_n_workers #1290

@frodo821

Description

@frodo821

I have been experiencing an issue with tag_images_by_wd14_tagger.py.
I ran finetune/tag_images_by_wd14_tagger.py by using the options shown below:

$ accelerate launch "sd-scripts/finetune/tag_images_by_wd14_tagger.py" \
>   --batch_size=8 --general_threshold=0.35 --character_threshold=0.35 \
>   --caption_extension=".txt" --caption_separator="," \
>   --model="SmilingWolf/wd-v1-4-convnextv2-tagger-v2" \
>   --max_data_loader_n_workers="2" --debug --remove_underscore \
>   --frequency_tags "data/images"

Then I got this UnboundLocalError:

Traceback (most recent call last):
  File "/workspace/sd-scripts/finetune/tag_images_by_wd14_tagger.py", line 514, in <module>
    main(args)
  File "/workspace/sd-scripts/finetune/tag_images_by_wd14_tagger.py", line 333, in main
    data = torch.utils.data.DataLoader(
UnboundLocalError: local variable 'torch' referenced before assignment
Traceback (most recent call last):
  File "/opt/conda/bin/accelerate", line 8, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.10/site-packages/accelerate/commands/accelerate_cli.py", line 46, in main
    args.func(args)
  File "/opt/conda/lib/python3.10/site-packages/accelerate/commands/launch.py", line 1075, in launch_command
    simple_launcher(args)
  File "/opt/conda/lib/python3.10/site-packages/accelerate/commands/launch.py", line 681, in simple_launcher
    raise subprocess.CalledProcessError(returncode=process.returncode, cmd=cmd)

I have investigated why the error occurs, and I noticed an unnecessary extra import of torch is made here:

# モデルを読み込む
if args.onnx:
import torch
import onnx

I suspect this extra import statement makes torch a local, unbound variable, and it is only available when run with the --onnx option.

torch is already imported this line:

And the import statement on line 115 must be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions