matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.53k stars 11.68k forks source link

Can we turn Mask_RCNN in a package? #485

Open jerpint opened 6 years ago

jerpint commented 6 years ago

One thing I've noticed from using Mask_RCNN is that it is designed not a package completely. This means that suppose you want to use Mask_RCNN as a building block in code, it will break. For example:

Suppose I have a repo, MyProject/ and contained within it I have a forked version of Mask_RCNN as such: ├── MyProject ├── some_code.py ├── Mask_RCNN │   ├── assets │   ├── coco.py │   ├── ...

If I want to use any of the useful functions in some_code.py , for example useful functions from coco.py, I would need to run

from Mask_RCNN import coco

This first off requires Mask_RCNN/ to contain an __init__.py, and second, in importing coco.py, the current statement is import utils which should then at this point be imported not directly, but from Mask_RCNN. There are similar examples in visualize.py and model.py.

I can create a pull request for this, it only requires a couple of minor changes in the headers of

CMCDragonkai commented 6 years ago

In my situation:

solar-panel-analysis/
  Mask_RCNN/
  solar/
    __init__.py
    main.py
  script.py

Inside script.py and main.py:

from Mask_RCNN.mrcnn import utils

Works.

To run main.py I need to use python -m solar.main.

jerpint commented 6 years ago

Roger, I believe you also need to add init.py under MaskRCNN/ so that it itself also becomes a package

My solution is pretty much the same as yours.. and I believe anyone using MaskRCNN standalone will be required to do this if they wish to be able to pull from master later on

On Wed, Apr 25, 2018, 22:27 Roger Qiu, notifications@github.com wrote:

In my situation:

solar-panel-analysis/ Mask_RCNN/ solar/ init.py main.py script.py

Inside script.py:

from Mask_RCNN.mrcnn import utils

Works.

However inside main.py. Doing the same thing doesn't work. It cannot find it. I think this requires doing the python setup.py install inside the Mask_RCNN which will make mrcnn a global package.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/matterport/Mask_RCNN/issues/485#issuecomment-384492808, or mute the thread https://github.com/notifications/unsubscribe-auth/ARmIxLRytnQwHTIN8IZQKyLHXbNtU2Qjks5tsTCcgaJpZM4Ti0D4 .

CMCDragonkai commented 6 years ago

You don't need to have __init__.py for python 3.3+. I'm running 3.6 so that's why it works.

CMCDragonkai commented 6 years ago

Hmm I think I have encountered a problem you are talking about.

The model.py performs from mrcnn import utils, and if Mask_RCNN repository is in that directory structure stated in your OP, then importing from Mask_RCNN.mrcnn import model as modellib fails with:

ImportError: No module named 'mrcnn'
CMCDragonkai commented 6 years ago

Swapping from mrcnn import utils to from Mask_RCNN.mrcnn import utils does end up working (even if you don't have __init__.py inside Mask_RCNN).

After reading a bunch of stuff on Python imports. It is in my opinion that one should be using explicit relative imports. Change from mrcnn import utils to from . import utils. In this case, it ends up working as normal if you install the mrcnn package. But also works if you use as an embedded subdirectory (git submodules).

But if you don't want to fork and change the source code. The quickest hack is to add Mask_RCNN to the sys.path prior to importing anything from Mask_RCNN.mrcnn. I can do this inside my __init__.py for my package.

import sys, os.path as path
from inspect import getsourcefile
parent_dir = path.dirname(path.dirname(path.abspath(getsourcefile(lambda: 0))))
sys.path.append(path.join(parent_dir, 'Mask_RCNN'))

from . import config
from .config import *

from . import dataset
from .dataset import *

from . import model
from .model import *

I decided to document this pattern here: https://gist.github.com/CMCDragonkai/fe342b958e013078d72500d286973075

eyildiz-ugoe commented 5 years ago

Swapping from mrcnn import utils to from Mask_RCNN.mrcnn import utils does end up working

@CMCDragonkai Not really, you just get another error which says ModuleNotFoundError: No module named 'Mask_RCNN'.

MichaelTendoSsemwanga commented 5 years ago

I am trying to run the notebooks, demo.ipynb in colab but, nothing imports correct

JulienMaille commented 5 years ago

I am trying to run the notebooks, demo.ipynb in colab but, nothing imports correct

I have the same issue, did you found a solution?