code: def get_mscoco(
      root_dir,
      train_dir="train2014",
      train_anno="annotations/captions_train2014.json",
      val_dir="val2014",
      val_anno="annotations/captions_val2014.json",
      unk_threshold=5,
  ):
      """Return the training and validation datasets for MSCOCO.
  
      The datasets can be used by the iterator during training.
  
      A vocabulary is dynamically created based on all captions and is
      returned as members of the training and validation dataset objects.
      """
      train = MsCocoDataset(root_dir, train_dir, train_anno)
      val = MsCocoDataset(root_dir, val_dir, val_anno)
  
      # Create a vocabulary based on the captions from the training set only
      # (excluding the validation sets). This is common practice.
      captions = [ann["caption"] for ann in train.anns]
  
      # Filter out rare words as UNK
      word_counts = defaultdict(int)
      for c in captions:
          for w in split(c):
              word_counts[w] += 1
  
      # This vocabulary is needed in order to convert the words in the captions
      # to integer tokens. When generating captions during testing, these tokens
      # are mapped back to their corresponding words. Note that this vocabulary
      # sorted alphanumerically.
      vocab = {"<bos>": _bos, "<eos>": _eos, "<unk>": _unk}
      for w, count in sorted(word_counts.items()):
          if w not in vocab and count >= unk_threshold:
              vocab[w] = len(vocab)
  
      train.vocab = vocab
      val.vocab = vocab
  
      return train, val
  
file_path: chainer/examples/image_captioning/datasets.py
imports: from collections import defaultdict
  import os
  import numpy
  from PIL import Image
  from pycocotools.coco import COCO
  from chainer import dataset
  from chainer.dataset.convert import to_device
label_desc: Use image node_id 2 for calling a global function with example usage: get_mscoco(root_dir, train_dir, train_anno, val_dir, val_anno, unk_threshold) and returns: train,  val