【Pytorch】 CVRN/IS teardown reports

Tent teardown reports

1. CVRN

  • CVRN github: https://github.com/jxhuang0508/CVRN
  • UPSNet github: https://github.com/uber-research/UPSNet

1.1 Install CVRN

  1. Clone the repo:
    $ git clone https://github.com/jxhuang0508/CVRN.git
    $ cd CVRN
    
  2. Creat conda environment:
    $ conda env create -f environment.yaml
    
  3. Clone UPSNet:
    cd CVRN
    $ git clone https://github.com/uber-research/UPSNet.git
    
  4. Initialization:
    $ cd UPSNet
    $ sh init.sh
    $ cp -r lib/dataset_devkit/panopticapi/panopticapi/ .
    

1.2 Import Deeplab-v2

$ cd CVRN/UPSNet
$ git clone https://github.com/yzou2/CRST.git

1.3 Prepare Dataset (Download Cityscapes dataset at UPSNet/data/cityscapes)

  • Follow UPS/installation [https://github.com/uber-research/UPSNet#installation]
  • Making labelTrainIds.png in cityscape. [https://github.com/uber-research/UPSNet/issues/5]
  • Install pip library [https://github.com/mcordts/cityscapesScripts]
$ python -m pip install cityscapesscripts
$ cd /workspace/panoptic/CVRN/UPSNet/data/cityscapes/cityscapesScripts/cityscapesscripts/preparation
$ export CITYSCAPES_DATASET=/workspace/panoptic/CVRN/UPSNet/data/cityscapes
$ python createTrainIdLabelImgs.py
$ cd UPSNet
$ sh init_cityscapes.sh
$ cd ..
$ python cvrn/init_citiscapes_19cls_to_16cls.py

1.3 Prepare CVRN

$ cp cvrn/models/* UPSNet/upsnet/models
$ cp cvrn/dataset/* UPSNet/upsnet/dataset
$ cp cvrn/upsnet/* UPSNet/upsnet
  • From now on, CVRN/cvrn is not needed anymore.

1.5 Pre-trained models

Pre-trained models can be downloaded here and put in CVRN/pretrained_models

1.6 Evaluation

$ cd UPSNet
$ python upsnet/test_cvrn_upsnet.py --cfg ../config/cvrn_upsnet.yaml --weight_path ../pretrained_models/cvrn_upsnet.pth
$ 2021-06-10 14:20:09,688 | base_dataset.py | line 499:           |    PQ     SQ     RQ     N
$ 2021-06-10 14:20:09,688 | base_dataset.py | line 500: --------------------------------------
$ 2021-06-10 14:20:09,688 | base_dataset.py | line 505: All       |  34.0   68.2   43.4    16
$ 2021-06-10 14:20:09,688 | base_dataset.py | line 505: Things    |  27.9   73.6   37.3     6
$ 2021-06-10 14:20:09,688 | base_dataset.py | line 505: Stuff     |  37.7   65.0   47.1    10
$ python upsnet/test_cvrn_pfpn.py --cfg ../config/cvrn_pfpn.yaml --weight_path ../pretrained_models/cvrn_pfpn.pth
$ 2021-06-10 14:27:36,841 | base_dataset.py | line 361:           |    PQ     SQ     RQ     N
$ 2021-06-10 14:27:36,842 | base_dataset.py | line 362: --------------------------------------
$ 2021-06-10 14:27:36,842 | base_dataset.py | line 367: All       |  31.4   66.4   40.0    16
$ 2021-06-10 14:27:36,842 | base_dataset.py | line 367: Things    |  20.7   68.1   28.2     6
$ 2021-06-10 14:27:36,842 | base_dataset.py | line 367: Stuff     |  37.9   65.4   47.0    10
$ cd CVRN/UPSNet
$ git clone https://github.com/yzou2/CRST.git
$ conda install -c anaconda h5py
$ python upsnet/test_cvrn_psn.py --cfg ../config/cvrn_psn.yaml --weight_path ../pretrained_models/cvrn_psn_maskrcnn_branch.pth
$ 2021-06-10 23:18:22,662 | test_cvrn_psn.py | line 240: combined pano result:
$ 2021-06-10 23:20:32,259 | base_dataset.py | line 361:           |    PQ     SQ     RQ     N
$ 2021-06-10 23:20:32,261 | base_dataset.py | line 362: --------------------------------------
$ 2021-06-10 23:20:32,261 | base_dataset.py | line 367: All       |  32.1   66.6   41.1    16
$ 2021-06-10 23:20:32,261 | base_dataset.py | line 367: Things    |  21.6   68.7   30.2     6
$ 2021-06-10 23:20:32,261 | base_dataset.py | line 367: Stuff     |  38.4   65.3   47.6    10


python usp
  • Debug launch config
    • sys.path.append 를 이용해 환경변수를 설정해줘도 되지만, 아래와 같이 cwd 를 지정해줌으로써 debug되는 최초 위치를 설정할 수 있다. cwd의 Default 위치는 vscode root 폴더이다.
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": false,
            "args": [
                "--cfg",
                "/workspace/panoptic/CVRN/UPSNet/upsnet/panoptic_uda_experiments/cvrn_upsnet.yaml",
                "--weight_path",
                "/workspace/panoptic/CVRN/UPSNet/model/pretrained_model/cvrn_upsnet.pth"
            ],
            "cwd": "/workspace/panoptic/CVRN/UPSNet"
        }
    ]
}

1.6 Output format

>> list(output.keys())
['scores', 'boxes', 'masks', 'ssegs', 'panos', 'cls_inds', 'pano_cls_inds']

>> for k, v in output.items(): print(len(v), type(v[0]))
1, <class 'numpy.ndarray'> 
..

>> for k, v in output.items(): print(v[0].shape)
'scores': (10,) 				 # The number of object from instance segmentation.
'boxes': (10, 4) 
'masks': (10, 7, 28, 28) # Not softmax but ''sigmoid!'' 
'cls_inds': (10,) 	     # Class of each bbox ## array([1, 1, 1, 1, 1, 1, 1, 3, 3, 3])
'ssegs': (1, 800, 1600) 
# UPSNet only
'panos': (1, 800, 1600)  # As output of panoptic_head, panoptic_output in resnt.py 
'pano_cls_inds': (5,)    # The number of object from panoptic head with stricter threshold (when rcnn -> mask_head)
# test_cvrn_upsnet.py
'scores'          <= cls_probs
'boxes'           <= pred_boxes
'masks'           <= mask_probs
'cls_inds'        <= cls_inds
'ssegs'           <= fcn_prob
'panos'           <= panoptic_outputs (UPSNet only)
'pano_cls_inds'   <= panoptic_cls_inds (UPSNet only)

# configs
has_fcn_head <= Using outputs from feature pyramid network, Extract semantic segmentation results.

# resnet_upsnet_test.py
cls_score.size, bbox_pred.size = ([1000, 7]), ([1000, 28]) # rcnn results
cls_prob_all, mask_rois, cls_idx = self.mask_roi(rois, bbox_pred, cls_prob, data['im_info']) # rcnn 1000 -> NMS

# BaseDataset.py
get_combined_pan_result >> PS, PFPN
get_unified_pan_result  >> UPSNet

1.7 Train

1.7.1 Prepare Dataset (Download SYNTHIA dataset at UPSNet/data/synthia)

    • Download The SYNTHIA Dataset SYNTHIA-RAND-CITYSCAPES (CVPR16) The data folder is structured as follow:
      CVRN/UPSNet/data:
       │   ├── synthia/ 
       |   |   ├── images/
       |   |   ├── labels/
       |   |   ├── ...
       |   |   ├── ...
      
  1. Dataset preprocessing
    $ cd CVRN
    $ cp cvrn_train_r/UPSNet/crop640_synthia_publish.py UPSNet/crop640_synthia_publish.py
    $ cd UPSNet
    $ python crop640_synthia_publish.py
    
  2. Download the converted instance labels and put the two instance label files of cityscapes (‘instancesonly_gtFine_train.json’ and ‘instancesonly_gtFine_val.json’) under ‘CVRN/UPSNet/data/cityscapes/annotations_7cls’.

  3. Download the SYNTHIA instance label file (‘instancesonly_train.json’) and put it under ‘CVRN/UPSNet/data/synthia_crop640/annotations_7cls_filtered’.

1.7.2 Prepare CVRN Train

  1. creat environment:
    $ cd CVRN/cvrn_train_r/cvrn_env
    $ conda env create -f seg_cross_style_reg.yml
    $ conda env create -f seg_st.yml
    $ conda env create -f instance_seg.yml
    
  2. move files into UPSNet, respectively (if the shell file does not work well, please move all files from ‘cvrn_train_r/UPSNet’ into the cloned ‘UPSNet’ mannually):
    $ cd CVRN
    $ cp -r cvrn_train_r/UPSNet/data/cityscapes/annotations_7cls UPSNet/data/cityscapes/
    $ cp -r cvrn_train_r/UPSNet/data/synthia_crop640 UPSNet/data/
    $ cp cvrn_train_r/UPSNet/upsnet/dataset/* UPSNet/upsnet/dataset
    $ cp cvrn_train_r/UPSNet/upsnet/models/* UPSNet/upsnet/models
    $ cp cvrn_train_r/UPSNet/upsnet/panoptic_uda_experiments/* UPSNet/upsnet/panoptic_uda_experiments
    $ cp cvrn_train_r/UPSNet/upsnet/* UPSNet/upsnet
    
  3. clone ADVENT and CRST:
    $ cd CVRN
    $ git clone https://github.com/valeoai/ADVENT.git
    $ cd ADVENT
    $ git clone https://github.com/yzou2/CRST.git
    
  4. move files into ADVENT and CRST, respectively (if the shell file does not work well, please move all files from ‘cvrn_train_r/ADVENT’ into the cloned ‘ADVENT’ mannually, and similar for ‘cvrn_train_r/CRST’ and the cloned ‘CRST’): ```bash $ cd CVRN $ cp cvrn_train_r/ADVENT/advent/dataset/* ADVENT/advent/dataset $ cp cvrn_train_r/ADVENT/advent/domain_adaptation/* ADVENT/advent/domain_adaptation $ cp cvrn_train_r/ADVENT/advent/scripts/configs/* ADVENT/advent/scripts/configs $ cp cvrn_train_r/ADVENT/advent/scripts/* ADVENT/advent/scripts

$ cp cvrn_train_r/ADVENT/CRST/deeplab/* ADVENT/CRST/deeplab $ cp cvrn_train_r/ADVENT/CRST/* ADVENT/CRST





### 1.7.3 Train Processes (Instance segmentation branch):
- <u>Step 1</u>: Instance segmentation: cross-style regularization pre-training:
```bash
$ cd CVRN/UPSNet
$ conda activate instance_seg
$ CUDA_VISIBLE_DEVICES=7 python upsnet/train_seed1234_co_training_cross_style.py --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style.yaml

# Source (supervision) and Target (consistency) training
# Style trainsformation : Histogram matching 
# 1) 'frc_loss'  		  : fcn_roi_score (PFPN segmentation segmentation) <-> mask_score (instance segmentation)
# 2) 'rcnn_sida_loss' : cls_prob_sc(style transformed) <-> cls_prob
# 3) 'mask_sida_loss' : mask_prob_sc <-> mask_prob_p
# 4) 'fcn_sida_loss'  : fcn_prob <-> fcn_prob_sc
  • Step 2. Instance segmentation: evaluate cross-style pre-trained models:

(2.a). Evaluate cross-style pre-trained models on validation set over bbox (detection):

$ cd CVRN/UPSNet
$ conda activate instance_seg
$ CUDA_VISIBLE_DEVICES=1 python upsnet/test_resnet101_dense_detection.py --iter 1000  --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style.yaml

# without pickle.dump 
# Using pkl files in *every* iter, find the highest detection score.

(2.b). Evaluate cross-style pre-trained models on validation set over instance mask:

$ cd CVRN/UPSNet
$ conda activate instance_seg
$ CUDA_VISIBLE_DEVICES=2 python upsnet/test_resnet101_maskrcnn.py --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style.yaml

# with pickle.dump
# I add config iter : 23000 (da_maskrcnn_cross_style.yaml)
# cp CVRN/pretrained_model/cvrn_psn_maskrcnn_branch.pth CVRN/UPSNet/outputs/da_maskrcnn_cross_style/train/resnet_101_23000.pth

(2.c). Evaluate cross-style pre-trained models on training set (Target dataset) over instance mask:

$ cd CVRN/UPSNet
$ conda activate instance_seg
$ CUDA_VISIBLE_DEVICES=7 python upsnet/cvrn_test_resnet101_maskrcnn_PL.py --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style.yaml

# with pickle.dump 
  • Step 3. Instance segmentation: instance pseudo label generation and fusion with pre-trained model:
$ cd CVRN/UPSNet
$ conda activate instance_seg
$ python upsnet/cvrn_st_psudolabeling_fuse.py --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style.yaml

####################### !!!!!  Interactive segmentation needed  !!!!  ########################
# No model inference, all outputs of instances/segmentation is already extracted while evaluating
# for images: for bboxs: calculate *reliability* based on entropy of the results of both segmantic and instance.
# It aims to make "bbox and mask" ( instancesonly_gtFine_train_pseudo_labels--.json )

# 5/11 4:30 아직까지 분석결과, 기존 mask 결과나 bounding box 결과를 바꾸진 않는다. 
# 그저 계산한 reliability를 활용해서, ignore, iscrowd = 1 로 바꿔준다. 이 정보는 여기서 활용한다. https://github.com/uber-research/UPSNet/blob/master/upsnet/dataset/json_dataset.py
  • Step 4. Instance segmentation: re-train model over ‘generated and fused instance pseudo labels’: ```bash $ cd CVRN/UPSNet $ conda activate instance_seg $ CUDA_VISIBLE_DEVICES=4 python upsnet/train_seed1234_co_training_cross_style_stage2_ST_w_PL.py –cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style_ST.yaml

ISR (inter style regulization) + cityscape instance_seg pseudo label


- <u>Step 5</u>. Instance segmentation: **evaluate** retrained models:
```bash
$ cd CVRN/UPSNet
$ conda activate instance_seg
$ CUDA_VISIBLE_DEVICES=1 python upsnet/test_resnet101_dense_detection.py --iter 1000  --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style_ST.yaml
$ cd CVRN/UPSNet
$ conda activate instance_seg
$ CUDA_VISIBLE_DEVICES=2 python upsnet/test_resnet101_maskrcnn.py --iter 6000  --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style_ST.yaml
  • Step 6. Panoptic segmentation evaluation: Please refer to previous sections.

1.7.4 Train Processes (Semantic segmentation branch):

  • Step 1: Semantic segmentation: cross-style regularization pre-training: ```bash $ cd CVRN/ADVENT/advent/scripts $ conda activate seg_cross_style_reg $ CUDA_VISIBLE_DEVICES=7 python train_synthia.py –cfg configs/synthia_self_supervised_aux_cross_style.yml

Exploit DeeplabV2 for semantic segmentation only.

This paper accommodate ADVENT Code and Model.

Any kind of model for semantic segmentation can be uased.

But, in ADVENT/train_UDA.py, the methods (only consistency on hist_match) is completely different from ADVENT which adapt Discrimintor.


- <u>Step 2</u>: Semantic segmentation: **evaluate** cross-style pre-trained models:
```bash
$ cd CVRN/ADVENT/advent/scripts
$ conda activate seg_cross_style_reg
$ CUDA_VISIBLE_DEVICES=7 python test.py --cfg configs/synthia_self_supervised_aux_cross_style.yml

# with pickle.dump
# >> cfg.TEST.SNAPSHOT_MAXITER: 200 (*cityscape*에 대해 가장 높은 mIoU를 가지는 Deeplab모델을 찾는다.)
# cp CVRN/pretrained_model/cvrn_psn_deeplab_branch.pth /workspace/panoptic/CVRN/ADVENT/experiments/snapshots/SYNTHIA2Cityscapes_DeepLabv2_SSAUX_cross_style/model_200.pth
  • Step 3: Semantic segmentation: Pseudo label generation, fusion and retraining:

(3.a). Semantic segmentation: Generation of pseudo label for 2975 images with best model (replace {best_model_dir} with the pretrained best model)

$ cd CVRN/ADVENT/CRST/
$ conda activate seg_st
$ CUDA_VISIBLE_DEVICES=1 python2 crst_seg_aux_ss_trg_cross_style_pseudo_label_generation.py --random-mirror --random-scale --test-flipping \
--num-classes 16 --data-src synthia --data-tgt-train-list ./dataset/list/cityscapes/train.lst \
--save results/synthia_cross_style_ep6 --data-tgt-dir dataset/Cityscapes --mr-weight-kld 0.1 \
--eval-scale 0.5 --test-scale '0.5' --restore-from {best_model_dir} \
--num-rounds 1 --test-scale '0.5,0.8,1.0' --weight-sil 0.1 --epr 6

# My restore-from setting
$ CUDA_VISIBLE_DEVICES=1 python crst_seg_aux_ss_trg_cross_style_pseudo_label_generation.py --random-mirror --random-scale --test-flipping --num-classes 16 --data-src synthia --data-tgt-train-list ./dataset/list/cityscapes/train.lst --save results/synthia_cross_style_ep6 --data-tgt-dir /workspace/panoptic/CVRN/UPSNet/data/cityscapes --mr-weight-kld 0.1 --eval-scale 0.5 --test-scale '0.5' --restore-from /workspace/panoptic/CVRN/ADVENT/experiments/snapshots/SYNTHIA2Cityscapes_DeepLabv2_SSAUX_cross_style/model_200.pth --num-rounds 1 --test-scale '0.5,0.8,1.0' --weight-sil 0.1 --epr 6

# See only functions: main() and val()
# conda install -c conda-forge packaging
####### 1. temporal all prob save [functino val()]
#  >> ADVENT/CRST/results/synthia_cross_style_ep6/0$ ls
#  pred  pred_vis  prob  pseudo_label_color
####### 2. class-wise thresholding [function label_selection()]
#  >> ADVENT/CRST/results/synthia_cross_style_ep6/0$ ls
# pred  pred_vis  prob  pseudo_label  pseudo_label_color  weighted_pred_vis

(3.b). Semantic segmentation: Fusion of Pseudo label for 2975 images

$ cd CVRN/UPSNet
$ conda activate instance_seg
$ python upsnet/cvrn_st_psudolabeling_fuse_for_semantic_seg.py --cfg upsnet/panoptic_uda_experiments/da_maskrcnn_cross_style.yaml

####################### !!!!!  Interactive segmentation needed  !!!!  ########################
# No model inference, all outputs of instances/segmentation is already extracted while evaluating.
# for images: for bboxs: Compare the results of both segmantic and instance based on **Entropy value**.
# It aims to make "semantic segmentation pseudo label" ( fused_semantic_seg_pseudo_label.png )

(3.c). Semantic segmentation: Re-train for 2975 images (replace {best_model_dir} with the pretrained best model):

$ cd CVRN/ADVENT/CRST/
$ conda activate seg_st
$ CUDA_VISIBLE_DEVICES=1 python crst_seg_aux_ss_trg_cross_style_pseudo_label_retrain.py --random-mirror --random-scale --test-flipping \
--num-classes 16 --data-src synthia --data-tgt-train-list ./dataset/list/cityscapes/train.lst \
--save results/synthia_cross_style_ep6 --data-tgt-dir dataset/Cityscapes --mr-weight-kld 0.1 \
--eval-scale 0.5 --test-scale '0.5' --restore-from {best_model_dir} \
--num-rounds 1 --test-scale '0.5,0.8,1.0' --weight-sil 0.1 --epr 6

$ CUDA_VISIBLE_DEVICES=0 python crst_seg_aux_ss_trg_cross_style_pseudo_label_retrain.py --random-mirror --random-scale --test-flipping --num-classes 16 --data-src synthia --data-tgt-train-list ./dataset/list/cityscapes/train.lst --save results/synthia_cross_style_ep6 --data-tgt-dir /workspace/panoptic/CVRN/UPSNet/data/cityscapes --mr-weight-kld 0.1 --eval-scale 0.5 --test-scale '0.5' --restore-from /workspace/panoptic/CVRN/ADVENT/experiments/snapshots/SYNTHIA2Cityscapes_DeepLabv2_SSAUX_cross_style/model_200.pth --num-rounds 1 --test-scale '0.5,0.8,1.0' --weight-sil 0.1 --epr 6

1.7.5 Training Schedule for Panoptic segmentation:

Run Instance Step 1: ISR (inter sytle regularization)
Run Instance Step 2: evalutation over BBox / instance mask on 'source' / instance mask on 'target'

Run Semantic Step 1: ISR
Run Semantic Step 2: evaluation
Run Semantic Step 3 (a): semantic pseudo label generation

Run Instance Step 3: instance pseudo label generation and fusion
Run Instance Step 4: re-train model over generated and fused instance pseudo labels
Run Instance Step 5: evalutation over BBox / instance mask on 'source' / instance mask on 'target'

Run Semantic Step 3 (b): semantic pseudo label fusion
Run Semantic Step 3 (c): re-train model

Run Panoptic evaluation
  • [Semantic Step 3.b]

image-20220512162808740

2. UPSNet

2.1 data preprocessing

  • For Cityscapes:
  • Assuming you already downloaded Cityscapes dataset at $CITYSCAPES_ROOT and TrainIds label images are generated, please create a soft link by ln -s $CITYSCAPES_ROOT data/cityscapes under UPSNet_ROOT, and run init_cityscapes.sh to prepare Cityscapes dataset for UPSNet.
  • Run download_weights.sh to get trained model weights for Cityscapes and COCO.

2.2 Train and Evaluate

  • Training: python upsnet/upsnet_end2end_train.py --cfg upsnet/experiments/$EXP.yaml
  • Test: python upsnet/upsnet_end2end_test.py --cfg upsnet/experiments/$EXP.yaml
    • python upsnet/upsnet_end2end_test.py --cfg upsnet/experiments/upsnet_resnet50_cityscapes_4gpu.yaml --weight_path ./model/upsnet_resnet_50_cityscapes_12000.pth
    • python upsnet/upsnet_end2end_test.py --cfg upsnet/experiments/upsnet_resnet50_cityscapes_16gpu.yaml --weight_path ./model/upsnet_resnet_50_cityscapes_12000.pth
    • python upsnet/upsnet_end2end_test.py --cfg upsnet/experiments/upsnet_resnet101_cityscapes_w_coco_16gpu.yaml --weight_path ./model/upsnet_resnet_101_cityscapes_w_coco_3000.pth

3. Interactive segmentation

from isegm.utils import vis, exp
from isegm.inference import utils
from isegm.inference.predictors import get_predictor
draw = vis.draw_with_blend_and_clicks(sample.image, mask=pred_mask, clicks_list=clicks_list)
draw = np.concatenate((draw,
    255 * pred_mask[:, :, np.newaxis].repeat(3, axis=2),
    255 * (gt_mask > 0)[:, :, np.newaxis].repeat(3, axis=2)
), axis=1)
plt.figure(figsize=(20, 30))
plt.imshow(draw)
plt.show()
  • Demo.pyapp.pycontroller.py

  • Controller methods

    1. set_image = Read image, Call reset_last_object
    2. reset_last_object = Reset predictor, mask, and clicks.
    3. set_mask = If you want to load mask at a first.
    4. add_click = Use predictor and append probs_history.
    5. undo_click: X
    6. partially_finish_object = X, Append probs_history, Reset predictor, mask, and clicks.
    7. finish_object = X, Reset predictor, mask, and clicks.
    8. reset_predictor: X
    9. reset_init_mask: X
    10. reset_all = I made.
    11. Current_object_prob = Return last probs in history.
    12. is_incomplete_mask: X
    13. result_mask: Return mask (current_object_prob > threshold).
    14. get_visualization: Return ‘draw’.

© All rights reserved By Junha Song.