matterport / Mask_RCNN

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

I would like to ask you a multi-class related question. #2316

Open moonplay85 opened 4 years ago

moonplay85 commented 4 years ago

TensorFlow 2.2 cuda 10.1 keras 2.3.1

I am going to multi-class using a balloon file. For 1 class, there is no problem. However, there is a problem when trying to detect more than two multi class. If you have any code with multi-class or any part that needs to be modified, please let me know. The modified part is as follows.

class levelDataset(utils.Dataset):

def load_level(self, dataset_dir, subset):
    """Load a subset of the level dataset.
    dataset_dir: Root directory of the dataset.
    subset: Subset to load: train or val
    """
    # Add classes according to the numbe of classes required to detect
    self.add_class("level", 1, "water")
    self.add_class("level", 2, "slope")
    self.add_class("level", 3, "bridge")
    self.add_class("level", 4, "water_side")

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

    # Load annotations
    # VGG Image Annotator (up to version 1.6) 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
    # Note: In VIA 2.0, regions was changed from a dict to a list.
    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. These are stores in the
        # shape_attributes (see json format above)
        # The if condition is needed to support VIA versions 1.x and 2.x.
        polygons = [r['shape_attributes'] for r in a['regions']]
        #labelling each class in the given image to a number

        level = [s['region_attributes'] for s in a['regions']]

        num_ids=[]
        #Add the classes according to the requirement
        for n in level:
            try:
                if n['label']=='water':
                    num_ids.append(1)
                elif n['label']=='slope':
                    num_ids.append(2)
                elif n['label']=='bridge':
                    num_ids.append(3)
                elif n['label']=='water_side':
                    num_ids.append(4)    
            except:
                pass
shriadke commented 4 years ago

Hi, Not sure whether you have done this or not. But you need to specify number of classes in the config class.

NUM_CLASSES = 1 + n (no of classes) # Background + class1+ class2+ and so on....

You can refer to the "shapes.py" sample example in the same folder. Feel free to post if it doesn't help.

cheers.

bhomik77luthra commented 4 years ago

I have put 1+6 classes as NUM_CLASSES, but I am getting the error :

ValueError: Error when checking input: expected input_image_meta to have shape (None, 19) but got array with shape (1, 18)