# cnos **Repository Path**: mlyin/cnos ## Basic Information - **Project Name**: cnos - **Description**: 基于CAD的新型对象分割--unseen objects instance segmentation - **Primary Language**: Python - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-08-30 - **Last Updated**: 2023-08-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README

CNOS: A Strong Baseline for CAD-based Novel Object Segmentation

Van Nguyen NguyenThibault GroueixGeorgy PonimatkinVincent LepetitTomáš Hodaň

![framework](./media/framework.png) ![qualitative](./media/qualitative.png)

CNOS is a simple three-stage approach for CAD-based novel object segmentation. It is based on [Segmenting Anything](https://github.com/facebookresearch/segment-anything), [DINOv2](https://github.com/facebookresearch/dinov2) and can be used for any objects without retraining. CNOS outperforms the supervised MaskRCNN (in CosyPose) which was trained on target objects. CNOS has been used as the baseline for [Task 5](https://bop.felk.cvut.cz/leaderboards/detection-unseen-bop23/core-datasets/) and [Task 6](https://bop.felk.cvut.cz/leaderboards/segmentation-unseen-bop23/core-datasets/) in [BOP challenge 2023](https://bop.felk.cvut.cz/challenges/bop-challenge-2023/)! ![bo results](./media/bop_results.png) Here are some qualitative results of CNOS on the YCBV dataset. We are displaying only detections with a confidence score greater than 0.5 for better visibility. ![ycbv](./media/ycbv.gif) If our project is helpful for your research, please consider citing : ```latex @article{nguyen_2023_cnos, title={CNOS: A Strong Baseline for CAD-based Novel Object Segmentation}, author={Nguyen, Van Nguyen and Groueix, Thibault and Ponimatkin, Georgy and Lepetit, Vincent and Hodan, Tomas}, journal={arXiv preprint arXiv:2307.11067}, year={2023}} ``` You can also put a star :star:, if the code is useful to you. If you like this project, check out related works from our group: - [NOPE: Novel Object Pose Estimation from a Single Image (arXiv 2023)](https://github.com/nv-nguyen/nope) - [Templates for 3D Object Pose Estimation Revisited: Generalization to New objects and Robustness to Occlusions (CVPR 2022)](https://github.com/nv-nguyen/template-pose) - [PIZZA: A Powerful Image-only Zero-Shot Zero-CAD Approach to 6DoF Tracking (3DV 2022)](https://github.com/nv-nguyen/pizza) - [BOP visualization toolkit](https://github.com/nv-nguyen/bop_viz_kit) ## Updates: - Adding [tutorial](https://github.com/nv-nguyen/cnos#testing-on-custom-datasets-rocket) to run CNOS on custom datasets ## Installation :construction_worker:
Click to expand Please make sure that you update this [user's configuration](https://github.com/nv-nguyen/cnos/blob/main/configs/user/default.yaml) before conducting any experiments. ### 1. Create conda environment ``` conda env create -f environment.yml conda activate cnos # for using SAM pip install git+https://github.com/facebookresearch/segment-anything.git # for using fastSAM pip install ultralytics ``` ### 2. Datasets and model weights #### 2.1. Download datasets from [BOP challenge](https://bop.felk.cvut.cz/datasets/): ``` python -m src.scripts.download_bop ``` #### 2.2. Rendering templates with [Pyrender](https://github.com/mmatl/pyrender): This rendering is fast. For example, using a single V100 GPU, it can be done within 10 minutes. Alternatively, you can access the rendered output through [this Google Drive link (4.64GB)](https://drive.google.com/file/d/1yz2-q74PGws1OtDalX3ySo7AhiwWveK1/view?usp=sharing) and unzip it into $ROOT_DIR. ``` python -m src.scripts.render_template_with_pyrender ``` #### 2.3. Download model weights of [Segmenting Anything](https://github.com/facebookresearch/segment-anything): ``` python -m src.scripts.download_sam ``` #### 2.4. Download model weights of [Fast Segmenting Anything](https://github.com/CASIA-IVA-Lab/FastSAM): ``` python -m src.scripts.download_fastsam ``` #### 2.5. Download [BlenderProc4BOP](https://bop.felk.cvut.cz/datasets/) set: This is only required when you want to use realistic rendering with BlenderProc for seven core datasets of BOP challenge. ``` python -m src.scripts.download_train_pbr ```
## Testing on [BOP datasets](https://bop.felk.cvut.cz/datasets/) :rocket: We provide CNOS's predictions for seven core dataset of BOP challenge with both SAM and FastSAM models in [this link](https://drive.google.com/drive/folders/1yGRKpz1RI4h5-u0drusVeXPuAsg_GIO5?usp=sharing).
Click to expand 1. Run CNOS to get predictions: ``` export DATASET_NAME=lmo # adding CUDA_VISIBLE_DEVICES=$GPU_IDS if you want to use a specific GPU # with FastSAM + PBR python run_inference.py dataset_name=$DATASET_NAME model=cnos_fast # with FastSAM + PBR + denser viewpoints python run_inference.py dataset_name=$DATASET_NAME model=cnos_fast model.onboarding_config.level_templates=1 # with FastSAM + PyRender python run_inference.py dataset_name=$DATASET_NAME model=cnos_fast model.onboarding_config.rendering_type=pyrender # with SAM + PyRender python run_inference.py dataset_name=$DATASET_NAME model.onboarding_config.rendering_type=pyrender # with SAM + PBR python run_inference.py dataset_name=$DATASET_NAME # using smaller models for FastSAM and DINOv2 python run_inference.py dataset_name=$DATASET_NAME model=cnos_fast model.descriptor_model.model_name=dinov2_vits14 model.segmentor_model.checkpoint_path= ``` After running this script, CNOS will output a prediction file at [this dir](https://github.com/nv-nguyen/cnos/blob/main/configs/run_inference.yaml#L9). You can then evaluate this prediction on [BOP challenge website](https://bop.felk.cvut.cz/). 2. Visualize the predictions: There are two options: 2.a. Using our custom visualization without Detectron2 (display only masks) ``` python -m src.scripts.visualize dataset_name=$DATASET_NAME input_file=$INPUT_FILE output_dir=$OUTPUT_DIR ``` 2.b. Using Detectron2 (display both masks, objectID, scores) ``` python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' python -m src.scripts.visualize_detectron2 dataset_name=$DATASET_NAME input_file=$INPUT_FILE output_dir=$OUTPUT_DIR ```
## Testing on custom datasets :rocket: You can run CNOS on your custom dataset given a RGB image and the CAD model of the target object. We provide an example of running CNOS on "BBQ sauce" sample taken from [MegaPose](https://github.com/megapose6d/megapose6d). ![qualitative](./media/demo/result.png)
Click to expand There are two steps to test CNOS on your own dataset: 0. Define the path to your dataset: ``` export CAD_PATH=./media/demo/hope_000002.ply export RGB_PATH=./media/demo/bba_sauce_rgb.png export OUTPUT_DIR=./tmp/custom_dataset ``` 1. Render the template from CAD models: ``` bash ./src/scripts/render_custom.sh ``` If the quality of rendering is not good, you can try to ajust the lightning conditions and distance between the camera and the object in [this script](https://github.com/nv-nguyen/cnos/tree/main/src/scripts/render_custom.sh). 2. Run CNOS and visualize the results: ``` bash ./src/scripts/run_inference_custom.sh ``` The detections will be saved at $OUTPUT_DIR/cnos_results. This script is used by default for single-CAD object segmentation. If you want to segment multiple objects, please make few adaptations [this script](https://github.com/nv-nguyen/cnos/tree/main/src/scripts/inference_custom.py).
Tricks and tips to improve results when running on custom objects with the sample taken from [jakob-ropers-snkeos](https://github.com/jakob-ropers-snkeos). ![tips](./media/demo2/result.png)
Click to expand Please note that SAM or FastSAM can perform exceptionally well, even on very small objects. However, certain parameters from the original implementation require adjustments to achieve optimal results. For example, it is recommended to reduce the default stability_score_thresh value of 0.97 to smaller settings, like 0.5 (applied after step 1 of rendering). ``` python -m src.scripts.inference_custom --template_dir $OUTPUT_DIR --rgb_path $RGB_PATH --stability_score_thresh 0.5 ```
## Acknowledgement The code is adapted from [Nope](https://github.com/nv-nguyen/nope), [Segmenting Anything](https://github.com/facebookresearch/segment-anything), [DINOv2](https://github.com/facebookresearch/dinov2). ## Contact If you have any question, feel free to create an issue or contact the first author at van-nguyen.nguyen@enpc.fr