#author("2024-10-15T18:53:11+09:00","default:ait-survey","ait-survey")
#author("2024-10-15T18:53:52+09:00","default:ait-survey","ait-survey")
*Detectron2での転移学習 with カスタムデータ(自作データ)&データ拡張の作業手順 [#ode10e7e]
In deep learning tasks, it's essential to define three key components: 'training data', 'data augmentation', and 'training methods'. Furthermore, perhaps supplementary information on the inference results is also necessary.~
**0. 学習や推論方法など:** [#ycc91643]
----
''設定ファイル'':[[Detectron2:https://github.com/facebookresearch/detectron2]]では,どのモデルやどのパラメータ(重み+バイアス)を使うか+ハイパーパラメータなどをファイル(&color(red){yaml};ファイル形式が採用されています.configsディレクトリに実践的なサンプルあり.要するに&color(red){設定ファイル};.)に記載して,それを読み込み,学習を実施するための標準的なスクリプト(train_net.py)が用意されています.デフォルトのtrain_net.pyでは,学習データの登録処理が含まれてなかった(どこかにサンプルプログラムを置いてくれてるかもしれません)ので,[[こちら:https://github.com/yoshiyama/detectron2_wkk-local_version/blob/master/tools/train_net.py]]+[[これ:https://github.com/yoshiyama/detectron2_wkk-local_version/blob/master/tools/register_custom_dataset.py]]に,その処理過程を加え,学習データの場所を引数で指定可能なスクリプトに改良した.~
''設定ファイル'':[[Detectron2:https://github.com/facebookresearch/detectron2]]では,どのモデルやどのパラメータ(重み+バイアス)を使うか+ハイパーパラメータなどをファイル(&color(red){yaml};ファイル形式が採用されています.configsディレクトリに実践的なサンプルあり.要するに&color(red){設定ファイル};.)に記載して,それを読み込み,学習を実施するための標準的なスクリプト(train_net.py)が用意されています.デフォルトのtrain_net.pyでは,学習データの登録処理が含まれてなかった(探し得なかった.どこかにサンプルプログラムを置いてくれてるかもしれません)ので,[[こちら:https://github.com/yoshiyama/detectron2_wkk-local_version/blob/master/tools/train_net.py]]+[[これ:https://github.com/yoshiyama/detectron2_wkk-local_version/blob/master/tools/register_custom_dataset.py]]に,その処理過程を加え,学習データの場所を引数で指定可能なスクリプトに改良した.~
----
''訓練'':train_net.py(学習データの登録(--dataset_rootでの指定.学習データの場所(train,valのディレクトリ(COCOデータセットの構成で)を有す)を指定.valも指定?した方がいい(学習後にvalデータで性能評価するプログラムになっている(train_net.py))+学習環境設定の読み込み(--config-fileでの指定))の実行となります.&color(red){データ拡張};用のtrain_net.pyは[[こちら:https://github.com/yoshiyama/detectron2_wkk-local_version/blob/master/tools/train_net_DA.py]].説明は[[こちら:https://detectron2.readthedocs.io/en/latest/modules/data_transforms.html]],チュートリアルは[[こちら:https://detectron2.readthedocs.io/en/latest/tutorials/augmentation.html]].ビルトインのデータ拡張内容は[[こちら:https://www.ait-survey.com/?%E3%83%87%E3%83%BC%E3%82%BF%E6%8B%A1%E5%BC%B5]].~
 python tools/train_net.py --config-file configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x_custom.yaml --dataset_root ./datasets/watanabe/ --output_dir ./output/watanabe
''評価'':学習済みモデル(重みとバイアス)を指定して性能評価を行う.なお,デフォルトでは,数量的な評価結果(mAPとか)しか出力されないので,推論画像も保存するように改変した([[here:https://github.com/yoshiyama/detectron2_wkk-local_version/blob/master/tools/train_net.py]]).評価結果の解釈は[[こちら:https://www.ait-survey.com/?%E7%89%A9%E4%BD%93%E6%A4%9C%E5%87%BA%E3%81%AE%E8%A9%95%E4%BE%A1%E3%81%AE%E8%A7%A3%E9%87%88]].
 python tools/train_net.py --config-file configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x_custom.yaml --dataset_root datasets/watanabe --eval-only MODEL.WEIGHTS output/watanabe/model_0159999.pth --output_dir output/watanabe/
''推論'':なお,学習後の重みによる推論は,demo/demo.pyを利用します(configファイル(設定ファイル)は例えば[[これ:https://github.com/yoshiyama/detectron2_wkk-local_version/blob/master/configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x_custom_test.yaml]]に変わる).例えば~
 python demo/demo.py --config-file configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x_custom_test.yaml --input datasets/val/JPEGImages/out1_0460_s.jpg --output datasets/result --opts MODEL.WEIGHTS output/model_0244999.pth
または,ディレクトリ内の画像を一気に処理するには,(でも推論結果の詳細情報(検出されたバウンディングボックスの位置数値情報など)は出力されない)~
 python demo/demo.py \
  --config-file configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x_custom_test.yaml \
  --input datasets/val/JPEGImages/*.jpg \
  --output datasets/result \
  --opts MODEL.WEIGHTS output/model_0244999.pth

%%なお,学習データの登録は,[[こちら:https://github.com/yoshiyama/detectron2_wkk/blob/local_version/register_custom_dataset.py]]のpythonスクリプトが有効かもしれません.%%~
学習の評価は~
 tensorboard --logdir=/home/survey/Desktop/detectron2_wkk-local_version/output/SETO_v1
なお,これは,
 tensorboard --logdir=/path/to/old_output_dir --port=6007
このようにして指定してもいい.
&ref(log_tensorboard2.jpg);~
**1. カスタム(自作)データの設定:** [#h7651a24]
扱うデータのセットの方法(書式,ディレクトリ構成など)については[[ここ:https://detectron2.readthedocs.io/en/latest/tutorials/builtin_datasets.html]]を参考にしてください。~
なお,[[Detectron2:https://github.com/facebookresearch/detectron2]]の使用では,&color(red){register_coco_instances};で学習データの登録が前提のようです.~
それ用のpythonスクリプト([[ここ:https://github.com/yoshiyama/detectron2_wkk/tree/local_version]]のregister_custom_dataset.py)を作りました.~

その前に&color(blue){labelme};(他のアプリでもいいが,われわれの研究室ではlabelmeを使用)で作成したannotaionファイル(jsonファイル)と画像をtrain用,val用,test用に分ける.8:1:1程度か?~

&ref(trainvaltest2.png);~
学習させるデータセットが重そうならば,[[こちら:https://github.com/yoshiyama/labelme_wkk/tree/under]](ここのresize.py(labelme用です))を使って画像サイズを変更しておくといいです.
***入力用にCOCOフォーマットへの変換*** [#x6bf2b31]
上のような構成において,それぞれのディレクトリ(暗に,train,test,valです)で&color(red){labelme2coco.py};でcocoフォーマットへ変換する.~
コマンドは以下のような感じ.labelmeのディレクトリにおいて実行.~
 python examples/instance_segmentation/labelme2coco.py /mnt/c/Users/keikan2/Desktop/labelme_reconst/train_input /mnt/c/Users/keikan2/Desktop/labelme_reconst/train --labels /mnt/c/Users/keikan2/Desktop/labelme_reconst/labels.txt
なお,labels.txtは,下のような表記.上2つ(__ignore__,_background_)は必ず書くのだと思ってます.~
 __ignore__
 _background_
 Hakusenn
下のように処理されていく~
&ref(lm_pr.png);~
最終的に下のようになり,目的とするannotaions.jsonができる.同様に,val,testも処理する.Detectron2の学習段階では,testは求められない.~
&ref(fin_lm.png);~
なお,datasetsというディレクトリはDetectron2にはない(detectron2/dataにある?)ので作っておく,~
&ref(tree2.jpg);
***Cityscapes*** [#naee02bb]
%%Cityscapesのfineレベルのデータから、poleとbuildingのクラスをインスタンス化するためには、cityscapesscriptsを使用します。以下は一般的な手順です。%%~
&color(red){cityscapesscripts};のjson2instanceImg.pyでいけるようです.~
ただし,helpers/labels.pyで&color(red){hasInstance};でインスタンス化したい対象(ここではpoleとbuilding)をTrueにしておかねばなりません.~
(車や道路などはdefaultでインスタンス化されてます.poleやbuildingはされてません)~
Terminalにおいて~
PYTHONPATHの設定(export PYTHONPATH="${PYTHONPATH}:/my/new/path"
)後に~
python json2instanceImg.py /mnt/c/Users/survey/Desktop/CITYSCAPES_DATASET/gtFine_trainvaltest/gtFine/train/aachen/aachen_000000_000019_gtFine_polygons.json /mnt/c/Users/survey/Desktop/CITYSCAPES_DATASET/gtFine_trainvaltest/gtFine/train/aachen/aachen_000000_000019_gtFine_polygons22345.png


	#!/bin/bash
	# Root directory where your json files are stored
	root_dir="/mnt/c/Users/survey/Desktop/CITYSCAPES_DATASET_pole_building/gtFine_trainvaltest/gtFine/"
	# Iterate over train, val and test directories
	for data_split in train val test
	do
	  # Iterate over each city directory in the data split
	  for city in $(ls $root_dir/$data_split)
	  do
		# Get the city directory path
		city_dir="$root_dir/$data_split/$city"
		# Iterate over each json file in the city directory
		for json_file in $city_dir/*_polygons.json
		do
		  # Construct the png filename from the json filename
		  png_file="${json_file%_polygons.json}_instanceIds.png"
		  # If png file already exists, delete it
		  if [ -f "$png_file" ]; then
			rm $png_file
		  fi
		  # Run the python script
		  python json2instanceImg.py $json_file $png_file
		done
	  done
	done


このスクリプトはCityscapesデータセットの全部の画像(というかjsonファイル)に対して適用されます。poleとbuildingのインスタンスだけが訓練データとして扱われ、他のすべてのインスタンスは無視されます。~
どうやら,labelTrainIds.pngは,自分で作成する必要があるようです.[[ここ:https://torch.classcat.com/2021/03/04/detectron2-tutorials-builtin-datasets/]]に書いてました.~
%%CITYSCAPES_DATASET=/path/to/abovementioned/cityscapes python cityscapesscripts/preparation/createTrainIdLabelImgs.py%%~
CITYSCAPES_DATASET=datasets/CITYSCAPES_DATASET_pole_building/gtFine_trainvaltest python cityscapesscripts/preparation/createTrainIdLabelImgs.py
***Custom data*** [#cbc5f9e2]
Cocoフォーマットにしておく。~
[[labelme:https://github.com/wkentaro/labelme]]なら[[labelme2coco:https://github.com/wkentaro/labelme/tree/main/examples/instance_segmentation#convert-to-coco-format-dataset]]を使うことができるようです.~
 python ./labelme2coco.py data_annotated data_dataset_coco --labels labels.txt
ただし,デフォルトだとclass idが0になってしまうので,ソースの改変が必要か?~
COCOフォーマットのデータセットを登録する際には、アノテーションファイル(annotations.json)と画像が存在するディレクトリ(JPEGImages)のパスが正しいことを確認してください。また、"thing_classes"で指定するクラスの名前は、アノテーションファイル内で使用されているものと一致していなければなりません。

**2. 転移学習:** [#d0f9c168]
***Mask R-CNN+Cityscapes*** [#t6eab983]
まずは,[[ここ:https://detectron2.readthedocs.io/en/latest/tutorials/getting_started.html]]を参考にするべきか~
次に、Detectron2のMask R-CNNを使用して転移学習を行います。Cityscapesの学習済みモデルを使用し、上記で作成した教師データに対して学習します。~
%%./train_net.py \%%
%%--config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml \%%
%%--num-gpus 1 SOLVER.IMS_PER_BATCH 2 SOLVER.BASE_LR 0.0025~%%~
[[Getting Started with Detectron2:https://detectron2.readthedocs.io/en/latest/tutorials/getting_started.html]]では,上記のようにかかれているが,cityscapesならばconfigsファイルは~
configs/Cityscapes/mask_rcnn_R_50_FPN.yamlを適用するのでは?~
そして,yamlファイルに&color(white,black){OUTPUT_DIR: "checkpoints/model"};を追記.しなければ学習後の重みは保存されない~
 export CUDA_VISIBLE_DEVICES=0,1 
 python train_net.py --config-file ../configs/Cityscapes/mask_rcnn_R_50_FPN_TL.yaml --num-gpus 1 SOLVER.IMS_PER_BATCH 2 SOLVER.BASE_LR 0.0025

この下は転移学習の方法の一例で,実行はしてないです.~
	from detectron2.data.datasets import register_coco_instances
	from detectron2.engine import DefaultTrainer
	from detectron2.config import get_cfg
	from detectron2 import model_zoo
	# 教師データセットの登録
	register_coco_instances("my_dataset_train", {}, "path_to_your_dataset.json", "path_to_your_images")
	# 設定の準備
	cfg = get_cfg()
	# Mask R-CNNの設定
	cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
	# Cityscapesの学習済みモデルを使用
	cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
	# 新たな教師データセットの指定
	cfg.DATASETS.TRAIN = ("my_dataset_train",)
	# 学習の開始
	trainer = DefaultTrainer(cfg)
	trainer.resume_or_load(resume=False)
	trainer.train()

ここでの主なポイントは、設定(cfg)においてCityscapesの学習済みモデル(重み)を指定し、新たに作成した(カスタム)教師データセットを訓練データとして指定することです。

このパイプラインは非常に基本的なものであり、多くの設定や詳細が省略されています。Detectron2とcityscapesscriptsの公式ドキュメンテーションを確認して、より詳細な設定を行ってください。また、データのパス、設定のパラメータ等はあなたの環境に合わせて適宜修正してください。

***Faster R-CNN+Custom Data*** [#qd456fc7]
Detectron2のFaster R-CNNを利用.~
だから,ひとまずは[[model zoo:https://github.com/facebookresearch/detectron2/blob/main/MODEL_ZOO.md#coco-object-detection-baselines]]を見よう.~
config file:configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml~
weights:model_weights/model_final_68b088.pkl~
ここはまだ編集中です.~
 export CUDA_VISIBLE_DEVICES=0,1 
 python train_net.py --config-file ../configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml --num-gpus 1 SOLVER.IMS_PER_BATCH 2 SOLVER.BASE_LR 0.0025 --opts MODEL.WEIGHTS ../checkpoints/model_final_be35db.pkl


***panoptic*** [#ja18414d]
ここはまだ編集中です.~
 export CUDA_VISIBLE_DEVICES=0,1 
 python train_net.py --config-file ../configs/Misc/panoptic_fpn_R_101_dconv_cascade_gn_3x.yaml --num-gpus 1 SOLVER.IMS_PER_BATCH 2 SOLVER.BASE_LR 0.0025 --opts MODEL.WEIGHTS ../model_weights/model_final_68b088.pkl

**3. データ拡張:** [#lbf4913e]
どうやら,detectron2の訓練での初期設定は,detectron2/data/detection_utils.pyに書かれているようです.そして,水平反転(鉛直反転)の設定をコントロールしているのが,detectron2/data/transforms/augmentation_impl.pyに書かれているclass RandomFlip(Augmentation):のようです(確率は0.5,水平反転が優先).

[[Cityscapes]]

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS