3

I'm trying to train a mask r cnn algorithm for object detection. Right now I have 21 classes with the corresponding annotations but my task is to detect only 13 of them, the other 8 classes should be recognize as background.

For the CustomConfig code part I tried to run the code setting the number of classes for the background with this three combinations:

  • 1+13
  • 8+13
  • 9+13 But none of them worked.

class CustomConfig(Config):
    """Configuration for training on the custom  dataset.
    Derives from the base Config class and overrides some values.
    """
    # Give the configuration a recognizable name
    NAME = "object"

    # We use a GPU with 12GB memory, which can fit two images.
    # Adjust down if you use a smaller GPU.
    IMAGES_PER_GPU = 2

    # Number of classes (including background)
    NUM_CLASSES = 1 + 13
    # Number of training steps per epoch
    STEPS_PER_EPOCH = 10

    # Skip detections with < 85% confidence
    DETECTION_MIN_CONFIDENCE = 0.85

The error that came out:

---> 56  num_ids = [name_dict[a] for a in objects]
KeyError: 'Class20'    (this class should be in the background)

Regarding the def load custom function I only added the 13 classes that I want to detect:


class CustomDataset(utils.Dataset):

    def load_custom(self, dataset_dir, subset):
        """Load a subset of the Dog-Cat 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("object", 1, "01")
        self.add_class("object", 2, "02")
        self.add_class("object", 3, " 03")
        self.add_class("object", 4, "04")
        self.add_class("object", 5, "05")
        self.add_class("object", 6, "06")
        self.add_class("object", 7, "07")
        self.add_class("object", 8, "8")
        self.add_class("object", 9, "9")
        self.add_class("object", 10, "10")
        self.add_class("object", 11, "11")
        self.add_class("object", 12, "12")
        self.add_class("object", 13, "13")

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

        annotations1 = json.load(open(os.path.join(dataset_dir, "set_labelling.json")))
        new_dict = {}
        
        for item in annotations1:
          name = item['filename'], item['Layer']
          new_dict[name] = item
        annotations = list(new_dict.values()) # don't need the dict key

        # Add images
        for a in annotations:
            polygons = [r['shape_attributes'] for r in a['region']]
            objects = [s['region_attribute']['name'] for s in a['region']]
            #print(polygons)
            print("objects:",objects)
            name_dict = {"01": 1,
                          "02": 2,
                          "03": 3,
                          "04": 4,
                          "05": 5,
                          "06": 6,
                          "07": 7,
                          "08": 8,
                          "09": 9, 
                          "10": 10,
                          "11": 11, 
                          "12": 12, 
                          "13":13} 
           
            num_ids = [name_dict[a] for a in objects]
     
            
            print("numids",num_ids)
            image_path = os.path.join(dataset_dir, a['filename'])
            image = skimage.io.imread(image_path)
            height, width = image.shape[:2]

            self.add_image(
                "object",  ## for a single class just add the name here
                image_id=a['filename'],  # use file name as a unique image id
                path=image_path,
                width=width, height=height,
                polygons=polygons,
                num_ids=num_ids
                )


I would not to delete the background classes annotations in the json file, so how can I handle this error?

Dvd
  • 117
  • 3
  • 7

0 Answers0