<jupyter_start><jupyter_text>Computer Vision Nanodegree Project: Image Captioning---The Microsoft **C**ommon **O**bjects in **CO**ntext (MS COCO) dataset is a large-scale dataset for scene understanding.  The dataset is commonly used to train and benchmark object detection, segmentation, and captioning algorithms.![Sample Dog Output](images/coco-examples.jpg)You can read more about the dataset on the [website](http://cocodataset.org/home) or in the [research paper](https://arxiv.org/pdf/1405.0312.pdf).In this notebook, you will explore this dataset, in preparation for the project. Step 1: Initialize the COCO APIWe begin by initializing the [COCO API](https://github.com/cocodataset/cocoapi) that you will use to obtain the data.<jupyter_code>import os

from pycocotools.coco import COCO

# Initialize COCO API for instance annotations.
dataDir = os.path.join('..', '..', 'Data', 'cocoapi')
dataType = 'val2014'
instances_annFile = os.path.join(dataDir, 'annotations', 'instances_{}.json'.format(dataType))
coco = COCO(instances_annFile)

# Initialize COCO API for caption annotations.
captions_annFile = os.path.join(dataDir, 'annotations', 'captions_{}.json'.format(dataType))
coco_caps = COCO(captions_annFile)

# Get image ids.
ids = list(coco.anns.keys())<jupyter_output>loading annotations into memory...
Done (t=3.29s)
creating index...
index created!
loading annotations into memory...
Done (t=0.25s)
creating index...
index created!<jupyter_text>Step 2: Plot a Sample ImageNext, we plot a random image from the dataset, along with its five corresponding captions.  Each time you run the code cell below, a different image is selected.In the project, you will use this dataset to train your own model to generate captions from images!<jupyter_code>import numpy as np
import skimage.io as io
import matplotlib.pyplot as plt
%matplotlib inline

# Pick a random image and obtain the corresponding URL.
ann_id = np.random.choice(ids)
img_id = coco.anns[ann_id]['image_id']
img = coco.loadImgs(img_id)[0]
url = img['coco_url']

# Print URL and visualize corresponding image.
print(url)
I = io.imread(url)
plt.axis('off')
plt.imshow(I)
plt.show()

# Load and display captions.
annIds = coco_caps.getAnnIds(imgIds=img['id'])
anns = coco_caps.loadAnns(annIds)
coco_caps.showAnns(anns)<jupyter_output>http://images.cocodataset.org/val2014/COCO_val2014_000000151978.jpg
StarcoderdataJupyterStructuredCleanDedup