atmaCup #11 の Public 4th / Pricvate 5th Solution のリポジトリです。

Overview

#11 atmaCup

目次

解法概要

詳細は discussion で公開しています [link]

3行まとめ:

  • SimSiam による事前学習
  • Classication / Regression それぞれのタスクで Fine-tuning
  • 後処理を行った上で Weight Optimization

ディレクトリ構成

.
├── input
│     └── atmaCup-11       # コンペデータを置く場所
├── output                 # 学習結果の出力先
└── src                    # preprocess, training, inference 等の code

./src 下の構成についてはその他補足に記載。

実行手順

以下ではスクリプトの実行を ./src ディレクトリで行ってください。

環境

GPU

  • TitanRTX(主にSimSiam と重い model の学習に使用)
  • GTX1080Ti(主に軽い model の学習と推論に使用)

batch size を落とす・Gradient Accumulation を使用する 等を行えば VRAM 容量が小さめの GPU でも動かせると思います。

Python & cuda

  • Python 3.8.6
  • CUDA 10.2 (CUDA driver 440.33.01)

主要なライブラリ

  • 抜け漏れがあるかもしれないです
  • 古すぎるとかでなければ Version が一致しなくても動くと思います
Name Version
albumentations 1.0.0
joblib 1.0.1
lightly 1.1.16
matplotlib 3.4.2
numpy 1.20.3
opencv-python 4.5.2.54
optuna 2.8.0
pandas 1.2.4
pytorch-pfn-extras 0.4.1
PyYAML 5.4.1
scikit-learn 0.24.2
scipy 1.6.3
timm 0.4.12
torch 1.9.0
torchvision 0.10.0
tqdm 4.61.0

準備

コンペティションデータの格納

コンペティションのページからダウンロードして ./input/atmaCup-11 に解凍、photos.zip もその場で解凍してください。
以下のような構成になることを想定しています。

.
├── input
│     └── atmaCup-11
│             ├── photos
│             ├── atmaCup#11_sample_submission.csv
│             ├── materials.csv
│             ├── techniques.csv
│             ├── test.csv
│             └── train.csv
.
.

前処理

以下を実行。

$ python preprocess.py

各画像のサイズ等が格納された img_info.csv 、データセット全体の(概算の)channel ごとの統計値が計算された stats_by_data.csvtrain.csv に Cross Validation のための分割(fold 列)が追加された train_sgkf-5fold.csv./input/atmaCup-11 下に生成されます。

学習

事前学習

まず ResNet18-D, ResNet34-D, ResNet50-D, Fast-ResNeSt50-D_1s4x24d の 4モデルについて SimSiam による事前学習を行います。 GPU に乗らない場合は gradient accumulation の使用を検討してください。

$ python train_simsiam.py -cfg exp_config/000.yml  # resnet18d
$ python train_simsiam.py -cfg exp_config/001.yml  # resnet34d
$ python train_simsiam.py -cfg exp_config/002.yml  # resnet50d
$ python train_simsiam.py -cfg exp_config/003.yml  # resnest50d_1s4x24d

Fine-tuning

自動で 5fold の training を実行。Regression / Classification の各タスクで行うので計8種のモデルが出来ます。 前述の SimSiam の学習結果が以下のように ./output下に出力されており、これらを読み込んで使います。

config file 内で ResNet18-D, ResNet34-D は 150 epoch, ResNet50-D, Fast-ResNeSt50-D_1s4x24d は 200 epoch 時点の事前学習モデルを使用するようにしてあります。(ただ gradient accumulation を使用すると少し挙動が変わるようなので、SimSiam での loss と std を確認して必要に応じて変更して下さい。)

.
├── output
│     ├── 000_resnet18d_simsiam
│     ├── 001_resnet34d_simsiam
│     ├── 002_resnet50d_simsiam
│     └── 003_resnest50d_1s4x24d_simsiam
.
.
Classification
$ python train.py -cfg exp_config/100.yml  # resnet18d
$ python train.py -cfg exp_config/101.yml  # resnet34d
$ python train.py -cfg exp_config/102.yml  # resnet50d
$ python train.py -cfg exp_config/103.yml  # resnest50d_1s4x24d
Regression
$ python train.py -cfg exp_config/200.yml  # resnet18d
$ python train.py -cfg exp_config/201.yml  # resnet34d
$ python train.py -cfg exp_config/202.yml  # resnet50d
$ python train.py -cfg exp_config/203.yml  # resnest50d_1s4x24d

推論

学習が完了していると ./output 下に各学習結果のディレクトリが生成されているはずです。これらを読み込んで使用します。

.
├── output
│     ├── 100_resnet18d_cls
│     ├── 101_resnet34d_cls
│     ├── 102_resnet50d_cls
│     ├── 103_resnest50d_1s4x24d_cls
│     ├── 200_resnet18d_reg
│     ├── 201_resnet34d_reg
│     ├── 202_resnet50d_reg
│     └── 203_resnest50d_1s4x24d_reg
.
.

モデルごと

各学習結果のディレクトリを指定する形で実行します。

!!注意!!:同じディレクトリ内に metric(今回は RMSE) での各 fold での best model が copy され、学習過程のチェックポイントは全て削除されます。

同じディレクトリ内に各 fold での best model での予測結果、5-fold averaging 、oof prediction ( + classification の場合は logit の状態のもの)、各 fold での CV の結果の csv が出力されます。logit 以外は後処理を実施した上での予測結果です。

Classification
$ python infer.py -e ../output/100_resnet18d_cls
$ python infer.py -e ../output/101_resnet34d_cls
$ python infer.py -e ../output/102_resnet50d_cls
$ python infer.py -e ../output/103_resnet50d_1s4x24d_cls
Regression
$ python infer.py -e ../output/200_resnet18d_reg
$ python infer.py -e ../output/201_resnet34d_reg
$ python infer.py -e ../output/202_resnet50d_reg
$ python infer.py -e ../output/203_resnet50d_1s4x24d_reg

アンサンブル

以下を実行してください。

$ python ensemble.py -cfg exp_config/900.yml

Classification/Regression モデルのみでの averaging 、全モデル(8 model)での averaging 、oputuna で weight optimization を行った結果、が出力されます。

その他補足

./src の構成について

少し補足しておくと、./src 下のディレクトリ・ファイルの中身はざっとこんな感じです。

.
├── src
│     ├── base_data         # コンペ問わず使いまわす dataset 等
│     ├── base_model        # コンペ問わず使いまわす model 等
│     ├── base_optimizer    # コンペ問わず使いまわす optimizer 等
│     ├── base_pfn_extras   # コンペ問わず使いまわす pfn-extras 関連
│     ├── utils             # その他の使いまわすコード
│     ├── data.py           # コンペ特有の dataset 等を作ったら書く
│     ├── model.py          # コンペ特有の model 等を作ったら書く
│     ├── global_config.py  # (コンペ特有の)全体的な設定などを記述
│     ├── preprocess.py     # コンペ特有の前処理
│     ├── train_simsiam.py  # SimSiam の学習
│     ├── train.py          # Fine-tuning の学習
│     ├── infer.py          # 推論
│     └── ensemble.py       # アンサンブル
.
.

base_XXXutils は固定で、コンペで都度都度必要になったものは model.pydata.py 等に新しく追加します。コンペ終了後「また使いそうだな」というものは base_XXX に統合する運用です(例えば今回なら SimSiam のために書いた Dataset を終了後に統合しました)。 一応再現性を保つという名目で model.pydata.pyglobal_config.pytrain[_simsiam].py は学習ごとに結果の出力先へコピーを取るようにしています。

train.py は基本使いまわしでコンペごとに一部(主にデータの読み込みの部分)を書き換えて使いますが、infer.py(, ensemble.py)は、指標等のせいで書き換える部分が多くなる場合がほとんどです(今回なら後処理の部分など)。

またこれは pytorch-pfn-extras のしかも Config System を使っている人にしか伝わらない話ですが、config_types の辞書は一旦各 base_XXX__init__.py に作って置き、それらを global_config.py 内で読み込んで一つの辞書(CONFIG_TYPES)に統合しています。data.pymodel.py で新しく作ったものについても global_config.py 内で追加します。

結果の再現性について

乱数等は固定するとともに torch.backends.cudnn.deterministic を True にしていますが、基本的に速度を優先して torch.backends.cudnn.benchmark を True にしているので実行ごとに結果が変わります(詳細:Reproducibility — PyTorch 1.9.0 documentation)。

完全に再現性を取りたい場合は torch.backends.cudnn.benchmark を False にすれば(多分)行けるはずです。

出力等について

  • このリポジトリは terminal での実行を前提としていますが、notebook に移植する場合は pfn-extras が出してくれるプログレスバーの表示がうまくいきません。もし移植するのであれば各 config yaml ファイルにある ProgressBar をコメントアウトし、train.py の 139行目にある Evaluator の引数 progress_bar を False にしてください。

  • 学習の出力結果を一切上げていないので何が出てくるか補足しておくと、学習ログの json ファイル、指定したタイミングでの model の snapshot、loss・metric・lr を可視化した png ファイルです。ここらへんの設定は config yaml ファイル の extensions で指定しています。

pytorch-pfn-extras使いでない方へ

特に Config System を使用しているせいで面食らう部分もあるかと思いますが、train[_simsiam].py を読んでいただけると流れ自体は basic な training loop とほぼ同じだとわかると思います(mixup とか gradient accumulation を入れたことでちょっとごちゃついてますが)。 manager と extensions の枠組みを使うことで素の training loop にあまり影響せずに前述の出力が出来るのが pytorch-pfn-extras の一番好きな所なので、興味がある方は是非使ってみてください!

Owner
Tawara
Research & Development Engineer, Kaggle 4x Master.
Tawara
Code for Max-Margin Contrastive Learning - AAAI 2022

Max-Margin Contrastive Learning This is a pytorch implementation for the paper Max-Margin Contrastive Learning accepted to AAAI 2022. This repository

Anshul Shah 12 Oct 22, 2022
Learning Correspondence from the Cycle-consistency of Time (CVPR 2019)

TimeCycle Code for Learning Correspondence from the Cycle-consistency of Time (CVPR 2019, Oral). The code is developed based on the PyTorch framework,

Xiaolong Wang 706 Nov 29, 2022
Adversarial Graph Representation Adaptation for Cross-Domain Facial Expression Recognition (AGRA, ACM 2020, Oral)

Cross Domain Facial Expression Recognition Benchmark Implementation of papers: Cross-Domain Facial Expression Recognition: A Unified Evaluation Benchm

89 Dec 09, 2022
Contour-guided image completion with perceptual grouping (BMVC 2021 publication)

Contour-guided Image Completion with Perceptual Grouping Authors Morteza Rezanejad*, Sidharth Gupta*, Chandra Gummaluru, Ryan Marten, John Wilder, Mic

Sid Gupta 6 Dec 27, 2022
📚 A collection of Jupyter notebooks for learning and experimenting with OpenVINO 👓

A collection of ready-to-run Python* notebooks for learning and experimenting with OpenVINO developer tools. The notebooks are meant to provide an introduction to OpenVINO basics and teach developers

OpenVINO Toolkit 840 Jan 03, 2023
Bytedance Inc. 2.5k Jan 06, 2023
A set of Deep Reinforcement Learning Agents implemented in Tensorflow.

Deep Reinforcement Learning Agents This repository contains a collection of reinforcement learning algorithms written in Tensorflow. The ipython noteb

Arthur Juliani 2.2k Jan 01, 2023
SSD: Single Shot MultiBox Detector pytorch implementation focusing on simplicity

SSD: Single Shot MultiBox Detector Introduction Here is my pytorch implementation of 2 models: SSD-Resnet50 and SSDLite-MobilenetV2.

Viet Nguyen 149 Jan 07, 2023
The project was to detect traffic signs, based on the Megengine framework.

trafficsign 赛题 旷视AI智慧交通开源赛道,初赛1/177,复赛1/12。 本赛题为复杂场景的交通标志检测,对五种交通标志进行识别。 框架 megengine 算法方案 网络框架 atss + resnext101_32x8d 训练阶段 图片尺寸 最终提交版本输入图片尺寸为(1500,2

20 Dec 02, 2022
Backdoor Attack through Frequency Domain

Backdoor Attack through Frequency Domain DEPENDENCIES python==3.8.3 numpy==1.19.4 tensorflow==2.4.0 opencv==4.5.1 idx2numpy==1.2.3 pytorch==1.7.0 Data

5 Jun 18, 2022
YOLOX-Paddle - A reproduction of YOLOX by PaddlePaddle

YOLOX-Paddle A reproduction of YOLOX by PaddlePaddle 数据集准备 下载COCO数据集,准备为如下路径 /ho

QuanHao Guo 6 Dec 18, 2022
Pca-on-genotypes - Mini bioinformatics project - PCA on genotypes

Mini bioinformatics project: PCA on genotypes This repo contains the code from t

Maria Nattestad 8 Dec 04, 2022
[CVPR 2022] Official Pytorch code for OW-DETR: Open-world Detection Transformer

OW-DETR: Open-world Detection Transformer (CVPR 2022) [Paper] Akshita Gupta*, Sanath Narayan*, K J Joseph, Salman Khan, Fahad Shahbaz Khan, Mubarak Sh

Akshita Gupta 127 Dec 27, 2022
PyTorch implementation of our Adam-NSCL algorithm from our CVPR2021 (oral) paper "Training Networks in Null Space for Continual Learning"

Adam-NSCL This is a PyTorch implementation of Adam-NSCL algorithm for continual learning from our CVPR2021 (oral) paper: Title: Training Networks in N

Shipeng Wang 34 Dec 21, 2022
Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow

Mask R-CNN for Object Detection and Segmentation This is an implementation of Mask R-CNN on Python 3, Keras, and TensorFlow. The model generates bound

Matterport, Inc 22.5k Jan 04, 2023
Deep Learning Based EDM Subgenre Classification using Mel-Spectrogram and Tempogram Features"

EDM-subgenre-classifier This repository contains the code for "Deep Learning Based EDM Subgenre Classification using Mel-Spectrogram and Tempogram Fea

11 Dec 20, 2022
Code for KDD'20 "An Efficient Neighborhood-based Interaction Model for Recommendation on Heterogeneous Graph"

Heterogeneous INteract and aggreGatE (GraphHINGE) This is a pytorch implementation of GraphHINGE model. This is the experiment code in the following w

Jinjiarui 69 Nov 24, 2022
⚡️Optimizing einsum functions in NumPy, Tensorflow, Dask, and more with contraction order optimization.

Optimized Einsum Optimized Einsum: A tensor contraction order optimizer Optimized einsum can significantly reduce the overall execution time of einsum

Daniel Smith 653 Dec 30, 2022
[NeurIPS-2021] Mosaicking to Distill: Knowledge Distillation from Out-of-Domain Data

MosaicKD Code for NeurIPS-21 paper "Mosaicking to Distill: Knowledge Distillation from Out-of-Domain Data" 1. Motivation Natural images share common l

ZJU-VIPA 37 Nov 10, 2022
This thesis is mainly concerned with state-space methods for a class of deep Gaussian process (DGP) regression problems

Doctoral dissertation of Zheng Zhao This thesis is mainly concerned with state-space methods for a class of deep Gaussian process (DGP) regression pro

Zheng Zhao 21 Nov 14, 2022