-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Reduce variance of classification references evaluation #4609
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f61b17
WIP
NicolasHug 5264b1a
i'm not inspired to write a message
NicolasHug 40a42ed
avoid some duplication
NicolasHug 4985725
Only warn on rank == 0
NicolasHug cb4c7d2
Merge branch 'main' of github.com:pytorch/vision into ref_classif_ran…
NicolasHug eaa0536
hopefully fix flake8
NicolasHug 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import datetime | ||
import os | ||
import time | ||
import warnings | ||
|
||
import presets | ||
import torch | ||
|
@@ -54,6 +55,8 @@ def evaluate(model, criterion, data_loader, device, print_freq=100, log_suffix=" | |
model.eval() | ||
metric_logger = utils.MetricLogger(delimiter=" ") | ||
header = f"Test: {log_suffix}" | ||
|
||
num_processed_samples = 0 | ||
with torch.no_grad(): | ||
for image, target in metric_logger.log_every(data_loader, print_freq, header): | ||
image = image.to(device, non_blocking=True) | ||
|
@@ -68,7 +71,23 @@ def evaluate(model, criterion, data_loader, device, print_freq=100, log_suffix=" | |
metric_logger.update(loss=loss.item()) | ||
metric_logger.meters["acc1"].update(acc1.item(), n=batch_size) | ||
metric_logger.meters["acc5"].update(acc5.item(), n=batch_size) | ||
num_processed_samples += batch_size | ||
# gather the stats from all processes | ||
|
||
num_processed_samples = utils.reduce_across_processes(num_processed_samples) | ||
if ( | ||
hasattr(data_loader.dataset, "__len__") | ||
and len(data_loader.dataset) != num_processed_samples | ||
and torch.distributed.get_rank() == 0 | ||
): | ||
# See FIXME above | ||
warnings.warn( | ||
f"It looks like the dataset has {len(data_loader.dataset)} samples, but {num_processed_samples} " | ||
"samples were used for the validation, which might bias the results. " | ||
"Try adjusting the batch size and / or the world size. " | ||
"Setting the world size to 1 is always a safe bet." | ||
) | ||
|
||
metric_logger.synchronize_between_processes() | ||
|
||
print(f"{header} Acc@1 {metric_logger.acc1.global_avg:.3f} Acc@5 {metric_logger.acc5.global_avg:.3f}") | ||
|
@@ -147,7 +166,7 @@ def load_data(traindir, valdir, args): | |
print("Creating data loaders") | ||
if args.distributed: | ||
train_sampler = torch.utils.data.distributed.DistributedSampler(dataset) | ||
test_sampler = torch.utils.data.distributed.DistributedSampler(dataset_test) | ||
test_sampler = torch.utils.data.distributed.DistributedSampler(dataset_test, shuffle=False) | ||
else: | ||
train_sampler = torch.utils.data.RandomSampler(dataset) | ||
test_sampler = torch.utils.data.SequentialSampler(dataset_test) | ||
|
@@ -164,7 +183,11 @@ def main(args): | |
|
||
device = torch.device(args.device) | ||
|
||
torch.backends.cudnn.benchmark = True | ||
if args.use_deterministic_algorithms: | ||
torch.backends.cudnn.benchmark = False | ||
torch.use_deterministic_algorithms(True) | ||
else: | ||
torch.backends.cudnn.benchmark = True | ||
|
||
train_dir = os.path.join(args.data_path, "train") | ||
val_dir = os.path.join(args.data_path, "val") | ||
|
@@ -277,6 +300,10 @@ def main(args): | |
model_ema.load_state_dict(checkpoint["model_ema"]) | ||
|
||
if args.test_only: | ||
# We disable the cudnn benchmarking because it can noticeably affect the accuracy | ||
torch.backends.cudnn.benchmark = False | ||
torch.backends.cudnn.deterministic = True | ||
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. I deliberately choose not to set
|
||
|
||
evaluate(model, criterion, data_loader_test, device=device) | ||
return | ||
|
||
|
@@ -394,6 +421,9 @@ def get_args_parser(add_help=True): | |
default=0.9, | ||
help="decay factor for Exponential Moving Average of model parameters(default: 0.9)", | ||
) | ||
parser.add_argument( | ||
"--use-deterministic-algorithms", action="store_true", help="Forces the use of deterministic algorithms only." | ||
) | ||
|
||
return parser | ||
|
||
|
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.