DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

1 1 1 1 1

CocoCaptions in PyTorch (3)

Buy Me a Coffee

*Memos:

  • My post explains CocoCaptions() using train2014 with captions_train2014.json, instances_train2014.json and person_keypoints_train2014.json, val2014 with captions_val2014.json, instances_val2014.json and person_keypoints_val2014.json and test2017 with image_info_test2014.json, image_info_test2015.json and image_info_test-dev2015.json.
  • My post explains CocoCaptions() using train2017 with captions_train2017.json, instances_train2017.json and person_keypoints_train2017.json, val2017 with captions_val2017.json, instances_val2017.json and person_keypoints_val2017.json and test2017 with image_info_test2017.json and image_info_test-dev2017.json.
  • My post explains CocoDetection() using train2014 with captions_train2014.json, instances_train2014.json and person_keypoints_train2014.json, val2014 with captions_val2014.json, instances_val2014.json and person_keypoints_val2014.json and test2017 with image_info_test2014.json, image_info_test2015.json and image_info_test-dev2015.json.
  • My post explains CocoDetection() using train2017 with captions_train2017.json, instances_train2017.json and person_keypoints_train2017.json, val2017 with captions_val2017.json, instances_val2017.json and person_keypoints_val2017.json and test2017 with image_info_test2017.json and image_info_test-dev2017.json.
  • My post explains CocoDetection() using train2017 with stuff_train2017.json, val2017 with stuff_val2017.json, stuff_train2017_pixelmaps with stuff_train2017.json, stuff_val2017_pixelmaps with stuff_val2017.json, panoptic_train2017 with panoptic_train2017.json, panoptic_val2017 with panoptic_val2017.json and unlabeled2017 with image_info_unlabeled2017.json.
  • My post explains MS COCO.

CocoCaptions() can use MS COCO dataset as shown below. *This is for train2017 with stuff_train2017.json, val2017 with stuff_val2017.json, stuff_train2017_pixelmaps with stuff_train2017.json, stuff_val2017_pixelmaps with stuff_val2017.json, panoptic_train2017 with panoptic_train2017.json, panoptic_val2017 with panoptic_val2017.json and unlabeled2017 with image_info_unlabeled2017.json:

from torchvision.datasets import CocoCaptions

stf_train2017_data = CocoCaptions(
    root="data/coco/imgs/train2017",
    annFile="data/coco/anns/stuff_trainval2017/stuff_train2017.json"
)

stf_val2017_data = CocoCaptions(
    root="data/coco/imgs/val2017",
    annFile="data/coco/anns/stuff_trainval2017/stuff_val2017.json"
)

len(stf_train2017_data), len(stf_val2017_data)
# (118287, 5000)

pms_stf_train2017_data = CocoCaptions(
    root="data/coco/anns/stuff_trainval2017/stuff_train2017_pixelmaps",
    annFile="data/coco/anns/stuff_trainval2017/stuff_train2017.json"
)

pms_stf_val2017_data = CocoCaptions(
    root="data/coco/anns/stuff_trainval2017/stuff_val2017_pixelmaps",
    annFile="data/coco/anns/stuff_trainval2017/stuff_val2017.json"
)

len(pms_stf_train2017_data), len(pms_stf_val2017_data)
# (118287, 5000)

# pan_train2017_data = CocoCaptions(
#     root="data/coco/anns/panoptic_trainval2017/panoptic_train2017",
#     annFile="data/coco/anns/panoptic_trainval2017/panoptic_train2017.json"
# ) # Error

# pan_val2017_data = CocoCaptions(
#     root="data/coco/anns/panoptic_trainval2017/panoptic_val2017",
#     annFile="data/coco/anns/panoptic_trainval2017/panoptic_val2017.json"
# ) # Error

unlabeled2017_data = CocoCaptions(
    root="data/coco/imgs/unlabeled2017",
    annFile="data/coco/anns/unlabeled2017/image_info_unlabeled2017.json"
)

len(unlabeled2017_data)
# 123403

stf_train2017_data[2] # Error

stf_train2017_data[47] # Error

stf_train2017_data[64] # Error

stf_val2017_data[2] # Error

stf_val2017_data[47] # Error

stf_val2017_data[64] # Error

pms_stf_train2017_data[2] # Error

pms_stf_train2017_data[47] # Error

pms_stf_train2017_data[64] # Error

pms_stf_val2017_data[2] # Error

pms_stf_val2017_data[47] # Error

pms_stf_val2017_data[64] # Error

unlabeled2017_data[2]
# (<PIL.Image.Image image mode=RGB size=640x427>, [])

unlabeled2017_data[47]
# (<PIL.Image.Image image mode=RGB size=428x640>, [])

unlabeled2017_data[64]
# (<PIL.Image.Image image mode=RGB size=640x480>, [])

import matplotlib.pyplot as plt

def show_images(data, ims, main_title=None):
    file = data.root.split('/')[-1]
    fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(14, 8))
    fig.suptitle(t=main_title, y=0.9, fontsize=14)
    for i, axis in zip(ims, axes.ravel()):
        if not data[i][1]:
            im, _ = data[i]
            axis.imshow(X=im)
    fig.tight_layout()
    plt.show()

ims = (2, 47, 64)

show_images(data=unlabeled2017_data, ims=ims,
            main_title="unlabeled2017_data")
Enter fullscreen mode Exit fullscreen mode

Image description

Please leave your appreciation by commenting on this post!

Okay, let's go.

Happy coding ❤️

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay