├── tests ├── __init__.py ├── datasets_tests │ ├── __init__.py │ ├── coco_tests │ │ ├── __init__.py │ │ └── test_coco_semantic_segmentation_dataset.py │ ├── cub_tests │ │ ├── __init__.py │ │ ├── test_cub_label_dataset.py │ │ └── test_cub_keypoint_dataset.py │ ├── sbd_tests │ │ ├── __init__.py │ │ └── test_sbd_instance_segmentation_dataset.py │ ├── voc_tests │ │ ├── __init__.py │ │ ├── test_voc_instance_segmentation_dataset.py │ │ └── test_voc_semantic_segmentation_dataset.py │ ├── ade20k_tests │ │ ├── __init__.py │ │ └── test_ade20k.py │ ├── camvid_tests │ │ ├── __init__.py │ │ └── test_camvid_dataset.py │ ├── cityscapes_tests │ │ └── __init__.py │ └── online_products_tests │ │ ├── __init__.py │ │ └── test_online_products_dataset.py ├── functions_tests │ └── __init__.py ├── links_tests │ ├── __init__.py │ ├── model_tests │ │ ├── __init__.py │ │ ├── fpn_tests │ │ │ ├── __init__.py │ │ │ └── test_fpn.py │ │ ├── senet_tests │ │ │ └── __init__.py │ │ ├── ssd_tests │ │ │ ├── __init__.py │ │ │ ├── test_random_distort.py │ │ │ ├── test_resize_with_random_interpolation.py │ │ │ ├── test_gradient_scaling.py │ │ │ ├── test_multibox.py │ │ │ └── test_normalize.py │ │ ├── vgg_tests │ │ │ └── __init__.py │ │ ├── yolo_tests │ │ │ └── __init__.py │ │ ├── resnet_tests │ │ │ └── __init__.py │ │ ├── segnet_tests │ │ │ └── __init__.py │ │ ├── faster_rcnn_tests │ │ │ ├── __init__.py │ │ │ └── utils_tests │ │ │ │ ├── __init__.py │ │ │ │ └── test_generate_anchor_base.py │ │ └── deeplab_tests │ │ │ ├── test_aspp.py │ │ │ └── test_xception.py │ └── connection_tests │ │ └── __init__.py ├── utils_tests │ ├── __init__.py │ ├── bbox_tests │ │ └── __init__.py │ ├── image_tests │ │ ├── __init__.py │ │ ├── test_tile_images.py │ │ └── test_read_label.py │ ├── mask_tests │ │ ├── __init__.py │ │ └── test_mask_to_bbox.py │ ├── testing_tests │ │ ├── __init__.py │ │ ├── assertions_tests │ │ │ ├── __init__.py │ │ │ └── test_assert_is_semantic_segmentation_link.py │ │ └── test_generate_random_bbox.py │ └── iterator_tests │ │ ├── __init__.py │ │ └── test_progress_hook.py ├── evaluations_tests │ └── __init__.py ├── experimental_tests │ ├── __init__.py │ └── links_tests │ │ ├── __init__.py │ │ └── model_tests │ │ ├── __init__.py │ │ ├── fcis_tests │ │ ├── __init__.py │ │ └── utils_tests │ │ │ ├── __init__.py │ │ │ └── test_mask_voting.py │ │ └── pspnet_tests │ │ ├── __init__.py │ │ └── test_convolution_crop.py ├── extensions_tests │ ├── __init__.py │ ├── evaluator_tests │ │ └── __init__.py │ └── vis_report_tests │ │ └── __init__.py ├── transforms_tests │ ├── __init__.py │ ├── bbox_tests │ │ ├── __init__.py │ │ ├── test_rotate_bbox.py │ │ ├── test_resize_bbox.py │ │ ├── test_translate_bbox.py │ │ ├── test_flip_bbox.py │ │ └── test_crop_bbox.py │ ├── image_tests │ │ ├── __init__.py │ │ ├── test_center_crop.py │ │ ├── test_random_rotate.py │ │ ├── test_pca_lighting.py │ │ ├── test_ten_crop.py │ │ ├── test_random_crop.py │ │ ├── test_random_flip.py │ │ └── test_flip_transform.py │ └── point_tests │ │ ├── __init__.py │ │ ├── test_resize_point.py │ │ ├── test_translate_point.py │ │ └── test_flip_point.py ├── visualizations_tests │ ├── __init__.py │ ├── test_vis_image.py │ └── test_vis_point.py └── chainer_experimental_tests │ ├── __init__.py │ ├── datasets_tests │ ├── __init__.py │ └── sliceable_tests │ │ ├── __init__.py │ │ ├── test_tuple_dataset.py │ │ └── test_concatenated_dataset.py │ └── training_tests │ ├── __init__.py │ └── extensions_tests │ ├── __init__.py │ └── test_make_shift.py ├── chainercv ├── links │ ├── model │ │ ├── __init__.py │ │ ├── faster_rcnn │ │ │ ├── utils │ │ │ │ └── __init__.py │ │ │ └── __init__.py │ │ ├── vgg │ │ │ └── __init__.py │ │ ├── segnet │ │ │ └── __init__.py │ │ ├── resnet │ │ │ └── __init__.py │ │ ├── deeplab │ │ │ └── __init__.py │ │ ├── yolo │ │ │ └── __init__.py │ │ ├── senet │ │ │ └── __init__.py │ │ ├── ssd │ │ │ ├── gradient_scaling.py │ │ │ ├── __init__.py │ │ │ └── normalize.py │ │ └── fpn │ │ │ ├── misc.py │ │ │ ├── __init__.py │ │ │ └── fpn.py │ ├── connection │ │ ├── __init__.py │ │ └── seblock.py │ └── __init__.py ├── utils │ ├── bbox │ │ ├── __init__.py │ │ ├── _nms_gpu_post.pyx │ │ └── bbox_iou.py │ ├── mask │ │ ├── __init__.py │ │ ├── mask_to_bbox.py │ │ └── mask_iou.py │ ├── iterator │ │ ├── __init__.py │ │ └── progress_hook.py │ ├── image │ │ ├── __init__.py │ │ ├── write_image.py │ │ ├── read_label.py │ │ └── tile_images.py │ ├── testing │ │ ├── attr.py │ │ ├── parameterized.py │ │ ├── assertions │ │ │ ├── __init__.py │ │ │ ├── assert_is_bbox.py │ │ │ ├── assert_is_semantic_segmentation_link.py │ │ │ ├── assert_is_image.py │ │ │ └── assert_is_semantic_segmentation_dataset.py │ │ ├── __init__.py │ │ └── generate_random_bbox.py │ └── __init__.py ├── datasets │ ├── ade20k │ │ ├── __init__.py │ │ └── ade20k_test_image_dataset.py │ ├── camvid │ │ └── __init__.py │ ├── coco │ │ └── __init__.py │ ├── cub │ │ └── __init__.py │ ├── sbd │ │ └── __init__.py │ ├── voc │ │ └── __init__.py │ ├── cityscapes │ │ └── __init__.py │ └── online_products │ │ └── __init__.py ├── transforms │ ├── bbox │ │ ├── __init__.py │ │ ├── translate_bbox.py │ │ ├── resize_bbox.py │ │ ├── flip_bbox.py │ │ └── rotate_bbox.py │ ├── image │ │ ├── __init__.py │ │ ├── flip.py │ │ ├── random_rotate.py │ │ ├── ten_crop.py │ │ ├── random_flip.py │ │ └── pca_lighting.py │ ├── point │ │ ├── __init__.py │ │ ├── translate_point.py │ │ ├── resize_point.py │ │ └── flip_point.py │ └── __init__.py ├── extensions │ ├── evaluator │ │ └── __init__.py │ ├── vis_report │ │ └── __init__.py │ └── __init__.py ├── experimental │ ├── links │ │ ├── model │ │ │ ├── __init__.py │ │ │ ├── fcis │ │ │ │ ├── utils │ │ │ │ │ └── __init__.py │ │ │ │ └── __init__.py │ │ │ ├── yolo │ │ │ │ └── __init__.py │ │ │ └── pspnet │ │ │ │ └── __init__.py │ │ └── __init__.py │ └── __init__.py ├── chainer_experimental │ ├── datasets │ │ ├── __init__.py │ │ └── sliceable │ │ │ ├── __init__.py │ │ │ └── concatenated_dataset.py │ ├── training │ │ ├── __init__.py │ │ └── extensions │ │ │ ├── __init__.py │ │ │ └── make_shift.py │ └── __init__.py ├── functions │ └── __init__.py ├── visualizations │ ├── __init__.py │ ├── colormap.py │ └── vis_image.py ├── __init__.py └── evaluations │ └── __init__.py ├── .pep8 ├── setup.cfg ├── MANIFEST.in ├── docs ├── requirements.txt ├── images │ └── logo.png ├── image │ ├── detection_tutorial_link_simple.png │ ├── detection_tutorial_simple_bbox.png │ ├── detection_tutorial_bbox_dataset_vis.png │ ├── detection_tutorial_link_two_images.png │ └── detection_tutorial_link_low_score_thresh.png ├── source │ ├── tutorial │ │ └── index.rst │ ├── reference │ │ ├── links │ │ │ ├── vgg.rst │ │ │ ├── classifier.rst │ │ │ ├── segnet.rst │ │ │ ├── general_chain.rst │ │ │ ├── connection.rst │ │ │ ├── resnet.rst │ │ │ ├── deeplab.rst │ │ │ ├── senet.rst │ │ │ ├── yolo.rst │ │ │ ├── faster_rcnn.rst │ │ │ ├── ssd.rst │ │ │ └── fpn.rst │ │ ├── chainer_experimental │ │ │ ├── extensions.rst │ │ │ └── sliceable.rst │ │ ├── experimental │ │ │ ├── yolo.rst │ │ │ ├── pspnet.rst │ │ │ └── fcis.rst │ │ ├── index.rst │ │ ├── chainer_experimental.rst │ │ ├── visualizations.rst │ │ ├── functions.rst │ │ ├── experimental.rst │ │ ├── extensions.rst │ │ ├── links.rst │ │ ├── evaluations.rst │ │ └── transforms.rst │ ├── index.rst │ ├── _static │ │ └── css │ │ │ └── modified_theme.css │ └── install.rst ├── Makefile └── make.bat ├── examples_tests ├── detection_tests │ ├── test_visualize_models.sh │ ├── test_eval_detection.sh │ └── test_eval_detection_multi.sh ├── faster_rcnn_tests │ ├── test_demo.sh │ └── test_train.sh ├── fcis_tests │ ├── test_train_sbd.sh │ ├── test_demo.sh │ ├── test_train_sbd_multi.sh │ └── test_train_coco_multi.sh ├── ssd_tests │ ├── test_demo.sh │ ├── test_train.sh │ └── test_train_multi.sh ├── instance_segmentation_tests │ ├── test_eval_instance_segmentation.sh │ └── test_eval_instance_segmentation_multi.sh ├── pspnet_tests │ └── test_train_multi.sh ├── fpn_tests │ ├── test_train_multi.sh │ └── test_demo.sh └── semantic_segmentation_tests │ ├── test_eval_semantic_segmentation.sh │ └── test_eval_semantic_segmentation_multi.sh ├── readthedocs.yml ├── .gitignore ├── environment_minimum.yml ├── environment.yml ├── examples ├── vgg │ ├── README.md │ └── caffe2npz.py ├── senet │ └── README.md ├── resnet │ └── README.md ├── segnet │ ├── calc_weight.py │ └── demo.py ├── faster_rcnn │ └── demo.py ├── instance_segmentation │ └── eval_instance_segmentation_multi.py ├── detection │ └── eval_detection_multi.py ├── ssd │ └── demo.py ├── semantic_segmentation │ └── eval_semantic_segmentation_multi.py ├── yolo │ ├── README.md │ └── demo.py └── fpn │ └── README.md ├── .travis.yml ├── .pfnci ├── cache.sh ├── tests_gpu.sh ├── docker │ └── devel-minimal │ │ └── Dockerfile ├── tests.sh └── examples_tests.sh ├── LICENSE └── CONTRIBUTING.md /tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/links/model/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/utils/bbox/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/utils/mask/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/functions_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/utils_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/ade20k/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/camvid/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/coco/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/cub/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/sbd/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/voc/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/transforms/bbox/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/transforms/image/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/transforms/point/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/evaluations_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/experimental_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/extensions_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/transforms_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/visualizations_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/cityscapes/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/extensions/evaluator/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/extensions/vis_report/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/coco_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/cub_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/sbd_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/voc_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/utils_tests/bbox_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/utils_tests/image_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/utils_tests/mask_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/utils_tests/testing_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/datasets/online_products/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/experimental/links/model/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/ade20k_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/camvid_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/experimental_tests/links_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/connection_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/transforms_tests/bbox_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/transforms_tests/point_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/utils_tests/iterator_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/links/model/faster_rcnn/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/cityscapes_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/extensions_tests/evaluator_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/extensions_tests/vis_report_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/fpn_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/senet_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/ssd_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/vgg_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/yolo_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /chainercv/experimental/links/model/fcis/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/datasets_tests/online_products_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/resnet_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/segnet_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/datasets_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/training_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/experimental_tests/links_tests/model_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/faster_rcnn_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/utils_tests/testing_tests/assertions_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.pep8: -------------------------------------------------------------------------------- 1 | [pep8] 2 | exclude=caffe_pb*,.eggs,*.egg,build 3 | diff=True 4 | -------------------------------------------------------------------------------- /tests/experimental_tests/links_tests/model_tests/fcis_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/experimental_tests/links_tests/model_tests/pspnet_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/faster_rcnn_tests/utils_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | exclude = .eggs,*.egg,build,caffe_pb2.py,docs,.git 3 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/datasets_tests/sliceable_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/training_tests/extensions_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/experimental_tests/links_tests/model_tests/fcis_tests/utils_tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include chainercv *.c 2 | recursive-exclude chainercv *.pyx 3 | -------------------------------------------------------------------------------- /chainercv/experimental/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.experimental import links # NOQA 2 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | chainer>=6.0 2 | Cython 3 | matplotlib 4 | numpy 5 | Pillow 6 | -------------------------------------------------------------------------------- /docs/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shinh/chainercv/master/docs/images/logo.png -------------------------------------------------------------------------------- /chainercv/links/model/vgg/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.vgg.vgg16 import VGG16 # NOQA 2 | -------------------------------------------------------------------------------- /chainercv/links/model/segnet/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.segnet.segnet_basic import SegNetBasic # NOQA 2 | -------------------------------------------------------------------------------- /chainercv/chainer_experimental/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.chainer_experimental.datasets import sliceable # NOQA 2 | -------------------------------------------------------------------------------- /chainercv/chainer_experimental/training/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.chainer_experimental.training import extensions # NOQA 2 | -------------------------------------------------------------------------------- /examples_tests/detection_tests/test_visualize_models.sh: -------------------------------------------------------------------------------- 1 | cd examples/detection 2 | 3 | $PFNCI_SKIP $PYTHON visualize_models.py 4 | -------------------------------------------------------------------------------- /docs/image/detection_tutorial_link_simple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shinh/chainercv/master/docs/image/detection_tutorial_link_simple.png -------------------------------------------------------------------------------- /docs/image/detection_tutorial_simple_bbox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shinh/chainercv/master/docs/image/detection_tutorial_simple_bbox.png -------------------------------------------------------------------------------- /docs/image/detection_tutorial_bbox_dataset_vis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shinh/chainercv/master/docs/image/detection_tutorial_bbox_dataset_vis.png -------------------------------------------------------------------------------- /docs/image/detection_tutorial_link_two_images.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shinh/chainercv/master/docs/image/detection_tutorial_link_two_images.png -------------------------------------------------------------------------------- /examples_tests/faster_rcnn_tests/test_demo.sh: -------------------------------------------------------------------------------- 1 | cd examples/faster_rcnn 2 | 3 | $PYTHON demo.py $SAMPLE_IMAGE 4 | $PYTHON demo.py --gpu 0 $SAMPLE_IMAGE 5 | -------------------------------------------------------------------------------- /chainercv/chainer_experimental/training/extensions/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.chainer_experimental.training.extensions.make_shift import make_shift # NOQA 2 | -------------------------------------------------------------------------------- /docs/image/detection_tutorial_link_low_score_thresh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shinh/chainercv/master/docs/image/detection_tutorial_link_low_score_thresh.png -------------------------------------------------------------------------------- /readthedocs.yml: -------------------------------------------------------------------------------- 1 | name: chainercv 2 | type: sphinx 3 | base: docs/source 4 | requirements_file: docs/requirements.txt 5 | python: 6 | setup_py_install: true 7 | -------------------------------------------------------------------------------- /chainercv/chainer_experimental/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.chainer_experimental import datasets # NOQA 2 | from chainercv.chainer_experimental import training # NOQA 3 | -------------------------------------------------------------------------------- /docs/source/tutorial/index.rst: -------------------------------------------------------------------------------- 1 | ChainerCV Tutorial 2 | ================== 3 | 4 | .. toctree:: 5 | :maxdepth: 2 6 | 7 | detection 8 | link 9 | sliceable 10 | -------------------------------------------------------------------------------- /docs/source/reference/links/vgg.rst: -------------------------------------------------------------------------------- 1 | VGG 2 | === 3 | 4 | .. module:: chainercv.links.model.vgg 5 | 6 | 7 | VGG16 8 | ----- 9 | 10 | .. autoclass:: VGG16 11 | :members: 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.egg-info/ 3 | *.egg/ 4 | *.pyc 5 | *.c 6 | *.cpp 7 | *.so 8 | build 9 | \#*\# 10 | .\#* 11 | .coverage 12 | dist/ 13 | .cache/ 14 | check_autopep8 15 | 16 | tests/bin/ 17 | -------------------------------------------------------------------------------- /environment_minimum.yml: -------------------------------------------------------------------------------- 1 | name: chainercv_minimum 2 | channels: 3 | - !!python/unicode 4 | 'anaconda' 5 | - !!python/unicode 6 | 'defaults' 7 | dependencies: 8 | - Cython 9 | - numpy 10 | - Pillow 11 | -------------------------------------------------------------------------------- /chainercv/experimental/links/model/yolo/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.experimental.links.model.yolo.yolo_v2_tiny import DarknetExtractor # NOQA 2 | from chainercv.experimental.links.model.yolo.yolo_v2_tiny import YOLOv2Tiny # NOQA 3 | -------------------------------------------------------------------------------- /docs/source/reference/chainer_experimental/extensions.rst: -------------------------------------------------------------------------------- 1 | Extensions 2 | ========== 3 | 4 | .. module:: chainercv.chainer_experimental.training.extensions 5 | 6 | 7 | make_shift 8 | ---------- 9 | .. autofunction:: make_shift 10 | -------------------------------------------------------------------------------- /docs/source/reference/links/classifier.rst: -------------------------------------------------------------------------------- 1 | Classifier 2 | ========== 3 | 4 | .. module:: chainercv.links 5 | 6 | PixelwiseSoftmaxClassifier 7 | -------------------------- 8 | .. autoclass:: PixelwiseSoftmaxClassifier 9 | :members: 10 | -------------------------------------------------------------------------------- /chainercv/utils/iterator/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.utils.iterator.apply_to_iterator import apply_to_iterator # NOQA 2 | from chainercv.utils.iterator.progress_hook import ProgressHook # NOQA 3 | from chainercv.utils.iterator.unzip import unzip # NOQA 4 | -------------------------------------------------------------------------------- /examples_tests/fcis_tests/test_train_sbd.sh: -------------------------------------------------------------------------------- 1 | cd examples/fcis 2 | sed -e "s/split='train')/split='train').slice[:5]/" -i train_sbd.py 3 | sed -e "s/split='val')/split='val').slice[:5]/" -i train_sbd.py 4 | 5 | $PYTHON train_sbd.py --epoch 2 --cooldown-epoch 1 --gpu 0 6 | -------------------------------------------------------------------------------- /examples_tests/ssd_tests/test_demo.sh: -------------------------------------------------------------------------------- 1 | cd examples/ssd 2 | 3 | $PYTHON demo.py --model ssd300 $SAMPLE_IMAGE 4 | $PYTHON demo.py --model ssd300 --gpu 0 $SAMPLE_IMAGE 5 | $PYTHON demo.py --model ssd512 $SAMPLE_IMAGE 6 | $PYTHON demo.py --model ssd512 --gpu 0 $SAMPLE_IMAGE 7 | -------------------------------------------------------------------------------- /docs/source/reference/links/segnet.rst: -------------------------------------------------------------------------------- 1 | SegNet 2 | ====== 3 | 4 | .. module:: chainercv.links.model.segnet 5 | 6 | 7 | Semantic Segmentation Link 8 | -------------------------- 9 | 10 | SegNetBasic 11 | ~~~~~~~~~~~ 12 | .. autoclass:: SegNetBasic 13 | :members: 14 | -------------------------------------------------------------------------------- /examples_tests/fcis_tests/test_demo.sh: -------------------------------------------------------------------------------- 1 | cd examples/fcis 2 | 3 | $PFNCI_SKIP $PYTHON demo.py --dataset sbd $SAMPLE_IMAGE 4 | $PYTHON demo.py --dataset sbd --gpu 0 $SAMPLE_IMAGE 5 | $PFNCI_SKIP $PYTHON demo.py --dataset coco $SAMPLE_IMAGE 6 | $PYTHON demo.py --dataset coco --gpu 0 $SAMPLE_IMAGE 7 | -------------------------------------------------------------------------------- /chainercv/utils/image/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.utils.image.read_image import read_image # NOQA 2 | from chainercv.utils.image.read_label import read_label # NOQA 3 | from chainercv.utils.image.tile_images import tile_images # NOQA 4 | from chainercv.utils.image.write_image import write_image # NOQA 5 | -------------------------------------------------------------------------------- /examples_tests/fcis_tests/test_train_sbd_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/fcis 2 | sed -e "s/split='train')/split='train').slice[:6]/" -i train_sbd_multi.py 3 | sed -e "s/split='val')/split='val').slice[:6]/" -i train_sbd_multi.py 4 | 5 | $MPIEXEC $PYTHON train_sbd_multi.py --batchsize 2 --epoch 2 --cooldown-epoch 1 6 | -------------------------------------------------------------------------------- /examples_tests/faster_rcnn_tests/test_train.sh: -------------------------------------------------------------------------------- 1 | cd examples/faster_rcnn 2 | sed -e 's/return_difficult=True)/return_difficult=True).slice[:20]/' -i train.py 3 | 4 | $PYTHON train.py --dataset voc07 --gpu 0 --step-size 5 --iteration 7 5 | $PYTHON train.py --dataset voc0712 --gpu 0 --step-size 5 --iteration 7 6 | 7 | -------------------------------------------------------------------------------- /examples_tests/ssd_tests/test_train.sh: -------------------------------------------------------------------------------- 1 | cd examples/ssd 2 | sed -e 's/return_difficult=True)/return_difficult=True).slice[:20]/' -i train.py 3 | 4 | $PYTHON train.py --model ssd300 --batchsize 2 --iteration 12 --step 8 10 --gpu 0 5 | $PYTHON train.py --model ssd512 --batchsize 2 --iteration 12 --step 8 10 --gpu 0 6 | -------------------------------------------------------------------------------- /examples_tests/instance_segmentation_tests/test_eval_instance_segmentation.sh: -------------------------------------------------------------------------------- 1 | cd examples/instance_segmentation 2 | sed -e 's/return_crowded=True)/return_crowded=True).slice[:20]/' -i eval_instance_segmentation.py 3 | 4 | $PYTHON eval_instance_segmentation.py --dataset coco --model mask_rcnn_fpn_resnet50 --batchsize 2 --gpu 0 5 | -------------------------------------------------------------------------------- /examples_tests/instance_segmentation_tests/test_eval_instance_segmentation_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/instance_segmentation 2 | sed -e 's/return_crowded=True)/return_crowded=True).slice[:20]/' -i eval_instance_segmentation.py 3 | 4 | $MPIEXEC $PYTHON eval_instance_segmentation_multi.py --dataset coco --model mask_rcnn_fpn_resnet50 --batchsize 2 5 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | name: chainercv 2 | channels: 3 | - !!python/unicode 4 | 'menpo' 5 | - !!python/unicode 6 | 'mpi4py' 7 | - !!python/unicode 8 | 'anaconda' 9 | - !!python/unicode 10 | 'defaults' 11 | dependencies: 12 | - Cython 13 | - opencv3 14 | - matplotlib 15 | - numpy 16 | - Pillow 17 | - mpi4py 18 | - scipy 19 | -------------------------------------------------------------------------------- /chainercv/links/connection/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.connection.conv_2d_activ import Conv2DActiv # NOQA 2 | from chainercv.links.connection.conv_2d_bn_activ import Conv2DBNActiv # NOQA 3 | from chainercv.links.connection.seblock import SEBlock # NOQA 4 | from chainercv.links.connection.separable_conv_2d_bn_activ import SeparableConv2DBNActiv # NOQA 5 | -------------------------------------------------------------------------------- /docs/source/reference/links/general_chain.rst: -------------------------------------------------------------------------------- 1 | General Chain 2 | ============= 3 | 4 | .. module:: chainercv.links 5 | 6 | 7 | FeaturePredictor 8 | ---------------- 9 | .. autoclass:: chainercv.links.FeaturePredictor 10 | :members: 11 | 12 | 13 | PickableSequentialChain 14 | ----------------------- 15 | .. autoclass:: PickableSequentialChain 16 | :members: 17 | -------------------------------------------------------------------------------- /examples_tests/ssd_tests/test_train_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/ssd 2 | sed -e 's/return_difficult=True)/return_difficult=True).slice[:20]/' -i train_multi.py 3 | 4 | $MPIEXEC $PYTHON train_multi.py --model ssd300 --batchsize 4 --test-batchsize 2 --iteration 12 --step 8 10 5 | $MPIEXEC $PYTHON train_multi.py --model ssd512 --batchsize 4 --test-batchsize 2 --iteration 12 --step 8 10 6 | -------------------------------------------------------------------------------- /chainercv/functions/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.functions.ps_roi_average_align_2d import ps_roi_average_align_2d # NOQA 2 | from chainercv.functions.ps_roi_average_pooling_2d import ps_roi_average_pooling_2d # NOQA 3 | from chainercv.functions.ps_roi_max_align_2d import ps_roi_max_align_2d # NOQA 4 | from chainercv.functions.ps_roi_max_pooling_2d import ps_roi_max_pooling_2d # NOQA 5 | -------------------------------------------------------------------------------- /docs/source/reference/experimental/yolo.rst: -------------------------------------------------------------------------------- 1 | YOLO 2 | ==== 3 | 4 | .. module:: chainercv.experimental.links.model.yolo 5 | 6 | Object Detection Link 7 | --------------------- 8 | 9 | YOLOv2Tiny 10 | ~~~~~~~~~~ 11 | .. autoclass:: YOLOv2Tiny 12 | 13 | Utility 14 | ------- 15 | 16 | DarknetExtractor 17 | ~~~~~~~~~~~~~~~~ 18 | .. autoclass:: DarknetExtractor 19 | :members: 20 | -------------------------------------------------------------------------------- /chainercv/experimental/links/model/pspnet/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.experimental.links.model.pspnet.pspnet import PSPNet # NOQA 2 | from chainercv.experimental.links.model.pspnet.pspnet import PSPNetResNet101 # NOQA 3 | from chainercv.experimental.links.model.pspnet.pspnet import PSPNetResNet50 # NOQA 4 | from chainercv.experimental.links.model.pspnet.transforms import convolution_crop # NOQA 5 | -------------------------------------------------------------------------------- /examples_tests/pspnet_tests/test_train_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/pspnet 2 | sed -e "s/data_dir=args.data_dir, split='val')/data_dir=args.data_dir, split='val').slice[:5]/" -i train_multi.py 3 | 4 | $MPIEXEC $PYTHON train_multi.py --dataset ade20k --model pspnet_resnet50 --batchsize 1 --iteration 10 5 | $MPIEXEC $PYTHON train_multi.py --dataset ade20k --model pspnet_resnet101 --batchsize 1 --iteration 10 6 | -------------------------------------------------------------------------------- /chainercv/visualizations/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.visualizations.vis_bbox import vis_bbox # NOQA 2 | from chainercv.visualizations.vis_image import vis_image # NOQA 3 | from chainercv.visualizations.vis_instance_segmentation import vis_instance_segmentation # NOQA 4 | from chainercv.visualizations.vis_point import vis_point # NOQA 5 | from chainercv.visualizations.vis_semantic_segmentation import vis_semantic_segmentation # NOQA 6 | -------------------------------------------------------------------------------- /docs/source/reference/links/connection.rst: -------------------------------------------------------------------------------- 1 | Connection 2 | ========== 3 | 4 | .. module:: chainercv.links.connection 5 | 6 | 7 | Conv2DActiv 8 | ----------- 9 | .. autoclass:: Conv2DActiv 10 | 11 | Conv2DBNActiv 12 | ------------- 13 | .. autoclass:: Conv2DBNActiv 14 | 15 | SEBlock 16 | ------- 17 | .. autoclass:: SEBlock 18 | 19 | SeparableConv2DBNActiv 20 | ---------------------- 21 | .. autoclass:: SeparableConv2DBNActiv 22 | -------------------------------------------------------------------------------- /examples_tests/detection_tests/test_eval_detection.sh: -------------------------------------------------------------------------------- 1 | cd examples/detection 2 | sed -e 's/return_difficult=True)/return_difficult=True).slice[:20]/' -i eval_detection.py 3 | sed -e 's/return_crowded=True)/return_crowded=True).slice[:20]/' -i eval_detection.py 4 | 5 | $PYTHON eval_detection.py --dataset voc --model ssd300 --batchsize 2 --gpu 0 6 | $PYTHON eval_detection.py --dataset coco --model faster_rcnn_fpn_resnet50 --batchsize 2 --gpu 0 7 | -------------------------------------------------------------------------------- /chainercv/links/model/resnet/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.resnet.resblock import Bottleneck # NOQA 2 | from chainercv.links.model.resnet.resblock import ResBlock # NOQA 3 | from chainercv.links.model.resnet.resnet import ResNet # NOQA 4 | from chainercv.links.model.resnet.resnet import ResNet101 # NOQA 5 | from chainercv.links.model.resnet.resnet import ResNet152 # NOQA 6 | from chainercv.links.model.resnet.resnet import ResNet50 # NOQA 7 | -------------------------------------------------------------------------------- /docs/source/reference/index.rst: -------------------------------------------------------------------------------- 1 | ************************** 2 | ChainerCV Reference Manual 3 | ************************** 4 | 5 | .. module:: chainercv 6 | 7 | .. toctree:: 8 | :maxdepth: 2 9 | 10 | chainer_experimental 11 | datasets 12 | evaluations 13 | experimental 14 | extensions 15 | functions 16 | links 17 | transforms 18 | 19 | 20 | .. toctree:: 21 | :maxdepth: 1 22 | 23 | visualizations 24 | utils 25 | -------------------------------------------------------------------------------- /examples_tests/fcis_tests/test_train_coco_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/fcis 2 | sed -e "s/split='train')/split='train').slice[:4]/" -i train_coco_multi.py 3 | sed -e "s/split='valminusminival')/split='valminusminival').slice[:2]/" -i train_coco_multi.py 4 | sed -e "s/return_crowded=True, return_area=True)/return_crowded=True, return_area=True).slice[:6]/" -i train_coco_multi.py 5 | 6 | $MPIEXEC $PYTHON train_coco_multi.py --batchsize 2 --epoch 2 --cooldown-epoch 1 7 | -------------------------------------------------------------------------------- /examples_tests/detection_tests/test_eval_detection_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/detection 2 | sed -e 's/return_difficult=True)/return_difficult=True).slice[:20]/' -i eval_detection.py 3 | sed -e 's/return_crowded=True)/return_crowded=True).slice[:20]/' -i eval_detection.py 4 | 5 | $MPIEXEC $PYTHON eval_detection_multi.py --dataset voc --model ssd300 --batchsize 2 6 | $MPIEXEC $PYTHON eval_detection_multi.py --dataset coco --model faster_rcnn_fpn_resnet50 --batchsize 2 7 | -------------------------------------------------------------------------------- /chainercv/experimental/links/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.experimental.links.model.fcis.fcis_resnet101 import FCISResNet101 # NOQA 2 | from chainercv.experimental.links.model.fcis.fcis_train_chain import FCISTrainChain # NOQA 3 | from chainercv.experimental.links.model.pspnet.pspnet import PSPNetResNet101 # NOQA 4 | from chainercv.experimental.links.model.pspnet.pspnet import PSPNetResNet50 # NOQA 5 | from chainercv.experimental.links.model.yolo import YOLOv2Tiny # NOQA 6 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | ========= 2 | ChainerCV 3 | ========= 4 | 5 | 6 | ChainerCV is a **deep learning based computer vision library** built on top of `Chainer `_. 7 | 8 | .. toctree:: 9 | :maxdepth: 1 10 | 11 | install 12 | tutorial/index 13 | reference/index 14 | naming_convention 15 | license 16 | 17 | 18 | Indices and tables 19 | ================== 20 | 21 | * :ref:`genindex` 22 | * :ref:`modindex` 23 | * :ref:`search` 24 | -------------------------------------------------------------------------------- /examples_tests/fpn_tests/test_train_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/fpn 2 | 3 | $MPIEXEC $PYTHON train_multi.py --model faster_rcnn_fpn_resnet50 --batchsize 4 --iteration 9 --step 6 8 4 | $MPIEXEC $PYTHON train_multi.py --model faster_rcnn_fpn_resnet101 --batchsize 4 --iteration 9 --step 6 8 5 | $MPIEXEC $PYTHON train_multi.py --model mask_rcnn_fpn_resnet50 --batchsize 4 --iteration 9 --step 6 8 6 | $MPIEXEC $PYTHON train_multi.py --model mask_rcnn_fpn_resnet101 --batchsize 4 --iteration 9 --step 6 8 7 | -------------------------------------------------------------------------------- /examples/vgg/README.md: -------------------------------------------------------------------------------- 1 | # VGG 2 | 3 | For evaluation, please go to [`examples/classification`](https://github.com/chainer/chainercv/tree/master/examples/classification). 4 | 5 | ## Convert Caffe model 6 | Convert `*.caffemodel` to `*.npz`. 7 | 8 | ``` 9 | $ python caffe2npz.py .caffemodel .npz 10 | ``` 11 | 12 | The pretrained `.caffemodel` for VGG-16 can be downloaded from here. 13 | http://www.robots.ox.ac.uk/%7Evgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - 2.7 4 | - 3.6 5 | notifications: 6 | email: false 7 | 8 | install: 9 | - pip install cython 10 | - pip install -e . 11 | 12 | script: 13 | - >- 14 | pip install 15 | autopep8 16 | flake8 17 | hacking==1.0.0 18 | mock 19 | pytest 20 | - autopep8 -r . | tee check_autopep8 21 | - test ! -s check_autopep8 22 | - python style_checker.py --exclude caffe_pb2.py -- . 23 | - pytest -m 'not gpu and not slow' tests 24 | -------------------------------------------------------------------------------- /chainercv/links/model/deeplab/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.deeplab.aspp import SeparableASPP # NOQA 2 | from chainercv.links.model.deeplab.deeplab_v3_plus import Decoder # NOQA 3 | from chainercv.links.model.deeplab.deeplab_v3_plus import DeepLabV3plus # NOQA 4 | from chainercv.links.model.deeplab.deeplab_v3_plus import DeepLabV3plusXception65 # NOQA 5 | from chainercv.links.model.deeplab.xception import Xception65 # NOQA 6 | from chainercv.links.model.deeplab.xception import XceptionBlock # NOQA 7 | -------------------------------------------------------------------------------- /examples/senet/README.md: -------------------------------------------------------------------------------- 1 | # Squeeze-and-Excitation Networks (SENet) 2 | 3 | For evaluation, please go to [`examples/classification`](https://github.com/chainer/chainercv/tree/master/examples/classification). 4 | 5 | ## Convert Caffe model 6 | Convert `*.caffemodel` to `*.npz`. 7 | 8 | ``` 9 | $ python caffe2npz.py [se-resnet50|se-resnet101|se-resnet152|se-resnext50|se-resnext101] .caffemodel .npz 10 | ``` 11 | 12 | Pretrained `.caffemodel` can be downloaded here: https://github.com/hujie-frank/SENet 13 | -------------------------------------------------------------------------------- /chainercv/links/model/yolo/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.yolo.yolo_base import YOLOBase # NOQA 2 | from chainercv.links.model.yolo.yolo_v2 import Darknet19Extractor # NOQA 3 | from chainercv.links.model.yolo.yolo_v2 import YOLOv2 # NOQA 4 | from chainercv.links.model.yolo.yolo_v2 import YOLOv2Base # NOQA 5 | from chainercv.links.model.yolo.yolo_v3 import Darknet53Extractor # NOQA 6 | from chainercv.links.model.yolo.yolo_v3 import ResidualBlock # NOQA 7 | from chainercv.links.model.yolo.yolo_v3 import YOLOv3 # NOQA 8 | -------------------------------------------------------------------------------- /examples/resnet/README.md: -------------------------------------------------------------------------------- 1 | # ResNet 2 | 3 | For evaluation, please go to [`examples/classification`](https://github.com/chainer/chainercv/tree/master/examples/classification). 4 | 5 | ## Convert Caffe model 6 | Convert `*.caffemodel` to `*.npz`. 7 | 8 | ``` 9 | $ python caffe2npz.py [resnet50|resnet101|resnet152] .caffemodel .npz 10 | ``` 11 | 12 | For the model architectures by Kaiming He, the pretrained `.caffemodel` for ResNet can be downloaded from here. 13 | https://github.com/KaimingHe/deep-residual-networks 14 | -------------------------------------------------------------------------------- /chainercv/links/model/senet/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.senet.se_resnet import SEResNet # NOQA 2 | from chainercv.links.model.senet.se_resnet import SEResNet101 # NOQA 3 | from chainercv.links.model.senet.se_resnet import SEResNet152 # NOQA 4 | from chainercv.links.model.senet.se_resnet import SEResNet50 # NOQA 5 | from chainercv.links.model.senet.se_resnext import SEResNeXt # NOQA 6 | from chainercv.links.model.senet.se_resnext import SEResNeXt101 # NOQA 7 | from chainercv.links.model.senet.se_resnext import SEResNeXt50 # NOQA 8 | -------------------------------------------------------------------------------- /docs/source/reference/chainer_experimental.rst: -------------------------------------------------------------------------------- 1 | Chainer Experimental 2 | ==================== 3 | This module contains WIP modules of Chainer. 4 | After they are merged into chainer, these modules will be removed from ChainerCV. 5 | 6 | .. module:: chainercv.chainer_experimental 7 | 8 | Datasets 9 | -------- 10 | 11 | Sliceable 12 | ~~~~~~~~~ 13 | .. toctree:: 14 | 15 | chainer_experimental/sliceable 16 | 17 | Training 18 | -------- 19 | 20 | Extensions 21 | ~~~~~~~~~~ 22 | .. toctree:: 23 | 24 | chainer_experimental/extensions 25 | -------------------------------------------------------------------------------- /docs/source/reference/experimental/pspnet.rst: -------------------------------------------------------------------------------- 1 | PSPNet 2 | ====== 3 | 4 | .. module:: chainercv.experimental.links.model.pspnet 5 | 6 | Semantic Segmentation Link 7 | -------------------------- 8 | 9 | PSPNetResNet101 10 | ~~~~~~~~~~~~~~~ 11 | .. autoclass:: PSPNetResNet101 12 | 13 | PSPNetResNet50 14 | ~~~~~~~~~~~~~~ 15 | .. autoclass:: PSPNetResNet50 16 | 17 | Utility 18 | ------- 19 | 20 | convolution_crop 21 | ~~~~~~~~~~~~~~~~ 22 | .. autofunction:: convolution_crop 23 | 24 | PSPNet 25 | ~~~~~~ 26 | .. autoclass:: PSPNet 27 | :members: 28 | -------------------------------------------------------------------------------- /docs/source/reference/visualizations.rst: -------------------------------------------------------------------------------- 1 | Visualizations 2 | ============== 3 | 4 | .. module:: chainercv.visualizations 5 | 6 | 7 | vis_bbox 8 | ~~~~~~~~ 9 | .. autofunction:: vis_bbox 10 | 11 | vis_image 12 | ~~~~~~~~~ 13 | .. autofunction:: vis_image 14 | 15 | vis_instance_segmentation 16 | ~~~~~~~~~~~~~~~~~~~~~~~~~ 17 | .. autofunction:: vis_instance_segmentation 18 | 19 | vis_point 20 | ~~~~~~~~~ 21 | .. autofunction:: vis_point 22 | 23 | vis_semantic_segmentation 24 | ~~~~~~~~~~~~~~~~~~~~~~~~~ 25 | .. autofunction:: vis_semantic_segmentation 26 | -------------------------------------------------------------------------------- /.pfnci/cache.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env sh 2 | set -eux 3 | 4 | systemctl stop docker.service 5 | mount -t tmpfs tmpfs /var/lib/docker/ -o size=100% 6 | systemctl start docker.service 7 | gcloud auth configure-docker 8 | 9 | for DOCKER_TAG in devel devel-minimal 10 | do 11 | DOCKER_IMAGE=asia.gcr.io/pfn-public-ci/chainercv:${DOCKER_TAG} 12 | docker pull ${DOCKER_IMAGE} || true 13 | docker build \ 14 | --cache-from ${DOCKER_IMAGE} \ 15 | --tag ${DOCKER_IMAGE} \ 16 | .pfnci/docker/${DOCKER_TAG}/ 17 | docker push ${DOCKER_IMAGE} 18 | done 19 | -------------------------------------------------------------------------------- /chainercv/chainer_experimental/datasets/sliceable/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.chainer_experimental.datasets.sliceable.sliceable_dataset import SliceableDataset # NOQA 2 | 3 | from chainercv.chainer_experimental.datasets.sliceable.concatenated_dataset import ConcatenatedDataset # NOQA 4 | from chainercv.chainer_experimental.datasets.sliceable.getter_dataset import GetterDataset # NOQA 5 | from chainercv.chainer_experimental.datasets.sliceable.transform_dataset import TransformDataset # NOQA 6 | from chainercv.chainer_experimental.datasets.sliceable.tuple_dataset import TupleDataset # NOQA 7 | -------------------------------------------------------------------------------- /docs/source/reference/functions.rst: -------------------------------------------------------------------------------- 1 | Functions 2 | ========= 3 | 4 | .. module:: chainercv.functions 5 | 6 | 7 | Spatial Pooling 8 | --------------- 9 | 10 | ps_roi_average_align_2d 11 | ~~~~~~~~~~~~~~~~~~~~~~~ 12 | 13 | .. autofunction:: ps_roi_average_align_2d 14 | 15 | ps_roi_average_pooling_2d 16 | ~~~~~~~~~~~~~~~~~~~~~~~~~ 17 | 18 | .. autofunction:: ps_roi_average_pooling_2d 19 | 20 | ps_roi_max_align_2d 21 | ~~~~~~~~~~~~~~~~~~~ 22 | 23 | .. autofunction:: ps_roi_max_align_2d 24 | 25 | ps_roi_max_pooling_2d 26 | ~~~~~~~~~~~~~~~~~~~~~ 27 | 28 | .. autofunction:: ps_roi_max_pooling_2d 29 | -------------------------------------------------------------------------------- /.pfnci/tests_gpu.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env sh 2 | set -eux 3 | 4 | . $(dirname $0)/common.sh 5 | 6 | docker run --runtime=nvidia --interactive --rm \ 7 | --volume $(pwd):/root/ --workdir /root/ \ 8 | --env MPLBACKEND=agg \ 9 | ${DOCKER_IMAGE} \ 10 | sh -ex << EOD 11 | . ./install.sh 12 | cd chainercv/ 13 | python${PYTHON} -m pytest --color=no \ 14 | -m 'not pfnci_skip and gpu and not mpi' tests/ 15 | if which mpiexec; then 16 | mpiexec -n 2 --allow-run-as-root \ 17 | python${PYTHON} -m pytest --color=no \ 18 | -m 'not pfnci_skip and gpu and mpi' tests/ 19 | fi 20 | EOD 21 | -------------------------------------------------------------------------------- /examples_tests/semantic_segmentation_tests/test_eval_semantic_segmentation.sh: -------------------------------------------------------------------------------- 1 | cd examples/semantic_segmentation 2 | 3 | sed -e 's/CamVidDataset(split='\''test'\'')/CamVidDataset(split='\''test'\'').slice[:20]/' -i eval_semantic_segmentation.py 4 | $PYTHON eval_semantic_segmentation.py --gpu 0 --dataset camvid --model segnet 5 | 6 | sed -e 's/label_resolution='\''fine'\'')/label_resolution='\''fine'\'').slice[:20]/' \ 7 | -i eval_semantic_segmentation.py 8 | $PYTHON eval_semantic_segmentation.py --gpu 0 --dataset cityscapes --model pspnet_resnet101 9 | $PYTHON eval_semantic_segmentation.py --gpu 0 --dataset cityscapes --model deeplab_v3plus_xception65 10 | -------------------------------------------------------------------------------- /examples/segnet/calc_weight.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import numpy as np 4 | 5 | from chainercv.datasets import CamVidDataset 6 | 7 | 8 | n_class = 11 9 | dataset = CamVidDataset(split='train') 10 | 11 | n_cls_pixels = np.zeros((n_class,)) 12 | n_img_pixels = np.zeros((n_class,)) 13 | 14 | for img, label in dataset: 15 | for cls_i in np.unique(label): 16 | if cls_i == -1: 17 | continue 18 | n_cls_pixels[cls_i] += np.sum(label == cls_i) 19 | n_img_pixels[cls_i] += label.size 20 | freq = n_cls_pixels / n_img_pixels 21 | median_freq = np.median(freq) 22 | 23 | np.save('class_weight', median_freq / freq) 24 | -------------------------------------------------------------------------------- /examples_tests/fpn_tests/test_demo.sh: -------------------------------------------------------------------------------- 1 | cd examples/fpn 2 | 3 | $PFNCI_SKIP $PYTHON demo.py --model faster_rcnn_fpn_resnet50 $SAMPLE_IMAGE 4 | $PYTHON demo.py --model faster_rcnn_fpn_resnet50 --gpu 0 $SAMPLE_IMAGE 5 | $PFNCI_SKIP $PYTHON demo.py --model faster_rcnn_fpn_resnet101 $SAMPLE_IMAGE 6 | $PYTHON demo.py --model faster_rcnn_fpn_resnet101 --gpu 0 $SAMPLE_IMAGE 7 | 8 | $PFNCI_SKIP $PYTHON demo.py --model mask_rcnn_fpn_resnet50 $SAMPLE_IMAGE 9 | $PYTHON demo.py --model mask_rcnn_fpn_resnet50 --gpu 0 $SAMPLE_IMAGE 10 | $PFNCI_SKIP $PYTHON demo.py --model mask_rcnn_fpn_resnet101 $SAMPLE_IMAGE 11 | $PYTHON demo.py --model mask_rcnn_fpn_resnet101 --gpu 0 $SAMPLE_IMAGE 12 | -------------------------------------------------------------------------------- /chainercv/extensions/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.extensions.evaluator.detection_coco_evaluator import DetectionCOCOEvaluator # NOQA 2 | from chainercv.extensions.evaluator.detection_voc_evaluator import DetectionVOCEvaluator # NOQA 3 | from chainercv.extensions.evaluator.instance_segmentation_coco_evaluator import InstanceSegmentationCOCOEvaluator # NOQA 4 | from chainercv.extensions.evaluator.instance_segmentation_voc_evaluator import InstanceSegmentationVOCEvaluator # NOQA 5 | from chainercv.extensions.evaluator.semantic_segmentation_evaluator import SemanticSegmentationEvaluator # NOQA 6 | from chainercv.extensions.vis_report.detection_vis_report import DetectionVisReport # NOQA 7 | -------------------------------------------------------------------------------- /examples_tests/semantic_segmentation_tests/test_eval_semantic_segmentation_multi.sh: -------------------------------------------------------------------------------- 1 | cd examples/semantic_segmentation 2 | 3 | sed -e 's/CamVidDataset(split='\''test'\'')/CamVidDataset(split='\''test'\'').slice[:20]/' -i eval_semantic_segmentation.py 4 | $MPIEXEC $PYTHON eval_semantic_segmentation_multi.py --dataset camvid --model segnet 5 | 6 | sed -e 's/label_resolution='\''fine'\'')/label_resolution='\''fine'\'').slice[:20]/' \ 7 | -i eval_semantic_segmentation.py 8 | $MPIEXEC $PYTHON eval_semantic_segmentation_multi.py --dataset cityscapes --model pspnet_resnet101 9 | $MPIEXEC $PYTHON eval_semantic_segmentation_multi.py --dataset cityscapes --model deeplab_v3plus_xception65 10 | -------------------------------------------------------------------------------- /docs/source/reference/links/resnet.rst: -------------------------------------------------------------------------------- 1 | ResNet 2 | ====== 3 | 4 | .. module:: chainercv.links.model.resnet 5 | 6 | 7 | Feature Extraction Link 8 | ----------------------- 9 | 10 | ResNet 11 | ~~~~~~ 12 | .. autoclass:: ResNet 13 | :members: 14 | 15 | ResNet50 16 | ~~~~~~~~ 17 | .. autoclass:: ResNet50 18 | :members: 19 | 20 | ResNet101 21 | ~~~~~~~~~ 22 | .. autoclass:: ResNet101 23 | :members: 24 | 25 | ResNet152 26 | ~~~~~~~~~ 27 | .. autoclass:: ResNet152 28 | :members: 29 | 30 | 31 | Utility 32 | ------- 33 | 34 | Bottleneck 35 | ~~~~~~~~~~ 36 | .. autoclass:: Bottleneck 37 | :members: 38 | 39 | ResBlock 40 | ~~~~~~~~ 41 | .. autoclass:: ResBlock 42 | :members: 43 | -------------------------------------------------------------------------------- /chainercv/experimental/links/model/fcis/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.experimental.links.model.fcis.fcis import FCIS # NOQA 2 | from chainercv.experimental.links.model.fcis.fcis_resnet101 import FCISResNet101 # NOQA 3 | from chainercv.experimental.links.model.fcis.fcis_resnet101 import FCISResNet101Head # NOQA 4 | from chainercv.experimental.links.model.fcis.fcis_resnet101 import ResNet101Extractor # NOQA 5 | from chainercv.experimental.links.model.fcis.fcis_train_chain import FCISTrainChain # NOQA 6 | from chainercv.experimental.links.model.fcis.utils.mask_voting import mask_voting # NOQA 7 | from chainercv.experimental.links.model.fcis.utils.proposal_target_creator import ProposalTargetCreator # NOQA 8 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SPHINXPROJ = ChainerCV 8 | SOURCEDIR = source 9 | BUILDDIR = build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -------------------------------------------------------------------------------- /chainercv/links/model/ssd/gradient_scaling.py: -------------------------------------------------------------------------------- 1 | from chainer.backends import cuda 2 | 3 | 4 | class GradientScaling(object): 5 | 6 | """Optimizer/UpdateRule hook function for scaling gradient. 7 | 8 | This hook function scales gradient by a constant value. 9 | 10 | Args: 11 | rate (float): Coefficient for scaling. 12 | Attributes: 13 | rate (float): Coefficient for scaling. 14 | """ 15 | name = 'GradientScaling' 16 | call_for_each_param = True 17 | 18 | def __init__(self, rate): 19 | self.rate = rate 20 | 21 | def __call__(self, rule, param): 22 | g = param.grad 23 | with cuda.get_device_from_array(g): 24 | g *= self.rate 25 | -------------------------------------------------------------------------------- /.pfnci/docker/devel-minimal/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM asia.gcr.io/pfn-public-ci/nvidia-cuda:9.2-cudnn7-devel-ubuntu18.04 2 | 3 | RUN apt-get update -y && apt-get install -y --no-install-recommends \ 4 | ca-certificates \ 5 | curl \ 6 | git \ 7 | python-dev \ 8 | python3-dev \ 9 | && apt-get clean \ 10 | && rm -rf /var/lib/apt/lists/* 11 | 12 | RUN curl -LO https://bootstrap.pypa.io/get-pip.py \ 13 | && python2 get-pip.py --no-cache-dir \ 14 | && python3 get-pip.py --no-cache-dir \ 15 | && rm get-pip.py 16 | 17 | RUN pip2 install --no-cache-dir \ 18 | cython \ 19 | mock \ 20 | pytest \ 21 | && pip3 install --no-cache-dir \ 22 | cython \ 23 | mock \ 24 | pytest 25 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/test_center_crop.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import center_crop 7 | 8 | 9 | class TestCenterCrop(unittest.TestCase): 10 | 11 | def test_center_crop(self): 12 | img = np.random.uniform(size=(3, 48, 32)) 13 | 14 | out, param = center_crop(img, (24, 16), return_param=True) 15 | y_slice = param['y_slice'] 16 | x_slice = param['x_slice'] 17 | 18 | np.testing.assert_equal(out, img[:, y_slice, x_slice]) 19 | self.assertEqual(y_slice, slice(12, 36)) 20 | self.assertEqual(x_slice, slice(8, 24)) 21 | 22 | 23 | testing.run_module(__name__, __file__) 24 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/test_random_rotate.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import random_rotate 7 | 8 | 9 | class TestRandomRotate(unittest.TestCase): 10 | 11 | def test_random_rotate(self): 12 | img = np.random.uniform(size=(3, 24, 24)) 13 | 14 | out, param = random_rotate(img, return_param=True) 15 | k = param['k'] 16 | 17 | expected = np.transpose(img, axes=(1, 2, 0)) 18 | expected = np.rot90(expected, k) 19 | expected = np.transpose(expected, axes=(2, 0, 1)) 20 | 21 | np.testing.assert_equal(out, expected) 22 | 23 | 24 | testing.run_module(__name__, __file__) 25 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/test_pca_lighting.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import pca_lighting 7 | 8 | 9 | class TestPCALighting(unittest.TestCase): 10 | 11 | def test_pca_lighting(self): 12 | img = np.random.uniform(size=(3, 48, 32)) 13 | 14 | out = pca_lighting(img, 0.1) 15 | self.assertEqual(img.shape, out.shape) 16 | self.assertEqual(img.dtype, out.dtype) 17 | 18 | out = pca_lighting(img, 0) 19 | self.assertEqual(img.shape, out.shape) 20 | self.assertEqual(img.dtype, out.dtype) 21 | np.testing.assert_equal(out, img) 22 | 23 | 24 | testing.run_module(__name__, __file__) 25 | -------------------------------------------------------------------------------- /chainercv/utils/testing/attr.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from chainer.testing.attr import check_available 4 | from chainer.testing.attr import gpu # NOQA 5 | from chainer.testing.attr import slow # NOQA 6 | 7 | 8 | try: 9 | import pytest 10 | pfnci_skip = pytest.mark.pfnci_skip 11 | 12 | except ImportError: 13 | from chainer.testing.attr import _dummy_callable 14 | pfnci_skip = _dummy_callable 15 | 16 | 17 | def mpi(f): 18 | check_available() 19 | import pytest 20 | 21 | try: 22 | import mpi4py.MPI # NOQA 23 | available = True 24 | except ImportError: 25 | available = False 26 | 27 | return unittest.skipUnless( 28 | available, 'mpi4py is not installed')(pytest.mark.mpi(f)) 29 | -------------------------------------------------------------------------------- /docs/source/reference/links/deeplab.rst: -------------------------------------------------------------------------------- 1 | DeepLab 2 | ======= 3 | 4 | .. module:: chainercv.links.model.deeplab 5 | 6 | Semantic Segmentation Link 7 | -------------------------- 8 | 9 | DeepLabV3plusXception65 10 | ~~~~~~~~~~~~~~~~~~~~~~~ 11 | .. autoclass:: DeepLabV3plusXception65 12 | 13 | Utility 14 | ------- 15 | 16 | Decoder 17 | ~~~~~~~ 18 | .. autoclass:: Decoder 19 | :members: 20 | 21 | DeepLabV3plus 22 | ~~~~~~~~~~~~~ 23 | .. autoclass:: DeepLabV3plus 24 | :members: 25 | 26 | SeparableASPP 27 | ~~~~~~~~~~~~~ 28 | .. autoclass:: SeparableASPP 29 | :members: 30 | 31 | Xception65 32 | ~~~~~~~~~~ 33 | 34 | .. autoclass:: Xception65 35 | :members: 36 | 37 | XceptionBlock 38 | ~~~~~~~~~~~~~ 39 | .. autoclass:: XceptionBlock 40 | :members: 41 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/test_ten_crop.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import ten_crop 7 | 8 | 9 | class TestTenCrop(unittest.TestCase): 10 | 11 | def test_ten_crop(self): 12 | img = np.random.uniform(size=(3, 48, 32)) 13 | 14 | out = ten_crop(img, (48, 32)) 15 | self.assertEqual(out.shape, (10, 3, 48, 32)) 16 | for crop in out[:5]: 17 | np.testing.assert_equal(crop, img) 18 | for crop in out[5:]: 19 | np.testing.assert_equal(crop[:, :, ::-1], img) 20 | 21 | out = ten_crop(img, (24, 12)) 22 | self.assertEqual(out.shape, (10, 3, 24, 12)) 23 | 24 | 25 | testing.run_module(__name__, __file__) 26 | -------------------------------------------------------------------------------- /tests/visualizations_tests/test_vis_image.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainercv.utils import testing 6 | from chainercv.visualizations import vis_image 7 | 8 | try: 9 | import matplotlib # NOQA 10 | _available = True 11 | except ImportError: 12 | _available = False 13 | 14 | 15 | @testing.parameterize( 16 | {'img': np.random.randint(0, 255, size=(3, 32, 32)).astype(np.float32)}, 17 | {'img': None} 18 | ) 19 | @unittest.skipUnless(_available, 'Matplotlib is not installed') 20 | class TestVisImage(unittest.TestCase): 21 | 22 | def test_vis_image(self): 23 | ax = vis_image(self.img) 24 | self.assertTrue(isinstance(ax, matplotlib.axes.Axes)) 25 | 26 | 27 | testing.run_module(__name__, __file__) 28 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/ssd_tests/test_random_distort.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | from chainer import testing 5 | from chainercv.links.model.ssd import random_distort 6 | 7 | try: 8 | import cv2 # NOQA 9 | _cv2_available = True 10 | except ImportError: 11 | _cv2_available = False 12 | 13 | 14 | @unittest.skipUnless(_cv2_available, 'cv2 is not installed') 15 | class TestRandomDistort(unittest.TestCase): 16 | 17 | def test_random_distort(self): 18 | img = np.random.randint(0, 256, size=(3, 48, 32)).astype(np.float32) 19 | 20 | out = random_distort(img) 21 | self.assertEqual(out.shape, img.shape) 22 | self.assertEqual(out.dtype, img.dtype) 23 | 24 | 25 | testing.run_module(__name__, __file__) 26 | -------------------------------------------------------------------------------- /docs/source/_static/css/modified_theme.css: -------------------------------------------------------------------------------- 1 | @import url('theme.css'); 2 | 3 | /* Main background color modification */ 4 | .btn-info, .wy-menu-vertical a:active, .wy-side-nav-search, .wy-side-nav-search img, .wy-nav .wy-menu-vertical a:hover, .wy-nav-top img, .wy-tray-item-info, .wy-side-nav-search, .wy-dropdown-menu > dd > a:hover, .wy-dropdown-menu a:hover, .wy-nav-top { 5 | background-color: #d17062 !important; 6 | } 7 | 8 | .wy-side-nav-search input[type=text] { 9 | border-color: #a47224 !important; 10 | } 11 | 12 | .rst-content .note .admonition-title { 13 | background-color: #c9c9c9 !important; 14 | } 15 | 16 | .rst-content .note { 17 | background-color: #e3e3e3 !important; 18 | } 19 | 20 | .wy-nav-content { 21 | max-width: 1000px !important; 22 | } 23 | -------------------------------------------------------------------------------- /docs/source/reference/experimental.rst: -------------------------------------------------------------------------------- 1 | Experimental 2 | ============ 3 | 4 | Links 5 | ----- 6 | 7 | Detection 8 | ~~~~~~~~~ 9 | 10 | Detection links share a common method :meth:`predict` to detect objects in images. 11 | 12 | .. toctree:: 13 | 14 | experimental/yolo 15 | 16 | 17 | Semantic Segmentation 18 | ~~~~~~~~~~~~~~~~~~~~~ 19 | 20 | Semantic segmentation links share a common method :meth:`predict` to conduct semantic segmentation of images. 21 | 22 | .. toctree:: 23 | 24 | experimental/pspnet 25 | 26 | Instance Segmentation 27 | ~~~~~~~~~~~~~~~~~~~~~ 28 | Instance segmentation share a common method :meth:`predict` to detect objects in images. 29 | For more details, please read :func:`FCIS.predict`. 30 | 31 | .. toctree:: 32 | 33 | experimental/fcis 34 | -------------------------------------------------------------------------------- /docs/source/reference/links/senet.rst: -------------------------------------------------------------------------------- 1 | SEResNet 2 | ======== 3 | 4 | .. module:: chainercv.links.model.senet 5 | 6 | 7 | Feature Extraction Link 8 | ----------------------- 9 | 10 | SEResNet 11 | ~~~~~~~~ 12 | .. autoclass:: SEResNet 13 | :members: 14 | 15 | SEResNet50 16 | ~~~~~~~~~~ 17 | .. autoclass:: SEResNet50 18 | :members: 19 | 20 | SEResNet101 21 | ~~~~~~~~~~~ 22 | .. autoclass:: SEResNet101 23 | :members: 24 | 25 | SEResNet152 26 | ~~~~~~~~~~~ 27 | .. autoclass:: SEResNet152 28 | :members: 29 | 30 | SEResNeXt 31 | ~~~~~~~~~ 32 | .. autoclass:: SEResNeXt 33 | :members: 34 | 35 | SEResNeXt50 36 | ~~~~~~~~~~~ 37 | .. autoclass:: SEResNeXt50 38 | :members: 39 | 40 | SEResNeXt101 41 | ~~~~~~~~~~~~ 42 | .. autoclass:: SEResNeXt101 43 | :members: 44 | -------------------------------------------------------------------------------- /docs/source/reference/chainer_experimental/sliceable.rst: -------------------------------------------------------------------------------- 1 | Sliceable 2 | ========= 3 | This module support sliceable feature. 4 | Please note that this module will be removed after Chainer implements sliceable feature. 5 | 6 | 7 | .. seealso:: https://github.com/chainer/chainercv/pull/454 8 | .. module:: chainercv.chainer_experimental.datasets.sliceable 9 | 10 | 11 | ConcatenatedDataset 12 | ------------------- 13 | 14 | .. autoclass:: ConcatenatedDataset 15 | :members: 16 | 17 | GetterDataset 18 | ------------- 19 | 20 | .. autoclass:: GetterDataset 21 | :members: 22 | 23 | TupleDataset 24 | ------------ 25 | 26 | .. autoclass:: TupleDataset 27 | :members: 28 | 29 | TransformDataset 30 | ---------------- 31 | 32 | .. autoclass:: TransformDataset 33 | :members: 34 | -------------------------------------------------------------------------------- /chainercv/__init__.py: -------------------------------------------------------------------------------- 1 | import pkg_resources 2 | 3 | from chainercv import chainer_experimental # NOQA 4 | from chainercv import datasets # NOQA 5 | from chainercv import evaluations # NOQA 6 | from chainercv import experimental # NOQA 7 | from chainercv import extensions # NOQA 8 | from chainercv import functions # NOQA 9 | from chainercv import links # NOQA 10 | from chainercv import transforms # NOQA 11 | from chainercv import utils # NOQA 12 | from chainercv import visualizations # NOQA 13 | 14 | 15 | __version__ = pkg_resources.get_distribution('chainercv').version 16 | 17 | 18 | from chainer.configuration import global_config # NOQA 19 | 20 | 21 | global_config.cv_read_image_backend = None 22 | global_config.cv_resize_backend = None 23 | global_config.cv_rotate_backend = None 24 | -------------------------------------------------------------------------------- /chainercv/transforms/image/flip.py: -------------------------------------------------------------------------------- 1 | def flip(img, y_flip=False, x_flip=False, copy=False): 2 | """Flip an image in vertical or horizontal direction as specified. 3 | 4 | Args: 5 | img (~numpy.ndarray): An array that gets flipped. This is in CHW 6 | format. 7 | y_flip (bool): Flip in vertical direction. 8 | x_flip (bool): Flip in horizontal direction. 9 | copy (bool): If False, a view of :obj:`img` will be returned. 10 | 11 | Returns: 12 | Transformed :obj:`img` in CHW format. 13 | """ 14 | 15 | assert img.ndim == 3, 'The dimension of image must be 3' 16 | if y_flip: 17 | img = img[:, ::-1, :] 18 | if x_flip: 19 | img = img[:, :, ::-1] 20 | 21 | if copy: 22 | img = img.copy() 23 | return img 24 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/test_random_crop.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import random_crop 7 | 8 | 9 | class TestRandomCrop(unittest.TestCase): 10 | 11 | def test_random_crop(self): 12 | img = np.random.uniform(size=(3, 48, 32)) 13 | 14 | out, param = random_crop(img, (48, 32), return_param=True) 15 | y_slice = param['y_slice'] 16 | x_slice = param['x_slice'] 17 | np.testing.assert_equal(out, img) 18 | self.assertEqual(y_slice, slice(0, 48)) 19 | self.assertEqual(x_slice, slice(0, 32)) 20 | 21 | out = random_crop(img, (24, 12)) 22 | self.assertEqual(out.shape[1:], (24, 12)) 23 | 24 | 25 | testing.run_module(__name__, __file__) 26 | -------------------------------------------------------------------------------- /docs/source/reference/links/yolo.rst: -------------------------------------------------------------------------------- 1 | YOLO 2 | ==== 3 | 4 | .. module:: chainercv.links.model.yolo 5 | 6 | 7 | Detection Links 8 | --------------- 9 | 10 | YOLOv2 11 | ~~~~~~ 12 | .. autoclass:: YOLOv2 13 | :members: 14 | 15 | YOLOv3 16 | ~~~~~~ 17 | .. autoclass:: YOLOv3 18 | :members: 19 | 20 | Utility 21 | ------- 22 | 23 | ResidualBlock 24 | ~~~~~~~~~~~~~ 25 | .. autoclass:: ResidualBlock 26 | :members: 27 | 28 | Darknet19Extractor 29 | ~~~~~~~~~~~~~~~~~~ 30 | .. autoclass:: Darknet19Extractor 31 | :members: 32 | 33 | Darknet53Extractor 34 | ~~~~~~~~~~~~~~~~~~ 35 | .. autoclass:: Darknet53Extractor 36 | :members: 37 | 38 | YOLOBase 39 | ~~~~~~~~ 40 | .. autoclass:: YOLOBase 41 | :members: 42 | 43 | YOLOv2Base 44 | ~~~~~~~~~~ 45 | .. autoclass:: YOLOv2Base 46 | :members: 47 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/test_random_flip.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import random_flip 7 | 8 | 9 | class TestRandomFlip(unittest.TestCase): 10 | 11 | def test_random_flip(self): 12 | img = np.random.uniform(size=(3, 24, 24)) 13 | 14 | out, param = random_flip( 15 | img, y_random=True, x_random=True, return_param=True) 16 | y_flip = param['y_flip'] 17 | x_flip = param['x_flip'] 18 | 19 | expected = img 20 | if y_flip: 21 | expected = expected[:, ::-1, :] 22 | if x_flip: 23 | expected = expected[:, :, ::-1] 24 | np.testing.assert_equal(out, expected) 25 | 26 | 27 | testing.run_module(__name__, __file__) 28 | -------------------------------------------------------------------------------- /tests/datasets_tests/camvid_tests/test_camvid_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from chainer import testing 4 | from chainer.testing import attr 5 | 6 | from chainercv.datasets import camvid_label_names 7 | from chainercv.datasets import CamVidDataset 8 | from chainercv.utils import assert_is_semantic_segmentation_dataset 9 | 10 | 11 | @testing.parameterize( 12 | {'split': 'train'}, 13 | {'split': 'val'}, 14 | {'split': 'test'} 15 | ) 16 | class TestCamVidDataset(unittest.TestCase): 17 | 18 | def setUp(self): 19 | self.dataset = CamVidDataset(split=self.split) 20 | 21 | @attr.slow 22 | def test_camvid_dataset(self): 23 | assert_is_semantic_segmentation_dataset( 24 | self.dataset, len(camvid_label_names), n_example=10) 25 | 26 | 27 | testing.run_module(__name__, __file__) 28 | -------------------------------------------------------------------------------- /.pfnci/tests.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env sh 2 | set -eux 3 | 4 | . $(dirname $0)/common.sh 5 | 6 | for ZIP in datasets-tiny.zip models.zip 7 | do 8 | gsutil -q cp gs://chainercv-asia-pfn-public-ci/${ZIP} . 9 | unzip -q ${ZIP} 10 | rm ${ZIP} 11 | done 12 | 13 | docker run --interactive --rm \ 14 | --volume $(pwd):/root/ --workdir /root/ \ 15 | --env MPLBACKEND=agg \ 16 | ${DOCKER_IMAGE} \ 17 | sh -ex << EOD 18 | . ./install.sh 19 | pip${PYTHON} install --user pytest-xdist 20 | cd chainercv/ 21 | python${PYTHON} -m pytest --color=no -n $(nproc) \ 22 | -m 'not pfnci_skip and not gpu and not mpi' tests/ 23 | if which mpiexec; then 24 | mpiexec -n 2 --allow-run-as-root \ 25 | python${PYTHON} -m pytest --color=no \ 26 | -m 'not pfnci_skip and not gpu and mpi' tests/ 27 | fi 28 | EOD 29 | -------------------------------------------------------------------------------- /docs/source/reference/extensions.rst: -------------------------------------------------------------------------------- 1 | Extensions 2 | ========== 3 | 4 | .. module:: chainercv.extensions 5 | 6 | 7 | Evaluator 8 | --------- 9 | 10 | DetectionCOCOEvaluator 11 | ~~~~~~~~~~~~~~~~~~~~~~ 12 | .. autoclass:: DetectionCOCOEvaluator 13 | 14 | DetectionVOCEvaluator 15 | ~~~~~~~~~~~~~~~~~~~~~ 16 | .. autoclass:: DetectionVOCEvaluator 17 | 18 | InstanceSegmentationCOCOEvaluator 19 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20 | .. autoclass:: InstanceSegmentationCOCOEvaluator 21 | 22 | InstanceSegmentationVOCEvaluator 23 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 24 | .. autoclass:: InstanceSegmentationVOCEvaluator 25 | 26 | SemanticSegmentationEvaluator 27 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 28 | .. autoclass:: SemanticSegmentationEvaluator 29 | 30 | 31 | Visualization Report 32 | -------------------- 33 | 34 | DetectionVisReport 35 | ~~~~~~~~~~~~~~~~~~ 36 | .. autoclass:: DetectionVisReport 37 | -------------------------------------------------------------------------------- /chainercv/visualizations/colormap.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def voc_colormap(labels): 5 | """Color map used in PASCAL VOC 6 | 7 | Args: 8 | labels (iterable of ints): Class ids. 9 | 10 | Returns: 11 | numpy.ndarray: Colors in RGB order. The shape is :math:`(N, 3)`, 12 | where :math:`N` is the size of :obj:`labels`. The range of the values 13 | is :math:`[0, 255]`. 14 | 15 | """ 16 | colors = [] 17 | for label in labels: 18 | r, g, b = 0, 0, 0 19 | i = label 20 | for j in range(8): 21 | if i & (1 << 0): 22 | r |= 1 << (7 - j) 23 | if i & (1 << 1): 24 | g |= 1 << (7 - j) 25 | if i & (1 << 2): 26 | b |= 1 << (7 - j) 27 | i >>= 3 28 | colors.append((r, g, b)) 29 | return np.array(colors, dtype=np.float32) 30 | -------------------------------------------------------------------------------- /tests/transforms_tests/bbox_tests/test_rotate_bbox.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import rotate_bbox 7 | from chainercv.utils import generate_random_bbox 8 | 9 | 10 | @testing.parameterize(*testing.product({ 11 | 'angle': [180, 90, 0, -90, -180] 12 | })) 13 | class TestRotateBbox(unittest.TestCase): 14 | 15 | def test_rotate_bbox(self): 16 | size = (32, 24) 17 | bbox = generate_random_bbox(10, size, 0, 24) 18 | 19 | out = rotate_bbox(bbox, self.angle, size) 20 | if self.angle % 180 != 0: 21 | rotate_size = size[::-1] 22 | else: 23 | rotate_size = size 24 | out = rotate_bbox(out, -1 * self.angle, rotate_size) 25 | 26 | np.testing.assert_almost_equal(out, bbox, decimal=6) 27 | 28 | 29 | testing.run_module(__name__, __file__) 30 | -------------------------------------------------------------------------------- /chainercv/utils/image/write_image.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from PIL import Image 3 | 4 | 5 | def write_image(img, file, format=None): 6 | """Save an image to a file. 7 | 8 | This function saves an image to given file. The image is in CHW format and 9 | the range of its value is :math:`[0, 255]`. 10 | 11 | Args: 12 | image (~numpy.ndarray): An image to be saved. 13 | file (string or file-like object): A path of image file or 14 | a file-like object of image. 15 | format (:obj:`{'bmp', 'jpeg', 'png'}`): The format of image. 16 | If :obj:`file` is a file-like object, 17 | this option must be specified. 18 | """ 19 | 20 | if img.shape[0] == 1: 21 | img = img[0] 22 | else: 23 | img = img.transpose((1, 2, 0)) 24 | 25 | img = Image.fromarray(img.astype(np.uint8)) 26 | img.save(file, format=format) 27 | -------------------------------------------------------------------------------- /docs/source/reference/experimental/fcis.rst: -------------------------------------------------------------------------------- 1 | FCIS 2 | ==== 3 | 4 | .. module:: chainercv.experimental.links.model.fcis 5 | 6 | Instance Segmentation Link 7 | -------------------------- 8 | 9 | FCISResNet101 10 | ~~~~~~~~~~~~~ 11 | .. autoclass:: FCISResNet101 12 | 13 | Utility 14 | ------- 15 | 16 | FCIS 17 | ~~~~ 18 | .. autoclass:: FCIS 19 | :members: 20 | 21 | FCISResNet101Head 22 | ~~~~~~~~~~~~~~~~~ 23 | .. autoclass:: FCISResNet101Head 24 | 25 | mask_voting 26 | ~~~~~~~~~~~ 27 | .. autofunction:: mask_voting 28 | 29 | ResNet101Extractor 30 | ~~~~~~~~~~~~~~~~~~ 31 | .. autoclass:: ResNet101Extractor 32 | 33 | Train-only Utility 34 | ------------------ 35 | 36 | FCISTrainChain 37 | ~~~~~~~~~~~~~~ 38 | .. autoclass:: FCISTrainChain 39 | :members: 40 | 41 | ProposalTargetCreator 42 | ~~~~~~~~~~~~~~~~~~~~~ 43 | .. autoclass:: ProposalTargetCreator 44 | :members: 45 | :special-members: __call__ 46 | -------------------------------------------------------------------------------- /tests/transforms_tests/image_tests/test_flip_transform.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import flip 7 | 8 | 9 | class TestRandomFlip(unittest.TestCase): 10 | 11 | def test_random_flip(self): 12 | img = np.random.uniform(size=(3, 24, 24)) 13 | 14 | out = flip(img, y_flip=True, x_flip=True) 15 | 16 | expected = img 17 | expected = expected[:, :, ::-1] 18 | expected = expected[:, ::-1, :] 19 | np.testing.assert_equal(out, expected) 20 | 21 | def test_random_flip_vertical(self): 22 | img = np.random.uniform(size=(3, 24, 24)) 23 | 24 | out = flip(img, y_flip=True, x_flip=False) 25 | 26 | expected = img 27 | expected = expected[:, ::-1, :] 28 | np.testing.assert_equal(out, expected) 29 | 30 | 31 | testing.run_module(__name__, __file__) 32 | -------------------------------------------------------------------------------- /tests/visualizations_tests/test_vis_point.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | 7 | from chainercv.visualizations import vis_point 8 | 9 | try: 10 | import matplotlib # NOQA 11 | _available = True 12 | except ImportError: 13 | _available = False 14 | 15 | 16 | @testing.parameterize( 17 | {'visible': np.array([[True, True, False]])}, 18 | {'visible': None} 19 | ) 20 | @unittest.skipUnless(_available, 'Matplotlib is not installed') 21 | class TestVisPoint(unittest.TestCase): 22 | 23 | def test_vis_point(self): 24 | img = np.random.randint( 25 | 0, 255, size=(3, 32, 32)).astype(np.float32) 26 | point = np.random.uniform(size=(1, 3, 2)).astype(np.float32) 27 | ax = vis_point(img, point, self.visible) 28 | 29 | self.assertTrue(isinstance(ax, matplotlib.axes.Axes)) 30 | 31 | 32 | testing.run_module(__name__, __file__) 33 | -------------------------------------------------------------------------------- /tests/transforms_tests/bbox_tests/test_resize_bbox.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import resize_bbox 7 | from chainercv.utils.testing.generate_random_bbox import generate_random_bbox 8 | 9 | 10 | class TestResizeBbox(unittest.TestCase): 11 | 12 | def test_resize_bbox(self): 13 | in_size = (32, 24) 14 | out_size = (in_size[0] * 2, in_size[1] * 4) 15 | bbox = generate_random_bbox(10, in_size, 0, min(in_size)) 16 | 17 | out = resize_bbox(bbox, in_size=in_size, out_size=out_size) 18 | bbox_expected = bbox.copy() 19 | bbox_expected[:, 0] = bbox[:, 0] * 2 20 | bbox_expected[:, 1] = bbox[:, 1] * 4 21 | bbox_expected[:, 2] = bbox[:, 2] * 2 22 | bbox_expected[:, 3] = bbox[:, 3] * 4 23 | np.testing.assert_equal(out, bbox_expected) 24 | 25 | 26 | testing.run_module(__name__, __file__) 27 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/training_tests/extensions_tests/test_make_shift.py: -------------------------------------------------------------------------------- 1 | import mock 2 | import unittest 3 | 4 | from chainer import testing 5 | from chainercv.chainer_experimental.training.extensions import make_shift 6 | 7 | 8 | @make_shift('x') 9 | def mod5_shift(trainer): 10 | return trainer.updater.iteration % 5 11 | 12 | 13 | class TestMakeShift(unittest.TestCase): 14 | 15 | def test_make_shift(self): 16 | trainer = testing.get_trainer_with_mock_updater( 17 | iter_per_epoch=10, extensions=[mod5_shift]) 18 | trainer.updater.get_optimizer.return_value = mock.MagicMock() 19 | trainer.updater.get_optimizer().x = -1 20 | 21 | mod5_shift.initialize(trainer) 22 | for i in range(100): 23 | self.assertEqual(trainer.updater.get_optimizer().x, i % 5) 24 | trainer.updater.update() 25 | mod5_shift(trainer) 26 | 27 | 28 | testing.run_module(__name__, __file__) 29 | -------------------------------------------------------------------------------- /tests/datasets_tests/coco_tests/test_coco_semantic_segmentation_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from chainer import testing 4 | from chainer.testing import attr 5 | 6 | from chainercv.datasets import coco_semantic_segmentation_label_names 7 | from chainercv.datasets import COCOSemanticSegmentationDataset 8 | from chainercv.utils import assert_is_semantic_segmentation_dataset 9 | 10 | 11 | @testing.parameterize( 12 | {'split': 'train'}, 13 | {'split': 'val'}, 14 | ) 15 | class TestCOCOSemanticSegmentationDataset(unittest.TestCase): 16 | 17 | def setUp(self): 18 | self.dataset = COCOSemanticSegmentationDataset(split=self.split) 19 | 20 | @attr.slow 21 | def test_coco_semantic_segmentation_dataset(self): 22 | assert_is_semantic_segmentation_dataset( 23 | self.dataset, 24 | len(coco_semantic_segmentation_label_names), 25 | n_example=10) 26 | 27 | 28 | testing.run_module(__name__, __file__) 29 | -------------------------------------------------------------------------------- /tests/transforms_tests/bbox_tests/test_translate_bbox.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import translate_bbox 7 | from chainercv.utils.testing.generate_random_bbox import generate_random_bbox 8 | 9 | 10 | class TestTranslateBbox(unittest.TestCase): 11 | 12 | def test_translate_bbox(self): 13 | size = (32, 24) 14 | y_offset, x_offset = 5, 3 15 | bbox = generate_random_bbox(10, size, 0, min(size)) 16 | 17 | out = translate_bbox(bbox, y_offset=y_offset, x_offset=x_offset) 18 | bbox_expected = np.empty_like(bbox) 19 | bbox_expected[:, 0] = bbox[:, 0] + y_offset 20 | bbox_expected[:, 1] = bbox[:, 1] + x_offset 21 | bbox_expected[:, 2] = bbox[:, 2] + y_offset 22 | bbox_expected[:, 3] = bbox[:, 3] + x_offset 23 | np.testing.assert_equal(out, bbox_expected) 24 | 25 | 26 | testing.run_module(__name__, __file__) 27 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | pushd %~dp0 4 | 5 | REM Command file for Sphinx documentation 6 | 7 | if "%SPHINXBUILD%" == "" ( 8 | set SPHINXBUILD=sphinx-build 9 | ) 10 | set SOURCEDIR=source 11 | set BUILDDIR=build 12 | set SPHINXPROJ=ChainerCV 13 | 14 | if "%1" == "" goto help 15 | 16 | %SPHINXBUILD% >NUL 2>NUL 17 | if errorlevel 9009 ( 18 | echo. 19 | echo.The 'sphinx-build' command was not found. Make sure you have Sphinx 20 | echo.installed, then set the SPHINXBUILD environment variable to point 21 | echo.to the full path of the 'sphinx-build' executable. Alternatively you 22 | echo.may add the Sphinx directory to PATH. 23 | echo. 24 | echo.If you don't have Sphinx installed, grab it from 25 | echo.http://sphinx-doc.org/ 26 | exit /b 1 27 | ) 28 | 29 | %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 30 | goto end 31 | 32 | :help 33 | %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% 34 | 35 | :end 36 | popd 37 | -------------------------------------------------------------------------------- /tests/datasets_tests/voc_tests/test_voc_instance_segmentation_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from chainer import testing 4 | from chainer.testing import attr 5 | 6 | from chainercv.datasets import voc_instance_segmentation_label_names 7 | from chainercv.datasets import VOCInstanceSegmentationDataset 8 | from chainercv.utils import assert_is_instance_segmentation_dataset 9 | 10 | 11 | @testing.parameterize( 12 | {'split': 'train'}, 13 | {'split': 'val'}, 14 | {'split': 'trainval'} 15 | ) 16 | class TestVOCInstanceSegmentationDataset(unittest.TestCase): 17 | 18 | def setUp(self): 19 | self.dataset = VOCInstanceSegmentationDataset(split=self.split) 20 | 21 | @attr.slow 22 | def test_voc_instance_segmentation_dataset(self): 23 | assert_is_instance_segmentation_dataset( 24 | self.dataset, 25 | len(voc_instance_segmentation_label_names), 26 | n_example=10) 27 | 28 | 29 | testing.run_module(__name__, __file__) 30 | -------------------------------------------------------------------------------- /tests/datasets_tests/voc_tests/test_voc_semantic_segmentation_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from chainer import testing 4 | from chainer.testing import attr 5 | 6 | from chainercv.datasets import voc_semantic_segmentation_label_names 7 | from chainercv.datasets import VOCSemanticSegmentationDataset 8 | from chainercv.utils import assert_is_semantic_segmentation_dataset 9 | 10 | 11 | @testing.parameterize( 12 | {'split': 'train'}, 13 | {'split': 'val'}, 14 | {'split': 'trainval'} 15 | ) 16 | class TestVOCSemanticSegmentationDataset(unittest.TestCase): 17 | 18 | def setUp(self): 19 | self.dataset = VOCSemanticSegmentationDataset(split=self.split) 20 | 21 | @attr.slow 22 | def test_voc_semantic_segmentation_dataset(self): 23 | assert_is_semantic_segmentation_dataset( 24 | self.dataset, 25 | len(voc_semantic_segmentation_label_names), 26 | n_example=10) 27 | 28 | 29 | testing.run_module(__name__, __file__) 30 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/ssd_tests/test_resize_with_random_interpolation.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | from chainer import testing 5 | from chainercv.links.model.ssd import resize_with_random_interpolation 6 | 7 | try: 8 | import cv2 # NOQA 9 | _cv2_available = True 10 | except ImportError: 11 | _cv2_available = False 12 | 13 | 14 | @unittest.skipUnless(_cv2_available, 'cv2 is not installed') 15 | class TestResizeWithRandomInterpolation(unittest.TestCase): 16 | 17 | def test_resize_color(self): 18 | img = np.random.uniform(size=(3, 24, 32)) 19 | out = resize_with_random_interpolation(img, size=(32, 64)) 20 | self.assertEqual(out.shape, (3, 32, 64)) 21 | 22 | def test_resize_grayscale(self): 23 | img = np.random.uniform(size=(1, 24, 32)) 24 | out = resize_with_random_interpolation(img, size=(32, 64)) 25 | self.assertEqual(out.shape, (1, 32, 64)) 26 | 27 | 28 | testing.run_module(__name__, __file__) 29 | -------------------------------------------------------------------------------- /.pfnci/examples_tests.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env sh 2 | set -eux 3 | 4 | . $(dirname $0)/common.sh 5 | 6 | for ZIP in datasets-tiny.zip models.zip 7 | do 8 | gsutil -q cp gs://chainercv-asia-pfn-public-ci/${ZIP} . 9 | unzip -q ${ZIP} 10 | rm ${ZIP} 11 | done 12 | 13 | curl -L https://cloud.githubusercontent.com/assets/2062128/26187667/9cb236da-3bd5-11e7-8bcf-7dbd4302e2dc.jpg \ 14 | -o sample.jpg 15 | 16 | docker run --runtime=nvidia --interactive --rm \ 17 | --volume $(pwd):/root/ --workdir /root/ \ 18 | --env SAMPLE_IMAGE=/root/sample.jpg \ 19 | --env PYTHON=python${PYTHON} \ 20 | --env MPIEXEC='mpiexec -n 2 --allow-run-as-root' \ 21 | --env MPLBACKEND=agg \ 22 | --env CHAINERCV_DOWNLOAD_REPORT=OFF \ 23 | --env PFNCI_SKIP='echo SKIP:' \ 24 | ${DOCKER_IMAGE} \ 25 | sh -ex << EOD 26 | . ./install.sh 27 | cd chainercv/ 28 | for SCRIPT in \$(find examples_tests/ -type f -name '*.sh') 29 | do 30 | sh -ex \${SCRIPT} 31 | done 32 | EOD 33 | -------------------------------------------------------------------------------- /chainercv/evaluations/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.evaluations.eval_detection_coco import eval_detection_coco # NOQA 2 | from chainercv.evaluations.eval_detection_voc import calc_detection_voc_ap # NOQA 3 | from chainercv.evaluations.eval_detection_voc import calc_detection_voc_prec_rec # NOQA 4 | from chainercv.evaluations.eval_detection_voc import eval_detection_voc # NOQA 5 | from chainercv.evaluations.eval_instance_segmentation_coco import eval_instance_segmentation_coco # NOQA 6 | from chainercv.evaluations.eval_instance_segmentation_voc import calc_instance_segmentation_voc_prec_rec # NOQA 7 | from chainercv.evaluations.eval_instance_segmentation_voc import eval_instance_segmentation_voc # NOQA 8 | from chainercv.evaluations.eval_semantic_segmentation import calc_semantic_segmentation_confusion # NOQA 9 | from chainercv.evaluations.eval_semantic_segmentation import calc_semantic_segmentation_iou # NOQA 10 | from chainercv.evaluations.eval_semantic_segmentation import eval_semantic_segmentation # NOQA 11 | -------------------------------------------------------------------------------- /tests/transforms_tests/bbox_tests/test_flip_bbox.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import flip_bbox 7 | from chainercv.utils.testing.generate_random_bbox import generate_random_bbox 8 | 9 | 10 | class TestFlipBbox(unittest.TestCase): 11 | 12 | def test_flip_bbox(self): 13 | size = (32, 24) 14 | bbox = generate_random_bbox(10, size, 0, min(size)) 15 | 16 | out = flip_bbox(bbox, size=size, y_flip=True) 17 | bbox_expected = bbox.copy() 18 | bbox_expected[:, 0] = size[0] - bbox[:, 2] 19 | bbox_expected[:, 2] = size[0] - bbox[:, 0] 20 | np.testing.assert_equal(out, bbox_expected) 21 | 22 | out = flip_bbox(bbox, size=size, x_flip=True) 23 | bbox_expected = bbox.copy() 24 | bbox_expected[:, 1] = size[1] - bbox[:, 3] 25 | bbox_expected[:, 3] = size[1] - bbox[:, 1] 26 | np.testing.assert_equal(out, bbox_expected) 27 | 28 | 29 | testing.run_module(__name__, __file__) 30 | -------------------------------------------------------------------------------- /chainercv/visualizations/vis_image.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def vis_image(img, ax=None): 5 | """Visualize a color image. 6 | 7 | Args: 8 | img (~numpy.ndarray): See the table below. 9 | If this is :obj:`None`, no image is displayed. 10 | ax (matplotlib.axes.Axis): The visualization is displayed on this 11 | axis. If this is :obj:`None` (default), a new axis is created. 12 | 13 | .. csv-table:: 14 | :header: name, shape, dtype, format 15 | 16 | :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ 17 | "RGB, :math:`[0, 255]`" 18 | 19 | Returns: 20 | ~matploblib.axes.Axes: 21 | Returns the Axes object with the plot for further tweaking. 22 | 23 | """ 24 | from matplotlib import pyplot as plot 25 | if ax is None: 26 | fig = plot.figure() 27 | ax = fig.add_subplot(1, 1, 1) 28 | if img is not None: 29 | # CHW -> HWC 30 | img = img.transpose((1, 2, 0)) 31 | ax.imshow(img.astype(np.uint8)) 32 | return ax 33 | -------------------------------------------------------------------------------- /chainercv/utils/testing/parameterized.py: -------------------------------------------------------------------------------- 1 | import six 2 | 3 | from chainer import testing 4 | 5 | 6 | def parameterize(*params): 7 | """:func:`chainer.testing.parameterize` for `pytest-xdist`. 8 | 9 | :func:`chainer.testing.parameterize` cannot work with `pytest-xdist` 10 | when the params contain functions (lambdas), classes, and random values. 11 | This wrapper replaces the params with their indices 12 | and restore the original params in :meth:`setUp`. 13 | """ 14 | 15 | def deco(cls): 16 | setUp_orig = cls.setUp 17 | 18 | def setUp(self): 19 | param = params[self._chainercv_parameterize_index] 20 | print('params: {}'.format(param)) 21 | for k, v in six.iteritems(param): 22 | setattr(self, k, v) 23 | setUp_orig(self) 24 | 25 | cls.setUp = setUp 26 | 27 | params_indices = [ 28 | {'_chainercv_parameterize_index': i} for i in range(len(params))] 29 | return testing.parameterize(*params_indices)(cls) 30 | 31 | return deco 32 | -------------------------------------------------------------------------------- /chainercv/links/model/faster_rcnn/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.faster_rcnn.faster_rcnn import FasterRCNN # NOQA 2 | from chainercv.links.model.faster_rcnn.faster_rcnn_train_chain import FasterRCNNTrainChain # NOQA 3 | from chainercv.links.model.faster_rcnn.faster_rcnn_vgg import FasterRCNNVGG16 # NOQA 4 | from chainercv.links.model.faster_rcnn.faster_rcnn_vgg import VGG16RoIHead # NOQA 5 | from chainercv.links.model.faster_rcnn.region_proposal_network import RegionProposalNetwork # NOQA 6 | from chainercv.links.model.faster_rcnn.utils.anchor_target_creator import AnchorTargetCreator # NOQA 7 | from chainercv.links.model.faster_rcnn.utils.bbox2loc import bbox2loc # NOQA 8 | from chainercv.links.model.faster_rcnn.utils.generate_anchor_base import generate_anchor_base # NOQA 9 | from chainercv.links.model.faster_rcnn.utils.loc2bbox import loc2bbox # NOQA 10 | from chainercv.links.model.faster_rcnn.utils.proposal_creator import ProposalCreator # NOQA 11 | from chainercv.links.model.faster_rcnn.utils.proposal_target_creator import ProposalTargetCreator # NOQA 12 | -------------------------------------------------------------------------------- /chainercv/links/model/ssd/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.ssd.gradient_scaling import GradientScaling # NOQA 2 | from chainercv.links.model.ssd.multibox import Multibox # NOQA 3 | from chainercv.links.model.ssd.multibox_coder import MultiboxCoder # NOQA 4 | from chainercv.links.model.ssd.multibox_loss import multibox_loss # NOQA 5 | from chainercv.links.model.ssd.normalize import Normalize # NOQA 6 | from chainercv.links.model.ssd.ssd import SSD # NOQA 7 | from chainercv.links.model.ssd.ssd_vgg16 import SSD300 # NOQA 8 | from chainercv.links.model.ssd.ssd_vgg16 import SSD512 # NOQA 9 | from chainercv.links.model.ssd.ssd_vgg16 import VGG16 # NOQA 10 | from chainercv.links.model.ssd.ssd_vgg16 import VGG16Extractor300 # NOQA 11 | from chainercv.links.model.ssd.ssd_vgg16 import VGG16Extractor512 # NOQA 12 | from chainercv.links.model.ssd.transforms import random_crop_with_bbox_constraints # NOQA 13 | from chainercv.links.model.ssd.transforms import random_distort # NOQA 14 | from chainercv.links.model.ssd.transforms import resize_with_random_interpolation # NOQA 15 | -------------------------------------------------------------------------------- /tests/transforms_tests/point_tests/test_resize_point.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import resize_point 7 | 8 | 9 | class TestResizePoint(unittest.TestCase): 10 | 11 | def test_resize_point_ndarray(self): 12 | point = np.random.uniform( 13 | low=0., high=32., size=(3, 12, 2)) 14 | 15 | out = resize_point(point, in_size=(16, 32), out_size=(8, 64)) 16 | point[:, :, 0] *= 0.5 17 | point[:, :, 1] *= 2 18 | np.testing.assert_equal(out, point) 19 | 20 | def test_resize_point_list(self): 21 | point = [ 22 | np.random.uniform(low=0., high=32., size=(12, 2)), 23 | np.random.uniform(low=0., high=32., size=(10, 2)) 24 | ] 25 | 26 | out = resize_point(point, in_size=(16, 32), out_size=(8, 64)) 27 | for i, pnt in enumerate(point): 28 | pnt[:, 0] *= 0.5 29 | pnt[:, 1] *= 2 30 | np.testing.assert_equal(out[i], pnt) 31 | 32 | 33 | testing.run_module(__name__, __file__) 34 | -------------------------------------------------------------------------------- /chainercv/transforms/bbox/translate_bbox.py: -------------------------------------------------------------------------------- 1 | def translate_bbox(bbox, y_offset=0, x_offset=0): 2 | """Translate bounding boxes. 3 | 4 | This method is mainly used together with image transforms, such as padding 5 | and cropping, which translates the left top point of the image from 6 | coordinate :math:`(0, 0)` to coordinate 7 | :math:`(y, x) = (y_{offset}, x_{offset})`. 8 | 9 | Args: 10 | bbox (~numpy.ndarray): See the table below. 11 | y_offset (int or float): The offset along y axis. 12 | x_offset (int or float): The offset along x axis. 13 | 14 | .. csv-table:: 15 | :header: name, shape, dtype, format 16 | 17 | :obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \ 18 | ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" 19 | 20 | Returns: 21 | ~numpy.ndarray: 22 | Bounding boxes translated according to the given offsets. 23 | 24 | """ 25 | 26 | out_bbox = bbox.copy() 27 | out_bbox[:, :2] += (y_offset, x_offset) 28 | out_bbox[:, 2:] += (y_offset, x_offset) 29 | 30 | return out_bbox 31 | -------------------------------------------------------------------------------- /tests/utils_tests/testing_tests/test_generate_random_bbox.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | from chainer import testing 5 | 6 | from chainercv.utils import assert_is_bbox 7 | from chainercv.utils import generate_random_bbox 8 | 9 | 10 | @testing.parameterize( 11 | {'n': 0}, 12 | {'n': 32} 13 | ) 14 | class TestGenerateRandomBbox(unittest.TestCase): 15 | 16 | def test_generate_random_bbox(self): 17 | img_size = (128, 256) 18 | min_length = 16 19 | max_length = 48 20 | 21 | bbox = generate_random_bbox(self.n, img_size, min_length, max_length) 22 | 23 | assert_is_bbox(bbox, img_size) 24 | self.assertEqual(bbox.shape[0], self.n) 25 | 26 | if self.n > 0: 27 | h = bbox[:, 2] - bbox[:, 0] 28 | w = bbox[:, 3] - bbox[:, 1] 29 | self.assertTrue(np.all(h < max_length)) 30 | self.assertTrue(np.all(h >= min_length)) 31 | self.assertTrue(np.all(w < max_length)) 32 | self.assertTrue(np.all(w >= min_length)) 33 | 34 | 35 | testing.run_module(__name__, __file__) 36 | -------------------------------------------------------------------------------- /chainercv/transforms/bbox/resize_bbox.py: -------------------------------------------------------------------------------- 1 | def resize_bbox(bbox, in_size, out_size): 2 | """Resize bounding boxes according to image resize. 3 | 4 | Args: 5 | bbox (~numpy.ndarray): See the table below. 6 | in_size (tuple): A tuple of length 2. The height and the width 7 | of the image before resized. 8 | out_size (tuple): A tuple of length 2. The height and the width 9 | of the image after resized. 10 | 11 | .. csv-table:: 12 | :header: name, shape, dtype, format 13 | 14 | :obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \ 15 | ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" 16 | 17 | Returns: 18 | ~numpy.ndarray: 19 | Bounding boxes rescaled according to the given image shapes. 20 | 21 | """ 22 | bbox = bbox.copy() 23 | y_scale = float(out_size[0]) / in_size[0] 24 | x_scale = float(out_size[1]) / in_size[1] 25 | bbox[:, 0] = y_scale * bbox[:, 0] 26 | bbox[:, 2] = y_scale * bbox[:, 2] 27 | bbox[:, 1] = x_scale * bbox[:, 1] 28 | bbox[:, 3] = x_scale * bbox[:, 3] 29 | return bbox 30 | -------------------------------------------------------------------------------- /docs/source/install.rst: -------------------------------------------------------------------------------- 1 | Installation Guide 2 | ================== 3 | 4 | Pip 5 | ~~~ 6 | 7 | You can install ChainerCV using `pip`. 8 | 9 | .. code-block:: shell 10 | 11 | pip install -U numpy 12 | pip install chainercv 13 | 14 | 15 | Anaconda 16 | ~~~~~~~~ 17 | 18 | Build instruction using Anaconda is as follows. 19 | 20 | .. code-block:: shell 21 | 22 | # For python 3 23 | # wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh 24 | wget https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda.sh 25 | 26 | bash miniconda.sh -b -p $HOME/miniconda 27 | export PATH="$HOME/miniconda/bin:$PATH" 28 | conda config --set always_yes yes --set changeps1 no 29 | conda update -q conda 30 | 31 | # Download ChainerCV and go to the root directory of ChainerCV 32 | git clone https://github.com/chainer/chainercv 33 | cd chainercv 34 | conda env create -f environment.yml 35 | source activate chainercv 36 | 37 | # Install ChainerCV 38 | pip install -e . 39 | 40 | # Try our demos at examples/* ! 41 | 42 | -------------------------------------------------------------------------------- /chainercv/utils/bbox/_nms_gpu_post.pyx: -------------------------------------------------------------------------------- 1 | cimport numpy as np 2 | from libc.stdint cimport uint64_t 3 | 4 | import numpy as np 5 | 6 | 7 | def _nms_gpu_post(np.ndarray[np.uint64_t, ndim=1] mask, 8 | int n_bbox, 9 | int threads_per_block, 10 | int col_blocks 11 | ): 12 | cdef: 13 | int i, j, nblock, index 14 | uint64_t inblock 15 | int n_selection = 0 16 | uint64_t one_ull = 1 17 | np.ndarray[np.int32_t, ndim=1] selection 18 | np.ndarray[np.uint64_t, ndim=1] remv 19 | 20 | selection= np.zeros((n_bbox,), dtype=np.int32) 21 | remv = np.zeros((col_blocks,), dtype=np.uint64) 22 | 23 | for i in range(n_bbox): 24 | nblock = i // threads_per_block 25 | inblock = i % threads_per_block 26 | 27 | if not (remv[nblock] & one_ull << inblock): 28 | selection[n_selection] = i 29 | n_selection += 1 30 | 31 | index = i * col_blocks 32 | for j in range(nblock, col_blocks): 33 | remv[j] |= mask[index + j] 34 | return selection, n_selection 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2017 Preferred Networks, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/datasets_tests/sbd_tests/test_sbd_instance_segmentation_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from chainer import testing 4 | from chainer.testing import attr 5 | 6 | from chainercv.datasets import sbd_instance_segmentation_label_names 7 | from chainercv.datasets import SBDInstanceSegmentationDataset 8 | from chainercv.utils import assert_is_instance_segmentation_dataset 9 | 10 | try: 11 | import scipy # NOQA 12 | _available = True 13 | except ImportError: 14 | _available = False 15 | 16 | 17 | @testing.parameterize( 18 | {'split': 'train'}, 19 | {'split': 'val'}, 20 | {'split': 'trainval'} 21 | ) 22 | @unittest.skipUnless(_available, 'SciPy is not installed') 23 | class TestSBDInstanceSegmentationDataset(unittest.TestCase): 24 | 25 | def setUp(self): 26 | self.dataset = SBDInstanceSegmentationDataset(split=self.split) 27 | 28 | @attr.slow 29 | def test_sbd_instance_segmentation_dataset(self): 30 | assert_is_instance_segmentation_dataset( 31 | self.dataset, 32 | len(sbd_instance_segmentation_label_names), 33 | n_example=10) 34 | 35 | 36 | testing.run_module(__name__, __file__) 37 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/deeplab_tests/test_aspp.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import chainer 4 | from chainer import testing 5 | from chainer.testing import attr 6 | 7 | from chainercv.links.model.deeplab import SeparableASPP 8 | 9 | 10 | class TestSeparableASPP(unittest.TestCase): 11 | 12 | def setUp(self): 13 | self.in_channels = 128 14 | self.out_channels = 32 15 | self.link = SeparableASPP( 16 | self.in_channels, self.out_channels) 17 | 18 | def check_call(self): 19 | xp = self.link.xp 20 | x = chainer.Variable(xp.random.uniform( 21 | low=-1, high=1, size=(2, self.in_channels, 64, 64) 22 | ).astype(xp.float32)) 23 | y = self.link(x) 24 | 25 | self.assertIsInstance(y, chainer.Variable) 26 | self.assertIsInstance(y.data, xp.ndarray) 27 | self.assertEqual(y.shape, (2, self.out_channels, 64, 64)) 28 | 29 | @attr.slow 30 | def test_call_cpu(self): 31 | self.check_call() 32 | 33 | @attr.gpu 34 | @attr.slow 35 | def test_call_gpu(self): 36 | self.link.to_gpu() 37 | self.check_call() 38 | 39 | 40 | testing.run_module(__name__, __file__) 41 | -------------------------------------------------------------------------------- /chainercv/links/model/fpn/misc.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import numpy as np 4 | 5 | from chainer.backends import cuda 6 | import chainer.functions as F 7 | 8 | from chainercv import transforms 9 | 10 | 11 | exp_clip = np.log(1000 / 16) 12 | 13 | 14 | def smooth_l1(x, t, beta): 15 | return F.huber_loss(x, t, beta, reduce='no') / beta 16 | 17 | 18 | # to avoid out of memory 19 | def argsort(x): 20 | xp = cuda.get_array_module(x) 21 | i = np.argsort(cuda.to_cpu(x)) 22 | if xp is np: 23 | return i 24 | else: 25 | return cuda.to_gpu(i) 26 | 27 | 28 | # to avoid out of memory 29 | def choice(x, size): 30 | xp = cuda.get_array_module(x) 31 | y = np.random.choice(cuda.to_cpu(x), size, replace=False) 32 | if xp is np: 33 | return y 34 | else: 35 | return cuda.to_gpu(y) 36 | 37 | 38 | def scale_img(img, min_size, max_size): 39 | """Process image.""" 40 | _, H, W = img.shape 41 | scale = min_size / min(H, W) 42 | if scale * max(H, W) > max_size: 43 | scale = max_size / max(H, W) 44 | H, W = int(H * scale), int(W * scale) 45 | img = transforms.resize(img, (H, W)) 46 | return img, scale 47 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/faster_rcnn_tests/utils_tests/test_generate_anchor_base.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import unittest 4 | 5 | import numpy as np 6 | 7 | from chainer import testing 8 | 9 | from chainercv.links.model.faster_rcnn import generate_anchor_base 10 | 11 | 12 | class TestGenerateAnchorBase(unittest.TestCase): 13 | 14 | def test_generaete_anchor_base(self): 15 | gt = np.array( 16 | [[-24., -120., 40., 136.], 17 | [-56., -248., 72., 264.], 18 | [-120., -504., 136., 520.], 19 | [-56., -56., 72., 72.], 20 | [-120., -120., 136., 136.], 21 | [-248., -248., 264., 264.], 22 | [-120., -24., 136., 40.], 23 | [-248., -56., 264., 72.], 24 | [-504., -120., 520., 136.]]) 25 | 26 | base_size = 16 27 | anchor_scales = [8, 16, 32] 28 | ratios = [0.25, 1, 4] 29 | out = generate_anchor_base(base_size=base_size, 30 | anchor_scales=anchor_scales, 31 | ratios=ratios) 32 | np.testing.assert_equal(gt, out) 33 | 34 | 35 | testing.run_module(__name__, __file__) 36 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/deeplab_tests/test_xception.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import chainer 4 | from chainer import testing 5 | from chainer.testing import attr 6 | 7 | from chainercv.links.model.deeplab.xception import Xception65 8 | 9 | 10 | class TestXception(unittest.TestCase): 11 | 12 | def setUp(self): 13 | self.link = Xception65() 14 | 15 | def check_call(self): 16 | xp = self.link.xp 17 | x = chainer.Variable(xp.random.uniform( 18 | low=-1, high=1, size=(2, 3, 64, 64) 19 | ).astype(xp.float32)) 20 | y1, y2 = self.link(x) 21 | 22 | self.assertIsInstance(y1, chainer.Variable) 23 | self.assertIsInstance(y1.data, xp.ndarray) 24 | self.assertEqual(y1.shape, (2, 256, 16, 16)) 25 | self.assertIsInstance(y2, chainer.Variable) 26 | self.assertIsInstance(y2.data, xp.ndarray) 27 | self.assertEqual(y2.shape, (2, 2048, 8, 8)) 28 | 29 | @attr.slow 30 | def test_call_cpu(self): 31 | self.check_call() 32 | 33 | @attr.gpu 34 | @attr.slow 35 | def test_call_gpu(self): 36 | self.link.to_gpu() 37 | self.check_call() 38 | 39 | 40 | testing.run_module(__name__, __file__) 41 | -------------------------------------------------------------------------------- /chainercv/transforms/bbox/flip_bbox.py: -------------------------------------------------------------------------------- 1 | def flip_bbox(bbox, size, y_flip=False, x_flip=False): 2 | """Flip bounding boxes accordingly. 3 | 4 | Args: 5 | bbox (~numpy.ndarray): See the table below. 6 | size (tuple): A tuple of length 2. The height and the width 7 | of the image before resized. 8 | y_flip (bool): Flip bounding box according to a vertical flip of 9 | an image. 10 | x_flip (bool): Flip bounding box according to a horizontal flip of 11 | an image. 12 | 13 | .. csv-table:: 14 | :header: name, shape, dtype, format 15 | 16 | :obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \ 17 | ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" 18 | 19 | Returns: 20 | ~numpy.ndarray: 21 | Bounding boxes flipped according to the given flips. 22 | 23 | """ 24 | H, W = size 25 | bbox = bbox.copy() 26 | if y_flip: 27 | y_max = H - bbox[:, 0] 28 | y_min = H - bbox[:, 2] 29 | bbox[:, 0] = y_min 30 | bbox[:, 2] = y_max 31 | if x_flip: 32 | x_max = W - bbox[:, 1] 33 | x_min = W - bbox[:, 3] 34 | bbox[:, 1] = x_min 35 | bbox[:, 3] = x_max 36 | return bbox 37 | -------------------------------------------------------------------------------- /tests/transforms_tests/point_tests/test_translate_point.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import translate_point 7 | 8 | 9 | class TestTranslatePoint(unittest.TestCase): 10 | 11 | def test_translate_point_ndarray(self): 12 | point = np.random.uniform( 13 | low=0., high=32., size=(3, 10, 2)) 14 | 15 | out = translate_point(point, y_offset=3, x_offset=5) 16 | expected = np.empty_like(point) 17 | expected[:, :, 0] = point[:, :, 0] + 3 18 | expected[:, :, 1] = point[:, :, 1] + 5 19 | np.testing.assert_equal(out, expected) 20 | 21 | def test_translate_point_list(self): 22 | point = [ 23 | np.random.uniform(low=0., high=32., size=(12, 2)), 24 | np.random.uniform(low=0., high=32., size=(10, 2)) 25 | ] 26 | 27 | out = translate_point(point, y_offset=3, x_offset=5) 28 | for i, pnt in enumerate(point): 29 | expected = np.empty_like(pnt) 30 | expected[:, 0] = pnt[:, 0] + 3 31 | expected[:, 1] = pnt[:, 1] + 5 32 | np.testing.assert_equal(out[i], expected) 33 | 34 | 35 | testing.run_module(__name__, __file__) 36 | -------------------------------------------------------------------------------- /chainercv/utils/testing/assertions/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.utils.testing.assertions.assert_is_bbox import assert_is_bbox # NOQA 2 | from chainercv.utils.testing.assertions.assert_is_bbox_dataset import assert_is_bbox_dataset # NOQA 3 | from chainercv.utils.testing.assertions.assert_is_detection_link import assert_is_detection_link # NOQA 4 | from chainercv.utils.testing.assertions.assert_is_image import assert_is_image # NOQA 5 | from chainercv.utils.testing.assertions.assert_is_instance_segmentation_dataset import assert_is_instance_segmentation_dataset # NOQA 6 | from chainercv.utils.testing.assertions.assert_is_instance_segmentation_link import assert_is_instance_segmentation_link # NOQA 7 | from chainercv.utils.testing.assertions.assert_is_label_dataset import assert_is_label_dataset # NOQA 8 | from chainercv.utils.testing.assertions.assert_is_point import assert_is_point # NOQA 9 | from chainercv.utils.testing.assertions.assert_is_point_dataset import assert_is_point_dataset # NOQA 10 | from chainercv.utils.testing.assertions.assert_is_semantic_segmentation_dataset import assert_is_semantic_segmentation_dataset # NOQA 11 | from chainercv.utils.testing.assertions.assert_is_semantic_segmentation_link import assert_is_semantic_segmentation_link # NOQA 12 | -------------------------------------------------------------------------------- /chainercv/links/model/fpn/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.model.fpn.bbox_head import bbox_head_loss_post # NOQA 2 | from chainercv.links.model.fpn.bbox_head import bbox_head_loss_pre # NOQA 3 | from chainercv.links.model.fpn.bbox_head import BboxHead # NOQA 4 | from chainercv.links.model.fpn.faster_rcnn import FasterRCNN # NOQA 5 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import FasterRCNNFPNResNet # NOQA 6 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import FasterRCNNFPNResNet101 # NOQA 7 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import FasterRCNNFPNResNet50 # NOQA 8 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import MaskRCNNFPNResNet101 # NOQA 9 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import MaskRCNNFPNResNet50 # NOQA 10 | from chainercv.links.model.fpn.fpn import FPN # NOQA 11 | from chainercv.links.model.fpn.mask_head import mask_head_loss_post # NOQA 12 | from chainercv.links.model.fpn.mask_head import mask_head_loss_pre # NOQA 13 | from chainercv.links.model.fpn.mask_head import MaskHead # NOQA 14 | from chainercv.links.model.fpn.mask_utils import mask_to_segm # NOQA 15 | from chainercv.links.model.fpn.mask_utils import segm_to_mask # NOQA 16 | from chainercv.links.model.fpn.rpn import RPN # NOQA 17 | from chainercv.links.model.fpn.rpn import rpn_loss # NOQA 18 | -------------------------------------------------------------------------------- /docs/source/reference/links.rst: -------------------------------------------------------------------------------- 1 | Links 2 | ===== 3 | 4 | 5 | Model 6 | ----- 7 | 8 | General Chain 9 | ~~~~~~~~~~~~~ 10 | 11 | .. toctree:: 12 | 13 | links/general_chain 14 | 15 | 16 | Feature Extraction 17 | ~~~~~~~~~~~~~~~~~~ 18 | Feature extraction links extract feature(s) from given images. 19 | 20 | .. toctree:: 21 | 22 | links/resnet 23 | links/senet 24 | links/vgg 25 | 26 | 27 | Detection 28 | ~~~~~~~~~ 29 | 30 | Detection links share a common method :meth:`predict` to detect objects in images. 31 | For more details, please read :func:`FasterRCNN.predict`. 32 | 33 | .. toctree:: 34 | 35 | links/faster_rcnn 36 | links/ssd 37 | links/yolo 38 | 39 | 40 | Semantic Segmentation 41 | ~~~~~~~~~~~~~~~~~~~~~ 42 | 43 | .. module:: chainercv.links.model.segnet 44 | 45 | Semantic segmentation links share a common method :meth:`predict` to conduct semantic segmentation of images. 46 | For more details, please read :func:`SegNetBasic.predict`. 47 | 48 | .. toctree:: 49 | 50 | links/segnet 51 | links/deeplab 52 | 53 | 54 | Links for Multiple Tasks 55 | ~~~~~~~~~~~~~~~~~~~~~~~~ 56 | 57 | .. toctree:: 58 | 59 | links/fpn 60 | 61 | 62 | Classifiers 63 | ~~~~~~~~~~~ 64 | 65 | .. toctree:: 66 | 67 | links/classifier 68 | 69 | 70 | Connection 71 | ---------- 72 | 73 | .. toctree:: 74 | links/connection 75 | -------------------------------------------------------------------------------- /chainercv/utils/mask/mask_to_bbox.py: -------------------------------------------------------------------------------- 1 | from chainer.backends import cuda 2 | import numpy as np 3 | 4 | 5 | def mask_to_bbox(mask): 6 | """Compute the bounding boxes around the masked regions. 7 | 8 | This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as 9 | inputs. 10 | 11 | Args: 12 | mask (array): An array whose shape is :math:`(R, H, W)`. 13 | :math:`R` is the number of masks. 14 | The dtype should be :obj:`numpy.bool`. 15 | 16 | Returns: 17 | array: 18 | The bounding boxes around the masked regions. 19 | This is an array whose shape is :math:`(R, 4)`. 20 | :math:`R` is the number of bounding boxes. 21 | The dtype should be :obj:`numpy.float32`. 22 | 23 | """ 24 | R, H, W = mask.shape 25 | xp = cuda.get_array_module(mask) 26 | 27 | instance_index, ys, xs = xp.nonzero(mask) 28 | bbox = xp.zeros((R, 4), dtype=np.float32) 29 | for i in range(R): 30 | ys_i = ys[instance_index == i] 31 | xs_i = xs[instance_index == i] 32 | if len(ys_i) == 0: 33 | continue 34 | y_min = ys_i.min() 35 | x_min = xs_i.min() 36 | y_max = ys_i.max() + 1 37 | x_max = xs_i.max() + 1 38 | bbox[i] = xp.array([y_min, x_min, y_max, x_max], dtype=np.float32) 39 | return bbox 40 | -------------------------------------------------------------------------------- /chainercv/transforms/point/translate_point.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def translate_point(point, y_offset=0, x_offset=0): 5 | """Translate points. 6 | 7 | This method is mainly used together with image transforms, such as padding 8 | and cropping, which translates the top left point of the image 9 | to the coordinate :math:`(y, x) = (y_{offset}, x_{offset})`. 10 | 11 | Args: 12 | point (~numpy.ndarray or list of arrays): See the table below. 13 | y_offset (int or float): The offset along y axis. 14 | x_offset (int or float): The offset along x axis. 15 | 16 | .. csv-table:: 17 | :header: name, shape, dtype, format 18 | 19 | :obj:`point`, ":math:`(R, K, 2)` or :math:`[(K, 2)]`", \ 20 | :obj:`float32`, ":math:`(y, x)`" 21 | 22 | Returns: 23 | ~numpy.ndarray: 24 | Points modified translation of an image. 25 | 26 | """ 27 | 28 | if isinstance(point, np.ndarray): 29 | out_point = point.copy() 30 | 31 | out_point[:, :, 0] += y_offset 32 | out_point[:, :, 1] += x_offset 33 | else: 34 | out_point = [] 35 | for pnt in point: 36 | out_pnt = pnt.copy() 37 | out_pnt[:, 0] += y_offset 38 | out_pnt[:, 1] += x_offset 39 | out_point.append(out_pnt) 40 | return out_point 41 | -------------------------------------------------------------------------------- /chainercv/utils/testing/__init__.py: -------------------------------------------------------------------------------- 1 | from chainer.testing import product # NOQA 2 | from chainer.testing import product_dict # NOQA 3 | from chainer.testing import run_module # NOQA 4 | 5 | from chainercv.utils.testing.assertions import assert_is_bbox # NOQA 6 | from chainercv.utils.testing.assertions import assert_is_bbox_dataset # NOQA 7 | from chainercv.utils.testing.assertions import assert_is_detection_link # NOQA 8 | from chainercv.utils.testing.assertions import assert_is_image # NOQA 9 | from chainercv.utils.testing.assertions import assert_is_instance_segmentation_dataset # NOQA 10 | from chainercv.utils.testing.assertions import assert_is_instance_segmentation_link # NOQA 11 | from chainercv.utils.testing.assertions import assert_is_label_dataset # NOQA 12 | from chainercv.utils.testing.assertions import assert_is_point # NOQA 13 | from chainercv.utils.testing.assertions import assert_is_point_dataset # NOQA 14 | from chainercv.utils.testing.assertions import assert_is_semantic_segmentation_dataset # NOQA 15 | from chainercv.utils.testing.assertions import assert_is_semantic_segmentation_link # NOQA 16 | from chainercv.utils.testing.constant_stub_link import ConstantStubLink # NOQA 17 | from chainercv.utils.testing.generate_random_bbox import generate_random_bbox # NOQA 18 | from chainercv.utils.testing.parameterized import parameterize # NOQA 19 | -------------------------------------------------------------------------------- /chainercv/utils/iterator/progress_hook.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import sys 4 | import time 5 | 6 | 7 | class ProgressHook(object): 8 | """A hook class reporting the progress of iteration. 9 | 10 | This is a hook class designed for 11 | :func:`~chainercv.utils.apply_prediction_to_iterator`. 12 | 13 | Args: 14 | n_total (int): The number of images. This argument is optional. 15 | """ 16 | 17 | def __init__(self, n_total=None): 18 | self.n_total = n_total 19 | self.start = time.time() 20 | self.n_processed = 0 21 | 22 | def __call__(self, in_values, out_values, rest_values): 23 | self.n_processed += len(in_values[0]) 24 | fps = self.n_processed / (time.time() - self.start) 25 | if self.n_total is not None and fps > 0: 26 | eta = int((self.n_total - self.n_processed) / fps) 27 | sys.stdout.write( 28 | '\r{:d} of {:d} samples, {:.2f} samples/sec,' 29 | ' ETA {:4d}:{:02d}:{:02d}'.format( 30 | self.n_processed, self.n_total, fps, 31 | eta // 60 // 60, (eta // 60) % 60, eta % 60)) 32 | else: 33 | sys.stdout.write( 34 | '\r{:d} samples, {:.2f} samples/sec'.format( 35 | self.n_processed, fps)) 36 | 37 | sys.stdout.flush() 38 | -------------------------------------------------------------------------------- /tests/datasets_tests/online_products_tests/test_online_products_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainer.testing import attr 7 | 8 | from chainercv.datasets import online_products_super_label_names 9 | from chainercv.datasets import OnlineProductsDataset 10 | from chainercv.utils import assert_is_label_dataset 11 | 12 | 13 | @testing.parameterize( 14 | {'split': 'train'}, 15 | {'split': 'test'} 16 | ) 17 | class TestOnlineProductsDataset(unittest.TestCase): 18 | 19 | def setUp(self): 20 | self.dataset = OnlineProductsDataset(split=self.split) 21 | 22 | @attr.slow 23 | def test_online_products_dataset(self): 24 | assert_is_label_dataset( 25 | self.dataset, 22634, n_example=10) 26 | 27 | for _ in range(10): 28 | i = np.random.randint(0, len(self.dataset)) 29 | _, _, super_label = self.dataset[i] 30 | 31 | assert isinstance(super_label, np.int32), \ 32 | 'label must be a numpy.int32.' 33 | assert super_label.ndim == 0, 'The ndim of label must be 0' 34 | assert (super_label >= 0 and 35 | super_label < len(online_products_super_label_names)), \ 36 | 'The value of label must be in [0, n_class - 1].' 37 | 38 | 39 | testing.run_module(__name__, __file__) 40 | -------------------------------------------------------------------------------- /chainercv/utils/image/read_label.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from PIL import Image 3 | 4 | 5 | def read_label(file, dtype=np.int32): 6 | """Read a label image from a file. 7 | 8 | This function reads an label image from given file. If reading label 9 | doesn't work collectly, try :func:`~chainercv.utils.read_image` with 10 | a parameter :obj:`color=True`. 11 | 12 | Args: 13 | file (string or file-like object): A path of image file or 14 | a file-like object of image. 15 | dtype: The type of array. The default value is :obj:`~numpy.int32`. 16 | color (bool): This option determines the number of channels. 17 | If :obj:`True`, the number of channels is three. In this case, 18 | the order of the channels is RGB. This is the default behaviour. 19 | If :obj:`False`, this function returns a grayscale image. 20 | 21 | Returns: 22 | ~numpy.ndarray: An image. 23 | """ 24 | 25 | f = Image.open(file) 26 | try: 27 | img = f.convert('P') 28 | img = np.array(img, dtype=dtype) 29 | finally: 30 | if hasattr(f, 'close'): 31 | f.close() 32 | 33 | if img.ndim == 2: 34 | return img 35 | elif img.shape[2] == 1: 36 | return img[:, :, 0] 37 | else: 38 | raise ValueError("Color image can't be accepted as label image.") 39 | -------------------------------------------------------------------------------- /tests/utils_tests/image_tests/test_tile_images.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | import math 3 | import numpy as np 4 | import unittest 5 | 6 | from chainercv.utils import testing 7 | from chainercv.utils import tile_images 8 | 9 | 10 | @testing.parameterize(*testing.product({ 11 | 'fill': [128, (104, 117, 123), np.random.uniform(255, size=(3, 1, 1))], 12 | 'pad': [0, 1, 2, 3, (3, 5), (5, 2)] 13 | })) 14 | class TestTileImages(unittest.TestCase): 15 | 16 | def test_tile_images(self): 17 | B = np.random.randint(10, 20) 18 | n_col = np.random.randint(2, 5) 19 | H = 30 20 | W = 40 21 | 22 | imgs = np.random.uniform(255, size=(B, 3, H, W)) 23 | tile = tile_images(imgs, n_col, self.pad, fill=self.fill) 24 | 25 | if isinstance(self.pad, int): 26 | pad_y = self.pad 27 | pad_x = self.pad 28 | else: 29 | pad_y, pad_x = self.pad 30 | n_row = int(math.ceil(B / n_col)) 31 | self.assertTrue(n_col >= 1 and n_row >= 1) 32 | start_y_11 = H + pad_y + pad_y // 2 33 | start_x_11 = W + pad_x + pad_x // 2 34 | tile_11 = tile[:, 35 | start_y_11:start_y_11 + H, 36 | start_x_11:start_x_11 + W] 37 | 38 | np.testing.assert_equal(tile_11, imgs[(n_col - 1) + 2]) 39 | 40 | 41 | testing.run_module(__name__, __file__) 42 | -------------------------------------------------------------------------------- /chainercv/utils/testing/assertions/assert_is_bbox.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def assert_is_bbox(bbox, size=None): 5 | """Checks if bounding boxes satisfy bounding box format. 6 | 7 | This function checks if given bounding boxes satisfy bounding boxes 8 | format or not. 9 | If the bounding boxes do not satifiy the format, this function raises an 10 | :class:`AssertionError`. 11 | 12 | Args: 13 | bbox (~numpy.ndarray): Bounding boxes to be checked. 14 | size (tuple of ints): The size of an image. 15 | If this argument is specified, 16 | Each bounding box should be within the image. 17 | """ 18 | 19 | assert isinstance(bbox, np.ndarray), \ 20 | 'bbox must be a numpy.ndarray.' 21 | assert bbox.dtype == np.float32, \ 22 | 'The type of bbox must be numpy.float32,' 23 | assert bbox.shape[1:] == (4,), \ 24 | 'The shape of bbox must be (*, 4).' 25 | assert (bbox[:, 0] < bbox[:, 2]).all(), \ 26 | 'The coordinate of top must be less than that of bottom.' 27 | assert (bbox[:, 1] < bbox[:, 3]).all(), \ 28 | 'The coordinate of left must be less than that of right.' 29 | 30 | if size is not None: 31 | assert (bbox[:, :2] >= 0).all() and (bbox[:, 2:] <= size).all(),\ 32 | 'The coordinates of bounding boxes ' \ 33 | 'should not exceed the size of image.' 34 | -------------------------------------------------------------------------------- /docs/source/reference/links/faster_rcnn.rst: -------------------------------------------------------------------------------- 1 | Faster R-CNN 2 | ============ 3 | 4 | .. module:: chainercv.links.model.faster_rcnn 5 | 6 | 7 | Detection Link 8 | -------------- 9 | 10 | FasterRCNNVGG16 11 | ~~~~~~~~~~~~~~~ 12 | .. autoclass:: FasterRCNNVGG16 13 | 14 | 15 | Utility 16 | ------- 17 | 18 | bbox2loc 19 | ~~~~~~~~ 20 | .. autofunction:: bbox2loc 21 | 22 | FasterRCNN 23 | ~~~~~~~~~~ 24 | .. autoclass:: FasterRCNN 25 | :members: 26 | 27 | generate_anchor_base 28 | ~~~~~~~~~~~~~~~~~~~~ 29 | .. autofunction:: generate_anchor_base 30 | 31 | loc2bbox 32 | ~~~~~~~~ 33 | .. autofunction:: loc2bbox 34 | 35 | ProposalCreator 36 | ~~~~~~~~~~~~~~~ 37 | .. autoclass:: ProposalCreator 38 | :members: 39 | :special-members: __call__ 40 | 41 | RegionProposalNetwork 42 | ~~~~~~~~~~~~~~~~~~~~~ 43 | .. autoclass:: RegionProposalNetwork 44 | :members: 45 | 46 | VGG16RoIHead 47 | ~~~~~~~~~~~~ 48 | .. autoclass:: VGG16RoIHead 49 | 50 | 51 | Train-only Utility 52 | ------------------ 53 | 54 | AnchorTargetCreator 55 | ~~~~~~~~~~~~~~~~~~~ 56 | .. autoclass:: AnchorTargetCreator 57 | :members: 58 | :special-members: __call__ 59 | 60 | FasterRCNNTrainChain 61 | ~~~~~~~~~~~~~~~~~~~~ 62 | .. autoclass:: FasterRCNNTrainChain 63 | :members: 64 | 65 | ProposalTargetCreator 66 | ~~~~~~~~~~~~~~~~~~~~~ 67 | .. autoclass:: ProposalTargetCreator 68 | :members: 69 | :special-members: __call__ 70 | -------------------------------------------------------------------------------- /examples/faster_rcnn/demo.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import matplotlib.pyplot as plt 3 | 4 | import chainer 5 | 6 | from chainercv.datasets import voc_bbox_label_names 7 | from chainercv.links import FasterRCNNVGG16 8 | from chainercv import utils 9 | from chainercv.visualizations import vis_bbox 10 | 11 | 12 | def main(): 13 | parser = argparse.ArgumentParser() 14 | parser.add_argument('--gpu', type=int, default=-1) 15 | parser.add_argument('--pretrained-model') 16 | parser.add_argument( 17 | '--dataset', choices=('voc',), default='voc') 18 | parser.add_argument('image') 19 | args = parser.parse_args() 20 | 21 | if args.dataset == 'voc': 22 | if args.pretrained_model is None: 23 | args.pretrained_model = 'voc07' 24 | label_names = voc_bbox_label_names 25 | 26 | model = FasterRCNNVGG16( 27 | n_fg_class=len(label_names), 28 | pretrained_model=args.pretrained_model) 29 | 30 | if args.gpu >= 0: 31 | chainer.cuda.get_device_from_id(args.gpu).use() 32 | model.to_gpu() 33 | 34 | img = utils.read_image(args.image, color=True) 35 | bboxes, labels, scores = model.predict([img]) 36 | bbox, label, score = bboxes[0], labels[0], scores[0] 37 | 38 | vis_bbox( 39 | img, bbox, label, score, label_names=label_names) 40 | plt.show() 41 | 42 | 43 | if __name__ == '__main__': 44 | main() 45 | -------------------------------------------------------------------------------- /chainercv/utils/testing/generate_random_bbox.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def generate_random_bbox(n, img_size, min_length, max_length): 5 | """Generate valid bounding boxes with random position and shape. 6 | 7 | Args: 8 | n (int): The number of bounding boxes. 9 | img_size (tuple): A tuple of length 2. The height and the width 10 | of the image on which bounding boxes locate. 11 | min_length (float): The minimum length of edges of bounding boxes. 12 | max_length (float): The maximum length of edges of bounding boxes. 13 | 14 | Return: 15 | numpy.ndarray: 16 | Coordinates of bounding boxes. Its shape is :math:`(R, 4)`. \ 17 | Here, :math:`R` equals :obj:`n`. 18 | The second axis contains :math:`y_{min}, x_{min}, y_{max}, x_{max}`, 19 | where 20 | :math:`min\_length \\leq y_{max} - y_{min} < max\_length`. 21 | and 22 | :math:`min\_length \\leq x_{max} - x_{min} < max\_length` 23 | 24 | """ 25 | H, W = img_size 26 | y_min = np.random.uniform(0, H - max_length, size=(n,)) 27 | x_min = np.random.uniform(0, W - max_length, size=(n,)) 28 | y_max = y_min + np.random.uniform(min_length, max_length, size=(n,)) 29 | x_max = x_min + np.random.uniform(min_length, max_length, size=(n,)) 30 | bbox = np.stack((y_min, x_min, y_max, x_max), axis=1).astype(np.float32) 31 | return bbox 32 | -------------------------------------------------------------------------------- /chainercv/utils/testing/assertions/assert_is_semantic_segmentation_link.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import six 3 | 4 | 5 | def assert_is_semantic_segmentation_link(link, n_class): 6 | """Checks if a link satisfies semantic segmentation link APIs. 7 | 8 | This function checks if a given link satisfies semantic segmentation link 9 | APIs or not. 10 | If the link does not satifiy the APIs, this function raises an 11 | :class:`AssertionError`. 12 | 13 | Args: 14 | link: A link to be checked. 15 | n_class (int): The number of classes including background. 16 | 17 | """ 18 | 19 | imgs = [ 20 | np.random.randint(0, 256, size=(3, 480, 640)).astype(np.float32), 21 | np.random.randint(0, 256, size=(3, 480, 320)).astype(np.float32)] 22 | 23 | labels = link.predict(imgs) 24 | assert len(labels) == len(imgs), \ 25 | 'The length of labels must be same as that of imgs.' 26 | 27 | for img, label in six.moves.zip(imgs, labels): 28 | assert isinstance(label, np.ndarray), \ 29 | 'label must be a numpy.ndarray.' 30 | assert label.dtype == np.int32, \ 31 | 'The type of label must be numpy.int32.' 32 | assert label.shape == img.shape[1:], \ 33 | 'The shape of label must be (H, W).' 34 | assert label.min() >= 0 and label.max() < n_class, \ 35 | 'The value of label must be in [0, n_class - 1].' 36 | -------------------------------------------------------------------------------- /chainercv/transforms/point/resize_point.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def resize_point(point, in_size, out_size): 5 | """Adapt point coordinates to the rescaled image space. 6 | 7 | Args: 8 | point (~numpy.ndarray or list of arrays): See the table below. 9 | in_size (tuple): A tuple of length 2. The height and the width 10 | of the image before resized. 11 | out_size (tuple): A tuple of length 2. The height and the width 12 | of the image after resized. 13 | 14 | .. csv-table:: 15 | :header: name, shape, dtype, format 16 | 17 | :obj:`point`, ":math:`(R, K, 2)` or :math:`[(K, 2)]`", \ 18 | :obj:`float32`, ":math:`(y, x)`" 19 | 20 | Returns: 21 | ~numpy.ndarray or list of arrays: 22 | Points rescaled according to the given image shapes. 23 | 24 | """ 25 | y_scale = float(out_size[0]) / in_size[0] 26 | x_scale = float(out_size[1]) / in_size[1] 27 | if isinstance(point, np.ndarray): 28 | out_point = point.copy() 29 | out_point[:, :, 0] = y_scale * point[:, :, 0] 30 | out_point[:, :, 1] = x_scale * point[:, :, 1] 31 | else: 32 | out_point = [] 33 | for pnt in point: 34 | out_pnt = pnt.copy() 35 | out_pnt[:, 0] = y_scale * pnt[:, 0] 36 | out_pnt[:, 1] = x_scale * pnt[:, 1] 37 | out_point.append(out_pnt) 38 | return out_point 39 | -------------------------------------------------------------------------------- /chainercv/links/connection/seblock.py: -------------------------------------------------------------------------------- 1 | import chainer 2 | import chainer.functions as F 3 | import chainer.links as L 4 | 5 | 6 | class SEBlock(chainer.Chain): 7 | 8 | """A squeeze-and-excitation block. 9 | 10 | This block is part of squeeze-and-excitation networks. Channel-wise 11 | multiplication weights are inferred from and applied to input feature map. 12 | Please refer to `the original paper 13 | `_ for more details. 14 | 15 | .. seealso:: 16 | :class:`chainercv.links.model.senet.SEResNet` 17 | 18 | Args: 19 | n_channel (int): The number of channels of the input and output array. 20 | ratio (int): Reduction ratio of :obj:`n_channel` to the number of 21 | hidden layer units. 22 | 23 | """ 24 | 25 | def __init__(self, n_channel, ratio=16): 26 | 27 | super(SEBlock, self).__init__() 28 | reduction_size = n_channel // ratio 29 | 30 | with self.init_scope(): 31 | self.down = L.Linear(n_channel, reduction_size) 32 | self.up = L.Linear(reduction_size, n_channel) 33 | 34 | def forward(self, u): 35 | B, C, H, W = u.shape 36 | 37 | z = F.average(u, axis=(2, 3)) 38 | x = F.relu(self.down(z)) 39 | x = F.sigmoid(self.up(x)) 40 | x = F.reshape(x, x.shape[:2] + (1, 1)) 41 | # Spatial axes of `x` will be broadcasted. 42 | return u * x 43 | -------------------------------------------------------------------------------- /tests/datasets_tests/ade20k_tests/test_ade20k.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | from chainer import testing 5 | from chainer.testing import attr 6 | 7 | from chainercv.datasets import ade20k_semantic_segmentation_label_names 8 | from chainercv.datasets import ADE20KSemanticSegmentationDataset 9 | from chainercv.datasets import ADE20KTestImageDataset 10 | from chainercv.utils import assert_is_semantic_segmentation_dataset 11 | from chainercv.utils.testing.assertions.assert_is_image import assert_is_image 12 | 13 | 14 | @testing.parameterize( 15 | {'split': 'train'}, 16 | {'split': 'val'}, 17 | ) 18 | class TestADE20KSemanticSegmentationDataset(unittest.TestCase): 19 | 20 | def setUp(self): 21 | self.dataset = ADE20KSemanticSegmentationDataset(split=self.split) 22 | 23 | @attr.slow 24 | def test_ade20k_dataset(self): 25 | assert_is_semantic_segmentation_dataset( 26 | self.dataset, len(ade20k_semantic_segmentation_label_names), 27 | n_example=10) 28 | 29 | 30 | class TestADE20KTestImageDataset(unittest.TestCase): 31 | 32 | def setUp(self): 33 | self.dataset = ADE20KTestImageDataset() 34 | 35 | @attr.slow 36 | def test_ade20k_dataset(self): 37 | indices = np.random.permutation(np.arange(len(self.dataset))) 38 | for i in indices[:10]: 39 | img = self.dataset[i] 40 | assert_is_image(img, color=True) 41 | 42 | 43 | testing.run_module(__name__, __file__) 44 | -------------------------------------------------------------------------------- /tests/datasets_tests/cub_tests/test_cub_label_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainer.testing import attr 7 | 8 | from chainercv.datasets import cub_label_names 9 | from chainercv.datasets import CUBLabelDataset 10 | from chainercv.utils import assert_is_bbox 11 | from chainercv.utils import assert_is_label_dataset 12 | 13 | 14 | @testing.parameterize(*testing.product({ 15 | 'return_bbox': [True, False], 16 | 'return_prob_map': [True, False] 17 | })) 18 | class TestCUBLabelDataset(unittest.TestCase): 19 | 20 | def setUp(self): 21 | self.dataset = CUBLabelDataset( 22 | return_bbox=self.return_bbox, return_prob_map=self.return_prob_map) 23 | 24 | @attr.slow 25 | def test_cub_label_dataset(self): 26 | assert_is_label_dataset( 27 | self.dataset, len(cub_label_names), n_example=10) 28 | idx = np.random.choice(np.arange(10)) 29 | if self.return_bbox: 30 | bbox = self.dataset[idx][2] 31 | assert_is_bbox(bbox) 32 | if self.return_prob_map: 33 | img = self.dataset[idx][0] 34 | prob_map = self.dataset[idx][-1] 35 | self.assertEqual(prob_map.dtype, np.float32) 36 | self.assertEqual(prob_map.shape, img.shape[1:]) 37 | self.assertTrue(np.min(prob_map) >= 0) 38 | self.assertTrue(np.max(prob_map) <= 1) 39 | 40 | 41 | testing.run_module(__name__, __file__) 42 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/ssd_tests/test_gradient_scaling.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | import chainer 5 | from chainer import optimizers 6 | from chainer import testing 7 | from chainer.testing import attr 8 | 9 | from chainercv.links.model.ssd import GradientScaling 10 | 11 | 12 | class SimpleLink(chainer.Link): 13 | 14 | def __init__(self, w, g): 15 | super(SimpleLink, self).__init__() 16 | with self.init_scope(): 17 | self.param = chainer.Parameter(w) 18 | self.param.grad = g 19 | 20 | 21 | class TestGradientScaling(unittest.TestCase): 22 | 23 | def setUp(self): 24 | self.target = SimpleLink( 25 | np.arange(6, dtype=np.float32).reshape((2, 3)), 26 | np.arange(3, -3, -1, dtype=np.float32).reshape((2, 3))) 27 | 28 | def check_gradient_scaling(self): 29 | w = self.target.param.array 30 | g = self.target.param.grad 31 | 32 | rate = 0.2 33 | expect = w - g * rate 34 | 35 | opt = optimizers.SGD(lr=1) 36 | opt.setup(self.target) 37 | opt.add_hook(GradientScaling(rate)) 38 | opt.update() 39 | 40 | testing.assert_allclose(expect, w) 41 | 42 | def test_gradient_scaling_cpu(self): 43 | self.check_gradient_scaling() 44 | 45 | @attr.gpu 46 | def test_gradient_scaling_gpu(self): 47 | self.target.to_gpu() 48 | self.check_gradient_scaling() 49 | 50 | 51 | testing.run_module(__name__, __file__) 52 | -------------------------------------------------------------------------------- /chainercv/transforms/image/random_rotate.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import warnings 3 | 4 | 5 | def random_rotate(img, return_param=False): 6 | """Randomly rotate images by 90, 180, 270 or 360 degrees. 7 | 8 | Args: 9 | img (~numpy.ndarray): An arrays that get flipped. This is in 10 | CHW format. 11 | return_param (bool): Returns information of rotation. 12 | 13 | Returns: 14 | ~numpy.ndarray or (~numpy.ndarray, dict): 15 | 16 | If :obj:`return_param = False`, 17 | returns an array :obj:`out_img` that is the result of rotation. 18 | 19 | If :obj:`return_param = True`, 20 | returns a tuple whose elements are :obj:`out_img, param`. 21 | :obj:`param` is a dictionary of intermediate parameters whose 22 | contents are listed below with key, value-type and the description 23 | of the value. 24 | 25 | * **k** (*int*): The integer that represents the number of\ 26 | times the image is rotated by 90 degrees. 27 | 28 | """ 29 | warnings.warn( 30 | 'chainercv.transforms.random_rotate is deprecated. ' 31 | 'Please use a random function and chainercv.transforms.rotate ' 32 | 'instead.', DeprecationWarning) 33 | k = np.random.randint(4) 34 | img = np.transpose(img, axes=(1, 2, 0)) 35 | img = np.rot90(img, k) 36 | img = np.transpose(img, axes=(2, 0, 1)) 37 | if return_param: 38 | return img, {'k': k} 39 | else: 40 | return img 41 | -------------------------------------------------------------------------------- /tests/utils_tests/iterator_tests/test_progress_hook.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | from chainer.iterators import SerialIterator 5 | from chainer import testing 6 | 7 | from chainercv.utils import apply_to_iterator 8 | from chainercv.utils import ProgressHook 9 | 10 | 11 | class TestProgressHook(unittest.TestCase): 12 | 13 | def setUp(self): 14 | def func(*in_values): 15 | n_sample = len(in_values[0]) 16 | return [np.random.uniform() for _ in range(n_sample)] 17 | 18 | self.func = func 19 | 20 | self.dataset = [] 21 | for _ in range(5): 22 | H, W = np.random.randint(8, 16, size=2) 23 | self.dataset.append(np.random.randint(0, 256, size=(3, H, W))) 24 | 25 | def test_progress_hook(self): 26 | iterator = SerialIterator(self.dataset, 2, repeat=False) 27 | 28 | in_values, out_values, rest_values = apply_to_iterator( 29 | self.func, iterator, 30 | hook=ProgressHook(n_total=len(self.dataset))) 31 | 32 | # consume all data 33 | for _ in in_values[0]: 34 | pass 35 | 36 | def test_progress_hook_with_infinite_iterator(self): 37 | iterator = SerialIterator(self.dataset, 2) 38 | 39 | in_values, out_values, rest_values = apply_to_iterator( 40 | self.func, iterator, hook=ProgressHook()) 41 | 42 | for _ in range(10): 43 | next(in_values[0]) 44 | 45 | 46 | testing.run_module(__name__, __file__) 47 | -------------------------------------------------------------------------------- /chainercv/chainer_experimental/training/extensions/make_shift.py: -------------------------------------------------------------------------------- 1 | from chainer.training import Extension 2 | 3 | 4 | def make_shift(attr, optimizer=None): 5 | """Decorator to make shift extensions. 6 | 7 | This decorator wraps a function and makes a shift extension. 8 | Base function should take :obj:`trainer` and return a new value of 9 | :obj:`attr`. 10 | 11 | Here is an example. 12 | 13 | >>> # define an extension that updates 'lr' attribute 14 | >>> @make_shift('lr') 15 | >>> def warmup(trainer): 16 | >>> base_lr = 0.01 17 | >>> rate = 0.1 18 | >>> 19 | >>> iteration = trainer.updater.iteration 20 | >>> if iteration < 1000: 21 | >>> return base_lr * (rate + (1 - rate) * iteraion / 1000) 22 | >>> else: 23 | >>> return base_lr 24 | >>> 25 | >>> # use the extension 26 | >>> trainer.extend(warmup) 27 | 28 | Args: 29 | attr (str): Name of the attribute to shift. 30 | optimizer (~chainer.Optimizer): Target optimizer to adjust the 31 | attribute. If it is ``None``, the main optimizer of the updater is 32 | used. 33 | 34 | """ 35 | 36 | def deco(func): 37 | def ext(trainer): 38 | opt = optimizer or trainer.updater.get_optimizer('main') 39 | setattr(opt, attr, func(trainer)) 40 | ext.default_name = func.__name__ 41 | ext.priority = Extension.priority 42 | ext.initialize = ext 43 | return ext 44 | 45 | return deco 46 | -------------------------------------------------------------------------------- /chainercv/transforms/point/flip_point.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def flip_point(point, size, y_flip=False, x_flip=False): 5 | """Modify points according to image flips. 6 | 7 | Args: 8 | point (~numpy.ndarray or list of arrays): See the table below. 9 | size (tuple): A tuple of length 2. The height and the width 10 | of the image, which is associated with the points. 11 | y_flip (bool): Modify points according to a vertical flip of 12 | an image. 13 | x_flip (bool): Modify keypoipoints according to a horizontal flip of 14 | an image. 15 | 16 | .. csv-table:: 17 | :header: name, shape, dtype, format 18 | 19 | :obj:`point`, ":math:`(R, K, 2)` or :math:`[(K, 2)]`", \ 20 | :obj:`float32`, ":math:`(y, x)`" 21 | 22 | Returns: 23 | ~numpy.ndarray or list of arrays: 24 | Points modified according to image flips. 25 | 26 | """ 27 | H, W = size 28 | if isinstance(point, np.ndarray): 29 | out_point = point.copy() 30 | if y_flip: 31 | out_point[:, :, 0] = H - out_point[:, :, 0] 32 | if x_flip: 33 | out_point[:, :, 1] = W - out_point[:, :, 1] 34 | else: 35 | out_point = [] 36 | for pnt in point: 37 | pnt = pnt.copy() 38 | if y_flip: 39 | pnt[:, 0] = H - pnt[:, 0] 40 | if x_flip: 41 | pnt[:, 1] = W - pnt[:, 1] 42 | out_point.append(pnt) 43 | return out_point 44 | -------------------------------------------------------------------------------- /tests/experimental_tests/links_tests/model_tests/pspnet_tests/test_convolution_crop.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | from chainer import testing 5 | from chainercv.experimental.links.model.pspnet import convolution_crop 6 | 7 | 8 | class TestConvolutionCrop(unittest.TestCase): 9 | 10 | def test_convolution_crop(self): 11 | size = (8, 6) 12 | stride = (8, 6) 13 | n_channel = 3 14 | img = np.random.uniform(size=(n_channel, 16, 12)).astype(np.float32) 15 | crop_imgs, param = convolution_crop( 16 | img, size, stride, return_param=True) 17 | 18 | self.assertEqual(crop_imgs.shape, (4, n_channel) + size) 19 | self.assertEqual(crop_imgs.dtype, np.float32) 20 | for y in range(2): 21 | for x in range(2): 22 | self.assertEqual(param['y_slices'][2 * y + x].start, 8 * y) 23 | self.assertEqual( 24 | param['y_slices'][2 * y + x].stop, 8 * (y + 1)) 25 | self.assertEqual(param['x_slices'][2 * y + x].start, 6 * x) 26 | self.assertEqual( 27 | param['x_slices'][2 * y + x].stop, 6 * (x + 1)) 28 | for i in range(4): 29 | self.assertEqual(param['crop_y_slices'][i].start, 0) 30 | self.assertEqual(param['crop_y_slices'][i].stop, 8) 31 | self.assertEqual(param['crop_x_slices'][i].start, 0) 32 | self.assertEqual(param['crop_x_slices'][i].stop, 6) 33 | 34 | 35 | testing.run_module(__name__, __file__) 36 | -------------------------------------------------------------------------------- /chainercv/utils/testing/assertions/assert_is_image.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def assert_is_image(img, color=True, check_range=True): 5 | """Checks if an image satisfies image format. 6 | 7 | This function checks if a given image satisfies image format or not. 8 | If the image does not satifiy the format, this function raises an 9 | :class:`AssertionError`. 10 | 11 | Args: 12 | img (~numpy.ndarray): An image to be checked. 13 | color (bool): A boolean that determines the expected channel size. 14 | If it is :obj:`True`, the number of channels 15 | should be :obj:`3`. Otherwise, it should be :obj:`1`. 16 | The default value is :obj:`True`. 17 | check_range (bool): A boolean that determines whether the range 18 | of values are checked or not. If it is :obj:`True`, 19 | The values of image must be in :math:`[0, 255]`. 20 | Otherwise, this function does not check the range. 21 | The default value is :obj:`True`. 22 | 23 | """ 24 | 25 | assert isinstance(img, np.ndarray), 'img must be a numpy.ndarray.' 26 | assert len(img.shape) == 3, 'img must be a 3-dimensional array.' 27 | C, H, W = img.shape 28 | 29 | if color: 30 | assert C == 3, 'The number of channels must be 3.' 31 | else: 32 | assert C == 1, 'The number of channels must be 1.' 33 | 34 | if check_range: 35 | assert img.min() >= 0 and img.max() <= 255, \ 36 | 'The values of img must be in [0, 255].' 37 | -------------------------------------------------------------------------------- /chainercv/transforms/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.transforms.bbox.crop_bbox import crop_bbox # NOQA 2 | from chainercv.transforms.bbox.flip_bbox import flip_bbox # NOQA 3 | from chainercv.transforms.bbox.resize_bbox import resize_bbox # NOQA 4 | from chainercv.transforms.bbox.rotate_bbox import rotate_bbox # NOQA 5 | from chainercv.transforms.bbox.translate_bbox import translate_bbox # NOQA 6 | from chainercv.transforms.image.center_crop import center_crop # NOQA 7 | from chainercv.transforms.image.flip import flip # NOQA 8 | from chainercv.transforms.image.pca_lighting import pca_lighting # NOQA 9 | from chainercv.transforms.image.random_crop import random_crop # NOQA 10 | from chainercv.transforms.image.random_expand import random_expand # NOQA 11 | from chainercv.transforms.image.random_flip import random_flip # NOQA 12 | from chainercv.transforms.image.random_rotate import random_rotate # NOQA 13 | from chainercv.transforms.image.random_sized_crop import random_sized_crop # NOQA 14 | from chainercv.transforms.image.resize import resize # NOQA 15 | from chainercv.transforms.image.resize_contain import resize_contain # NOQA 16 | from chainercv.transforms.image.rotate import rotate # NOQA 17 | from chainercv.transforms.image.scale import scale # NOQA 18 | from chainercv.transforms.image.ten_crop import ten_crop # NOQA 19 | from chainercv.transforms.point.flip_point import flip_point # NOQA 20 | from chainercv.transforms.point.resize_point import resize_point # NOQA 21 | from chainercv.transforms.point.translate_point import translate_point # NOQA 22 | -------------------------------------------------------------------------------- /docs/source/reference/links/ssd.rst: -------------------------------------------------------------------------------- 1 | SSD (Single Shot Multibox Detector) 2 | =================================== 3 | 4 | .. module:: chainercv.links.model.ssd 5 | 6 | 7 | Detection Links 8 | --------------- 9 | 10 | SSD300 11 | ~~~~~~ 12 | .. autoclass:: SSD300 13 | :members: 14 | 15 | SSD512 16 | ~~~~~~ 17 | .. autoclass:: SSD512 18 | :members: 19 | 20 | 21 | Utility 22 | ------- 23 | 24 | Multibox 25 | ~~~~~~~~ 26 | .. autoclass:: Multibox 27 | :members: 28 | 29 | MultiboxCoder 30 | ~~~~~~~~~~~~~ 31 | .. autoclass:: MultiboxCoder 32 | :members: 33 | 34 | Normalize 35 | ~~~~~~~~~ 36 | .. autoclass:: Normalize 37 | :members: 38 | 39 | SSD 40 | ~~~ 41 | .. autoclass:: SSD 42 | :members: 43 | 44 | VGG16 45 | ~~~~~ 46 | .. autoclass:: VGG16 47 | :members: 48 | 49 | VGG16Extractor300 50 | ~~~~~~~~~~~~~~~~~ 51 | .. autoclass:: VGG16Extractor300 52 | :members: 53 | 54 | VGG16Extractor512 55 | ~~~~~~~~~~~~~~~~~ 56 | .. autoclass:: VGG16Extractor512 57 | :members: 58 | 59 | 60 | Train-only Utility 61 | ------------------ 62 | 63 | GradientScaling 64 | ~~~~~~~~~~~~~~~ 65 | .. autoclass:: GradientScaling 66 | 67 | multibox_loss 68 | ~~~~~~~~~~~~~ 69 | .. autofunction:: multibox_loss 70 | 71 | random_crop_with_bbox_constraints 72 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 73 | .. autofunction:: random_crop_with_bbox_constraints 74 | 75 | random_distort 76 | ~~~~~~~~~~~~~~ 77 | .. autofunction:: random_distort 78 | 79 | resize_with_random_interpolation 80 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 81 | .. autofunction:: resize_with_random_interpolation 82 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to ChainerCV 2 | 3 | :tada: Thank you for your interest in contributing to ChainerCV :tada: 4 | 5 | We welcome any kind of contributions to ChainerCV! 6 | 7 | Feel free to submit a PR or raise an issue even if the related tasks are not currently supported by ChainerCV. 8 | ChainerCV is intended to include any code that makes computer vision research and development easier with Chainer. 9 | 10 | When sending a PR to ChainerCV, please make sure the following: 11 | 12 | + Follow the coding guideline used in Chainer. This will be tested automatically by Travis CI. 13 | + Write unittests to verify your code. The tests should follow the styles used in Chainer. 14 | + Write documentations. 15 | + When adding a neural network architecture (e.g. Faster R-CNN), **make sure that the implementation achieves performance on par with the reported scores in the original paper**. 16 | Also, please write a summary on the behavioral differences between the original implementation, if there is any. 17 | For example, this summary can include a note on the difference between the hyperparameters used by you and the original authors. 18 | + Try to follow the coding conventions used in ChainerCV (e.g. variable names and directory structures). If you are adding a code without any related precedents, try to follow conventions used in Chainer, if any. 19 | Please feel free to discuss on the conventions to use including those that are already implemented. 20 | Also, [this issue](https://github.com/pfnet/chainercv/issues/159) is a good place to start knowing the coding conventions. 21 | -------------------------------------------------------------------------------- /examples/instance_segmentation/eval_instance_segmentation_multi.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import chainer 4 | from chainer import iterators 5 | import chainermn 6 | 7 | from chainercv.utils import apply_to_iterator 8 | from chainercv.utils import ProgressHook 9 | 10 | from eval_instance_segmentation import models 11 | from eval_instance_segmentation import setup 12 | 13 | 14 | def main(): 15 | parser = argparse.ArgumentParser() 16 | parser.add_argument('--dataset', choices=('sbd', 'coco')) 17 | parser.add_argument('--model', choices=sorted(models.keys())) 18 | parser.add_argument('--pretrained-model') 19 | parser.add_argument('--batchsize', type=int) 20 | args = parser.parse_args() 21 | 22 | comm = chainermn.create_communicator('pure_nccl') 23 | device = comm.intra_rank 24 | 25 | dataset, eval_, model, batchsize = setup( 26 | args.dataset, args.model, args.pretrained_model, args.batchsize) 27 | 28 | chainer.cuda.get_device_from_id(device).use() 29 | model.to_gpu() 30 | 31 | if not comm.rank == 0: 32 | apply_to_iterator(model.predict, None, comm=comm) 33 | return 34 | 35 | iterator = iterators.MultithreadIterator( 36 | dataset, batchsize * comm.size, repeat=False, shuffle=False) 37 | 38 | in_values, out_values, rest_values = apply_to_iterator( 39 | model.predict, iterator, hook=ProgressHook(len(dataset)), comm=comm) 40 | # delete unused iterators explicitly 41 | del in_values 42 | 43 | eval_(out_values, rest_values) 44 | 45 | 46 | if __name__ == '__main__': 47 | main() 48 | -------------------------------------------------------------------------------- /examples/detection/eval_detection_multi.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import chainer 4 | from chainer import iterators 5 | 6 | import chainermn 7 | 8 | from chainercv.utils import apply_to_iterator 9 | from chainercv.utils import ProgressHook 10 | 11 | from eval_detection import models 12 | from eval_detection import setup 13 | 14 | 15 | def main(): 16 | parser = argparse.ArgumentParser() 17 | parser.add_argument('--dataset', choices=('voc', 'coco')) 18 | parser.add_argument('--model', choices=sorted(models.keys())) 19 | parser.add_argument('--pretrained-model') 20 | parser.add_argument('--batchsize', type=int) 21 | args = parser.parse_args() 22 | 23 | comm = chainermn.create_communicator('pure_nccl') 24 | device = comm.intra_rank 25 | 26 | dataset, eval_, model, batchsize = setup( 27 | args.dataset, args.model, args.pretrained_model, args.batchsize) 28 | 29 | chainer.cuda.get_device_from_id(device).use() 30 | model.to_gpu() 31 | 32 | model.use_preset('evaluate') 33 | 34 | if not comm.rank == 0: 35 | apply_to_iterator(model.predict, None, comm=comm) 36 | return 37 | 38 | iterator = iterators.MultithreadIterator( 39 | dataset, batchsize * comm.size, repeat=False, shuffle=False) 40 | 41 | in_values, out_values, rest_values = apply_to_iterator( 42 | model.predict, iterator, hook=ProgressHook(len(dataset)), comm=comm) 43 | # delete unused iterators explicitly 44 | del in_values 45 | 46 | eval_(out_values, rest_values) 47 | 48 | 49 | if __name__ == '__main__': 50 | main() 51 | -------------------------------------------------------------------------------- /tests/datasets_tests/cub_tests/test_cub_keypoint_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainer.testing import attr 7 | 8 | from chainercv.datasets import CUBKeypointDataset 9 | from chainercv.utils import assert_is_bbox 10 | from chainercv.utils import assert_is_point_dataset 11 | 12 | 13 | @testing.parameterize(*testing.product({ 14 | 'return_bbox': [True, False], 15 | 'return_prob_map': [True, False]} 16 | )) 17 | class TestCUBKeypointDataset(unittest.TestCase): 18 | 19 | def setUp(self): 20 | self.dataset = CUBKeypointDataset(return_bbox=self.return_bbox, 21 | return_prob_map=self.return_prob_map) 22 | 23 | @attr.slow 24 | def test_cub_point_dataset(self): 25 | assert_is_point_dataset( 26 | self.dataset, n_point=15, n_example=10) 27 | 28 | idx = np.random.choice(np.arange(10)) 29 | if self.return_bbox: 30 | if self.return_prob_map: 31 | bbox = self.dataset[idx][-2] 32 | else: 33 | bbox = self.dataset[idx][-1] 34 | assert_is_bbox(bbox) 35 | if self.return_prob_map: 36 | img = self.dataset[idx][0] 37 | prob_map = self.dataset[idx][-1] 38 | self.assertEqual(prob_map.dtype, np.float32) 39 | self.assertEqual(prob_map.shape, img.shape[1:]) 40 | self.assertTrue(np.min(prob_map) >= 0) 41 | self.assertTrue(np.max(prob_map) <= 1) 42 | 43 | 44 | testing.run_module(__name__, __file__) 45 | -------------------------------------------------------------------------------- /docs/source/reference/evaluations.rst: -------------------------------------------------------------------------------- 1 | Evaluations 2 | =========== 3 | 4 | .. module:: chainercv.evaluations 5 | 6 | 7 | Detection COCO 8 | -------------- 9 | 10 | eval_detection_coco 11 | ~~~~~~~~~~~~~~~~~~~ 12 | .. autofunction:: eval_detection_coco 13 | 14 | 15 | Detection VOC 16 | ------------- 17 | 18 | eval_detection_voc 19 | ~~~~~~~~~~~~~~~~~~ 20 | .. autofunction:: eval_detection_voc 21 | 22 | calc_detection_voc_ap 23 | ~~~~~~~~~~~~~~~~~~~~~ 24 | .. autofunction:: calc_detection_voc_ap 25 | 26 | calc_detection_voc_prec_rec 27 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 28 | .. autofunction:: calc_detection_voc_prec_rec 29 | 30 | Instance Segmentation COCO 31 | -------------------------- 32 | 33 | eval_instance_segmentation_coco 34 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 35 | .. autofunction:: eval_instance_segmentation_coco 36 | 37 | Instance Segmentation VOC 38 | ------------------------- 39 | 40 | eval_instance_segmentation_voc 41 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 42 | .. autofunction:: eval_instance_segmentation_voc 43 | 44 | calc_instance_segmentation_voc_prec_rec 45 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 46 | .. autofunction:: calc_instance_segmentation_voc_prec_rec 47 | 48 | Semantic Segmentation IoU 49 | ------------------------- 50 | 51 | eval_semantic_segmentation 52 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ 53 | .. autofunction:: eval_semantic_segmentation 54 | 55 | calc_semantic_segmentation_confusion 56 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 57 | .. autofunction:: calc_semantic_segmentation_confusion 58 | 59 | calc_semantic_segmentation_iou 60 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 61 | .. autofunction:: calc_semantic_segmentation_iou 62 | -------------------------------------------------------------------------------- /chainercv/transforms/image/ten_crop.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def ten_crop(img, size): 5 | """Crop 10 regions from an array. 6 | 7 | This method crops 10 regions. All regions will be in shape 8 | :obj:`size`. These regions consist of 1 center crop and 4 corner 9 | crops and horizontal flips of them. 10 | 11 | The crops are ordered in this order. 12 | 13 | * center crop 14 | * top-left crop 15 | * bottom-left crop 16 | * top-right crop 17 | * bottom-right crop 18 | * center crop (flipped horizontally) 19 | * top-left crop (flipped horizontally) 20 | * bottom-left crop (flipped horizontally) 21 | * top-right crop (flipped horizontally) 22 | * bottom-right crop (flipped horizontally) 23 | 24 | Args: 25 | img (~numpy.ndarray): An image array to be cropped. This is in 26 | CHW format. 27 | size (tuple): The size of output images after cropping. 28 | This value is :math:`(height, width)`. 29 | 30 | Returns: 31 | The cropped arrays. The shape of tensor is :math:`(10, C, H, W)`. 32 | 33 | """ 34 | H, W = size 35 | iH, iW = img.shape[1:3] 36 | 37 | if iH < H or iW < W: 38 | raise ValueError('shape of image is smaller than output shape') 39 | 40 | crops = np.stack(( 41 | img[:, (iH - H) // 2:(iH + H) // 2, (iW - W) // 2:(iW + W) // 2], 42 | img[:, 0:H, 0:W], 43 | img[:, iH - H:iH, 0:W], 44 | img[:, 0:H, iW - W:iW], 45 | img[:, iH - H:iH, iW - W:iW], 46 | )) 47 | 48 | crops = np.vstack((crops, crops[:, :, :, ::-1])) 49 | 50 | return crops 51 | -------------------------------------------------------------------------------- /tests/transforms_tests/point_tests/test_flip_point.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | import numpy as np 4 | 5 | from chainer import testing 6 | from chainercv.transforms import flip_point 7 | 8 | 9 | class TestFlipPoint(unittest.TestCase): 10 | 11 | def test_flip_point_ndarray(self): 12 | point = np.random.uniform( 13 | low=0., high=32., size=(3, 12, 2)) 14 | 15 | out = flip_point(point, size=(34, 32), y_flip=True) 16 | point_expected = point.copy() 17 | point_expected[:, :, 0] = 34 - point[:, :, 0] 18 | np.testing.assert_equal(out, point_expected) 19 | 20 | out = flip_point(point, size=(34, 32), x_flip=True) 21 | point_expected = point.copy() 22 | point_expected[:, :, 1] = 32 - point[:, :, 1] 23 | np.testing.assert_equal(out, point_expected) 24 | 25 | def test_flip_point_list(self): 26 | point = [ 27 | np.random.uniform(low=0., high=32., size=(12, 2)), 28 | np.random.uniform(low=0., high=32., size=(10, 2)), 29 | ] 30 | 31 | out = flip_point(point, size=(34, 32), y_flip=True) 32 | for i, pnt in enumerate(point): 33 | pnt_expected = pnt.copy() 34 | pnt_expected[:, 0] = 34 - pnt[:, 0] 35 | np.testing.assert_equal(out[i], pnt_expected) 36 | 37 | out = flip_point(point, size=(34, 32), x_flip=True) 38 | for i, pnt in enumerate(point): 39 | pnt_expected = pnt.copy() 40 | pnt_expected[:, 1] = 32 - pnt[:, 1] 41 | np.testing.assert_equal(out[i], pnt_expected) 42 | 43 | 44 | testing.run_module(__name__, __file__) 45 | -------------------------------------------------------------------------------- /examples/ssd/demo.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import matplotlib.pyplot as plt 3 | 4 | import chainer 5 | 6 | from chainercv.datasets import voc_bbox_label_names 7 | from chainercv.links import SSD300 8 | from chainercv.links import SSD512 9 | from chainercv import utils 10 | from chainercv.visualizations import vis_bbox 11 | 12 | 13 | def main(): 14 | parser = argparse.ArgumentParser() 15 | parser.add_argument( 16 | '--model', choices=('ssd300', 'ssd512'), default='ssd300') 17 | parser.add_argument('--gpu', type=int, default=-1) 18 | parser.add_argument('--pretrained-model') 19 | parser.add_argument( 20 | '--dataset', choices=('voc',), default='voc') 21 | parser.add_argument('image') 22 | args = parser.parse_args() 23 | 24 | if args.model == 'ssd300': 25 | cls = SSD300 26 | elif args.model == 'ssd512': 27 | cls = SSD512 28 | 29 | if args.dataset == 'voc': 30 | if args.pretrained_model is None: 31 | args.pretrained_model = 'voc0712' 32 | label_names = voc_bbox_label_names 33 | 34 | model = cls(n_fg_class=len(label_names), 35 | pretrained_model=args.pretrained_model) 36 | 37 | if args.gpu >= 0: 38 | chainer.cuda.get_device_from_id(args.gpu).use() 39 | model.to_gpu() 40 | 41 | img = utils.read_image(args.image, color=True) 42 | bboxes, labels, scores = model.predict([img]) 43 | bbox, label, score = bboxes[0], labels[0], scores[0] 44 | 45 | vis_bbox( 46 | img, bbox, label, score, label_names=label_names) 47 | plt.show() 48 | 49 | 50 | if __name__ == '__main__': 51 | main() 52 | -------------------------------------------------------------------------------- /chainercv/transforms/bbox/rotate_bbox.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def rotate_bbox(bbox, angle, size): 5 | """Rotate bounding boxes by degrees. 6 | 7 | Args: 8 | bbox (~numpy.ndarray): See the table below. 9 | angle (float): Counter clock-wise rotation angle (degree). 10 | image is rotated by 90 degrees. 11 | size (tuple): A tuple of length 2. The height and the width 12 | of the image. 13 | 14 | .. csv-table:: 15 | :header: name, shape, dtype, format 16 | 17 | :obj:`bbox`, ":math:`(R, 4)`", :obj:`float32`, \ 18 | ":math:`(y_{min}, x_{min}, y_{max}, x_{max})`" 19 | 20 | Returns: 21 | ~numpy.ndarray: 22 | Bounding boxes rescaled according to the given :obj:`k`. 23 | 24 | """ 25 | if angle % 90 != 0: 26 | raise ValueError( 27 | 'angle which satisfies angle % 90 == 0 is only supported: {}' 28 | .format(angle)) 29 | H, W = size 30 | if angle % 360 == 0: 31 | return bbox 32 | 33 | if angle % 360 == 90: 34 | rotated_bbox = np.concatenate( 35 | (W - bbox[:, 3:4], bbox[:, 0:1], 36 | W - bbox[:, 1:2], bbox[:, 2:3]), axis=1) 37 | elif angle % 360 == 180: 38 | rotated_bbox = np.concatenate( 39 | (H - bbox[:, 2:3], W - bbox[:, 3:4], 40 | H - bbox[:, 0:1], W - bbox[:, 1:2]), axis=1) 41 | elif angle % 360 == 270: 42 | rotated_bbox = np.concatenate( 43 | (bbox[:, 1:2], H - bbox[:, 2:3], 44 | bbox[:, 3:4], H - bbox[:, 0:1]), axis=1) 45 | rotated_bbox = rotated_bbox.astype(bbox.dtype) 46 | return rotated_bbox 47 | -------------------------------------------------------------------------------- /examples/segnet/demo.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import matplotlib.pyplot as plt 3 | 4 | import chainer 5 | 6 | from chainercv.datasets import camvid_label_colors 7 | from chainercv.datasets import camvid_label_names 8 | from chainercv.links import SegNetBasic 9 | from chainercv import utils 10 | from chainercv.visualizations import vis_image 11 | from chainercv.visualizations import vis_semantic_segmentation 12 | 13 | 14 | def main(): 15 | chainer.config.train = False 16 | 17 | parser = argparse.ArgumentParser() 18 | parser.add_argument('--gpu', type=int, default=-1) 19 | parser.add_argument('--pretrained-model') 20 | parser.add_argument('--dataset', choices=('camvid',), default='camvid') 21 | parser.add_argument('image') 22 | args = parser.parse_args() 23 | 24 | if args.dataset == 'camvid': 25 | if args.pretrained_model is None: 26 | args.pretrained_model = 'camvid' 27 | label_names = camvid_label_names 28 | colors = camvid_label_colors 29 | 30 | model = SegNetBasic( 31 | n_class=len(label_names), 32 | pretrained_model=args.pretrained_model) 33 | 34 | if args.gpu >= 0: 35 | chainer.cuda.get_device_from_id(args.gpu).use() 36 | model.to_gpu() 37 | 38 | img = utils.read_image(args.image, color=True) 39 | labels = model.predict([img]) 40 | label = labels[0] 41 | 42 | fig = plt.figure() 43 | ax1 = fig.add_subplot(1, 2, 1) 44 | vis_image(img, ax=ax1) 45 | ax2 = fig.add_subplot(1, 2, 2) 46 | # Do not overlay the label image on the color image 47 | vis_semantic_segmentation(None, label, label_names, colors, ax=ax2) 48 | plt.show() 49 | 50 | 51 | if __name__ == '__main__': 52 | main() 53 | -------------------------------------------------------------------------------- /examples/semantic_segmentation/eval_semantic_segmentation_multi.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import chainer 4 | from chainer import iterators 5 | 6 | import chainermn 7 | 8 | from chainercv.utils import apply_to_iterator 9 | from chainercv.utils import ProgressHook 10 | 11 | from eval_semantic_segmentation import models 12 | from eval_semantic_segmentation import setup 13 | 14 | 15 | def main(): 16 | parser = argparse.ArgumentParser() 17 | parser.add_argument( 18 | '--dataset', choices=('cityscapes', 'ade20k', 'camvid', 'voc')) 19 | parser.add_argument('--model', choices=sorted(models.keys())) 20 | parser.add_argument('--pretrained-model') 21 | parser.add_argument('--batchsize', type=int) 22 | parser.add_argument('--input-size', type=int, default=None) 23 | args = parser.parse_args() 24 | 25 | comm = chainermn.create_communicator('pure_nccl') 26 | device = comm.intra_rank 27 | 28 | dataset, eval_, model, batchsize = setup( 29 | args.dataset, args.model, args.pretrained_model, 30 | args.batchsize, args.input_size) 31 | 32 | chainer.cuda.get_device_from_id(device).use() 33 | model.to_gpu() 34 | 35 | if not comm.rank == 0: 36 | apply_to_iterator(model.predict, None, comm=comm) 37 | return 38 | 39 | it = iterators.MultithreadIterator( 40 | dataset, batchsize * comm.size, repeat=False, shuffle=False) 41 | 42 | in_values, out_values, rest_values = apply_to_iterator( 43 | model.predict, it, hook=ProgressHook(len(dataset)), comm=comm) 44 | # Delete an iterator of images to save memory usage. 45 | del in_values 46 | 47 | eval_(out_values, rest_values) 48 | 49 | 50 | if __name__ == '__main__': 51 | main() 52 | -------------------------------------------------------------------------------- /examples/vgg/caffe2npz.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import re 3 | 4 | import chainer 5 | from chainer import Link 6 | import chainer.links.caffe.caffe_function as caffe 7 | 8 | 9 | """ 10 | Please download a weight from here. 11 | http://www.robots.ox.ac.uk/%7Evgg/software/very_deep/caffe/VGG_ILSVRC_16_layers.caffemodel 12 | """ 13 | 14 | 15 | def rename(name): 16 | m = re.match(r'conv(\d+)_(\d+)$', name) 17 | if m: 18 | i, j = map(int, m.groups()) 19 | return 'conv{:d}_{:d}/conv'.format(i, j) 20 | 21 | return name 22 | 23 | 24 | class VGGCaffeFunction(caffe.CaffeFunction): 25 | 26 | def __init__(self, model_path): 27 | print('loading weights from {:s} ... '.format(model_path)) 28 | super(VGGCaffeFunction, self).__init__(model_path) 29 | 30 | def __setattr__(self, name, value): 31 | if self.within_init_scope and isinstance(value, Link): 32 | new_name = rename(name) 33 | 34 | if new_name == 'conv1_1/conv': 35 | # BGR -> RGB 36 | value.W.array[:, ::-1] = value.W.array 37 | print('{:s} -> {:s} (BGR -> RGB)'.format(name, new_name)) 38 | else: 39 | print('{:s} -> {:s}'.format(name, new_name)) 40 | else: 41 | new_name = name 42 | 43 | super(VGGCaffeFunction, self).__setattr__(new_name, value) 44 | 45 | 46 | def main(): 47 | parser = argparse.ArgumentParser() 48 | parser.add_argument('caffemodel') 49 | parser.add_argument('output') 50 | args = parser.parse_args() 51 | 52 | model = VGGCaffeFunction(args.caffemodel) 53 | chainer.serializers.save_npz(args.output, model) 54 | 55 | 56 | if __name__ == '__main__': 57 | main() 58 | -------------------------------------------------------------------------------- /chainercv/utils/mask/mask_iou.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | 4 | from chainer.backends import cuda 5 | 6 | 7 | def mask_iou(mask_a, mask_b): 8 | """Calculate the Intersection of Unions (IoUs) between masks. 9 | 10 | IoU is calculated as a ratio of area of the intersection 11 | and area of the union. 12 | 13 | This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as 14 | inputs. Please note that both :obj:`mask_a` and :obj:`mask_b` need to be 15 | same type. 16 | The output is same type as the type of the inputs. 17 | 18 | Args: 19 | mask_a (array): An array whose shape is :math:`(N, H, W)`. 20 | :math:`N` is the number of masks. 21 | The dtype should be :obj:`numpy.bool`. 22 | mask_b (array): An array similar to :obj:`mask_a`, 23 | whose shape is :math:`(K, H, W)`. 24 | The dtype should be :obj:`numpy.bool`. 25 | 26 | Returns: 27 | array: 28 | An array whose shape is :math:`(N, K)`. \ 29 | An element at index :math:`(n, k)` contains IoUs between \ 30 | :math:`n` th mask in :obj:`mask_a` and :math:`k` th mask \ 31 | in :obj:`mask_b`. 32 | 33 | """ 34 | if mask_a.shape[1:] != mask_b.shape[1:]: 35 | raise IndexError 36 | xp = cuda.get_array_module(mask_a) 37 | 38 | n_mask_a = len(mask_a) 39 | n_mask_b = len(mask_b) 40 | iou = xp.empty((n_mask_a, n_mask_b), dtype=xp.float32) 41 | for n, m_a in enumerate(mask_a): 42 | for k, m_b in enumerate(mask_b): 43 | intersect = xp.bitwise_and(m_a, m_b).sum() 44 | union = xp.bitwise_or(m_a, m_b).sum() 45 | iou[n, k] = intersect / union 46 | return iou 47 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/datasets_tests/sliceable_tests/test_tuple_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | from chainer import testing 5 | 6 | from chainercv.chainer_experimental.datasets.sliceable import SliceableDataset 7 | from chainercv.chainer_experimental.datasets.sliceable import TupleDataset 8 | 9 | 10 | class SampleDataset(SliceableDataset): 11 | 12 | def __len__(self): 13 | return 10 14 | 15 | @property 16 | def keys(self): 17 | return ('item0', 'item1', 'item2') 18 | 19 | def get_example_by_keys(self, i, key_indices): 20 | return tuple( 21 | '{:s}({:d})'.format(self.keys[key_index], i) 22 | for key_index in key_indices) 23 | 24 | 25 | class TestTupleDataset(unittest.TestCase): 26 | 27 | def setUp(self): 28 | self.dataset = SampleDataset() 29 | 30 | def test_basic(self): 31 | dataset = TupleDataset( 32 | self.dataset, 33 | ('item3', np.arange(len(self.dataset))), 34 | np.arange(len(self.dataset)) * 2) 35 | self.assertIsInstance(dataset, SliceableDataset) 36 | self.assertEqual(len(dataset), len(self.dataset)) 37 | self.assertEqual( 38 | dataset.keys, ('item0', 'item1', 'item2', 'item3', None)) 39 | self.assertEqual( 40 | dataset[1], ('item0(1)', 'item1(1)', 'item2(1)', 1, 2)) 41 | 42 | def test_empty(self): 43 | with self.assertRaises(ValueError): 44 | TupleDataset() 45 | 46 | def test_invalid_length(self): 47 | with self.assertRaises(ValueError): 48 | TupleDataset( 49 | self.dataset, ('item3', np.arange(5))) 50 | 51 | 52 | testing.run_module(__name__, __file__) 53 | -------------------------------------------------------------------------------- /examples/yolo/README.md: -------------------------------------------------------------------------------- 1 | # Examples of YOLO [1, 2] 2 | 3 | ## Performance 4 | PASCAL VOC2007 Test 5 | 6 | | Model | Original | Ours (weight conversion) | 7 | |:-:|:-:|:-:| 8 | | YOLOv2 | 75.8 % * | 75.8 % | 9 | | YOLOv2 tiny | 54.0 % ** | 53.5 % | 10 | | YOLOv3 | 80.2 % | 80.2 % | 11 | 12 | Scores are mean Average Precision (mAP) with PASCAL VOC2007 metric. 13 | 14 | \*: Although the original paper [1] reports 76.8 %, the darknet implementation and the provided weights achieved the lower score. 15 | Similar issue is reported [here](https://github.com/AlexeyAB/darknet#how-to-calculate-map-on-pascalvoc-2007). 16 | \**: Although the author's website reports 57.1 %, the darknet implementation and the provided weights achieved the lower score. 17 | 18 | ## Demo 19 | Detect objects in an given image. This demo downloads Pascal VOC pretrained model automatically if a pretrained model path is not given. 20 | ``` 21 | $ python demo.py [--model yolo_v2|yolo_v2_tiny|yolo_v3] [--gpu ] [--pretrained-model ] .jpg 22 | ``` 23 | 24 | ## Convert Darknet model 25 | Convert `*.weights` to `*.npz`. YOLOv2, YOLOv2 tiny, and YOLOv3 are supported. 26 | Note that the number of classes should be specified if it is not 80 (the number of classes in COCO). 27 | ``` 28 | $ python darknet2npz.py [--model yolo_v2|yolo_v2_tiny|yolo_v3] [--n-fg-class <#fg_class>] .weights .npz 29 | ``` 30 | 31 | ## Evaluation 32 | The evaluation can be conducted using [`chainercv/examples/detection/eval_detection.py`](https://github.com/chainer/chainercv/blob/master/examples/detection). 33 | 34 | ## References 35 | 1. Joseph Redmon et al. "YOLO9000: Better, Faster, Stronger" CVPR 2017. 36 | 2. Joseph Redmon et al. "YOLOv3: An Incremental Improvement" arXiv 2018. 37 | -------------------------------------------------------------------------------- /tests/utils_tests/mask_tests/test_mask_to_bbox.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import unittest 4 | 5 | import numpy as np 6 | 7 | from chainer.backends import cuda 8 | from chainer import testing 9 | from chainer.testing import attr 10 | 11 | from chainercv.utils import mask_to_bbox 12 | 13 | 14 | @testing.parameterize( 15 | {'mask': np.array( 16 | [[[False, False, False, False], 17 | [False, True, True, True], 18 | [False, True, True, True] 19 | ]]), 20 | 'expected': np.array([[1, 1, 3, 4]], dtype=np.float32) 21 | }, 22 | {'mask': np.array( 23 | [[[False, False], 24 | [False, True]], 25 | [[True, False], 26 | [False, True]]]), 27 | 'expected': np.array([[1, 1, 2, 2], [0, 0, 2, 2]], dtype=np.float32) 28 | }, 29 | {'mask': np.array( 30 | [[[False, False], 31 | [False, False]], 32 | [[True, False], 33 | [False, True]]]), 34 | 'expected': np.array([[0, 0, 0, 0], [0, 0, 2, 2]], dtype=np.float32) 35 | }, 36 | ) 37 | class TestMaskToBbox(unittest.TestCase): 38 | 39 | def check(self, mask, expected): 40 | bbox = mask_to_bbox(mask) 41 | 42 | self.assertIsInstance(bbox, type(expected)) 43 | self.assertEqual(bbox.dtype, expected.dtype) 44 | np.testing.assert_equal( 45 | cuda.to_cpu(bbox), 46 | cuda.to_cpu(expected)) 47 | 48 | def test_mask_to_bbox_cpu(self): 49 | self.check(self.mask, self.expected) 50 | 51 | @attr.gpu 52 | def test_mask_to_bbox_gpu(self): 53 | self.check( 54 | cuda.to_gpu(self.mask), 55 | cuda.to_gpu(self.expected)) 56 | 57 | 58 | testing.run_module(__name__, __file__) 59 | -------------------------------------------------------------------------------- /chainercv/utils/bbox/bbox_iou.py: -------------------------------------------------------------------------------- 1 | from chainer.backends import cuda 2 | 3 | 4 | def bbox_iou(bbox_a, bbox_b): 5 | """Calculate the Intersection of Unions (IoUs) between bounding boxes. 6 | 7 | IoU is calculated as a ratio of area of the intersection 8 | and area of the union. 9 | 10 | This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as 11 | inputs. Please note that both :obj:`bbox_a` and :obj:`bbox_b` need to be 12 | same type. 13 | The output is same type as the type of the inputs. 14 | 15 | Args: 16 | bbox_a (array): An array whose shape is :math:`(N, 4)`. 17 | :math:`N` is the number of bounding boxes. 18 | The dtype should be :obj:`numpy.float32`. 19 | bbox_b (array): An array similar to :obj:`bbox_a`, 20 | whose shape is :math:`(K, 4)`. 21 | The dtype should be :obj:`numpy.float32`. 22 | 23 | Returns: 24 | array: 25 | An array whose shape is :math:`(N, K)`. \ 26 | An element at index :math:`(n, k)` contains IoUs between \ 27 | :math:`n` th bounding box in :obj:`bbox_a` and :math:`k` th bounding \ 28 | box in :obj:`bbox_b`. 29 | 30 | """ 31 | if bbox_a.shape[1] != 4 or bbox_b.shape[1] != 4: 32 | raise IndexError 33 | xp = cuda.get_array_module(bbox_a) 34 | 35 | # top left 36 | tl = xp.maximum(bbox_a[:, None, :2], bbox_b[:, :2]) 37 | # bottom right 38 | br = xp.minimum(bbox_a[:, None, 2:], bbox_b[:, 2:]) 39 | 40 | area_i = xp.prod(br - tl, axis=2) * (tl < br).all(axis=2) 41 | area_a = xp.prod(bbox_a[:, 2:] - bbox_a[:, :2], axis=1) 42 | area_b = xp.prod(bbox_b[:, 2:] - bbox_b[:, :2], axis=1) 43 | return area_i / (area_a[:, None] + area_b - area_i) 44 | -------------------------------------------------------------------------------- /tests/transforms_tests/bbox_tests/test_crop_bbox.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | 5 | from chainer import testing 6 | from chainercv.transforms import crop_bbox 7 | 8 | 9 | class TestCropBbox(unittest.TestCase): 10 | 11 | def setUp(self): 12 | self.bbox = np.array(( 13 | (0, 0, 3, 4), 14 | (0, 0, 5, 6), 15 | (0, 5, 3, 6), 16 | (1, 2, 3, 4), 17 | (3, 3, 4, 6), 18 | ), dtype=np.float32) 19 | self.y_slice = slice(1, 5) 20 | self.x_slice = slice(0, 4) 21 | 22 | def test_crop_bbox(self): 23 | expected = np.array(( 24 | (0, 0, 2, 4), 25 | (0, 0, 4, 4), 26 | (0, 2, 2, 4), 27 | (2, 3, 3, 4), 28 | ), dtype=np.float32) 29 | 30 | out, param = crop_bbox( 31 | self.bbox, y_slice=self.y_slice, x_slice=self.x_slice, 32 | return_param=True) 33 | np.testing.assert_equal(out, expected) 34 | np.testing.assert_equal(param['index'], (0, 1, 3, 4)) 35 | np.testing.assert_equal(param['truncated_index'], (0, 1, 3)) 36 | 37 | def test_crop_bbox_disallow_outside_center(self): 38 | expected = np.array(( 39 | (0, 0, 2, 4), 40 | (0, 0, 4, 4), 41 | (0, 2, 2, 4), 42 | ), dtype=np.float32) 43 | 44 | out, param = crop_bbox( 45 | self.bbox, y_slice=self.y_slice, x_slice=self.x_slice, 46 | allow_outside_center=False, return_param=True) 47 | np.testing.assert_equal(out, expected) 48 | np.testing.assert_equal(param['index'], (0, 1, 3)) 49 | np.testing.assert_equal(param['truncated_index'], (0, 1)) 50 | 51 | 52 | testing.run_module(__name__, __file__) 53 | -------------------------------------------------------------------------------- /chainercv/chainer_experimental/datasets/sliceable/concatenated_dataset.py: -------------------------------------------------------------------------------- 1 | from chainercv.chainer_experimental.datasets.sliceable import SliceableDataset 2 | 3 | 4 | class ConcatenatedDataset(SliceableDataset): 5 | """A sliceable version of :class:`chainer.datasets.ConcatenatedDataset`. 6 | 7 | Here is an example. 8 | 9 | >>> dataset_a = TupleDataset([0, 1, 2], [0, 1, 4]) 10 | >>> dataset_b = TupleDataset([3, 4, 5], [9, 16, 25]) 11 | >>> 12 | >>> dataset = ConcatenatedDataset(dataset_a, dataset_b) 13 | >>> dataset.slice[:, 0][:] # [0, 1, 2, 3, 4, 5] 14 | 15 | Args: 16 | datasets: The underlying datasets. 17 | Each dataset should inherit 18 | :class:`~chainercv.chainer_experimental.datasets.sliceable.Sliceabledataset` 19 | and should have the same keys. 20 | """ 21 | 22 | def __init__(self, *datasets): 23 | if len(datasets) == 0: 24 | raise ValueError('At least one dataset is required') 25 | self._datasets = datasets 26 | self._keys = datasets[0].keys 27 | for dataset in datasets[1:]: 28 | if not dataset.keys == self._keys: 29 | raise ValueError('All datasets should have the same keys') 30 | 31 | def __len__(self): 32 | return sum(len(dataset) for dataset in self._datasets) 33 | 34 | @property 35 | def keys(self): 36 | return self._keys 37 | 38 | def get_example_by_keys(self, index, key_indices): 39 | if index < 0: 40 | raise IndexError 41 | for dataset in self._datasets: 42 | if index < len(dataset): 43 | return dataset.get_example_by_keys(index, key_indices) 44 | index -= len(dataset) 45 | raise IndexError 46 | -------------------------------------------------------------------------------- /examples/yolo/demo.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import matplotlib.pyplot as plt 3 | 4 | import chainer 5 | 6 | from chainercv.datasets import voc_bbox_label_names 7 | from chainercv.experimental.links import YOLOv2Tiny 8 | from chainercv.links import YOLOv2 9 | from chainercv.links import YOLOv3 10 | from chainercv import utils 11 | from chainercv.visualizations import vis_bbox 12 | 13 | 14 | def main(): 15 | parser = argparse.ArgumentParser() 16 | parser.add_argument( 17 | '--model', choices=('yolo_v2', 'yolo_v2_tiny', 'yolo_v3'), 18 | default='yolo_v2') 19 | parser.add_argument('--gpu', type=int, default=-1) 20 | parser.add_argument('--pretrained-model') 21 | parser.add_argument( 22 | '--dataset', choices=('voc',), default='voc') 23 | parser.add_argument('image') 24 | args = parser.parse_args() 25 | 26 | if args.model == 'yolo_v2': 27 | cls = YOLOv2 28 | elif args.model == 'yolo_v2_tiny': 29 | cls = YOLOv2Tiny 30 | elif args.model == 'yolo_v3': 31 | cls = YOLOv3 32 | 33 | if args.dataset == 'voc': 34 | if args.pretrained_model is None: 35 | args.pretrained_model = 'voc0712' 36 | label_names = voc_bbox_label_names 37 | 38 | model = cls(n_fg_class=len(label_names), 39 | pretrained_model=args.pretrained_model) 40 | 41 | if args.gpu >= 0: 42 | chainer.cuda.get_device_from_id(args.gpu).use() 43 | model.to_gpu() 44 | 45 | img = utils.read_image(args.image, color=True) 46 | bboxes, labels, scores = model.predict([img]) 47 | bbox, label, score = bboxes[0], labels[0], scores[0] 48 | 49 | vis_bbox( 50 | img, bbox, label, score, label_names=label_names) 51 | plt.show() 52 | 53 | 54 | if __name__ == '__main__': 55 | main() 56 | -------------------------------------------------------------------------------- /tests/chainer_experimental_tests/datasets_tests/sliceable_tests/test_concatenated_dataset.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | from chainer import testing 4 | 5 | from chainercv.chainer_experimental.datasets.sliceable \ 6 | import ConcatenatedDataset 7 | from chainercv.chainer_experimental.datasets.sliceable import SliceableDataset 8 | 9 | 10 | class SampleDataset(SliceableDataset): 11 | 12 | def __len__(self): 13 | return 10 14 | 15 | @property 16 | def keys(self): 17 | return ('item0', 'item1', 'item2') 18 | 19 | def get_example_by_keys(self, i, key_indices): 20 | return tuple( 21 | '{:s}({:d})'.format(self.keys[key_index], i) 22 | for key_index in key_indices) 23 | 24 | 25 | class TestConcatenatedDataset(unittest.TestCase): 26 | 27 | def setUp(self): 28 | self.dataset = SampleDataset() 29 | 30 | def test_basic(self): 31 | dataset = ConcatenatedDataset(self.dataset, self.dataset) 32 | self.assertIsInstance(dataset, SliceableDataset) 33 | self.assertEqual(len(dataset), len(self.dataset) * 2) 34 | self.assertEqual(dataset.keys, ('item0', 'item1', 'item2')) 35 | self.assertEqual( 36 | dataset[1], ('item0(1)', 'item1(1)', 'item2(1)')) 37 | self.assertEqual( 38 | dataset[11], ('item0(1)', 'item1(1)', 'item2(1)')) 39 | 40 | def test_empty(self): 41 | with self.assertRaises(ValueError): 42 | ConcatenatedDataset() 43 | 44 | def test_invalid_keys(self): 45 | dataset0 = self.dataset.slice[:, ('item0', 'item1')] 46 | dataset1 = self.dataset.slice[:, ('item0', 'item2')] 47 | with self.assertRaises(ValueError): 48 | ConcatenatedDataset(dataset0, dataset1) 49 | 50 | 51 | testing.run_module(__name__, __file__) 52 | -------------------------------------------------------------------------------- /docs/source/reference/transforms.rst: -------------------------------------------------------------------------------- 1 | Transforms 2 | ========== 3 | 4 | .. module:: chainercv.transforms 5 | 6 | 7 | Image 8 | ----- 9 | 10 | center_crop 11 | ~~~~~~~~~~~ 12 | .. autofunction:: center_crop 13 | 14 | flip 15 | ~~~~ 16 | .. autofunction:: flip 17 | 18 | pca_lighting 19 | ~~~~~~~~~~~~ 20 | .. autofunction:: pca_lighting 21 | 22 | random_crop 23 | ~~~~~~~~~~~ 24 | .. autofunction:: random_crop 25 | 26 | random_expand 27 | ~~~~~~~~~~~~~ 28 | .. autofunction:: random_expand 29 | 30 | random_flip 31 | ~~~~~~~~~~~ 32 | .. autofunction:: random_flip 33 | 34 | random_rotate 35 | ~~~~~~~~~~~~~ 36 | .. autofunction:: random_rotate 37 | 38 | random_sized_crop 39 | ~~~~~~~~~~~~~~~~~ 40 | .. autofunction:: random_sized_crop 41 | 42 | resize 43 | ~~~~~~ 44 | .. autofunction:: resize 45 | 46 | resize_contain 47 | ~~~~~~~~~~~~~~ 48 | .. autofunction:: resize_contain 49 | 50 | rotate 51 | ~~~~~~ 52 | .. autofunction:: rotate 53 | 54 | scale 55 | ~~~~~ 56 | .. autofunction:: scale 57 | 58 | ten_crop 59 | ~~~~~~~~ 60 | .. autofunction:: ten_crop 61 | 62 | 63 | Bounding Box 64 | ------------ 65 | 66 | crop_bbox 67 | ~~~~~~~~~ 68 | .. autofunction:: crop_bbox 69 | 70 | flip_bbox 71 | ~~~~~~~~~ 72 | .. autofunction:: flip_bbox 73 | 74 | resize_bbox 75 | ~~~~~~~~~~~ 76 | .. autofunction:: resize_bbox 77 | 78 | rotate_bbox 79 | ~~~~~~~~~~~ 80 | .. autofunction:: rotate_bbox 81 | 82 | translate_bbox 83 | ~~~~~~~~~~~~~~ 84 | .. autofunction:: translate_bbox 85 | 86 | Point 87 | ----- 88 | 89 | flip_point 90 | ~~~~~~~~~~ 91 | .. autofunction:: flip_point 92 | 93 | resize_point 94 | ~~~~~~~~~~~~ 95 | .. autofunction:: resize_point 96 | 97 | translate_point 98 | ~~~~~~~~~~~~~~~ 99 | .. autofunction:: translate_point 100 | -------------------------------------------------------------------------------- /chainercv/links/model/ssd/normalize.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | import chainer 4 | import chainer.functions as F 5 | from chainer import initializers 6 | from chainer import variable 7 | 8 | 9 | class Normalize(chainer.Link): 10 | """Learnable L2 normalization [#]_. 11 | 12 | This link normalizes input along the channel axis and scales it. 13 | The scale factors are trained channel-wise. 14 | 15 | .. [#] Wei Liu, Andrew Rabinovich, Alexander C. Berg. 16 | ParseNet: Looking Wider to See Better. ICLR 2016. 17 | 18 | Args: 19 | n_channel (int): The number of channels. 20 | initial: A value to initialize the scale factors. It is pased to 21 | :meth:`chainer.initializers._get_initializer`. The default value 22 | is 0. 23 | eps (float): A small value to avoid zero-division. The default value 24 | is :math:`1e-5`. 25 | 26 | """ 27 | 28 | def __init__(self, n_channel, initial=0, eps=1e-5): 29 | super(Normalize, self).__init__() 30 | self.eps = eps 31 | with self.init_scope(): 32 | initializer = initializers._get_initializer(initial) 33 | self.scale = variable.Parameter(initializer) 34 | self.scale.initialize((n_channel),) 35 | 36 | def forward(self, x): 37 | """Normalize input and scale it. 38 | 39 | Args: 40 | x (chainer.Variable): A variable holding 4-dimensional array. 41 | Its :obj:`dtype` is :obj:`numpy.float32`. 42 | 43 | Returns: 44 | chainer.Variable: 45 | The shape and :obj:`dtype` are same as those of input. 46 | """ 47 | 48 | x = F.normalize(x, eps=self.eps, axis=1) 49 | scale = F.broadcast_to(self.scale[:, np.newaxis, np.newaxis], x.shape) 50 | return x * scale 51 | -------------------------------------------------------------------------------- /chainercv/transforms/image/random_flip.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | def random_flip(img, y_random=False, x_random=False, 5 | return_param=False, copy=False): 6 | """Randomly flip an image in vertical or horizontal direction. 7 | 8 | Args: 9 | img (~numpy.ndarray): An array that gets flipped. This is in 10 | CHW format. 11 | y_random (bool): Randomly flip in vertical direction. 12 | x_random (bool): Randomly flip in horizontal direction. 13 | return_param (bool): Returns information of flip. 14 | copy (bool): If False, a view of :obj:`img` will be returned. 15 | 16 | Returns: 17 | ~numpy.ndarray or (~numpy.ndarray, dict): 18 | 19 | If :obj:`return_param = False`, 20 | returns an array :obj:`out_img` that is the result of flipping. 21 | 22 | If :obj:`return_param = True`, 23 | returns a tuple whose elements are :obj:`out_img, param`. 24 | :obj:`param` is a dictionary of intermediate parameters whose 25 | contents are listed below with key, value-type and the description 26 | of the value. 27 | 28 | * **y_flip** (*bool*): Whether the image was flipped in the\ 29 | vertical direction or not. 30 | * **x_flip** (*bool*): Whether the image was flipped in the\ 31 | horizontal direction or not. 32 | 33 | """ 34 | y_flip, x_flip = False, False 35 | if y_random: 36 | y_flip = random.choice([True, False]) 37 | if x_random: 38 | x_flip = random.choice([True, False]) 39 | 40 | if y_flip: 41 | img = img[:, ::-1, :] 42 | if x_flip: 43 | img = img[:, :, ::-1] 44 | 45 | if copy: 46 | img = img.copy() 47 | 48 | if return_param: 49 | return img, {'y_flip': y_flip, 'x_flip': x_flip} 50 | else: 51 | return img 52 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/fpn_tests/test_fpn.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import numpy as np 4 | import unittest 5 | 6 | import chainer 7 | from chainer import testing 8 | from chainer.testing import attr 9 | 10 | from chainercv.links.model.fpn import FPN 11 | 12 | 13 | def _random_array(xp, shape): 14 | return xp.array( 15 | np.random.uniform(-1, 1, size=shape), dtype=np.float32) 16 | 17 | 18 | class DummyExtractor(chainer.Link): 19 | mean = _random_array(np, (3, 1, 1)) 20 | 21 | def forward(self, x): 22 | n, _, h, w = x.shape 23 | return [ 24 | chainer.Variable(_random_array(self.xp, (n, 16, h // 2, w // 2))), 25 | chainer.Variable(_random_array(self.xp, (n, 32, h // 4, w // 4))), 26 | chainer.Variable(_random_array(self.xp, (n, 64, h // 8, w // 8))), 27 | ] 28 | 29 | 30 | class TestFPN(unittest.TestCase): 31 | 32 | def setUp(self): 33 | self.link = FPN( 34 | base=DummyExtractor(), 35 | n_base_output=3, 36 | scales=(1 / 2, 1 / 4, 1 / 8)) 37 | 38 | def test_mean(self): 39 | np.testing.assert_equal(self.link.mean, self.link.base.mean) 40 | 41 | def _check_call(self): 42 | x = _random_array(self.link.xp, (2, 3, 32, 32)) 43 | hs = self.link(x) 44 | 45 | self.assertEqual(len(hs), 3) 46 | for l in range(len(hs)): 47 | self.assertIsInstance(hs[l], chainer.Variable) 48 | self.assertIsInstance(hs[l].array, self.link.xp.ndarray) 49 | self.assertEqual(hs[l].shape, (2, 256, 16 >> l, 16 >> l)) 50 | 51 | def test_call_cpu(self): 52 | self._check_call() 53 | 54 | @attr.gpu 55 | def test_call_gpu(self): 56 | self.link.to_gpu() 57 | self._check_call() 58 | 59 | 60 | testing.run_module(__name__, __file__) 61 | -------------------------------------------------------------------------------- /tests/utils_tests/image_tests/test_read_label.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import tempfile 3 | import unittest 4 | 5 | from chainer import testing 6 | 7 | from chainercv.utils import read_label 8 | from chainercv.utils import write_image 9 | 10 | 11 | @testing.parameterize(*testing.product({ 12 | 'file_obj': [False, True], 13 | 'format': ['bmp', 'jpeg', 'png'], 14 | 'size': [(48, 32)], 15 | 'dtype': [np.float32, np.uint8, np.int32, bool], 16 | })) 17 | class TestReadLabel(unittest.TestCase): 18 | 19 | def setUp(self): 20 | if self.file_obj: 21 | self.f = tempfile.TemporaryFile() 22 | self.file = self.f 23 | format = self.format 24 | else: 25 | if self.format == 'jpeg': 26 | suffix = '.jpg' 27 | else: 28 | suffix = '.' + self.format 29 | self.f = tempfile.NamedTemporaryFile(suffix=suffix, delete=False) 30 | self.file = self.f.name 31 | format = None 32 | 33 | self.img = np.random.randint( 34 | 0, 255, size=self.size, dtype=np.uint8) 35 | write_image(self.img[None], self.file, format=format) 36 | if self.file_obj: 37 | self.file.seek(0) 38 | 39 | def test_read_label(self): 40 | if self.dtype == np.int32: 41 | img = read_label(self.file) 42 | else: 43 | img = read_label(self.file, dtype=self.dtype) 44 | 45 | self.assertEqual(img.shape, self.size) 46 | self.assertEqual(img.dtype, self.dtype) 47 | 48 | if self.format in {'bmp', 'png'}: 49 | np.testing.assert_equal(img, self.img.astype(self.dtype)) 50 | 51 | def test_read_label_mutable(self): 52 | img = read_label(self.file, dtype=self.dtype) 53 | img[:] = 0 54 | np.testing.assert_equal(img, 0) 55 | 56 | 57 | testing.run_module(__name__, __file__) 58 | -------------------------------------------------------------------------------- /chainercv/datasets/ade20k/ade20k_test_image_dataset.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import os 3 | 4 | from chainercv.chainer_experimental.datasets.sliceable import GetterDataset 5 | from chainercv.datasets.ade20k.ade20k_utils import get_ade20k 6 | from chainercv.utils import read_image 7 | 8 | root = 'pfnet/chainercv/ade20k' 9 | url = 'http://data.csail.mit.edu/places/ADEchallenge/release_test.zip' 10 | 11 | 12 | class ADE20KTestImageDataset(GetterDataset): 13 | 14 | """Image dataset for test split of `ADE20K`_. 15 | 16 | This is an image dataset of test split in ADE20K dataset distributed at 17 | MIT Scene Parsing Benchmark website. It has 3,352 test images. 18 | 19 | .. _`MIT Scene Parsing Benchmark`: http://sceneparsing.csail.mit.edu/ 20 | 21 | Args: 22 | data_dir (string): Path to the dataset directory. The directory should 23 | contain the :obj:`release_test` dir. If :obj:`auto` is given, the 24 | dataset is automatically downloaded into 25 | :obj:`$CHAINER_DATASET_ROOT/pfnet/chainercv/ade20k`. 26 | 27 | This dataset returns the following data. 28 | 29 | .. csv-table:: 30 | :header: name, shape, dtype, format 31 | 32 | :obj:`img`, ":math:`(3, H, W)`", :obj:`float32`, \ 33 | "RGB, :math:`[0, 255]`" 34 | """ 35 | 36 | def __init__(self, data_dir='auto'): 37 | super(ADE20KTestImageDataset, self).__init__() 38 | 39 | if data_dir is 'auto': 40 | data_dir = get_ade20k(root, url) 41 | img_dir = os.path.join(data_dir, 'release_test', 'testing') 42 | self.img_paths = sorted(glob.glob(os.path.join(img_dir, '*.jpg'))) 43 | 44 | self.add_getter('img', self._get_image) 45 | self.keys = 'img' # do not return tuple 46 | 47 | def __len__(self): 48 | return len(self.img_paths) 49 | 50 | def _get_image(self, i): 51 | return read_image(self.img_paths[i]) 52 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/ssd_tests/test_multibox.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | import chainer 5 | from chainer import testing 6 | from chainer.testing import attr 7 | 8 | from chainercv.links.model.ssd import Multibox 9 | 10 | 11 | @testing.parameterize(*testing.product({ 12 | 'n_class': [1, 5], 13 | 'aspect_ratios': [((2,),), ((2,), (3, 4), (5,))], 14 | 'batchsize': [1, 2], 15 | })) 16 | class TestMultibox(unittest.TestCase): 17 | 18 | def setUp(self): 19 | self.link = Multibox(self.n_class, self.aspect_ratios) 20 | 21 | xs = [] 22 | n_bbox = 0 23 | for ar in self.aspect_ratios: 24 | C, H, W = np.random.randint(1, 10, size=3) 25 | xs.append( 26 | np.random.uniform(size=(self.batchsize, C, H, W)) 27 | .astype(np.float32)) 28 | n_bbox += H * W * (len(ar) + 1) * 2 29 | 30 | self.xs = xs 31 | self.n_bbox = n_bbox 32 | 33 | def _check_forward(self, xs): 34 | mb_locs, mb_confs = self.link(xs) 35 | 36 | self.assertIsInstance(mb_locs, chainer.Variable) 37 | self.assertIsInstance(mb_locs.array, type(xs[0])) 38 | self.assertEqual(mb_locs.shape, (self.batchsize, self.n_bbox, 4)) 39 | self.assertEqual(mb_locs.dtype, xs[0].dtype) 40 | 41 | self.assertIsInstance(mb_confs, chainer.Variable) 42 | self.assertIsInstance(mb_confs.array, type(xs[0])) 43 | self.assertEqual( 44 | mb_confs.shape, (self.batchsize, self.n_bbox, self.n_class)) 45 | self.assertEqual(mb_confs.dtype, xs[0].dtype) 46 | 47 | def test_forward_cpu(self): 48 | self._check_forward(self.xs) 49 | 50 | @attr.gpu 51 | def test_forward_gpu(self): 52 | self.link.to_gpu() 53 | self._check_forward(list(map(chainer.backends.cuda.to_gpu, self.xs))) 54 | 55 | 56 | testing.run_module(__name__, __file__) 57 | -------------------------------------------------------------------------------- /chainercv/utils/image/tile_images.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | import math 4 | import numpy as np 5 | 6 | 7 | def tile_images(imgs, n_col, pad=2, fill=0): 8 | """Make a tile of images 9 | 10 | Args: 11 | imgs (numpy.ndarray): A batch of images whose shape is BCHW. 12 | n_col (int): The number of columns in a tile. 13 | pad (int or tuple of two ints): :obj:`pad_y, pad_x`. This is the 14 | amounts of padding in y and x directions. If this is an integer, 15 | the amounts of padding in the two directions are the same. 16 | The default value is 2. 17 | fill (float, tuple or ~numpy.ndarray): The value of padded pixels. 18 | If it is :class:`numpy.ndarray`, 19 | its shape should be :math:`(C, 1, 1)`, 20 | where :math:`C` is the number of channels of :obj:`img`. 21 | 22 | Returns: 23 | ~numpy.ndarray: 24 | An image array in CHW format. 25 | The size of this image is 26 | :math:`((H + pad_{y}) \\times \\lceil B / n_{n_{col}} \\rceil, 27 | (W + pad_{x}) \\times n_{col})`. 28 | 29 | """ 30 | if isinstance(pad, int): 31 | pad = (pad, pad) 32 | pad_y, pad_x = pad 33 | 34 | B, C, H, W = imgs.shape 35 | n_col = min(n_col, B) 36 | n_row = int(math.ceil(B / n_col)) 37 | 38 | shape = (C, 39 | (H + pad_y) * n_row, 40 | (W + pad_x) * n_col) 41 | tile = np.empty(shape, dtype=imgs.dtype) 42 | tile[:] = np.array(fill).reshape((-1, 1, 1)) 43 | 44 | k = 0 45 | for y in range(n_row): 46 | for x in range(n_col): 47 | if k >= B: 48 | break 49 | start_y = y * (H + pad_y) + pad_y // 2 50 | start_x = x * (W + pad_x) + pad_x // 2 51 | tile[:, 52 | start_y: start_y + H, 53 | start_x: start_x + W] = imgs[k] 54 | k += 1 55 | 56 | return tile 57 | -------------------------------------------------------------------------------- /chainercv/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.utils.bbox.bbox_iou import bbox_iou # NOQA 2 | from chainercv.utils.bbox.non_maximum_suppression import non_maximum_suppression # NOQA 3 | from chainercv.utils.download import cached_download # NOQA 4 | from chainercv.utils.download import download_model # NOQA 5 | from chainercv.utils.download import extractall # NOQA 6 | from chainercv.utils.image import read_image # NOQA 7 | from chainercv.utils.image import read_label # NOQA 8 | from chainercv.utils.image import tile_images # NOQA 9 | from chainercv.utils.image import write_image # NOQA 10 | from chainercv.utils.iterator import apply_to_iterator # NOQA 11 | from chainercv.utils.iterator import ProgressHook # NOQA 12 | from chainercv.utils.iterator import unzip # NOQA 13 | from chainercv.utils.link import prepare_pretrained_model # NOQA 14 | from chainercv.utils.mask.mask_iou import mask_iou # NOQA 15 | from chainercv.utils.mask.mask_to_bbox import mask_to_bbox # NOQA 16 | from chainercv.utils.mask.scale_mask import scale_mask # NOQA 17 | from chainercv.utils.testing import assert_is_bbox # NOQA 18 | from chainercv.utils.testing import assert_is_bbox_dataset # NOQA 19 | from chainercv.utils.testing import assert_is_detection_link # NOQA 20 | from chainercv.utils.testing import assert_is_image # NOQA 21 | from chainercv.utils.testing import assert_is_instance_segmentation_dataset # NOQA 22 | from chainercv.utils.testing import assert_is_instance_segmentation_link # NOQA 23 | from chainercv.utils.testing import assert_is_label_dataset # NOQA 24 | from chainercv.utils.testing import assert_is_point # NOQA 25 | from chainercv.utils.testing import assert_is_point_dataset # NOQA 26 | from chainercv.utils.testing import assert_is_semantic_segmentation_dataset # NOQA 27 | from chainercv.utils.testing import assert_is_semantic_segmentation_link # NOQA 28 | from chainercv.utils.testing import ConstantStubLink # NOQA 29 | from chainercv.utils.testing import generate_random_bbox # NOQA 30 | -------------------------------------------------------------------------------- /chainercv/transforms/image/pca_lighting.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def pca_lighting(img, sigma, eigen_value=None, eigen_vector=None): 5 | """AlexNet style color augmentation 6 | 7 | This method adds a noise vector drawn from a Gaussian. The direction of 8 | the Gaussian is same as that of the principal components of the dataset. 9 | 10 | This method is used in training of AlexNet [#]_. 11 | 12 | .. [#] Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton. \ 13 | ImageNet Classification with Deep Convolutional Neural Networks. \ 14 | NIPS 2012. 15 | 16 | Args: 17 | img (~numpy.ndarray): An image array to be augmented. This is in 18 | CHW and RGB format. 19 | sigma (float): Standard deviation of the Gaussian. In the original 20 | paper, this value is 10% of the range of intensity 21 | (25.5 if the range is :math:`[0, 255]`). 22 | eigen_value (~numpy.ndarray): An array of eigen values. The shape 23 | has to be :math:`(3,)`. If it is not specified, the values computed 24 | from ImageNet are used. 25 | eigen_vector (~numpy.ndarray): An array of eigen vectors. The shape 26 | has to be :math:`(3, 3)`. If it is not specified, the vectors 27 | computed from ImageNet are used. 28 | 29 | Returns: 30 | An image in CHW format. 31 | """ 32 | 33 | if sigma <= 0: 34 | return img 35 | 36 | # these values are copied from facebook/fb.resnet.torch 37 | if eigen_value is None: 38 | eigen_value = np.array((0.2175, 0.0188, 0.0045)) 39 | if eigen_vector is None: 40 | eigen_vector = np.array(( 41 | (-0.5675, -0.5808, -0.5836), 42 | (0.7192, -0.0045, -0.6948), 43 | (0.4009, -0.814, 0.4203))) 44 | 45 | alpha = np.random.normal(0, sigma, size=3) 46 | 47 | img = img.copy() 48 | img += eigen_vector.dot(eigen_value * alpha).reshape((-1, 1, 1)) 49 | 50 | return img 51 | -------------------------------------------------------------------------------- /tests/links_tests/model_tests/ssd_tests/test_normalize.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | import chainer 5 | from chainer import initializers 6 | from chainer import testing 7 | from chainer.testing import attr 8 | 9 | from chainercv.links.model.ssd import Normalize 10 | 11 | 12 | @testing.parameterize(*testing.product({ 13 | 'shape': [(5, 5), (25, 25), (5, 25)], 14 | 'n_channel': [1, 10], 15 | 'eps': [1e-5, 1], 16 | })) 17 | class TestNormalize(unittest.TestCase): 18 | 19 | def setUp(self): 20 | self.link = Normalize( 21 | self.n_channel, initializers.Normal(), eps=self.eps) 22 | self.x = np.random.uniform(size=(1, self.n_channel) + self.shape) \ 23 | .astype(np.float32) 24 | 25 | def _check_forward(self, x): 26 | y = self.link(x) 27 | 28 | self.assertIsInstance(y, chainer.Variable) 29 | self.assertIsInstance(y.array, type(x)) 30 | self.assertEqual(y.shape, x.shape) 31 | self.assertEqual(y.dtype, x.dtype) 32 | 33 | x = chainer.backends.cuda.to_cpu(x) 34 | y = chainer.backends.cuda.to_cpu(y.array) 35 | scale = chainer.backends.cuda.to_cpu(self.link.scale.array) 36 | 37 | norm = np.linalg.norm(x, axis=1, keepdims=True) + self.eps 38 | expect = x / norm * scale[:, np.newaxis, np.newaxis] 39 | np.testing.assert_almost_equal(y, expect) 40 | 41 | def test_forward_cpu(self): 42 | self._check_forward(self.x) 43 | 44 | @attr.gpu 45 | def test_forward_gpu(self): 46 | self.link.to_gpu() 47 | self._check_forward(chainer.backends.cuda.to_gpu(self.x)) 48 | 49 | def test_forward_zero_cpu(self): 50 | self._check_forward(np.zeros_like(self.x)) 51 | 52 | @attr.gpu 53 | def test_forward_zero_gpu(self): 54 | self.link.to_gpu() 55 | self._check_forward( 56 | chainer.backends.cuda.to_gpu(np.zeros_like(self.x))) 57 | 58 | 59 | testing.run_module(__name__, __file__) 60 | -------------------------------------------------------------------------------- /tests/utils_tests/testing_tests/assertions_tests/test_assert_is_semantic_segmentation_link.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | import chainer 5 | 6 | from chainercv.utils import assert_is_semantic_segmentation_link 7 | from chainercv.utils import testing 8 | 9 | 10 | class SemanticSegmentationLink(chainer.Link): 11 | 12 | def predict(self, imgs): 13 | labels = [] 14 | 15 | for img in imgs: 16 | labels.append(np.random.randint( 17 | 0, 21, size=img.shape[1:]).astype(np.int32)) 18 | 19 | return labels 20 | 21 | 22 | class InvalidPredictionSizeLink(SemanticSegmentationLink): 23 | 24 | def predict(self, imgs): 25 | labels = super( 26 | InvalidPredictionSizeLink, self).predict(imgs) 27 | return labels[1:] 28 | 29 | 30 | class InvalidLabelSizeLink(SemanticSegmentationLink): 31 | 32 | def predict(self, imgs): 33 | labels = super( 34 | InvalidLabelSizeLink, self).predict(imgs) 35 | return [label[1:] for label in labels] 36 | 37 | 38 | class InvalidLabelValueLink(SemanticSegmentationLink): 39 | 40 | def predict(self, imgs): 41 | labels = super( 42 | InvalidLabelValueLink, self).predict(imgs) 43 | return [label + 1000 for label in labels] 44 | 45 | 46 | @testing.parameterize( 47 | {'link': SemanticSegmentationLink(), 'valid': True}, 48 | {'link': InvalidPredictionSizeLink(), 'valid': False}, 49 | {'link': InvalidLabelSizeLink(), 'valid': False}, 50 | {'link': InvalidLabelValueLink(), 'valid': False}, 51 | ) 52 | class TestAssertIsSemanticSegmentationLink(unittest.TestCase): 53 | 54 | def test_assert_is_semantic_segmentation_link(self): 55 | if self.valid: 56 | assert_is_semantic_segmentation_link(self.link, 21) 57 | else: 58 | with self.assertRaises(AssertionError): 59 | assert_is_semantic_segmentation_link(self.link, 21) 60 | 61 | 62 | testing.run_module(__name__, __file__) 63 | -------------------------------------------------------------------------------- /docs/source/reference/links/fpn.rst: -------------------------------------------------------------------------------- 1 | FPN (Feature Pyramid Networks) 2 | ============================== 3 | 4 | .. module:: chainercv.links.model.fpn 5 | 6 | 7 | Detection Links 8 | --------------- 9 | 10 | FasterRCNNFPNResnet50 11 | ~~~~~~~~~~~~~~~~~~~~~ 12 | .. autoclass:: FasterRCNNFPNResNet50 13 | :members: 14 | 15 | FasterRCNNFPNResnet101 16 | ~~~~~~~~~~~~~~~~~~~~~~ 17 | .. autoclass:: FasterRCNNFPNResNet101 18 | :members: 19 | 20 | 21 | Instance Segmentation Links 22 | --------------------------- 23 | 24 | MaskRCNNFPNResNet50 25 | ~~~~~~~~~~~~~~~~~~~ 26 | .. autoclass:: MaskRCNNFPNResNet50 27 | :members: 28 | 29 | MaskRCNNFPNResNet101 30 | ~~~~~~~~~~~~~~~~~~~~ 31 | .. autoclass:: MaskRCNNFPNResNet101 32 | :members: 33 | 34 | 35 | Utility 36 | ------- 37 | 38 | FasterRCNN 39 | ~~~~~~~~~~ 40 | .. autoclass:: FasterRCNN 41 | :members: 42 | 43 | FasterRCNNFPNResNet 44 | ~~~~~~~~~~~~~~~~~~~ 45 | .. autoclass:: FasterRCNNFPNResNet 46 | :members: 47 | 48 | 49 | FPN 50 | ~~~ 51 | .. autoclass:: FPN 52 | :members: 53 | 54 | BboxHead 55 | ~~~~~~~~ 56 | .. autoclass:: BboxHead 57 | :members: 58 | 59 | RPN 60 | ~~~~ 61 | .. autoclass:: RPN 62 | :members: 63 | 64 | MaskHead 65 | ~~~~~~~~ 66 | .. autoclass:: MaskHead 67 | :members: 68 | 69 | segm_to_mask 70 | ~~~~~~~~~~~~ 71 | .. autofunction:: segm_to_mask 72 | 73 | 74 | Train-only Utility 75 | ------------------ 76 | 77 | bbox_head_loss_pre 78 | ~~~~~~~~~~~~~~~~~~ 79 | .. autofunction:: bbox_head_loss_pre 80 | 81 | bbox_head_loss_post 82 | ~~~~~~~~~~~~~~~~~~~ 83 | .. autofunction:: bbox_head_loss_post 84 | 85 | rpn_loss 86 | ~~~~~~~~ 87 | .. autofunction:: rpn_loss 88 | 89 | mask_head_loss_pre 90 | ~~~~~~~~~~~~~~~~~~ 91 | .. autofunction:: mask_head_loss_pre 92 | 93 | mask_head_loss_post 94 | ~~~~~~~~~~~~~~~~~~~ 95 | .. autofunction:: mask_head_loss_post 96 | 97 | mask_to_segm 98 | ~~~~~~~~~~~~ 99 | .. autofunction:: mask_to_segm 100 | -------------------------------------------------------------------------------- /chainercv/links/__init__.py: -------------------------------------------------------------------------------- 1 | from chainercv.links.connection.conv_2d_activ import Conv2DActiv # NOQA 2 | from chainercv.links.connection.conv_2d_bn_activ import Conv2DBNActiv # NOQA 3 | from chainercv.links.connection.seblock import SEBlock # NOQA 4 | from chainercv.links.connection.separable_conv_2d_bn_activ import SeparableConv2DBNActiv # NOQA 5 | 6 | from chainercv.links.model.feature_predictor import FeaturePredictor # NOQA 7 | from chainercv.links.model.pickable_sequential_chain import PickableSequentialChain # NOQA 8 | from chainercv.links.model.pixelwise_softmax_classifier import PixelwiseSoftmaxClassifier # NOQA 9 | 10 | from chainercv.links.model.deeplab import DeepLabV3plusXception65 # NOQA 11 | from chainercv.links.model.faster_rcnn.faster_rcnn_vgg import FasterRCNNVGG16 # NOQA 12 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import FasterRCNNFPNResNet101 # NOQA 13 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import FasterRCNNFPNResNet50 # NOQA 14 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import MaskRCNNFPNResNet101 # NOQA 15 | from chainercv.links.model.fpn.faster_rcnn_fpn_resnet import MaskRCNNFPNResNet50 # NOQA 16 | from chainercv.links.model.resnet import ResNet101 # NOQA 17 | from chainercv.links.model.resnet import ResNet152 # NOQA 18 | from chainercv.links.model.resnet import ResNet50 # NOQA 19 | from chainercv.links.model.segnet.segnet_basic import SegNetBasic # NOQA 20 | from chainercv.links.model.senet import SEResNet101 # NOQA 21 | from chainercv.links.model.senet import SEResNet152 # NOQA 22 | from chainercv.links.model.senet import SEResNet50 # NOQA 23 | from chainercv.links.model.senet import SEResNeXt101 # NOQA 24 | from chainercv.links.model.senet import SEResNeXt50 # NOQA 25 | from chainercv.links.model.ssd import SSD300 # NOQA 26 | from chainercv.links.model.ssd import SSD512 # NOQA 27 | from chainercv.links.model.vgg import VGG16 # NOQA 28 | from chainercv.links.model.yolo import YOLOv2 # NOQA 29 | from chainercv.links.model.yolo import YOLOv3 # NOQA 30 | -------------------------------------------------------------------------------- /chainercv/links/model/fpn/fpn.py: -------------------------------------------------------------------------------- 1 | import chainer 2 | import chainer.functions as F 3 | from chainer import initializers 4 | import chainer.links as L 5 | 6 | 7 | class FPN(chainer.Chain): 8 | """An extractor class of Feature Pyramid Networks. 9 | 10 | This class wraps a feature extractor and provides 11 | multi-scale features. 12 | 13 | Args: 14 | base (Link): A base feature extractor. 15 | It should have :meth:`forward` and :obj:`mean`. 16 | :meth:`forward` should take a batch of images and return 17 | feature maps of them. The size of the :math:`k+1`-th feature map 18 | should be the half as that of the :math:`k`-th feature map. 19 | n_base_output (int): The number of feature maps 20 | that :obj:`base` returns. 21 | scales (tuple of floats): The scales of feature maps. 22 | 23 | """ 24 | 25 | def __init__(self, base, n_base_output, scales): 26 | super(FPN, self).__init__() 27 | with self.init_scope(): 28 | self.base = base 29 | self.inner = chainer.ChainList() 30 | self.outer = chainer.ChainList() 31 | 32 | init = {'initialW': initializers.GlorotNormal()} 33 | for _ in range(n_base_output): 34 | self.inner.append(L.Convolution2D(256, 1, **init)) 35 | self.outer.append(L.Convolution2D(256, 3, pad=1, **init)) 36 | 37 | self.scales = scales 38 | 39 | @property 40 | def mean(self): 41 | return self.base.mean 42 | 43 | def forward(self, x): 44 | hs = list(self.base(x)) 45 | 46 | for i in reversed(range(len(hs))): 47 | hs[i] = self.inner[i](hs[i]) 48 | if i + 1 < len(hs): 49 | hs[i] += F.unpooling_2d(hs[i + 1], 2, cover_all=False) 50 | 51 | for i in range(len(hs)): 52 | hs[i] = self.outer[i](hs[i]) 53 | 54 | while len(hs) < len(self.scales): 55 | hs.append(F.max_pooling_2d(hs[-1], 1, stride=2, cover_all=False)) 56 | 57 | return hs 58 | -------------------------------------------------------------------------------- /chainercv/utils/testing/assertions/assert_is_semantic_segmentation_dataset.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import six 3 | 4 | from chainercv.utils.testing.assertions.assert_is_image import assert_is_image 5 | 6 | 7 | def assert_is_semantic_segmentation_dataset(dataset, n_class, n_example=None): 8 | """Checks if a dataset satisfies semantic segmentation dataset APIs. 9 | 10 | This function checks if a given dataset satisfies semantic segmentation 11 | dataset APIs or not. 12 | If the dataset does not satifiy the APIs, this function raises an 13 | :class:`AssertionError`. 14 | 15 | Args: 16 | dataset: A dataset to be checked. 17 | n_class (int): The number of classes including background. 18 | n_example (int): The number of examples to be checked. 19 | If this argument is specified, this function picks 20 | examples ramdomly and checks them. Otherwise, 21 | this function checks all examples. 22 | 23 | """ 24 | 25 | assert len(dataset) > 0, 'The length of dataset must be greater than zero.' 26 | 27 | if n_example: 28 | for _ in six.moves.range(n_example): 29 | i = np.random.randint(0, len(dataset)) 30 | _check_example(dataset[i], n_class) 31 | else: 32 | for i in six.moves.range(len(dataset)): 33 | _check_example(dataset[i], n_class) 34 | 35 | 36 | def _check_example(example, n_class): 37 | assert len(example) >= 2, \ 38 | 'Each example must have at least two elements:' \ 39 | 'img and label.' 40 | 41 | img, label = example[:2] 42 | 43 | assert_is_image(img, color=True) 44 | 45 | assert isinstance(label, np.ndarray), \ 46 | 'label must be a numpy.ndarray.' 47 | assert label.dtype == np.int32, \ 48 | 'The type of label must be numpy.int32.' 49 | assert label.shape == img.shape[1:], \ 50 | 'The shape of label must be (H, W).' 51 | assert label.min() >= -1 and label.max() < n_class, \ 52 | 'The value of label must be in [-1, n_class - 1].' 53 | -------------------------------------------------------------------------------- /examples/fpn/README.md: -------------------------------------------------------------------------------- 1 | # Examples of Faster R-CNN and related models using FPN [1, 2] 2 | 3 | ## Performance 4 | MS COCO 2017 Val 5 | 6 | | Backbone | Supervision | Original (Bbox) | Ours (Bbox) | Original (Mask) | Ours (Mask)| 7 | |:-:|:-:|:-:|:-:|:-:|:-:| 8 | | FPN w/ ResNet50 | bbox | 36.7 % [3] | 37.1 % | | | 9 | | FPN w/ ResNet101 | bbox | 39.4 % [3] | 39.5 % | | | 10 | | FPN w/ ResNet50 | bbox + mask | 37.3 % [3] | 38.0 % | 33.7% [3] | 34.2 %| 11 | | FPN w/ ResNet101 | bbox + mask | 39.4 % [3] | 40.4% | 35.6% [3] | 36.0% | 12 | 13 | Scores are the mean of mean Average Precision (mmAP). 14 | 15 | ## Demo 16 | If `faster_rcnn_*` is used as `--model`, the script conducts object detection. 17 | If `mask_rcnn_*` is used as `--model`, the script conducts instance segmentation instead. 18 | This demo downloads MS COCO pretrained model automatically if a pretrained model path is not given. 19 | ``` 20 | $ python demo.py [--model faster_rcnn_fpn_resnet50|faster_rcnn_fpn_101|mask_rcnn_fpn_50|mask_rcnn_fpn_101] [--gpu ] [--pretrained-model ] .jpg 21 | ``` 22 | 23 | ## Evaluation 24 | For object detection, use [`chainercv/examples/detection/eval_detection.py`](https://github.com/chainer/chainercv/blob/master/examples/detection) for evaluation. 25 | For instance segmentation, use [`chainercv/examples/detection/eval_instance_segmentation.py`](https://github.com/chainer/chainercv/blob/master/examples/instance_segmentation) for evaluation. 26 | 27 | ## Train 28 | You can train the model with the following code. 29 | Note that this code requires `chainermn` module. 30 | ``` 31 | $ mpiexec -n <#gpu> python train_multi.py [--model faster_rcnn_fpn_resnet50|faster_rcnn_fpn_resnet101|mask_rcnn_fpn_resnet50|mask_rcnn_fpn_resnet101] [--batchsize ] 32 | ``` 33 | 34 | Note that `cv2` is required for training Mask R-CNN. 35 | 36 | ## References 37 | 1. Tsung-Yi Lin et al. "Feature Pyramid Networks for Object Detection" CVPR 2017 38 | 2. Kaiming He et al. "Mask R-CNN" ICCV 2017 39 | 3. [Detectron](https://github.com/facebookresearch/Detectron) 40 | -------------------------------------------------------------------------------- /tests/experimental_tests/links_tests/model_tests/fcis_tests/utils_tests/test_mask_voting.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import unittest 3 | 4 | import chainer 5 | from chainer import testing 6 | 7 | from chainercv.experimental.links.model.fcis.utils.mask_voting \ 8 | import mask_voting 9 | from chainercv.utils import assert_is_bbox 10 | from chainercv.utils import generate_random_bbox 11 | 12 | 13 | class TestMaskVoting(unittest.TestCase): 14 | 15 | def setUp(self): 16 | n_roi = 5 17 | n_class = 6 18 | self.roi_size = 7 19 | self.size = (18, 24) 20 | self.bg_label = 0 21 | self.roi_mask_prob = np.random.uniform( 22 | size=(n_roi, self.roi_size, self.roi_size)).astype(np.float32) 23 | self.roi_prob = np.random.uniform( 24 | size=(n_roi, n_class)).astype(np.float32) 25 | self.bbox = generate_random_bbox(n_roi, self.size, 0, 18) 26 | 27 | def check_mask_voting( 28 | self, seg_prob, bbox, cls_prob, 29 | size, bg_label, roi_size): 30 | xp = chainer.cuda.get_array_module(seg_prob) 31 | seg_prob, bbox, label, cls_prob = mask_voting( 32 | seg_prob, bbox, cls_prob, size, 33 | 0.5, 0.3, 0.5, 0.4, bg_label=bg_label) 34 | 35 | n_roi = seg_prob.shape[0] 36 | self.assertIsInstance(seg_prob, xp.ndarray) 37 | self.assertEqual(seg_prob.shape[1:], (roi_size, roi_size)) 38 | self.assertTrue( 39 | xp.all(xp.logical_and(seg_prob >= 0.0, seg_prob <= 1.0))) 40 | 41 | self.assertIsInstance(label, xp.ndarray) 42 | self.assertEqual(label.shape, (n_roi, )) 43 | 44 | self.assertIsInstance(cls_prob, xp.ndarray) 45 | self.assertEqual(cls_prob.shape, (n_roi, )) 46 | 47 | assert_is_bbox(bbox, size) 48 | 49 | def test_mask_voting_cpu(self): 50 | self.check_mask_voting( 51 | self.roi_mask_prob, self.bbox, self.roi_prob, 52 | self.size, self.bg_label, self.roi_size) 53 | 54 | 55 | testing.run_module(__name__, __file__) 56 | --------------------------------------------------------------------------------