Skip to content

boostcampaitech5/level1_imageclassification-cv-09

Repository files navigation

Image Classication Competition ๐Ÿ‘จโ€๐Ÿ‘จโ€๐Ÿ‘ฆโ€๐Ÿ‘ฆ

image

About

๋ชฉํ‘œ : ์‚ฌ๋žŒ์˜ ์ •๋ฉด ์‚ฌ์ง„์œผ๋กœ 1. ๋งˆ์Šคํฌ ์ฐฉ์šฉ ์—ฌ๋ถ€ 2. ์„ฑ๋ณ„ 3. ๋‚˜์ด๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด 18๊ฐœ class๋กœ ์˜ˆ์ธก

#ViT32 #ViT16 #ModelSoups #Relabeling #Oversampling #ContrastiveLearnig #WeightedAverageEnsemble #HardVoting #SoftVoting #Optuna #Wandb


Setting Step

1. ๊ฐ€์ƒ ํ™˜๊ฒฝ ์„ค์น˜

conda env create -f environment.yml
conda activate model_soups

2. ์ถ”๊ฐ€ ํŒจํ‚ค์ง€ ์„ค์น˜

  • wandb, albumentations ๋“ฑ ์ถ”๊ฐ€ ์„ค์น˜

3. pretrained model ๋‹ค์šด๋กœ๋“œ

  • Model soups์—์„œ ์ œ๊ณตํ•œ ViT-B/32 ๋ชจ๋ธ ๋‹ค์šด
  • ์ด 72๊ฐœ, ๋ณธ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์ตœ๋Œ€ 40๊ฐœ๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.
python main.py --download-models --model-location <where models will be stored>  

Training Step

1. ViT-B/32, ViT-B/16

1-1. Fine Tuning

python finetune.py --name {๋ชจ๋ธ๋ช…} --i {๋ชจ๋ธ number} --random-seed {์‹œ๋“œ ์„ค์ •}
  • Model soups์—์„œ ์ œ๊ณตํ•œ pretrained ๋ชจ๋ธ์„ 18๊ฐœ์˜ class vector๋ฅผ output์œผ๋กœ ํ•˜๋Š” 1๊ฐœ์˜ linear layer๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค.
  • ViT-B/16 ์˜ ๊ฒฝ์šฐ Model soups pretrained weight ๊ฐ€ ์—†์œผ๋ฏ€๋กœ clip ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ImageNet pretrained weight ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • --model {ViT-B/32 | ViT-B/16} : base ๋ชจ๋ธ ์„ค์ •
  • --name : ์ €์žฅํ•  ๋ชจ๋ธ ์ด๋ฆ„
  • --i : pretrained model์˜ index
  • --random-seed : random seed
  • --lr, --batch-size, --epochs, --data-location, --model-location : learning rate, batch size, epoch, ๋ฐ์ดํ„ฐ ๊ฒฝ๋กœ, ์ €์žฅํ•  ๋ชจ๋ธ ๊ฒฝ๋กœ
  • Tip : ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•™์Šต ์ž๋™ํ™”ํ•˜๊ธฐ. training.sh ํŒŒ์ผ ์ž‘์„ฑ ํ›„ ๋‹ค์Œ ๋ช…๋ น์–ด ์‹คํ–‰
bash training.sh

1-2. Data oversampling

  • Age ์†์„ฑ์˜ Old class์˜ ์ ์€ train dataset์œผ๋กœ ์ €ํ•˜๋œ ํ•™์Šต ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด Old class data๋งŒ์„ ์ถ”๊ฐ€๋กœ Over samplingํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • --old-aug True : Old class 1ํšŒ ์ถ”๊ฐ€ over sampling

1-3. Loss Function ์„ค์ •

  • Interclass์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋„“ํžˆ๊ณ , Intraclass์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ขํžˆ๋Š” Contrastive Learning์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • --loss-fn : ContrastiveLoss or CrossEntropyLoss, default๋Š” CrossEntropyLoss

2. Model Soups

  • Model soups๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋™์ผํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ pretrained ํ•™์Šต ๋ชจ๋ธ๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ํ•™์Šต ๋ชจ๋ธ์„ ๋งŒ๋“œ๋Š” ์•™์ƒ๋ธ” ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.
  • ์ˆ˜ํ–‰ ๊ณผ์ •์„ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  1. ์—ฌ๋Ÿฌ๊ฐœ์˜ pretrained model์„ Test ํ•˜์—ฌ Accuracy๋ฅผ ์–ป๋Š”๋‹ค.
  2. Accuracy ๊ฐ’์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  3. ์ˆœ์ฐจ์ ์œผ๋กœ ๋‹ค์Œ ๋ชจ๋ธ๊ณผ์˜ weight๊ฐ’์„ averageํ•˜์—ฌ ํ•˜๋‚˜์˜ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•œ๋‹ค.
  4. ์ƒ์„ฑ๋œ ๋ชจ๋ธ์˜ ์„ฑ๋Šฅ์„ ์ธก์ •์„ ํ•˜์˜€์„ ๋•Œ ํ˜„์žฌ๊นŒ์ง€ ๊ฐ€์žฅ ์ข‹์€ Accuracy๋ณด๋‹ค ์„ฑ๋Šฅ์ด ์ข‹์œผ๋ฉด ์ €์žฅํ•˜๊ณ , 3, 4๋ฒˆ์„ ๋ฐ˜๋ณตํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด averageํ•˜์ง€ ์•Š๊ณ  3, 4๋ฒˆ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
  5. ๊ฐ€์žฅ Accuracy๊ฐ€ ๋†’์€ ๋ชจ๋ธ์„ ์ตœ์ข… ๋ชจ๋ธ๋กœ ์„ ์ •ํ•œ๋‹ค.

2-1. Fine Tuning

  • Model soups์—์„œ ์ œ๊ณตํ•œ pretrained model์€ ViT-B/32 ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.
  • 1๋ฒˆ๊ณผ ๋™์ผํ•˜๊ฒŒ Fine tuning์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

2-2. Individual Evaluation

python main.py --eval-individual-models --name {๋ชจ๋ธ๋ช…} --model-num {๋ชจ๋ธ ๊ฐœ์ˆ˜} --random-seed {๋žœ๋ค ์‹œ๋“œ}
  • finetune์„ ํ†ตํ•ด ๋งŒ๋“  ๋ชจ๋ธ๋“ค์˜ accuracy๋ฅผ ์ธก์ •ํ•˜์—ฌ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
  • --name : ์ €์žฅ๋œ ๋ชจ๋ธ๋ช…
  • --model-num : Evaludationํ•  ๋ชจ๋ธ์˜ ๊ฐœ์ˆ˜
  • --random-seed : ๋žœ๋ค ์‹œ๋“œ
  • --val-ratio, --epoch, --data-location, --model-locatoin : validation dataset ๋น„์œจ, epoch, ๋ฐ์ดํ„ฐ์…‹ ๊ฒฝ๋กœ, ์ €์žฅํ•  ๋ชจ๋ธ ๊ฒฝ๋กœ
  • ์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋ฉด logs ํด๋” ์•ˆ์— ๊ฐ ๋ชจ๋ธ์˜ accuracy๊ฐ€ ์ ํžŒ jsonl ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

2-3. Greedy Soup

python main.py --greedy-soup --name {๋ชจ๋ธ๋ช…} --model-num {๋ชจ๋ธ ๊ฐœ์ˆ˜} --random-seed {๋žœ๋ค ์‹œ๋“œ}
  • individual Evaluation์—์„œ ์ €์žฅํ•œ ์—ฌ๋Ÿฌ ๋ชจ๋ธ์˜ accuracy์ •๋ณด๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ด๋Š” ๋ชจ๋ธ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ๋ถˆ๋Ÿฌ์™€ greedyํ•˜๊ฒŒ ์กฐํ•ฉํ•˜์—ฌ(averaging) ๋” ์ข‹์€ ์„ฑ๋Šฅ์„ ๋‚ด๋„๋ก ํ•˜๋Š” ์ตœ์ข… ๋ชจ๋ธ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • --name : ์ €์žฅ๋œ ๋ชจ๋ธ๋ช…
  • --model-num : Evaludationํ•  ๋ชจ๋ธ์˜ ๊ฐœ์ˆ˜
  • --random-seed : ๋žœ๋ค ์‹œ๋“œ
  • --val-ratio, --epoch, --data-location, --model-locatoin : validation dataset ๋น„์œจ, epoch, ๋ฐ์ดํ„ฐ์…‹ ๊ฒฝ๋กœ, ์ €์žฅํ•  ๋ชจ๋ธ ๊ฒฝ๋กœ
  • ์‹คํ–‰ ๊ฒฐ๊ณผ model ํด๋” ์•ˆ์— ์ตœ์ข… ๋ชจ๋ธ์ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.
  • log ํด๋” ์•ˆ์— ๋ณ€์ˆ˜ GREEDY_SOUP_LOG_FILE๊ฐ€ ์ด๋ฆ„์ž„ ๋กœ๊ทธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ๋กœ๊ทธ์—๋Š” averaging๋œ ๋ชจ๋ธ ์ •๋ณด๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

Inference Step

1. ์˜ˆ์ธก ์„ฑ๋Šฅ ๋ถ„์„ w/ Validation dataset

python validation.py --model-name {๋ชจ๋ธ๋ช….pt ํŒŒ์ผ}
  • Validation set์—์„œ class๋ณ„๋กœ ์ž˜๋ชป ์˜ˆ์ธกํ•œ ๋น„์œจ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  • ํ•ด๋‹น ๋ชจ๋ธ์„ ํ•™์Šตํ–ˆ์„ ๋•Œ, ์‚ฌ์šฉํ–ˆ๋˜ random seed ๊ฐ’์„ ๋™์ผํ•˜๊ฒŒ ์œ ์ง€ํ•ด ์ฃผ์–ด์•ผ ์ •ํ™•ํ•œ ํ™•๋ฅ ๊ณผ ์˜ˆ์ธก๊ฐ’์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.
  • --model-name : evaluationํ•  ๋ชจ๋ธ๋ช…,
  • --i : pretrained model์˜ index
  • --random-seed : ๋žœ๋ค ์‹œ๋“œ

1-1. Weighted Average Ensemble

  • Age class์˜ ๋ถ„๋ฅ˜ ์„ฑ๋Šฅ์„ ๋†’์ด๊ณ ์ž Age ์†์„ฑ๋งŒ์„ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ชจ๋ธ์„ ํ•™์Šตํ•˜์—ฌ, ์ด๋ฅผ ์ „์ฒด class(18๊ฐœ) ๋ถ„๋ฅ˜ ๋ชจ๋ธ์˜ ์˜ˆ์ธก๊ฐ’๊ณผ weighted sum์„ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • --weighted-ensemble : Age class๋ฅผ ํ•™์Šตํ•œ ๋ชจ๋ธ๋ช…, Default๋Š” None
python finetune_age.py --name {๋ชจ๋ธ๋ช…} --i {๋ชจ๋ธ number} --random-seed {์‹œ๋“œ ์„ค์ •}
  • finetune_age.py๋Š” Age class๋งŒ์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค.
  • --name, --i, --random-seed๋Š” finetune.py์™€ ๋™์ผํ•˜๊ฒŒ ์„ค์ •

1-2. Soft voting (Ensemble)

  • 2๊ฐœ์˜ ํ•™์Šต ๋ชจ๋ธ์˜ ๊ฐ class์˜ ํ™•๋ฅ ๊ฐ’์„ minmax scaling ํ›„ ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
  • --soft-voting : soft votingํ•  ๋ชจ๋ธ๋ช…, Default๋Š” None

1-3. Hard voting (Ensemble)

  • ์ตœ์ข… ์˜ˆ์ธก ๊ฒฐ๊ณผ csv ํŒŒ์ผ์˜ ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์ตœ์ข…์ ์œผ๋กœ Hard voting์„ ์ˆ˜ํ–‰ํ•˜์—ฌ Ensemble์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • hard_voting.ipynb ์„ ์‹คํ–‰ํ•˜์—ฌ, ์•™์ƒ๋ธ”์„ ์›ํ•˜๋Š” csv๋ฅผ ๊ฐ€์ง€๊ณ  hard voting์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ๊ทธ๋ฆผ์€ ์ถœ๋ ฅ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

2. Test w/ Test dataset

python inference.py --model-name {๋ชจ๋ธ๋ช….pt ํŒŒ์ผ}
  • ์ƒ์„ฑํ•œ ๋ชจ๋ธ ํŒŒ์ผ(.pt)๋ฅผ ์ด์šฉํ•˜์—ฌ Test data๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.
  • --model-name : inferenceํ•  ๋ชจ๋ธ๋ช…
  • --weighted-ensemble, --soft-voting : Weighted average ensemble ์‹œ ๋ชจ๋ธ๋ช…, Soft Voting ์‹œ ๋ชจ๋ธ๋ช…
  • ์ตœ์ข… ๊ฒฐ๊ณผ csv ํŒŒ์ผ์ด output ํด๋”์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

Additional Step

1. Dataset Relabeling

image

  • ์ž˜๋ชป ๋ผ๋ฒจ๋ง๋œ ๋ฐ์ดํ„ฐ id ๋ชฉ๋ก์„ ๋‹ด์€ relabel_dict ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Relabeling์„ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

2. Hyperparameter Tuning

python optuna_script.py
  • Optuna๋ฅผ ์ด์šฉํ•˜์—ฌ Hyper paramter tuning์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • optuna_script.py ํŒŒ์ผ์—์„œ hyper parameter tuning์„ ์œ„ํ•œ ์„ค์ •์„ ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ๋„ฃ์–ด์ฃผ๊ณ  ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

Result

  • Private score 3rd / F1 score - 0.7613 / Accuracy - 81.3175
  • Public score 6th / F1 score - 0.7653 / Accuracy - 81.3968 ํ™”๋ฉด ์บก์ฒ˜ 2023-04-26 022440

Contributors

์‹ ํ˜„์ค€ ํ•œํ˜„๋ฏผ ์ •ํ˜„์„ ๊น€์ง€๋ฒ” ์˜ค์œ ๋ฆผ






Reference

Model soups : Model soups: averaging weights of multiple fine-tuned models improves accuracy without increasing inference time.

ViT : https://github.com/google-research/vision_transformer

ContrastiveLoss : https://github.com/KevinMusgrave/pytorch-metric-learning

Optuna : https://optuna.org/

albumentations : https://albumentations.ai/

PyTorch : https://pytorch.org/

Wandb : https://wandb.ai/site

About

level1_imageclassification-cv-09 created by GitHub Classroom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published