# 持續學習操作手冊
## Environment setup
* **MMDetection official github:** https://github.com/open-mmlab/mmdetection
* **SoftTeacher official github:** https://github.com/microsoft/SoftTeacher
1. [Download mmdetection by official guide](https://github.com/open-mmlab/mmdetection/blob/master/docs/en/get_started.md/#Installation)
2. Download SoftTeacher
```
git clone https://github.com/microsoft/SoftTeacher
make install
```
3. 將configs_rail放進SofTeacher資料夾底下
## Training
**Fully-supervised**
1. 打開 **faster_rcnn_fully_supervised_rail.py**
2. 將 **ann_file** 路徑改為json檔路徑
3. 將 **img_prefix** 改為圖資資料夾路徑
```python=
data = dict(
samples_per_gpu=5,
workers_per_gpu=4,
train=dict(
ann_file="/home/wayne/charley/SoftTeacher/dataset/railway/L_100/train/_annotations.coco.json",
img_prefix="/home/wayne/charley/SoftTeacher/dataset/railway/L_100/train/",
classes=classes
),
val=dict(
ann_file='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/_annotations.coco.json',
img_prefix='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/',
classes=classes
),
test=dict(
ann_file='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/_annotations.coco.json',
img_prefix='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/',
classes=classes
)
)
```
4. 執行 **dist_train.sh**
```scrip
bash tools/dist_train.sh configs_rail/soft_teacher/faster_rcnn_fully_supervised_rail.py <NUM_GPUS>
```
**Semi-supervised**
1. 打開 faster_rcnn_semi_supervised_rail.py
2. 路徑設定和fully-supervised相同
3. sup裡放置labeled的圖資
4. unsup裡放置unlabeled的圖資
```python=
data = dict(
samples_per_gpu=5,
workers_per_gpu=4,
train=dict(
sup=dict(
type="CocoDataset",
ann_file="/home/wayne/charley/SoftTeacher/dataset/railway/L_100/train/_annotations.coco.json",
img_prefix="/home/wayne/charley/SoftTeacher/dataset/railway/L_100/train/",
classes=classes
),
unsup=dict(
type="CocoDataset",
ann_file="/home/wayne/charley/SoftTeacher/dataset/railway/L_10_UL_90_plus300/train/ann_unlabel.json",
img_prefix="/home/wayne/charley/SoftTeacher/dataset/railway/L_10_UL_90_plus300/train/",
classes=classes
),
),
val=dict(
ann_file='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/_annotations.coco.json',
img_prefix='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/',
classes=classes
),
test=dict(
ann_file='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/_annotations.coco.json',
img_prefix='/home/wayne/charley/SoftTeacher/dataset/railway/L_100/val/',
classes=classes
),
sampler=dict(
train=dict(
sample_ratio=[1, 4],
)
),
)
```
5. 將 **dist_train_partially.sh** 改為以下程式碼
```scrip
#!/usr/bin/env bash
set -x
TYPE=$1
FOLD=$2
PERCENT=$3
GPUS=$4
PORT=${PORT:-29570}
PYTHONPATH="$(dirname $0)/..":$PYTHONPATH
if [[ ${TYPE} == 'baseline' ]]; then
python -m torch.distributed.launch --nproc_per_node=$GPUS --master_port=$PORT \
$(dirname "$0")/train.py configs/baseline/faster_rcnn_r50_caffe_fpn_coco_partial_180k.py --launcher pytorch \
--cfg-options fold=${FOLD} percent=${PERCENT} ${@:5}
else
python -m torch.distributed.launch --nproc_per_node=$GPUS --master_port=$PORT \
$(dirname "$0")/train.py configs_rail/soft_teacher/faster_rcnn_semi_supervised_rail.py --launcher pytorch \
--cfg-options fold=${FOLD} percent=${PERCENT} ${@:5}
fi
```
6. 執行 **dist_train_partially.sh**
```scrip
bash tools/dist_train_partially.sh semi 1 10 <NUM_GPUS>
```
## Inference
1. Image -> 使用demo/image_demo.py
2. Video -> 使用demo/video_demo.py
## 如何產生unlabeled data使用的json檔
1. 使用 **dataset/railway** 資料夾底下之 **img2coco_unlabel.py**
2. 將 **root** 改為放置unlabeled data的資料夾,即可產生訓練需要的json檔
```pytho=
import os
import json
from PIL import Image
img_list = []
root = '<unlabeled data路徑>'
total = os.listdir(root)
#print(total)
print('total img:', len(total))
id=0
for file in total:
#print('#####')
file_path = os.path.join(root,file)
#print("file: ", file, ' , fold_path:', file_path)
img = Image.open(file_path)
w = img.width
h = img.height
#print(w,h)
#id = os.path.splitext(file)[0]
img_data = {
'file_name': file,
'height': h,
'width': w,
'id': int(id)
}
id+=1
#print(img_data)
img_list.append(img_data)
all_dat = {
'images': img_list,
'annotations': [],
'categories': []
}
#print('all_dat:', all_dat)
with open('ann_unlabel.json', 'w') as f:
json.dump(all_dat, f)
```