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

fix multi-process dataloader with same random seeds #1078

Merged
merged 2 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion paddleseg/core/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import paddle
import paddle.nn.functional as F

from paddleseg.utils import TimeAverager, calculate_eta, resume, logger
from paddleseg.utils import TimeAverager, calculate_eta, resume, logger, worker_init_fn
from paddleseg.core.val import evaluate


Expand Down Expand Up @@ -113,6 +113,7 @@ def train(model,
batch_sampler=batch_sampler,
num_workers=num_workers,
return_list=True,
worker_init_fn=worker_init_fn,
)

if use_vdl:
Expand Down
5 changes: 5 additions & 0 deletions paddleseg/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import math
import os
import tempfile
import numpy as np
from urllib.parse import urlparse, unquote

import paddle
Expand Down Expand Up @@ -118,3 +119,7 @@ def resume(model, optimizer, resume_model):
format(resume_model))
else:
logger.info('No model needed to resume.')


def worker_init_fn(worker_id):
np.random.seed(np.random.get_state()[1][0] + worker_id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你好,这里有考虑epoch层面的seed吗?就像https://tanelp.github.io/posts/a-bug-that-plagues-thousands-of-open-source-ml-projects/ 中说的np.random.seed(initial_seed + epoch)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

啊,漏掉了,我补一下