How can i load coco caption data set using prebuilt tools?

I want to load coco caption data set.
How can i install the coco caption api in here(https://github.com/pdollar/cocoa)?
What i want to do is to run the code below without causing an import error.

import torchvision.datasets as dset
import torchvision.transforms as transforms

cap = dset.CocoCaptions(root = '../data/train2014',
                        annFile = '../data/annotations/captions_train2014.json',
                        transform=transforms.ToTensor())

print('Number of samples: ', len(cap))
img, target = cap[3] # load 4th sample

print("Image Size: ", img.size())
print(target)

I have cloned the coco caption api repo to several directories. However, when i ran the code above, I got an import error such as ImportError: No module named pycocotools.coco. To solve this problem, I ran the code above inside the coco/PythonAPI directory, but i received another import error as below.

/Users/yunjey/JupyterProjects/kwtl-pytorch/coco/PythonAPI/pycocotools/coco.py in <module>()
     53 import copy
     54 import itertools
---> 55 from . import mask as maskUtils
     56 import os
     57 from collections import defaultdict

/Users/yunjey/JupyterProjects/kwtl-pytorch/coco/PythonAPI/pycocotools/mask.py in <module>()
      1 __author__ = 'tsungyi'
      2 
----> 3 import pycocotools._mask as _mask
      4 
      5 # Interface for manipulating masks stored in RLE format.

ImportError: No module named _mask

Is there a simple way to install the coco api to solve this problem? Or, which directory should i clone the coco api repo?

I have solved this problem by running make inside the coco/PythonAPI. Still remaining problem is that i should run the code inside coco/PythonAPI, this is inconvenient. (from pycocotools.coco import COCO)

You’d better provide your python version, since import behaves differently in python2 and python3

And I highly recommend you to try out the importlib package, since it behaves more naturally as far as I’m concerned.

Thanks for your reply. I am using Python 2.7.

  • The first problem you came across is the same with https://github.com/pdollar/coco/issues/14, which is caused by forgetting make.(The cython was not compiled.)

  • The second problem is that this package is not in the path your python script would search in.

    1. You may solve this running the setup.py in PythonAPI folder to copy this package to your pip folder.
    2. Or you can add such code in your script```
      import sys
      sys.path.append(“The path of PythonAPI folder”)
1 Like

Thank you very much! Your reply helped a lot.

Alternatively, you could run python setup.py install, and the tools should be available from all scripts without a need to modify sys.path.

It still fails for me. these are the following steps I took

$ git clone https://github.com/pdollar/coco.git
$ cd coco/PythonAPI
$ make
$python setup.py install

I open a new notebook in AWS SageMaker in the PythonAPI directory and tried to import pycocotools, it succeeded.
Next, I tried “from pycocotools.coco import COCO” and got the “No module named _mask” output. I have done everything in the comments above and I still fail. The only difference I notice is that I’m using SageMaker . should that be the issue? or if its something else please let me know!