From 04126c673e3134f9415c13a9cfaae0a03e40600b Mon Sep 17 00:00:00 2001 From: Marius Montebaur Date: Wed, 3 Feb 2021 19:21:45 +0100 Subject: [PATCH 1/2] Applied some bug fixes people have been discussing in Issues to keep the repo usable --- README.md | 32 ++++- docker/Dockerfile | 122 +++++++++++++++++- .../datasets/evaluation/coco/coco_eval.py | 2 +- .../balanced_positive_negative_sampler.py | 4 +- .../modeling/roi_heads/mask_head/inference.py | 1 + maskrcnn_benchmark/modeling/rpn/inference.py | 4 +- .../structures/segmentation_mask.py | 4 +- 7 files changed, 154 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f7ac61b3a..a5353147c 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,32 @@ **maskrcnn-benchmark has been deprecated. Please see [detectron2](https://github.com/facebookresearch/detectron2), which includes implementations for all models in maskrcnn-benchmark** +## Bugfixest: Keep maskrcnn-benchmark running to use with legacy software + +Fixes: +- [Issue #1182](https://github.com/facebookresearch/maskrcnn-benchmark/issues/1182#issuecomment-563896684): Replacing `torch.bool` for `torch.uint8` in +[maskrcnn_benchmark/structures/segmentation_mask.py](maskrcnn_benchmark/structures/segmentation_mask.py) and +[maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py](maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py) +- [Issue #1053](https://github.com/facebookresearch/maskrcnn-benchmark/pull/1053#issuecomment-523340294): Encode as `torch.uint8` instead of `torch.bool` in +[maskrcnn_benchmark/data/datasets/evaluation/coco/coco_eval.py](maskrcnn_benchmark/data/datasets/evaluation/coco/coco_eval.py) +- [Issue #1148](https://github.com/facebookresearch/maskrcnn-benchmark/issues/1148#issuecomment-590051409): Replacing `torch.bool` for `torch.uint8` in [maskrcnn_benchmark/modeling/rpn/inference.py](maskrcnn_benchmark/modeling/rpn/inference.py) +- [Issue #1156](https://github.com/facebookresearch/maskrcnn-benchmark/issues/1156#issuecomment-554193539): Convert to integer before using `res` for single image inference in [maskrcnn_benchmark/modeling/roi_heads/mask_head/inference.py](maskrcnn_benchmark/modeling/roi_heads/mask_head/inference.py) +- Replacing `torch.bool` for `torch.uint8` in [maskrcnn_benchmark/modeling/rpn/inference.py](maskrcnn_benchmark/modeling/rpn/inference.py) in `select_over_all_levels(..)` + +Instructions to train and run using your own dataset after fixing this: [Issue #521](https://github.com/facebookresearch/maskrcnn-benchmark/issues/521) + +### Trim pretrained Models + +A downloaded pretrained model needs to be trimmed (i.e. the head of the network is removed) to then be retrained for your configuration and your number of classes. I've had problems doing this with the script in this repository but an alternative has been posted [here](https://gist.github.com/bernhardschaefer/01905b0fe83615f79e2928a2a10b6f28). + +### Dockerfile + +The Dockerfile wasn' working for me out of the box which is why I updated this as well. + +--- + +## Repository Description + This project aims at providing the necessary building blocks for easily creating detection and segmentation models using PyTorch 1.0. @@ -99,7 +125,7 @@ ln -s /path_to_VOCdevkit_dir datasets/voc ``` P.S. `COCO_2017_train` = `COCO_2014_train` + `valminusminival` , `COCO_2017_val` = `minival` - + You can also configure your own paths to the datasets. For that, all you need to do is to modify `maskrcnn_benchmark/config/paths_catalog.py` to @@ -262,7 +288,7 @@ note = {Accessed: [Insert date here]} ## Projects using maskrcnn-benchmark -- [RetinaMask: Learning to predict masks improves state-of-the-art single-shot detection for free](https://arxiv.org/abs/1901.03353). +- [RetinaMask: Learning to predict masks improves state-of-the-art single-shot detection for free](https://arxiv.org/abs/1901.03353). Cheng-Yang Fu, Mykhailo Shvets, and Alexander C. Berg. Tech report, arXiv,1901.03353. - [FCOS: Fully Convolutional One-Stage Object Detection](https://arxiv.org/abs/1904.01355). @@ -274,7 +300,7 @@ note = {Accessed: [Insert date here]} - [Is Sampling Heuristics Necessary in Training Deep Object Detectors?](https://arxiv.org/abs/1909.04868) Joya Chen, Dong Liu, Tong Xu, Shilong Zhang, Shiwei Wu, Bin Luo, Xuezheng Peng, Enhong Chen. Tech report, arXiv,1909.04868. [[code](https://github.com/ChenJoya/sampling-free)] - + ## License maskrcnn-benchmark is released under the MIT license. See [LICENSE](LICENSE) for additional details. diff --git a/docker/Dockerfile b/docker/Dockerfile index 762441fe2..3ac9cf902 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,5 +1,40 @@ -ARG CUDA="9.0" + +# maskrcnn-benchmark Dockerfile +# +# Updated and it worked in Feb 2021 + +# If you've never worked with docker before: +# +# This script installs a lot of software in a docker image. This image can be used to +# create a docker container in which you can execute code. + +# I also included ROS and OpenCV installs in here. This will increase the size of the +# image to >10GB. Feel free to delete this if you don't need it. + +# You can also choose whether to install the upstream maskrcnn-benchmark or my fork where +# I tried to fix some issues according to what people discussed in the issues. + + +# === Useful commands: === + +# I'm using nvidia-docker for all of this since I want to use my GPU inside the container. + +# Build an image from the Dockerfile: +# $ cd < cloned maskrcnn-benchmark repository > +# $ nvidia-docker build -t maskrcnn-benchmark-ros docker/ +# < name for the image > + +# Start new container from image: +# $ nvidia-docker run -it --entrypoint bash maskrcnn-benchmark + +# Enter running container: +# $ nvidia-docker exec -it maskrcnn-benchmark bash + + +ARG CUDA="10.0" ARG CUDNN="7" +# cuda 9 + cudnn 7 had problems with cudnn on NVIDIA Turing Hardware (RTX Gen 1) +# cuda 10 + cudnn 7 works for Turing FROM nvidia/cuda:${CUDA}-cudnn${CUDNN}-devel-ubuntu16.04 @@ -11,7 +46,7 @@ RUN apt-get update -y \ && apt-get install -y libglib2.0-0 libsm6 libxext6 libxrender-dev # Install Miniconda -RUN curl -so /miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh \ +RUN curl -L -so /miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh \ && chmod +x /miniconda.sh \ && /miniconda.sh -b -p /miniconda \ && rm /miniconda.sh @@ -30,15 +65,18 @@ ENV CONDA_AUTO_UPDATE_CONDA=false RUN conda install -y ipython RUN pip install requests ninja yacs cython matplotlib opencv-python tqdm +RUN pip install attrs scikit-image +RUN pip3 install cityscapesScripts # Install PyTorch 1.0 Nightly ARG CUDA -RUN conda install pytorch-nightly cudatoolkit=${CUDA} -c pytorch \ +RUN conda install pytorch-nightly=1.0 cudatoolkit=${CUDA} -c pytorch \ && conda clean -ya # Install TorchVision master RUN git clone https://github.com/pytorch/vision.git \ && cd vision \ + && git checkout "v0.2.2_branch" \ && python setup.py install # install pycocotools @@ -49,13 +87,87 @@ RUN git clone https://github.com/cocodataset/cocoapi.git \ # install apex RUN git clone https://github.com/NVIDIA/apex.git \ && cd apex \ + && git checkout f3a960f80244cf9e80558ab30f7f7e8cbf03c0a0 \ && python setup.py install --cuda_ext --cpp_ext -# install PyTorch Detection + +# install Detection +# TO USE THE UPSTREAM REPO WHICH DIDN'T WORK FOR ME: +# The three lines with sed try to fix at least some problems. Look at README.md for more info. +# ARG FORCE_CUDA="1" +# ENV FORCE_CUDA=${FORCE_CUDA} +# RUN git clone https://github.com/facebookresearch/maskrcnn-benchmark.git \ +# && cd maskrcnn-benchmark \ +# && sed -i"" "s/torch\.bool/torch.uint8/g" maskrcnn_benchmark/structures/segmentation_mask.py \ +# && sed -i"" "s/torch\.bool/torch.uint8/g" maskrcnn_benchmark/modeling/rpn/inference.py \ +# && sed -i"" "s/torch\.bool/torch.uint8/g" maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py \ +# && python setup.py build develop + +# USE FIXED REPO: ARG FORCE_CUDA="1" ENV FORCE_CUDA=${FORCE_CUDA} -RUN git clone https://github.com/facebookresearch/maskrcnn-benchmark.git \ +RUN git clone https://github.com/montioo/maskrcnn-benchmark.git \ && cd maskrcnn-benchmark \ && python setup.py build develop + + +# === ROS === +# only necessary if you want to use maskrcnn-benchmark with ros + +RUN apt-get install -y apt-transport-https + +RUN sh -c 'echo "deb http://packages.ros.org/ros/ubuntu xenial main" > /etc/apt/sources.list.d/ros-latest.list' + +RUN apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654 + +# install bootstrap tools +RUN apt-get update && apt-get install --no-install-recommends -y \ + build-essential \ + python-rosdep \ + python-rosinstall \ + python-vcstools \ + && rm -rf /var/lib/apt/lists/* + +# bootstrap rosdep +RUN rosdep init && \ + rosdep update --rosdistro kinetic + +# install ros packages +RUN apt-get update && apt-get install -y --no-install-recommends \ + ros-kinetic-ros-base \ + && rm -rf /var/lib/apt/lists/* + +RUN apt-get update \ + && apt-get install -y vim python-rospy python-opencv + +RUN apt-get install -y python3-pip python3-yaml +RUN pip3 install rospkg catkin_pkg +RUN apt-get install -y python-catkin-tools python3-dev python3-numpy +RUN apt-get install -y ros-kinetic-cv-bridge +RUN apt-get install -y ros-kinetic-vision-opencv + + + +# === Open CV === + +RUN apt-get install -y gcc g++ +RUN apt-get install -y python3-dev python3-numpy +RUN apt-get install -y libavcodec-dev libavformat-dev libswscale-dev +RUN apt-get install -y libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev +RUN apt-get install -y libgtk-3-dev + +RUN git clone https://github.com/opencv/opencv.git \ + && cd opencv \ + && git checkout 3.4 \ + && mkdir build \ + && cd build \ + && cmake ../ -DPYTHON_EXECUTABLE=/miniconda/envs/py36/bin/python3 -DPYTHON_INCLUDE_DIR=/miniconda/envs/py36/include/python3.6m -DPYTHON_LIBRARY=/miniconda/envs/py36/lib/libpython3.6m.so \ + && make -j 16 + +RUN conda install -y boost + +RUN echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc +RUN echo "set -o vi" >> ~/.bashrc + WORKDIR /maskrcnn-benchmark diff --git a/maskrcnn_benchmark/data/datasets/evaluation/coco/coco_eval.py b/maskrcnn_benchmark/data/datasets/evaluation/coco/coco_eval.py index a8fdc280e..a23e10f6a 100644 --- a/maskrcnn_benchmark/data/datasets/evaluation/coco/coco_eval.py +++ b/maskrcnn_benchmark/data/datasets/evaluation/coco/coco_eval.py @@ -133,7 +133,7 @@ def prepare_for_coco_segmentation(predictions, dataset): # rles = prediction.get_field('mask') rles = [ - mask_util.encode(np.array(mask[0, :, :, np.newaxis], order="F"))[0] + mask_util.encode(np.array(mask[0, :, :, np.newaxis], dtype=np.uint8, order="F"))[0] for mask in masks ] for rle in rles: diff --git a/maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py b/maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py index 52a332d41..902a60eb2 100644 --- a/maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py +++ b/maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py @@ -54,10 +54,10 @@ def __call__(self, matched_idxs): # create binary mask from indices pos_idx_per_image_mask = torch.zeros_like( - matched_idxs_per_image, dtype=torch.bool + matched_idxs_per_image, dtype=torch.uint8 ) neg_idx_per_image_mask = torch.zeros_like( - matched_idxs_per_image, dtype=torch.bool + matched_idxs_per_image, dtype=torch.uint8 ) pos_idx_per_image_mask[pos_idx_per_image] = 1 neg_idx_per_image_mask[neg_idx_per_image] = 1 diff --git a/maskrcnn_benchmark/modeling/roi_heads/mask_head/inference.py b/maskrcnn_benchmark/modeling/roi_heads/mask_head/inference.py index 953bb520c..4a5e51f3a 100644 --- a/maskrcnn_benchmark/modeling/roi_heads/mask_head/inference.py +++ b/maskrcnn_benchmark/modeling/roi_heads/mask_head/inference.py @@ -172,6 +172,7 @@ def __init__(self, threshold=0.5, padding=1): def forward_single_image(self, masks, boxes): boxes = boxes.convert("xyxy") im_w, im_h = boxes.size + res = [o.int() for o in res] res = [ paste_mask_in_image(mask[0], box, im_h, im_w, self.threshold, self.padding) for mask, box in zip(masks, boxes.bbox) diff --git a/maskrcnn_benchmark/modeling/rpn/inference.py b/maskrcnn_benchmark/modeling/rpn/inference.py index 5f2eb1742..31bca82d3 100644 --- a/maskrcnn_benchmark/modeling/rpn/inference.py +++ b/maskrcnn_benchmark/modeling/rpn/inference.py @@ -156,7 +156,7 @@ def select_over_all_levels(self, boxlists): # different behavior during training and during testing: # during training, post_nms_top_n is over *all* the proposals combined, while # during testing, it is over the proposals for each image - # NOTE: it should be per image, and not per batch. However, to be consistent + # NOTE: it should be per image, and not per batch. However, to be consistent # with Detectron, the default is per batch (see Issue #672) if self.training and self.fpn_post_nms_per_batch: objectness = torch.cat( @@ -165,7 +165,7 @@ def select_over_all_levels(self, boxlists): box_sizes = [len(boxlist) for boxlist in boxlists] post_nms_top_n = min(self.fpn_post_nms_top_n, len(objectness)) _, inds_sorted = torch.topk(objectness, post_nms_top_n, dim=0, sorted=True) - inds_mask = torch.zeros_like(objectness, dtype=torch.bool) + inds_mask = torch.zeros_like(objectness, dtype=torch.uint8) inds_mask[inds_sorted] = 1 inds_mask = inds_mask.split(box_sizes) for i in range(num_images): diff --git a/maskrcnn_benchmark/structures/segmentation_mask.py b/maskrcnn_benchmark/structures/segmentation_mask.py index 84ef9dbc9..c515ace48 100644 --- a/maskrcnn_benchmark/structures/segmentation_mask.py +++ b/maskrcnn_benchmark/structures/segmentation_mask.py @@ -441,7 +441,7 @@ def convert_to_binarymask(self): ) else: size = self.size - masks = torch.empty([0, size[1], size[0]], dtype=torch.bool) + masks = torch.empty([0, size[1], size[0]], dtype=torch.uint8) return BinaryMaskList(masks, size=self.size) @@ -456,7 +456,7 @@ def __getitem__(self, item): else: # advanced indexing on a single dimension selected_polygons = [] - if isinstance(item, torch.Tensor) and item.dtype == torch.bool: + if isinstance(item, torch.Tensor) and item.dtype == torch.uint8: item = item.nonzero() item = item.squeeze(1) if item.numel() > 0 else item item = item.tolist() From 3a8b2480951397ed0493e5b6168f61b9bd873067 Mon Sep 17 00:00:00 2001 From: Marius Montebaur Date: Wed, 3 Feb 2021 19:32:11 +0100 Subject: [PATCH 2/2] Fixed typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a5353147c..00193c090 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ A downloaded pretrained model needs to be trimmed (i.e. the head of the network ### Dockerfile -The Dockerfile wasn' working for me out of the box which is why I updated this as well. +The Dockerfile wasn't working for me out of the box which is why I updated this as well. ---