Inspect and visualize data loading and pre-processing code.
import os
import sys
import itertools
import math
import logging
import json
import re
import random
from collections import OrderedDict
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.lines as lines
from matplotlib.patches import Polygon
# Root directory of the project
ROOT_DIR = os.path.abspath("../../")
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
from mrcnn import visualize
from mrcnn.visualize import display_images
import mrcnn.model as modellib
from mrcnn.model import log
%matplotlib inline
Using TensorFlow backend.
Run one of the code blocks below to import and load the configurations to use.
# Run one of the code blocks
# Shapes toy dataset
# import shapes
# config = shapes.ShapesConfig()
# MS COCO Dataset
import coco
config = coco.CocoConfig()
COCO_DIR = "path to COCO dataset" # TODO: enter value here
# Load dataset
if config.NAME == 'shapes':
dataset = shapes.ShapesDataset()
dataset.load_shapes(500, config.IMAGE_SHAPE[0], config.IMAGE_SHAPE[1])
elif config.NAME == "coco":
dataset = coco.CocoDataset()
dataset.load_coco(COCO_DIR, "train")
# Must call before using the dataset
dataset.prepare()
print("Image Count: {}".format(len(dataset.image_ids)))
print("Class Count: {}".format(dataset.num_classes))
for i, info in enumerate(dataset.class_info):
print("{:3}. {:50}".format(i, info['name']))
loading annotations into memory... Done (t=11.93s) creating index... index created! Image Count: 82081 Class Count: 81 0. BG 1. person 2. bicycle 3. car 4. motorcycle 5. airplane 6. bus 7. train 8. truck 9. boat 10. traffic light 11. fire hydrant 12. stop sign 13. parking meter 14. bench 15. bird 16. cat 17. dog 18. horse 19. sheep 20. cow 21. elephant 22. bear 23. zebra 24. giraffe 25. backpack 26. umbrella 27. handbag 28. tie 29. suitcase 30. frisbee 31. skis 32. snowboard 33. sports ball 34. kite 35. baseball bat 36. baseball glove 37. skateboard 38. surfboard 39. tennis racket 40. bottle 41. wine glass 42. cup 43. fork 44. knife 45. spoon 46. bowl 47. banana 48. apple 49. sandwich 50. orange 51. broccoli 52. carrot 53. hot dog 54. pizza 55. donut 56. cake 57. chair 58. couch 59. potted plant 60. bed 61. dining table 62. toilet 63. tv 64. laptop 65. mouse 66. remote 67. keyboard 68. cell phone 69. microwave 70. oven 71. toaster 72. sink 73. refrigerator 74. book 75. clock 76. vase 77. scissors 78. teddy bear 79. hair drier 80. toothbrush
Load and display images and masks.
# Load and display random samples
image_ids = np.random.choice(dataset.image_ids, 4)
for image_id in image_ids:
image = dataset.load_image(image_id)
mask, class_ids = dataset.load_mask(image_id)
visualize.display_top_masks(image, mask, class_ids, dataset.class_names)
Rather than using bounding box coordinates provided by the source datasets, we compute the bounding boxes from masks instead. This allows us to handle bounding boxes consistently regardless of the source dataset, and it also makes it easier to resize, rotate, or crop images because we simply generate the bounding boxes from the updates masks rather than computing bounding box transformation for each type of image transformation.
# Load random image and mask.
image_id = random.choice(dataset.image_ids)
image = dataset.load_image(image_id)
mask, class_ids = dataset.load_mask(image_id)
# Compute Bounding box
bbox = utils.extract_bboxes(mask)
# Display image and additional stats
print("image_id ", image_id, dataset.image_reference(image_id))
log("image", image)
log("mask", mask)
log("class_ids", class_ids)
log("bbox", bbox)
# Display image and instances
visualize.display_instances(image, bbox, mask, class_ids, dataset.class_names)
image_id 74886 http://cocodataset.org/#explore?id=118535 image shape: (375, 500, 3) min: 0.00000 max: 255.00000 mask shape: (375, 500, 5) min: 0.00000 max: 1.00000 class_ids shape: (5,) min: 1.00000 max: 35.00000 bbox shape: (5, 4) min: 1.00000 max: 329.00000