Registry / ai-ml / pycocoevalcap

pycocoevalcap

JSON →
library1.2pypypi✓ verified 24d ago

pycocoevalcap provides Python 3 support for evaluating image captions using standard MS-COCO metrics (BLEU, METEOR, ROUGE-L, CIDEr, SPICE). It is derived from the original Python 2.7 coco-caption repository and depends on the COCO API. The latest version is 1.2, released in November 2020, indicating a maintenance-oriented release cadence.

pip install pycocoevalcap
INSTALL
IMPORT
SIG · PYCOCOEVALCAP
P
pycocoevalcap
ai-mlpythonv1.2
Install
5.3s avg
Import
429ms
Disk
191MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.438s · 191.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 5.3s · import 0.420s · 188MB
191MB installed
● package 191MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

COCOEvalCap
from pycocoevalcap.eval import COCOEvalCap
This class orchestrates the evaluation of multiple metrics.
Bleu
from pycocoevalcap.bleu.bleu import Bleu
Import individual metric scorers if you need granular control or specific metrics.
Meteor
from pycocoevalcap.meteor.meteor import Meteor
Import individual metric scorers if you need granular control or specific metrics.
Rouge
from pycocoevalcap.rouge.rouge import Rouge
Import individual metric scorers if you need granular control or specific metrics.
Cider
from pycocoevalcap.cider.cider import Cider
Import individual metric scorers if you need granular control or specific metrics.
Spice
from pycocoevalcap.spice.spice import Spice
Import individual metric scorers if you need granular control or specific metrics.

This quickstart demonstrates how to set up and run the evaluation using `COCOEvalCap`. It uses mocked ground truth and predicted caption data to illustrate the expected data structure. In a real application, you would load your ground truth annotations into a `pycocotools.coco.COCO` object and your prediction results into another `COCO` object (using `loadRes`). The `evaluate()` method then computes all standard metrics.

import json from pycocoevalcap.eval import COCOEvalCap # Mock ground truth and predicted captions data # In a real scenario, these would be loaded from JSON files # 'gts' should map image_id to a list of ground truth captions # 'res' should map image_id to a list of predicted captions gts_data = { "annotations": [ {"image_id": 1, "id": 101, "caption": "A man is riding a bicycle."}, {"image_id": 1, "id": 102, "caption": "A person on a bike on a street."}, {"image_id": 2, "id": 201, "caption": "Two dogs playing in the grass."}, {"image_id": 2, "id": 202, "caption": "Dogs are running on a lawn."} ] } res_data = [ {"image_id": 1, "caption": "A man cycling on a road.", "id": 301}, {"image_id": 2, "caption": "Two puppies in a field.", "id": 302} ] # To initialize COCOEvalCap, you need COCO objects for ground truth and results. # These COCO objects are typically created from JSON files matching the COCO format. # For a quickstart, we'll manually structure the data to match expected input. # The COCO object expects a dictionary with 'images' and 'annotations' keys. # We only need 'annotations' for caption evaluation. # Mock COCO objects (simplified for quickstart, actual COCO objects handle more fields) class MockCoco: def __init__(self, data): self.anns = {ann['id']: ann for ann in data.get('annotations', [])} self.imgToAnns = {} for ann in data.get('annotations', []): self.imgToAnns.setdefault(ann['image_id'], []).append(ann) def loadRes(self, res_json_or_list): # For simplicity, just store results. COCO.loadRes is more complex. res_anns = [] for r in res_json_or_list: # Assign a unique ID if not present, similar to COCO API behavior if 'id' not in r: r['id'] = max(self.anns.keys(), default=0) + len(res_anns) + 1 res_anns.append(r) res_coco = MockCoco({'annotations': res_anns}) return res_coco def getImgIds(self): return list(self.imgToAnns.keys()) def loadAnns(self, ids): return [self.anns[i] for i in ids] # Initialize Mock COCO objects # gts_coco_obj = COCO(gts_json_path) # In a real application gts_coco_obj = MockCoco(gts_data) # res_coco_obj = gts_coco_obj.loadRes(res_json_path) # In a real application res_coco_obj = gts_coco_obj.loadRes(res_data) eval_ids = gts_coco_obj.getImgIds() cocoEval = COCOEvalCap(gts_coco_obj, res_coco_obj, eval_ids) cocoEval.evaluate() print("Evaluation results:") for metric, score in cocoEval.eval.items(): print(f"{metric}: {score:.3f}")
Debug
Known issues
gotchaJava 1.8.0 is a mandatory runtime dependency for SPICE and the PTBTokenizer. Ensure Java is installed and properly configured in your environment PATH, or you may encounter `java.lang.UnsatisfiedLinkError` or `CalledProcessError` issues related to Java execution during evaluation.
fix
Install Java 1.8.0+ and verify it's accessible via your system's PATH. On Windows, adding `_JAVA_OPTIONS -Xmx1024M` to system variables might fix JVM memory errors.
affects: All versions
gotchaThe `pycocotools` dependency can be challenging to install, especially on Windows or if specific C/C++ build tools are missing. It often requires a C compiler (e.g., build tools for Visual Studio on Windows, or `gcc` on Linux/macOS) to compile its C extensions.
fix
Ensure you have the necessary C/C++ build tools for your operating system before running `pip install pycocoevalcap`. For Windows, this often means installing 'Desktop development with C++' from Visual Studio Installer.
affects: All versions
gotchaSPICE (Semantic Propositional Image Caption Evaluation) automatically downloads Stanford CoreNLP models on its first run. This process can fail due to network issues, incorrect permissions for caching, or Java environment problems, leading to 'Could not cache item for SPICE' or similar errors.
fix
Ensure stable internet connectivity for the initial download. Check write permissions for the `./spice/cache/` directory (or configure `CACHE_DIR` in `./spice/spice.py`). Verify Java is correctly installed and configured.
affects: All versions
gotchaThe CIDEr metric, due to its TF-IDF weighting, may return a score of 0 when evaluating only a single ground truth-prediction pair. It's designed for corpus-level evaluation.
fix
Evaluate CIDEr over a corpus of multiple image-caption pairs for meaningful scores. When testing individual pairs, be aware that a score of 0 might not indicate a complete mismatch, but rather a characteristic of the metric's design.
affects: All versions
gotchaUsers have reported issues with METEOR score computation, sometimes leading to `subprocess.CalledProcessError` or incorrect score aggregation, possibly due to changes in the underlying Java METEOR implementation or inconsistencies in data processing.
fix
Refer to GitHub issues for potential patches or workarounds (e.g., modifications to `meteor.py` as suggested in Issue #19). Ensure input data formats are strictly followed.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pycocoevalcap' OR ERROR: Could not find a version that satisfies the requirement pycocoevalcap
The `pycocoevalcap` package is often not directly installable via `pip` from PyPI without specifying its GitHub repository due to its dependencies or specific packaging.
fix
Install the package directly from its GitHub repository using `pip install "git+https://github.com/salaniz/pycocoevalcap.git"`.
ERROR: Could not build wheels for pycocotools (or similar C/C++ compilation errors for pycocotools) OR ModuleNotFoundError: No module named 'pycocotools._mask'
`pycocotools`, a core dependency of `pycocoevalcap`, requires compilation of C/C++ extensions, which fails if the necessary build tools (like Visual C++ Build Tools on Windows or `gcc`/`build-essential` on Linux/macOS) or `cython` are missing.
fix
Ensure `cython` is installed (`pip install cython`) and a C/C++ compiler is available on your system (e.g., `sudo apt-get install build-essential` on Ubuntu, Xcode Command Line Tools on macOS, or Visual C++ Build Tools for Windows). You might need to install `pycocotools` separately, potentially from a specific GitHub fork if `pip install pycocoevalcap` continues to fail.
subprocess.CalledProcessError: Command '['java', '-jar', ...]` returned non-zero exit status 1 (often during METEOR or SPICE computation) OR java.lang.UnsatisfiedLinkError
The METEOR and SPICE metrics rely on Java and Stanford CoreNLP. This error occurs if Java Development Kit (JDK) is not installed, not correctly configured in the system's PATH, or the required Java `.jar` files for METEOR/SPICE are missing or cannot be accessed by the Python subprocess.
fix
Install Java Development Kit (JDK 1.8.0 or later) and ensure the `java` executable is in your system's PATH. For SPICE, ensure adequate write permissions and internet access, as Stanford CoreNLP models are downloaded automatically the first time SPICE is evaluated.
Resource punkt not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('punkt')
The ROUGE-L metric, or other text processing components within `pycocoevalcap`, often utilize NLTK's `punkt` tokenizer, which is not automatically installed with the `nltk` package and must be downloaded separately.
fix
Run these commands in a Python interpreter or script once: `import nltk; nltk.download('punkt')`. Ensure your environment has write permissions to the NLTK data directory or specify a custom download directory.
Upgrade
Version history
1.2latest on PyPI · released Nov 18, 2020
Audit
Dependencies
pycocotoolsrequiredRequired for COCO API interaction and data structures.
Java 1.8.0requiredRequired for SPICE and PTBTokenizer components. Stanford CoreNLP will be downloaded automatically by SPICE.
Agent activity
16 hits · last 30 days
node
12
Perplexity
1
OpenAI (training)
1
Resources