Skip to content

swin ported. ./run.sh to reproduce #1

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 24 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,27 @@ checkpoints/
.idea/
wandb/
*.pth
debug*

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
2 changes: 1 addition & 1 deletion datasets_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
from timm.data import create_transform
from datasets import load_dataset
from debug import DEBUG

class INatDataset(ImageFolder):
Expand Down Expand Up @@ -65,6 +64,7 @@ def build_dataset(is_train, data_path, args):
dataset = datasets.ImageFolder(root, transform=transform)
nb_classes = 1000
elif args.data_set == 'HUGGINGFACE':
from datasets import load_dataset
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved import here to reduce number of dependencies

def huggingface_transform(examples):
examples["image"] = [transform(x.convert(mode="RGB")) for x in examples["image"]]
return examples
Expand Down
1 change: 1 addition & 0 deletions debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEBUG=False
45 changes: 32 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ def get_args_parser():
help='use scale-aware embeds')
parser.add_argument('--grid-to-random-ratio', default=0.7, type=float, help='hybrid sampler grid to random ratio')

parser.add_argument('--model-type', default='deit', type=str, choices=["deit", "swin"])
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added special flag to decouple arguments of deit from "add on" models like swin (pyramid vit in future) which have vastly different config shapes.



return parser


Expand Down Expand Up @@ -356,19 +359,35 @@ def log_to_wandb(log_dict, step):
mixup_fn = None

print(f"Creating model: {args.model}")
model = create_model(
args.model,
pretrained=False,
num_classes=args.nb_classes,
drop_rate=args.drop,
drop_path_rate=args.drop_path,
drop_block_rate=None,
img_size=args.input_size,

Patch_layer=PatchEmbedHybrid,
use_learned_pos_embed = args.use_learned_pos_embed,
quantize_pos_embed = args.quantize_pos_embed
)
if args.model_type == "deit":
model = create_model(
args.model,
pretrained=False,
num_classes=args.nb_classes,
drop_rate=args.drop,
drop_path_rate=args.drop_path,
drop_block_rate=None,
img_size=args.input_size,

Patch_layer=PatchEmbedHybrid,
use_learned_pos_embed = args.use_learned_pos_embed,
quantize_pos_embed = args.quantize_pos_embed
)
elif args.model_type == "swin":
from swin.config import get_config
from swin.models import build_model
class _Args:
pass
_args = _Args()
cfg_path = "swin/swin_base_patch4_window7_224.yaml"
setattr(_args, "cfg", cfg_path)
setattr(_args, "opts", [])
setattr(_args, "local_rank", 0)
setattr(_args, "data_path", args.data_path)
config = get_config(_args)
model = build_model(config)
else:
assert False
Comment on lines +376 to +390
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hardcoded loading of swin vit of size B. We will not need other swin architectures, for simplicity implemented only this one.


if args.finetune:
if args.finetune.startswith('https'):
Expand Down
54 changes: 54 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

model_name=STD_SWINB
model_path=models/swin_base_patch4_window7_224.pth
input_size=224
patch_size=16
patch_shake=0
patch_dropout=0
patch_select=0
patch_select_mode=drop
patch_zoom=0


#model_name=$1
#model_path=$2
#input_size=$3
#patch_size=$4
#patch_shake=$5
#patch_dropout=$6
#patch_select=$7
#patch_select_mode=$8
#patch_zoom=$9

run_name="${model_name}_GRID${input_size}x${patch_size}_SHAKE${patch_shake}_DROP${patch_dropout}_SELECT${patch_select}_MODE${patch_select_mode}_ZOOM${patch_zoom}"
echo $run_name
if [[ $model_name == *"STD"* ]]; then
quantize_pos_embed=1
use_learned_pos_embed=1
else
quantize_pos_embed=0
use_learned_pos_embed=0
fi

python main.py \
--model-type swin \
--no-log-to-wandb \
--hard_dropout 0 \
--merge_patches 1 \
--model _ \
--data-path ~/datasets/imagenet/ \
--batch 16 --input-size $input_size --eval-crop-ratio 1.0 --seed 0 \
--grid-patch-size $patch_size \
--num_workers 10 \
--eval --resume=$model_path \
--patch_shake=$patch_shake \
--patch_dropout=$patch_dropout \
--patch_select=$patch_select \
--patch_select_mode=$patch_select_mode \
--quantize_pos_embed=$quantize_pos_embed \
--use_learned_pos_embed=$use_learned_pos_embed \
--patch_zoom=$patch_zoom \
--modify_patches

#--log-to-wandb --wandb_run_name=$run_name --wandb_model_name=$model_name \
14 changes: 8 additions & 6 deletions scripts/run_all.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
#!/bin/bash

source scripts/sweep_grid_scale.sh
set -e

#source scripts/sweep_zoom_exp.sh
#source scripts/sweep_dropout_exp.sh
#source scripts/sweep_shake_exp.sh
#source scripts/sweep_grid_scale.sh

source scripts/sweep_zoom_exp.sh
source scripts/sweep_dropout_exp.sh
source scripts/sweep_shake_exp.sh

source scripts/sweep_zoom_shake_exp.sh
source scripts/sweep_zoom_dropout_exp.sh
#source scripts/sweep_dropout_shake_exp.sh
source scripts/sweep_dropout_shake_exp.sh
#source random

#source edge counting
#source scripts/sweep_central_sampling_exp.sh

#source 30/70, 70/30 vits
#source scripts/sweep_dropout_vs_rescale_exp.sh
source scripts/sweep_dropout_vs_rescale_exp.sh
37 changes: 31 additions & 6 deletions scripts/single_grid_mod_exp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,27 @@ patch_select=$7
patch_select_mode=$8
patch_zoom=$9

run_name="${model_name}_GRID${input_size}x${patch_size}_SHAKE${patch_shake}_DROP${patch_dropout}_SELECT${patch_select}_MODE${patch_select_mode}_ZOOM${patch_zoom}"

# change if needed
hard_dropout=0
merge_patches=1
wandb="--log-to-wandb"
data_set="IMNET"
data_path="~/datasets/imagenet"
# change if needed

if [[ $model_name == *"PVT"* ]]; then
model_type="pvt"
model="pvt_v2_b5"
elif [[ $model_name == *"SWIN"* ]]; then
model_type="swin"
model="swinb_p4_w7"
else
model_type="deit"
model="deit_base_patch16_LS"
fi

run_name="${model_type}_${model_name}_HARD_DROP${hard_dropout}_MERGE_PATCHES_${merge_patches}_GRID${input_size}x${patch_size}_SHAKE${patch_shake}_DROP${patch_dropout}_SELECT${patch_select}_MODE${patch_select_mode}_ZOOM${patch_zoom}"
echo $run_name
if [[ $model_name == *"STD"* ]]; then
quantize_pos_embed=1
Expand All @@ -20,14 +40,19 @@ else
use_learned_pos_embed=0
fi


python main.py \
--model deit_base_patch16_LS \
--data-set HUGGINGFACE \
--data-path /home/jan.olszewski/datasets/imagenet-1k/default/1.0.0/a1e9bfc56c3a7350165007d1176b15e9128fcaf9ab972147840529aed3ae52bc \
--model-type $model_type \
--hard_dropout $hard_dropout \
--merge_patches $merge_patches \
--model $model \
--data-set $data_set \
--data-path $data_path \
--batch 16 --input-size $input_size --eval-crop-ratio 1.0 --seed 0 \
--grid-patch-size $patch_size \
--num_workers 10 \
--log-to-wandb --wandb_run_name=$run_name --wandb_model_name=$model_name \
$wandb \
--wandb_run_name=$run_name --wandb_model_name=$model_name \
--eval --resume=$model_path \
--patch_shake=$patch_shake \
--patch_dropout=$patch_dropout \
Expand All @@ -36,4 +61,4 @@ python main.py \
--quantize_pos_embed=$quantize_pos_embed \
--use_learned_pos_embed=$use_learned_pos_embed \
--patch_zoom=$patch_zoom \
--modify_patches
--modify_patches
19 changes: 3 additions & 16 deletions scripts/sweep_dropout_exp.sh
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
#!/bin/bash

set -e

model_name=ELASTIC_VITB
model_path=models/base_448.pth
input_size=448
patch_size=32
patch_shake=0
patch_dropout=0
patch_select=0
patch_select_mode=drop
patch_zoom=0

for ((patch_dropout=0; patch_dropout<=160; patch_dropout+=20)); do
source scripts/single_grid_mod_exp.sh $model_name $model_path $input_size $patch_size $patch_shake $patch_dropout $patch_select $patch_select_mode $patch_zoom
done

model_name=STD_VITB
model_path=https://dl.fbaipublicfiles.com/deit/deit_3_base_224_1k.pth
model_name=STD_SWINB
model_path=models/swin_base_patch4_window7_224.pth
input_size=224
patch_size=16
patch_shake=0
Expand Down
23 changes: 4 additions & 19 deletions scripts/sweep_dropout_shake_exp.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
#!/bin/bash

set -e

model_name=ELASTIC_VITB
model_path=models/base_448.pth
input_size=448
patch_size=32
patch_shake=0
patch_dropout=0
patch_select=0
patch_select_mode=drop
patch_zoom=0

for ((i=0; i<=8; i+=1)); do
patch_shake=$((2*i))
patch_dropout=$((20*i))
source scripts/single_grid_mod_exp.sh $model_name $model_path $input_size $patch_size $patch_shake $patch_dropout $patch_select $patch_select_mode $patch_zoom
done

model_name=STD_VITB
model_path=https://dl.fbaipublicfiles.com/deit/deit_3_base_224_1k.pth
model_name=STD_SWINB
model_path=models/swin_base_patch4_window7_224.pth
input_size=224
patch_size=16
patch_shake=0
Expand All @@ -31,4 +16,4 @@ for ((i=0; i<=8; i+=1)); do
patch_shake=$i
patch_dropout=$((20*i))
source scripts/single_grid_mod_exp.sh $model_name $model_path $input_size $patch_size $patch_shake $patch_dropout $patch_select $patch_select_mode $patch_zoom
done
done
38 changes: 6 additions & 32 deletions scripts/sweep_dropout_vs_rescale_exp.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
#!/bin/bash


model_name=ELASTIC_VITB
model_path=models/base_448.pth
input_size=448
patch_size=32
patch_shake=0
patch_dropout=0
patch_select=0
patch_select_mode=drop
patch_selects=(30 40 49)
patch_zoom=0

for patch_select in ${patch_selects[@]}; do
source scripts/single_grid_mod_exp.sh $model_name $model_path $input_size $patch_size $patch_shake $patch_dropout $patch_select $patch_select_mode $patch_zoom
done
set -e


patch_selects=(0 10 20 30 40 49)
model_name=ELASTIC_VITB
model_path=models/base_448.pth
input_size=448
patch_size=32
patch_shake=0
patch_dropout=0
patch_select=0
patch_select_mode=resize
patch_zoom=0

for patch_select in ${patch_selects[@]}; do
source scripts/single_grid_mod_exp.sh $model_name $model_path $input_size $patch_size $patch_shake $patch_dropout $patch_select $patch_select_mode $patch_zoom
done

model_name=STD_VITB
model_path=https://dl.fbaipublicfiles.com/deit/deit_3_base_224_1k.pth
model_name=STD_SWINB
model_path=models/swin_base_patch4_window7_224.pth
input_size=224
patch_size=16
patch_shake=0
Expand All @@ -47,8 +21,8 @@ for patch_select in ${patch_selects[@]}; do
done


model_name=STD_VITB
model_path=https://dl.fbaipublicfiles.com/deit/deit_3_base_224_1k.pth
model_name=STD_SWINB
model_path=models/swin_base_patch4_window7_224.pth
input_size=224
patch_size=16
patch_shake=0
Expand All @@ -59,4 +33,4 @@ patch_zoom=0

for patch_select in ${patch_selects[@]}; do
source scripts/single_grid_mod_exp.sh $model_name $model_path $input_size $patch_size $patch_shake $patch_dropout $patch_select $patch_select_mode $patch_zoom
done
done
Loading