-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
1,828 additions
and
402 deletions.
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
30 changes: 30 additions & 0 deletions
30
benchmarks/classification/knn_imagenet/dist_test_knn_epoch.sh
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
set -x | ||
|
||
CFG=$1 | ||
EPOCH=$2 | ||
PY_ARGS=${@:3} | ||
GPUS=${GPUS:-8} | ||
NNODES=${NNODES:-1} | ||
NODE_RANK=${NODE_RANK:-0} | ||
PORT=${PORT:-29500} | ||
MASTER_ADDR=${MASTER_ADDR:-"127.0.0.1"} | ||
|
||
WORK_DIR=$(echo ${CFG%.*} | sed -e "s/configs/work_dirs/g")/ | ||
|
||
if [ ! -f $WORK_DIR/epoch_${EPOCH}.pth ]; then | ||
echo "ERROR: File not exist: $WORK_DIR/epoch_${EPOCH}.pth" | ||
exit | ||
fi | ||
|
||
python -m torch.distributed.launch \ | ||
--nnodes=$NNODES \ | ||
--node_rank=$NODE_RANK \ | ||
--master_addr=$MASTER_ADDR \ | ||
--nproc_per_node=$GPUS \ | ||
--master_port=$PORT \ | ||
tools/benchmarks/classification/knn_imagenet/test_knn.py $CFG \ | ||
--checkpoint $WORK_DIR/epoch_${EPOCH}.pth \ | ||
--work-dir $WORK_DIR --launcher="pytorch" ${PY_ARGS} |
27 changes: 27 additions & 0 deletions
27
benchmarks/classification/knn_imagenet/dist_test_knn_pretrain.sh
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
set -x | ||
|
||
CFG=$1 | ||
PRETRAIN=$2 # pretrained model | ||
PY_ARGS=${@:3} | ||
GPUS=${GPUS:-8} | ||
NNODES=${NNODES:-1} | ||
NODE_RANK=${NODE_RANK:-0} | ||
PORT=${PORT:-29500} | ||
MASTER_ADDR=${MASTER_ADDR:-"127.0.0.1"} | ||
|
||
# set work_dir according to config path and pretrained model to distinguish different models | ||
WORK_DIR="$(echo ${CFG%.*} | sed -e "s/configs/work_dirs/g")/$(echo $PRETRAIN | rev | cut -d/ -f 1 | rev)" | ||
|
||
python -m torch.distributed.launch \ | ||
--nnodes=$NNODES \ | ||
--node_rank=$NODE_RANK \ | ||
--master_addr=$MASTER_ADDR \ | ||
--nproc_per_node=$GPUS \ | ||
--master_port=$PORT \ | ||
tools/benchmarks/classification/knn_imagenet/test_knn.py $CFG \ | ||
--cfg-options model.backbone.init_cfg.type=Pretrained \ | ||
model.backbone.init_cfg.checkpoint=$PRETRAIN \ | ||
--work-dir $WORK_DIR --launcher="pytorch" ${PY_ARGS} |
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
|
||
# This file is borrowed from | ||
# https://github.com/facebookresearch/dino/blob/main/eval_knn.py | ||
|
||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
@torch.no_grad() | ||
def knn_classifier(train_features, | ||
train_labels, | ||
test_features, | ||
test_labels, | ||
k, | ||
T, | ||
num_classes=1000): | ||
"""Compute accuracy of knn classifier predictions. | ||
Args: | ||
train_features (Tensor): Extracted features in the training set. | ||
train_labels (Tensor): Labels in the training set. | ||
test_features (Tensor): Extracted features in the testing set. | ||
test_labels (Tensor): Labels in the testing set. | ||
k (int): Number of NN to use. | ||
T (float): Temperature used in the voting coefficient. | ||
num_classes (int): Number of classes. Defaults to 1000. | ||
""" | ||
top1, top5, total = 0.0, 0.0, 0 | ||
train_features = nn.functional.normalize(train_features, dim=1) | ||
test_features = nn.functional.normalize(test_features, dim=1) | ||
train_features = train_features.t() | ||
num_test_images, num_chunks = test_labels.shape[0], 100 | ||
# split all test images into several chunks to prevent out-of-memory | ||
imgs_per_chunk = num_test_images // num_chunks | ||
retrieval_one_hot = torch.zeros(k, num_classes).to(train_features.device) | ||
for idx in range(0, num_test_images, imgs_per_chunk): | ||
# get the features for test images | ||
features = test_features[idx:min((idx + | ||
imgs_per_chunk), num_test_images), :] | ||
targets = test_labels[idx:min((idx + imgs_per_chunk), num_test_images)] | ||
batch_size = targets.shape[0] | ||
|
||
# calculate the dot product and compute top-k neighbors | ||
similarity = torch.mm(features, train_features) | ||
distances, indices = similarity.topk(k, largest=True, sorted=True) | ||
candidates = train_labels.view(1, -1).expand(batch_size, -1) | ||
retrieved_neighbors = torch.gather(candidates, 1, indices) | ||
|
||
retrieval_one_hot.resize_(batch_size * k, num_classes).zero_() | ||
retrieval_one_hot.scatter_(1, retrieved_neighbors.view(-1, 1), 1) | ||
distances_transform = distances.clone().div_(T).exp_() | ||
probs = torch.sum( | ||
torch.mul( | ||
retrieval_one_hot.view(batch_size, -1, num_classes), | ||
distances_transform.view(batch_size, -1, 1), | ||
), | ||
1, | ||
) | ||
_, predictions = probs.sort(1, True) | ||
|
||
# find the predictions that match the target | ||
correct = predictions.eq(targets.data.view(-1, 1)) | ||
top1 = top1 + correct.narrow(1, 0, 1).sum().item() | ||
top5 = top5 + correct.narrow(1, 0, min( | ||
5, k)).sum().item() # top5 does not make sense if k < 5 | ||
total += targets.size(0) | ||
top1 = top1 * 100.0 / total | ||
top5 = top5 * 100.0 / total | ||
return top1, top5 |
36 changes: 36 additions & 0 deletions
36
benchmarks/classification/knn_imagenet/slurm_test_knn_epoch.sh
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
set -x | ||
|
||
PARTITION=$1 | ||
JOB_NAME=$2 | ||
CFG=$3 | ||
EPOCH=$4 | ||
PY_ARGS=${@:5} | ||
GPUS=${GPUS:-8} | ||
GPUS_PER_NODE=${GPUS_PER_NODE:-8} | ||
CPUS_PER_TASK=${CPUS_PER_TASK:-5} | ||
PORT=${PORT:-29500} | ||
SRUN_ARGS=${SRUN_ARGS:-""} | ||
|
||
WORK_DIR=$(echo ${CFG%.*} | sed -e "s/configs/work_dirs/g")/ | ||
|
||
if [ ! -f $WORK_DIR/epoch_${EPOCH}.pth ]; then | ||
echo "ERROR: File not exist: $WORK_DIR/epoch_${EPOCH}.pth" | ||
exit | ||
fi | ||
|
||
PYTHONPATH="$(dirname $0)/..":$PYTHONPATH \ | ||
srun -p ${PARTITION} \ | ||
--job-name=${JOB_NAME} \ | ||
--gres=gpu:${GPUS_PER_NODE} \ | ||
--ntasks=${GPUS} \ | ||
--ntasks-per-node=${GPUS_PER_NODE} \ | ||
--cpus-per-task=${CPUS_PER_TASK} \ | ||
--kill-on-bad-exit=1 \ | ||
${SRUN_ARGS} \ | ||
python -u tools/benchmarks/classification/knn_imagenet/test_knn.py $CFG \ | ||
--checkpoint $WORK_DIR/epoch_${EPOCH}.pth \ | ||
--cfg-options dist_params.port=$PORT \ | ||
--work-dir $WORK_DIR --launcher="slurm" ${PY_ARGS} |
33 changes: 33 additions & 0 deletions
33
benchmarks/classification/knn_imagenet/slurm_test_knn_pretrain.sh
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
set -x | ||
|
||
PARTITION=$1 | ||
JOB_NAME=$2 | ||
CFG=$3 | ||
PRETRAIN=$4 # pretrained model | ||
PY_ARGS=${@:5} | ||
GPUS=${GPUS:-8} | ||
GPUS_PER_NODE=${GPUS_PER_NODE:-8} | ||
CPUS_PER_TASK=${CPUS_PER_TASK:-5} | ||
PORT=${PORT:-29500} | ||
SRUN_ARGS=${SRUN_ARGS:-""} | ||
|
||
# set work_dir according to config path and pretrained model to distinguish different models | ||
WORK_DIR="$(echo ${CFG%.*} | sed -e "s/configs/work_dirs/g")/$(echo $PRETRAIN | rev | cut -d/ -f 1 | rev)" | ||
|
||
PYTHONPATH="$(dirname $0)/..":$PYTHONPATH \ | ||
srun -p ${PARTITION} \ | ||
--job-name=${JOB_NAME} \ | ||
--gres=gpu:${GPUS_PER_NODE} \ | ||
--ntasks=${GPUS} \ | ||
--ntasks-per-node=${GPUS_PER_NODE} \ | ||
--cpus-per-task=${CPUS_PER_TASK} \ | ||
--kill-on-bad-exit=1 \ | ||
${SRUN_ARGS} \ | ||
python -u tools/benchmarks/classification/knn_imagenet/test_knn.py $CFG \ | ||
--cfg-options model.backbone.init_cfg.type=Pretrained \ | ||
model.backbone.init_cfg.checkpoint=$PRETRAIN \ | ||
dist_params.port=$PORT \ | ||
--work-dir $WORK_DIR --launcher="slurm" ${PY_ARGS} |
Oops, something went wrong.