0
TEST_MODE = "inference"
ROOT_DIR = "/content/Mask_RCNN/dataset"

 def get_ax(rows=1, cols=1, size=16):
  """Return a Matplotlib Axes array to be used in all visualizations in the notebook.  Provide 
     a central point to control graph sizes. Adjust the size attribute to control how big to 
     render images"""
     _, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows))
          return ax

# Load validation dataset
# Must call before using the dataset
CUSTOM_DIR = "/content/Mask_RCNN/dataset"
dataset = CustomDataset()
dataset.load_custom(CUSTOM_DIR, "val")
dataset.prepare()
print("Images: {}\nClasses: {}".format(len(dataset.image_ids), dataset.class_names))

i am trying to classify 3 images using maskrcnn i had created custom dataset. but i am fetching this error

                                                                        "
objects: ['laptop', 'tab', 'phone']
numids [1, 2, 3]
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-16-32aa7809facd> in <module>()
     11 CUSTOM_DIR = "/content/Mask_RCNN/dataset"
     12 dataset = CustomDataset()
---> 13 dataset.load_custom(CUSTOM_DIR, "val")
     14 dataset.prepare()
     15 print("Images: {}\nClasses: {}".format(len(dataset.image_ids), dataset.class_names))
7 frames
/usr/local/lib/python3.7/dist-packages/imageio/core/request.py in _parse_uri(self, uri)
    271                 # Reading: check that the file exists (but is allowed a dir)
    272                 if not os.path.exists(fn):
--> 273                     raise FileNotFoundError("No such file: '%s'" % fn)
    274             else:
    275                 # Writing: check that the directory to write to does exist

FileNotFoundError: No such file: '/content/Mask_RCNN/dataset/val/download.jpg'

i am trying to classify 3 images using maskrcnn i had created custom dataset. but i am fetching this error

  • 1
    Build an absolute path, do not rely on relative pathes, your CWD might not be what you expect it to be. – Mike Scotty Sep 28 '21 at 06:53
  • what should I do to make absolute path. I already created dataset folder in that i created train and val @MikeScotty – engineering Baba Sep 28 '21 at 06:58
  • 1
    your ``CUSTOM_DIR`` is a relative path (relative to your project root) right? Instead, provide the full path (on your system). Related: https://stackoverflow.com/questions/3430372/how-do-i-get-the-full-path-of-the-current-files-directory and https://stackoverflow.com/questions/51520/how-to-get-an-absolute-file-path-in-python You can either hard code it or build it relative to ``__file__``, which should be reliable. – Mike Scotty Sep 28 '21 at 07:03
  • I am using google colab. but still not able to resolve this error @MikeScotty – engineering Baba Sep 28 '21 at 13:41

1 Answers1

1

The error is just saying no file can be found at that path. If you are going to use any paths going forward it is better to keep them as absolute paths rather than relative. I will give you the tools to get started, but since I don't know your file layout you will need to fill in the blanks.

Relative vs Absolute paths (abspath)

Here is a quick example, let's say your on windows and you want to get that file, and it's in a folder on the desktop called "images". If your cwd is the desktop then the relative path is \\images\\image.jpg, but the absolute path is <drive letter>:\Users\<name>\Desktop\images\image.jpg.

Python includes some options to do this using os.path, most notably os.path.abspath().

Environment variables

If you want your code to be transportable then build your absolute paths using environment variables. For example if you want a placeholder for someone's desktop you can use:

os.environ["USERPROFILE"]

or on linux/mac

os.environ["HOME"]

So using our example from before we can get the path for the images folder using:

if os.name == "nt": # Windows
    desktop = os.path.join(os.environ["USERPROFILE"], "Desktop")
else:
    desktop = os.path.join(os.environ["HOME"], "Desktop) 

full_path = os.path.join(desktop, "images")

which would give us <drive letter>:\Users\<name>\Desktop\images

Using the current file as a starting point

If your file is in a folder close to the python file you're running you can also access the folder the python file is in by using:

import os

python_file_folder = os.path.abspath(os.path.dirname(__file__))

and then go from there with os.path.join()'s. This is less ideal, but still works better than relative paths, because relative paths are based on where your terminal runs the file from, not where the python file your running is located. So your relative path could change on every run if you run the file from a different directory, but unless you move the python file the __file__ stays the same.

Extra resources

There are some extra details that can be found here or here

Kieran Wood
  • 1,297
  • 8
  • 15