Skip to content

Commit

Permalink
fix v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lupin1998 committed Apr 8, 2022
1 parent 7d288a2 commit b8e65ad
Show file tree
Hide file tree
Showing 57 changed files with 1,828 additions and 402 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This repo will be continued to update in the next two months! Please watch us fo

Please refer to [CHANGELOG.md](docs/CHANGELOG.md) for details and release history.

[2020-04-08] Configs reoriganized and new methods supported in `OpenMixup` v0.2.0.
[2020-03-31] `OpenMixup` v0.2.0 is released.

## Installation
Expand All @@ -37,7 +38,7 @@ Then, see [tutorials](docs/tutorials) for more tech details (based on MMClassifi

## Benchmark and Model Zoo

[Model Zoos](docs/model_zoos) and list of [Awesome Mixups](docs/awesome_mixups) will be updated in the next two months (before 2022.04)!
[Model Zoos](docs/model_zoos) and lists of [Awesome Mixups](docs/awesome_mixups) have been released, and will be updated in the next two months. Checkpoints and traning logs will be updated soon!

## License

Expand All @@ -50,12 +51,12 @@ This project is released under the [Apache 2.0 license](LICENSE).

## Citation

If you find this project useful in your research, please consider cite:
If you find this project useful in your research, please consider cite our repo:

```BibTeX
@misc{2022openmixup,
title={{OpenMixup}: Open Mixup Toolbox and Benchmark for Visual Representation},
author={Li, Siyuan and Liu, Zichen and Wu, Di},
title={{OpenMixup}: Open Mixup Toolbox and Benchmark for Visual Representation Learning},
author={Li, Siyuan and Liu, Zichen and Wu, Di, Stan Z. Li},
howpublished = {\url{https://github.com/Westlake-AI/openmixup}},
year={2022}
}
Expand Down
30 changes: 30 additions & 0 deletions benchmarks/classification/knn_imagenet/dist_test_knn_epoch.sh
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 benchmarks/classification/knn_imagenet/dist_test_knn_pretrain.sh
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}
70 changes: 70 additions & 0 deletions benchmarks/classification/knn_imagenet/knn_classifier.py
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 benchmarks/classification/knn_imagenet/slurm_test_knn_epoch.sh
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 benchmarks/classification/knn_imagenet/slurm_test_knn_pretrain.sh
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}
Loading

0 comments on commit b8e65ad

Please sign in to comment.