matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.64k stars 11.7k forks source link

Error while loading my own dataset(images) of multiple classes #1003

Open aakashkodes opened 6 years ago

aakashkodes commented 6 years ago

I am getting an error while I am trying to load my own dataset.

An example of my json annotations for one of the images looks like this. "RectangleCircleCenter16.jpg40433":{"fileref":"","size":40433,"filename":"RectangleCircleCenter16.jpg","base64_img_data":"","file_attributes":{},"regions":{"0":{"shape_attributes":{"name":"polygon","all_points_x":[469,444,440,455,483,562,609,639,685,728,766,821,824,803,762,716,671,632,565,489,469],"all_points_y":[286,349,418,471,519,572,580,583,580,559,531,444,381,305,238,210,199,194,202,251,286]},"region_attributes":{"class_ids":"Circle"}},"1":{"shape_attributes":{"name":"polygon","all_points_x":[345,346,821,817,345],"all_points_y":[261,628,626,258,261]},"region_attributes":{"class_ids":"Rectangle"}}}}}

My load function looks like this. class BalloonConfig(Config): """Configuration for training on the toy dataset. Derives from the base Config class and overrides some values. """

Give the configuration a recognizable name

NAME = "balloon"

We use a GPU with 12GB memory, which can fit two images.

Adjust down if you use a smaller GPU.

IMAGES_PER_GPU = 1

Number of classes (including background)

NUM_CLASSES = 1 + 2 # Background + balloon

Number of training steps per epoch

STEPS_PER_EPOCH = 100

Skip detections with < 90% confidence

DETECTION_MIN_CONFIDENCE = 0.9 ############################################################

Dataset ############################################################

class BalloonDataset(utils.Dataset):

def load_balloon(self, dataset_dir, subset): """Load a subset of the Balloon dataset. dataset_dir: Root directory of the dataset. subset: Subset to load: train or val """

Add classes. We have only one class to add.

self.add_class("balloon", 1, "Circle")
self.add_class("balloon", 2, "Rectangle")

# Train or validation dataset?
assert subset in ["train", "val"]
dataset_dir = os.path.join(dataset_dir, subset)

# Load annotations
# VGG Image Annotator saves each image in the form:
# { 'filename': '28503151_5b5b7ec140_b.jpg',
#   'regions': {
#       '0': {
#           'region_attributes': {},
#           'shape_attributes': {
#               'all_points_x': [...],
#               'all_points_y': [...],
#               'name': 'polygon'}},
#       ... more regions ...
#   },
#   'size': 100202
# }
# We mostly care about the x and y coordinates of each region
annotations = json.load(open(os.path.join(dataset_dir, "via_region_data.json")))
annotations = list(annotations.values())  # don't need the dict keys

# The VIA tool saves images in the JSON even if they don't have any
# annotations. Skip unannotated images.
annotations = [a for a in annotations if a['regions']]

# Add images
for a in annotations:
    # Get the x, y coordinaets of points of the polygons that make up
    # the outline of each object instance. There are stores in the
    # shape_attributes (see json format above)
    polygons = [r['shape_attributes'] for r in a['regions'].values()]
    class_ids = [s['region_attributes'] for s in a['regions'].values()]
    # load_mask() needs the image size to convert polygons to masks.
    # Unfortunately, VIA doesn't include it in JSON, so we must read
    # the image. This is only managable since the dataset is tiny.
    image_path = os.path.join(dataset_dir, a['filename'])
    image = skimage.io.imread(image_path)
    height, width = image.shape[:2]

    self.add_image(
        "balloon",
        image_id=a['filename'],  # use file name as a unique image id
        path=image_path,
        width=width, height=height,
        polygons=polygons,
        class_ids=class_ids)

def load_mask(self, image_id): """Generate instance masks for an image. Returns: masks: A bool array of shape [height, width, instance count] with one mask per instance. class_ids: a 1D array of class IDs of the instance masks. """

If not a balloon dataset image, delegate to parent class.

image_info = self.image_info[image_id]
if image_info["source"] != "balloon":
    return super(self.__class__, self).load_mask(image_id)

# Convert polygons to a bitmap mask of shape
# [height, width, instance_count]
info = self.image_info[image_id]
mask = np.zeros([info["height"], info["width"], len(info["polygons"])],
                dtype=np.uint8)
for i, p in enumerate(info["polygons"]):
    # Get indexes of pixels inside the polygon and set them to 1
    rr, cc = skimage.draw.polygon(p['all_points_y'], p['all_points_x'])
    mask[rr, cc, i] = 1

# Return mask, and array of class IDs of each instance. Since we have
# one class ID only, we return an array of 1s
return mask, info['class_ids']

The error i get is mentioned below.

C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\tensorflow\python\ops\gradients_impl.py:97: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory. "Converting sparse IndexedSlices to a dense Tensor of unknown shape. " Epoch 1/30 C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\skimage\transform_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images. warn("Anti-aliasing will be enabled by default in skimage 0.15 to " ERROR:root:Error processing image {'id': 'RectangleCircle6.jpg', 'source': 'balloon', 'path': 'C:\Windows\System32\Mask_RCNN\samples\DrawingsPoli\Object\train\RectangleCircle6.jpg', 'width': 1169, 'height': 827, 'polygons': [{'name': 'polygon', 'all_points_x': [479, 429, 394, 369, 342, 334, 337, 384, 484, 568, 602, 627, 636, 637, 621, 592, 568, 530, 493, 479], 'all_points_y': [303, 314, 331, 358, 398, 443, 514, 578, 615, 586, 557, 514, 478, 452, 412, 374, 344, 321, 306, 303]}, {'name': 'polygon', 'all_points_x': [479, 481, 836, 836, 479], 'all_points_y': [216, 466, 463, 221, 216]}], 'class_ids': [{'class_ids': 'Circle'}, {'class_ids': 'Rectangle'}]} Traceback (most recent call last): File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1717, in data_generator use_mini_mask=config.USE_MINI_MASK) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1272, in load_image_gt class_ids = class_ids[_idx] TypeError: only integer scalar arrays can be converted to a scalar index C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\skimage\transform_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images. warn("Anti-aliasing will be enabled by default in skimage 0.15 to " ERROR:root:Error processing image {'id': 'Circle7.jpg', 'source': 'balloon', 'path': 'C:\Windows\System32\Mask_RCNN\samples\DrawingsPoli\Object\train\Circle7.jpg', 'width': 1169, 'height': 827, 'polygons': [{'name': 'polygon', 'all_points_x': [404, 463, 536, 642, 711, 761, 798, 755, 692, 622, 550, 479, 419, 383, 381, 395, 404], 'all_points_y': [317, 249, 212, 214, 252, 310, 416, 534, 587, 615, 619, 592, 542, 459, 375, 332, 317]}], 'class_ids': [{'class_ids': 'Circle'}]} Traceback (most recent call last): File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1717, in data_generator use_mini_mask=config.USE_MINI_MASK) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1272, in load_image_gt class_ids = class_ids[_idx] TypeError: only integer scalar arrays can be converted to a scalar index C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\skimage\transform_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images. warn("Anti-aliasing will be enabled by default in skimage 0.15 to " ERROR:root:Error processing image {'id': 'Circle2.jpg', 'source': 'balloon', 'path': 'C:\Windows\System32\Mask_RCNN\samples\DrawingsPoli\Object\train\Circle2.jpg', 'width': 1169, 'height': 827, 'polygons': [{'name': 'polygon', 'all_points_x': [485, 528, 557, 594, 625, 699, 734, 733, 725, 697, 636, 571, 532, 497, 469, 449, 442, 442, 460, 485], 'all_points_y': [308, 280, 273, 270, 276, 318, 394, 423, 462, 512, 554, 560, 550, 530, 506, 473, 432, 368, 337, 308]}], 'class_ids': [{'class_ids': 'Circle'}]} Traceback (most recent call last): File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1717, in data_generator use_mini_mask=config.USE_MINI_MASK) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1272, in load_image_gt class_ids = class_ids[_idx] TypeError: only integer scalar arrays can be converted to a scalar index C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\skimage\transform_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images. warn("Anti-aliasing will be enabled by default in skimage 0.15 to " ERROR:root:Error processing image {'id': 'RectangleCircleCenter16.jpg', 'source': 'balloon', 'path': 'C:\Windows\System32\Mask_RCNN\samples\DrawingsPoli\Object\train\RectangleCircleCenter16.jpg', 'width': 1169, 'height': 827, 'polygons': [{'name': 'polygon', 'all_points_x': [469, 444, 440, 455, 483, 562, 609, 639, 685, 728, 766, 821, 824, 803, 762, 716, 671, 632, 565, 489, 469], 'all_points_y': [286, 349, 418, 471, 519, 572, 580, 583, 580, 559, 531, 444, 381, 305, 238, 210, 199, 194, 202, 251, 286]}, {'name': 'polygon', 'all_points_x': [345, 346, 821, 817, 345], 'all_points_y': [261, 628, 626, 258, 261]}], 'class_ids': [{'class_ids': 'Circle'}, {'class_ids': 'Rectangle'}]} Traceback (most recent call last): File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1717, in data_generator use_mini_mask=config.USE_MINI_MASK) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1272, in load_image_gt class_ids = class_ids[_idx] TypeError: only integer scalar arrays can be converted to a scalar index C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\skimage\transform_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images. warn("Anti-aliasing will be enabled by default in skimage 0.15 to " ERROR:root:Error processing image {'id': 'D2Rectangle4.jpg', 'source': 'balloon', 'path': 'C:\Windows\System32\Mask_RCNN\samples\DrawingsPoli\Object\train\D2Rectangle4.jpg', 'width': 1169, 'height': 827, 'polygons': [{'name': 'polygon', 'all_points_x': [469, 469, 604, 604, 469], 'all_points_y': [371, 582, 582, 366, 371]}, {'name': 'polygon', 'all_points_x': [492, 493, 704, 706, 492], 'all_points_y': [242, 385, 385, 241, 242]}], 'class_ids': [{'class_ids': 'Rectangle'}, {'class_ids': 'Rectangle'}]} Traceback (most recent call last): File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1717, in data_generator use_mini_mask=config.USE_MINI_MASK) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1272, in load_image_gt class_ids = class_ids[_idx] TypeError: only integer scalar arrays can be converted to a scalar index C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\skimage\transform_warps.py:110: UserWarning: Anti-aliasing will be enabled by default in skimage 0.15 to avoid aliasing artifacts when down-sampling images. warn("Anti-aliasing will be enabled by default in skimage 0.15 to " ERROR:root:Error processing image {'id': 'Rectangle6.jpg', 'source': 'balloon', 'path': 'C:\Windows\System32\Mask_RCNN\samples\DrawingsPoli\Object\train\Rectangle6.jpg', 'width': 1169, 'height': 827, 'polygons': [{'name': 'polygon', 'all_points_x': [459, 461, 688, 687, 459], 'all_points_y': [264, 457, 459, 261, 264]}], 'class_ids': [{'class_ids': 'Rectangle'}]} Traceback (most recent call last): File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1717, in data_generator use_mini_mask=config.USE_MINI_MASK) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1272, in load_image_gt class_ids = class_ids[_idx] TypeError: only integer scalar arrays can be converted to a scalar index Traceback (most recent call last): File "balloon.py", line 361, in train(model) File "balloon.py", line 196, in train layers='heads') File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 2381, in train use_multiprocessing=True, File "C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\keras\engine\training.py", line 1415, in fit_generator initial_epoch=initial_epoch) File "C:\ProgramData\Anaconda3\envs\MaskRCNN\lib\site-packages\keras\engine\training_generator.py", line 177, in fit_generator generator_output = next(output_generator) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1717, in data_generator use_mini_mask=config.USE_MINI_MASK) File "C:\Windows\System32\Mask_RCNN\mrcnn\model.py", line 1272, in load_image_gt class_ids = class_ids[_idx] TypeError: only integer scalar arrays can be converted to a scalar index

bat3a commented 5 years ago

i got this issue too! any idea how what to fix?

koukouzasg commented 5 years ago

Have the same error, please post your solution if possible @aakashkodes

errorHandling commented 4 years ago

I too have the same error