detectron2
SANなど、detectron2をもとにして動かしているものもあるので、関数の勉強のメモ
Trainer Abstraction
We also provide a standardized “trainer” abstraction with a hook system that helps simplify the standard training behavior. It includes the following two instantiations:
SimpleTrainer provides a minimal training loop for single-cost single-optimizer single-data-source training, with nothing else. Other tasks (checkpointing, logging, etc) can be implemented using the hook system.
DefaultTrainer is a SimpleTrainer initialized from a yacs config, used by tools/train_net.py and many scripts. It includes more standard default behaviors that one might want to opt in, including default configurations for optimizer, learning rate schedule, logging, evaluation, checkpointing etc.
Trainer抽象化を行っている。
Simpletrainer
single-cost(?), single-optimizer, single-dataの最小限のtrainingloop
Defaulttrainer
tools/train_net.pyにあるyacs configから初期化されたSimpletrainer
より標準的なdefault behaviorを搭載
学習率のスケジュール、logging、evaluation、checkpointingを含む
Q. poetryでうまくインストールできないが?
A. pyproject.tomlのtool.poetry.dependenciesに以下を追加し、poetry update packageをする
build_model関数
Dataloader
Modelを使用する
yacs config objectから、モデルやその部分となるsub-modelを構築が可能である
code:python
from detectron2.modeling import build_model
model = build_model(cfg) # nn.Moduleとして渡す
build_modelは、モデル構造を作成し、ランダムなパラメータとして初期化する。既存のcheckpointから呼び出すためには以下を参照
Load/Save Checkpoint
code:python
from detectron2.checkpoint import DetectionCheckpointer
DetectionCheckpointer(model).load(filepath_or_url) # usually from cfg.MODEL.WEIGHTS
checkpointer = DetectionCheckpointer(model, save_dir="output")
checkpointer.save("XXX") # save to ./output/XXX.pth
pkl形式の場合は、更に詳細な以下のurlを参照
use a Model
それぞれのモデルはoutputs = model(inputs)でcallされる。このとき、inputsはList[dict]であり、各dictは1枚の画像に対応しており、modelの形に応じたkeyが必要になる。
そして、dictの内部は以下の形式である。
image
[C,H,W]形式でのTensorが入っている。
height, width
望ましい出力の高さと横幅が入っている。originalのsizeではないことに注意
instances
学習に使用するinstances object。具体的には以下
gt_boxes
gt_classes
gt_maskes
gt_keypoints
sem_seg
[H,W]の形のTensor[int]。ssのGTであり、値は0-indexでのカテゴリラベルを表している。
proposals
DatasetMapperから出力されるdefaultは上記のフォーマットに従っている。したがって、minibatch処理が終わったあとは自動的に上記のフォーマットになっていることに注意すること。
Config Setting
cfgインスタンスとして用いられている。initのコンストラクタのkwargsに渡されるためにやっている
code:python
# Usage 1: Decorator on __init__:
class A:
@configurable
def __init__(self, a, b=2, c=3):
pass
@classmethod
def from_config(cls, cfg): # 'cfg' must be the first argument
# Returns kwargs to be passed to __init__
return {"a": cfg.A, "b": cfg.B}
a1 = A(a=1, b=2) # regular construction
a2 = A(cfg) # construct with a cfg
a3 = A(cfg, b=3, c=4) # construct with extra overwrite
SAN側のinitとして、以下が渡されている。
code:python
{
"clip_visual_extractor": clip_visual_extractor,
"clip_rec_head": clip_rec_head,
"side_adapter_network": build_side_adapter_network(
cfg, clip_visual_extractor.output_shapes
),
"ov_classifier": ov_classifier,
"criterion": criterion,
"size_divisibility": cfg.MODEL.SAN.SIZE_DIVISIBILITY,
"asymetric_input": cfg.MODEL.SAN.ASYMETRIC_INPUT,
"clip_resolution": cfg.MODEL.SAN.CLIP_RESOLUTION,
"sem_seg_postprocess_before_inference": cfg.MODEL.SAN.SEM_SEG_POSTPROCESS_BEFORE_INFERENCE,
"pixel_mean": pixel_mean,
"pixel_std": pixel_std,
}
つまり、関数・クラスなど、全てのコンフィグ情報を包括的に管理可能なのがcfgインスタンス
逆に言えば、global変数のcfgファイルさえあれば、detectron2から脱却してpytorchオンリーで書ける?