-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 seed #2744
Open
zhangyubo0722
wants to merge
4
commits into
PaddlePaddle:develop
Choose a base branch
from
zhangyubo0722:fix_seed
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
fix seed #2744
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,7 +63,7 @@ def __init__(self, config, mode="train"): | |
|
||
# set seed | ||
seed = self.config["Global"].get("seed", False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的get直接返回None会不会好点。
|
||
if seed or seed == 0: | ||
if seed or not isinstance(seed, bool): | ||
assert isinstance(seed, int), "The 'seed' must be a integer!" | ||
paddle.seed(seed) | ||
np.random.seed(seed) | ||
|
@@ -123,26 +123,6 @@ def __init__(self, config, mode="train"): | |
"epochs": self.config["Global"]["epochs"] | ||
}) | ||
|
||
# build dataloader | ||
if self.mode == 'train': | ||
self.train_dataloader = build_dataloader( | ||
self.config["DataLoader"], "Train", self.device, self.use_dali) | ||
if self.config["DataLoader"].get('UnLabelTrain', None) is not None: | ||
self.unlabel_train_dataloader = build_dataloader( | ||
self.config["DataLoader"], "UnLabelTrain", self.device, | ||
self.use_dali) | ||
else: | ||
self.unlabel_train_dataloader = None | ||
|
||
self.iter_per_epoch = len( | ||
self.train_dataloader) - 1 if platform.system( | ||
) == "Windows" else len(self.train_dataloader) | ||
if self.config["Global"].get("iter_per_epoch", None): | ||
# set max iteration per epoch mannualy, when training by iteration(s), such as XBM, FixMatch. | ||
self.iter_per_epoch = self.config["Global"].get( | ||
"iter_per_epoch") | ||
self.iter_per_epoch = self.iter_per_epoch // self.update_freq * self.update_freq | ||
|
||
if self.mode == "eval" or (self.mode == "train" and | ||
self.config["Global"]["eval_during_train"]): | ||
if self.eval_mode in ["classification", "adaface"]: | ||
|
@@ -183,21 +163,6 @@ def __init__(self, config, mode="train"): | |
else: | ||
self.eval_loss_func = None | ||
|
||
# build metric | ||
if self.mode == 'train' and "Metric" in self.config and "Train" in self.config[ | ||
"Metric"] and self.config["Metric"]["Train"]: | ||
metric_config = self.config["Metric"]["Train"] | ||
if hasattr(self.train_dataloader, "collate_fn" | ||
) and self.train_dataloader.collate_fn is not None: | ||
for m_idx, m in enumerate(metric_config): | ||
if "TopkAcc" in m: | ||
msg = f"Unable to calculate accuracy when using \"batch_transform_ops\". The metric \"{m}\" has been removed." | ||
logger.warning(msg) | ||
metric_config.pop(m_idx) | ||
self.train_metric_func = build_metrics(metric_config) | ||
else: | ||
self.train_metric_func = None | ||
|
||
if self.mode == "eval" or (self.mode == "train" and | ||
self.config["Global"]["eval_during_train"]): | ||
if self.eval_mode == "classification": | ||
|
@@ -231,13 +196,6 @@ def __init__(self, config, mode="train"): | |
[self.model, getattr(self, 'train_loss_func', None)], | ||
self.config["Global"]["pretrained_model"]) | ||
|
||
# build optimizer | ||
if self.mode == 'train': | ||
self.optimizer, self.lr_sch = build_optimizer( | ||
self.config["Optimizer"], self.config["Global"]["epochs"], | ||
self.iter_per_epoch // self.update_freq, | ||
[self.model, self.train_loss_func]) | ||
|
||
# AMP training and evaluating | ||
self.amp = "AMP" in self.config and self.config["AMP"] is not None | ||
self.amp_eval = False | ||
|
@@ -331,6 +289,48 @@ def __init__(self, config, mode="train"): | |
paddle.seed(int(seed) + dist.get_rank()) | ||
np.random.seed(int(seed) + dist.get_rank()) | ||
random.seed(int(seed) + dist.get_rank()) | ||
|
||
# build dataloader | ||
if self.mode == 'train': | ||
self.train_dataloader = build_dataloader( | ||
self.config["DataLoader"], "Train", self.device, self.use_dali) | ||
if self.config["DataLoader"].get('UnLabelTrain', None) is not None: | ||
self.unlabel_train_dataloader = build_dataloader( | ||
self.config["DataLoader"], "UnLabelTrain", self.device, | ||
self.use_dali) | ||
else: | ||
self.unlabel_train_dataloader = None | ||
|
||
self.iter_per_epoch = len( | ||
self.train_dataloader) - 1 if platform.system( | ||
) == "Windows" else len(self.train_dataloader) | ||
if self.config["Global"].get("iter_per_epoch", None): | ||
# set max iteration per epoch mannualy, when training by iteration(s), such as XBM, FixMatch. | ||
self.iter_per_epoch = self.config["Global"].get( | ||
"iter_per_epoch") | ||
self.iter_per_epoch = self.iter_per_epoch // self.update_freq * self.update_freq | ||
|
||
# build optimizer | ||
if self.mode == 'train': | ||
self.optimizer, self.lr_sch = build_optimizer( | ||
self.config["Optimizer"], self.config["Global"]["epochs"], | ||
self.iter_per_epoch // self.update_freq, | ||
[self.model, self.train_loss_func]) | ||
|
||
# build metric | ||
if self.mode == 'train' and "Metric" in self.config and "Train" in self.config[ | ||
"Metric"] and self.config["Metric"]["Train"]: | ||
metric_config = self.config["Metric"]["Train"] | ||
if hasattr(self.train_dataloader, "collate_fn" | ||
) and self.train_dataloader.collate_fn is not None: | ||
for m_idx, m in enumerate(metric_config): | ||
if "TopkAcc" in m: | ||
msg = f"Unable to calculate accuracy when using \"batch_transform_ops\". The metric \"{m}\" has been removed." | ||
logger.warning(msg) | ||
metric_config.pop(m_idx) | ||
self.train_metric_func = build_metrics(metric_config) | ||
else: | ||
self.train_metric_func = None | ||
|
||
# build postprocess for infer | ||
if self.mode == 'infer': | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不太理解这句话。分布式对随机种子有要求的地方,是不是只有distributed sampler。