Mask_rcnn: Can't import coco

Created on 22 May 2019  路  4Comments  路  Source: matterport/Mask_RCNN

I've followed the readme instructions to install the require packages. Coco is not installed as part of requirements.txt.

When I run the demo.ipynb I get the following error message:

ImportError                               Traceback (most recent call last)
<ipython-input-5-ebe7095df7bb> in <module>
     18 # Import COCO config
     19 sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version
---> 20 import coco
     21 
     22 get_ipython().run_line_magic('matplotlib', 'inline ')

~/projects/Mask_RCNN/samples/coco/coco.py in <module>
     40 # If the PR is merged then use the original repo.
     41 # Note: Edit PythonAPI/Makefile and replace "python" with "python3".
---> 42 from pycocotools.coco import COCO
     43 from pycocotools.cocoeval import COCOeval
     44 from pycocotools import mask as maskUtils

ImportError: No module named 'pycocotools'

I then followed the build instructions for coco from here:
https://github.com/cocodataset/cocoapi/tree/master/PythonAPI

But I modified the makefile to build it for Pyhon3. This appeared to work as I didn't get any build issues.

make install

This produces the following output:

# install pycocotools to the Python site-packages
python3 setup.py build_ext install
running build_ext
skipping 'pycocotools/_mask.c' Cython extension (up-to-date)
running install
running bdist_egg
running egg_info
writing dependency_links to pycocotools.egg-info/dependency_links.txt
writing pycocotools.egg-info/PKG-INFO
writing requirements to pycocotools.egg-info/requires.txt
writing top-level names to pycocotools.egg-info/top_level.txt
reading manifest file 'pycocotools.egg-info/SOURCES.txt'
writing manifest file 'pycocotools.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
copying pycocotools/__init__.py -> build/lib.linux-x86_64-3.5/pycocotools
copying pycocotools/mask.py -> build/lib.linux-x86_64-3.5/pycocotools
copying pycocotools/coco.py -> build/lib.linux-x86_64-3.5/pycocotools
copying pycocotools/cocoeval.py -> build/lib.linux-x86_64-3.5/pycocotools
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/pycocotools
copying build/lib.linux-x86_64-3.5/pycocotools/_mask.cpython-35m-x86_64-linux-gnu.so -> build/bdist.linux-x86_64/egg/pycocotools
copying build/lib.linux-x86_64-3.5/pycocotools/__init__.py -> build/bdist.linux-x86_64/egg/pycocotools
copying build/lib.linux-x86_64-3.5/pycocotools/mask.py -> build/bdist.linux-x86_64/egg/pycocotools
copying build/lib.linux-x86_64-3.5/pycocotools/coco.py -> build/bdist.linux-x86_64/egg/pycocotools
copying build/lib.linux-x86_64-3.5/pycocotools/cocoeval.py -> build/bdist.linux-x86_64/egg/pycocotools
byte-compiling build/bdist.linux-x86_64/egg/pycocotools/__init__.py to __init__.cpython-35.pyc
byte-compiling build/bdist.linux-x86_64/egg/pycocotools/mask.py to mask.cpython-35.pyc
byte-compiling build/bdist.linux-x86_64/egg/pycocotools/coco.py to coco.cpython-35.pyc
byte-compiling build/bdist.linux-x86_64/egg/pycocotools/cocoeval.py to cocoeval.cpython-35.pyc
creating stub loader for pycocotools/_mask.cpython-35m-x86_64-linux-gnu.so
byte-compiling build/bdist.linux-x86_64/egg/pycocotools/_mask.py to _mask.cpython-35.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying pycocotools.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pycocotools.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pycocotools.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pycocotools.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying pycocotools.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
pycocotools.__pycache__._mask.cpython-35: module references __file__
creating 'dist/pycocotools-2.0-py3.5-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing pycocotools-2.0-py3.5-linux-x86_64.egg
removing '/usr/local/lib/python3.5/dist-packages/pycocotools-2.0-py3.5-linux-x86_64.egg' (and everything under it)
creating /usr/local/lib/python3.5/dist-packages/pycocotools-2.0-py3.5-linux-x86_64.egg
Extracting pycocotools-2.0-py3.5-linux-x86_64.egg to /usr/local/lib/python3.5/dist-packages
pycocotools 2.0 is already the active version in easy-install.pth

Installed /usr/local/lib/python3.5/dist-packages/pycocotools-2.0-py3.5-linux-x86_64.egg
Processing dependencies for pycocotools==2.0
Searching for matplotlib==3.0.3
Best match: matplotlib 3.0.3
Adding matplotlib 3.0.3 to easy-install.pth file

Using /usr/local/lib/python3.5/dist-packages
Searching for Cython==0.29.7
Best match: Cython 0.29.7
Adding Cython 0.29.7 to easy-install.pth file
Installing cythonize script to /usr/local/bin
Installing cython script to /usr/local/bin
Installing cygdb script to /usr/local/bin

Using /usr/local/lib/python3.5/dist-packages
Searching for setuptools==41.0.1
Best match: setuptools 41.0.1
Adding setuptools 41.0.1 to easy-install.pth file
Installing easy_install-3.6 script to /usr/local/bin
Installing easy_install script to /usr/local/bin

Using /home/nlbutts/.local/lib/python3.5/site-packages
Searching for kiwisolver==1.1.0
Best match: kiwisolver 1.1.0
Adding kiwisolver 1.1.0 to easy-install.pth file

Using /usr/local/lib/python3.5/dist-packages
Searching for pyparsing==2.0.3
Best match: pyparsing 2.0.3
Adding pyparsing 2.0.3 to easy-install.pth file

Using /usr/lib/python3/dist-packages
Searching for python-dateutil==2.8.0
Best match: python-dateutil 2.8.0
Adding python-dateutil 2.8.0 to easy-install.pth file

Using /home/nlbutts/.local/lib/python3.5/site-packages
Searching for numpy==1.16.3
Best match: numpy 1.16.3
Adding numpy 1.16.3 to easy-install.pth file
Installing f2py3 script to /usr/local/bin
Installing f2py script to /usr/local/bin
Installing f2py3.5 script to /usr/local/bin

Using /usr/local/lib/python3.5/dist-packages
Searching for cycler==0.10.0
Best match: cycler 0.10.0
Adding cycler 0.10.0 to easy-install.pth file

Using /usr/local/lib/python3.5/dist-packages
Searching for six==1.12.0
Best match: six 1.12.0
Adding six 1.12.0 to easy-install.pth file

Using /home/nlbutts/.local/lib/python3.5/site-packages
Finished processing dependencies for pycocotools==2.0
rm -rf build

It appears to build and install correctly. If I open up ipython3 and import coco I get the same error.
Python 3.5.2 (default, Nov 12 2018, 13:43:14)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import coco                                                                                                                                                                                                
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-c01e95d1762c> in <module>
----> 1 import coco

ImportError: No module named 'coco'

In [2]:                         

What am I doing wrong?

Most helpful comment

Hi, the module's name is pycocotools. Try import pycocotools and it should work.

All 4 comments

Hi, the module's name is pycocotools. Try import pycocotools and it should work.

I found coco.py in samples/coco. For some reason the Jupyter notebook wasn't picking it up, so I just copied and pasted each line into ipython3 based on this notebook:
https://github.com/matterport/Mask_RCNN/blob/master/samples/demo.ipynb

I get the following results:

In [6]: # Run detection 
   ...: results = model.detect([image], verbose=1) 
   ...:                                                                                                                                                                                                            
Processing 1 images
image                    shape: (1080, 1920, 3)       min:    0.00000  max:  255.00000  uint8
molded_images            shape: (1, 1024, 1024, 3)    min: -123.70000  max:  151.10000  float64
image_metas              shape: (1, 93)               min:    0.00000  max: 1920.00000  float64
anchors                  shape: (1, 261888, 4)        min:   -0.35390  max:    1.29134  float32
2019-05-24 15:20:48.921155: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node mrcnn_detection/strided_slice_8. Error: ValidateStridedSliceOp returned partial shapes [1,?,?] and [?,?]
2019-05-24 15:20:48.921279: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node mrcnn_detection/strided_slice_31. Error: ValidateStridedSliceOp returned partial shapes [1,?,?] and [?,?]
2019-05-24 15:20:48.922478: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_15. Error: ValidateStridedSliceOp returned partial shapes [1,?,4] and [?,4]
2019-05-24 15:20:48.922564: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_27. Error: ValidateStridedSliceOp returned partial shapes [1,?,4] and [?,4]
2019-05-24 15:20:48.922610: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_41. Error: ValidateStridedSliceOp returned partial shapes [1,?] and [?]
2019-05-24 15:20:48.922677: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_44. Error: ValidateStridedSliceOp returned partial shapes [1,?] and [?]
2019-05-24 15:20:49.865228: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node mrcnn_detection/strided_slice_8. Error: ValidateStridedSliceOp returned partial shapes [1,?,?] and [?,?]
2019-05-24 15:20:49.865312: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node mrcnn_detection/strided_slice_31. Error: ValidateStridedSliceOp returned partial shapes [1,?,?] and [?,?]
2019-05-24 15:20:49.866111: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_15. Error: ValidateStridedSliceOp returned partial shapes [1,?,4] and [?,4]
2019-05-24 15:20:49.866190: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_27. Error: ValidateStridedSliceOp returned partial shapes [1,?,4] and [?,4]
2019-05-24 15:20:49.866230: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_41. Error: ValidateStridedSliceOp returned partial shapes [1,?] and [?]
2019-05-24 15:20:49.866270: W ./tensorflow/core/grappler/optimizers/graph_optimizer_stage.h:241] Failed to run optimizer ArithmeticOptimizer, stage RemoveStackStridedSliceSameAxis node ROI/strided_slice_44. Error: ValidateStridedSliceOp returned partial shapes [1,?] and [?]
2019-05-24 15:20:50.333091: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at strided_slice_op.cc:106 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2019-05-24 15:20:50.333256: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at strided_slice_op.cc:106 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2019-05-24 15:20:56.250747: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at strided_slice_op.cc:106 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2019-05-24 15:20:56.267093: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at strided_slice_op.cc:106 : Invalid argument: slice index 1 of dimension 0 out of bounds.
2019-05-24 15:20:56.276561: W tensorflow/core/framework/op_kernel.cc:1401] OP_REQUIRES failed at strided_slice_op.cc:106 : Invalid argument: slice index 1 of dimension 0 out of bounds.
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-6-1ef1067241c2> in <module>
      1 # Run detection
----> 2 results = model.detect([image], verbose=1)

/usr/local/lib/python3.5/dist-packages/mask_rcnn-2.1-py3.5.egg/mrcnn/model.py in detect(self, images, verbose)
   2522         # Run object detection
   2523         detections, _, _, mrcnn_mask, _, _, _ =\
-> 2524             self.keras_model.predict([molded_images, image_metas, anchors], verbose=0)
   2525         # Process detections
   2526         results = []

/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
   1167                                             batch_size=batch_size,
   1168                                             verbose=verbose,
-> 1169                                             steps=steps)
   1170 
   1171     def train_on_batch(self, x, y,

/usr/local/lib/python3.5/dist-packages/keras/engine/training_arrays.py in predict_loop(model, f, ins, batch_size, verbose, steps)
    292                 ins_batch[i] = ins_batch[i].toarray()
    293 
--> 294             batch_outs = f(ins_batch)
    295             batch_outs = to_list(batch_outs)
    296             if batch_index == 0:

/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2713                 return self._legacy_call(inputs)
   2714 
-> 2715             return self._call(inputs)
   2716         else:
   2717             if py_any(is_tensor(x) for x in inputs):

/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in _call(self, inputs)
   2673             fetched = self._callable_fn(*array_vals, run_metadata=self.run_metadata)
   2674         else:
-> 2675             fetched = self._callable_fn(*array_vals)
   2676         return fetched[:len(self.outputs)]
   2677 

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1437           ret = tf_session.TF_SessionRunCallable(
   1438               self._session._session, self._handle, args, status,
-> 1439               run_metadata_ptr)
   1440         if run_metadata:
   1441           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    526             None, None,
    527             compat.as_text(c_api.TF_Message(self.status.status)),
--> 528             c_api.TF_GetCode(self.status.status))
    529     # Delete the underlying status object from memory otherwise it stays alive
    530     # as there is a reference to status from this from the traceback due to

InvalidArgumentError: slice index 1 of dimension 0 out of bounds.
     [[{{node ROI/strided_slice_12}}]]

I'm experiencing the same problem @nlbutts @CSU-NXY , were you able to solve your problem?
I'm trying to run: https://github.com/matterport/Mask_RCNN/blob/master/samples/demo.ipynb

ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-f2cc2c0c9e12> in <module>
      18 # Import COCO config
      19 sys.path.append (os.path.join (ROOT_DIR, "samples / coco /")) # To find local version
---> 20 import coco
      21
      22 get_ipython (). Run_line_magic ('matplotlib', 'inline')

~ \ Mask_RCNN-master \ samples / coco \ coco.py in <module>
      40 # If the PR is merged then use the original repo.
      41 # Note: Edit PythonAPI / Makefile and replace "python" with "python3".
---> 42 from pycocotools.coco import COCO
      43 from pycocotools.cocoeval import COCOeval
      44 from pycocotools import mask as maskUtils

ModuleNotFoundError: No module named 'pycocotools'

ROOT_DIR = os.path.abspath("") - This will make it work in the first cell

Was this page helpful?
0 / 5 - 0 ratings