Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to train ssd mobilenetv2 using negative samples #195

Open
mepl432 opened this issue Sep 13, 2024 · 1 comment
Open

how to train ssd mobilenetv2 using negative samples #195

mepl432 opened this issue Sep 13, 2024 · 1 comment

Comments

@mepl432
Copy link

mepl432 commented Sep 13, 2024

Hello,

Looking for info on how to train SSD MobileNetv2 model with negative samples. How is 'BACKGROUND' label handled for negative samples.

Here is my annotation xml for pascal voc dataset for one negative sample

<annotation>
  <folder>negative_samples</folder>
  <filename>image1.jpg</filename>
  <size>
    <width>640</width>
    <height>480</height>
    <depth>3</depth>
  </size>
  <segmented>0</segmented>
  <!-- No object tags -->
</annotation>
@eltonfernando
Copy link

eltonfernando commented Oct 2, 2024

in vision/datasets/voc_dataset.py
create new method

def _gen_random_background(self):
 return (np.array([[10, 30, 40, 50]], dtype=np.float32), np.array([0], dtype=np.int64), np.array([0], dtype=np.uint8))

change method def _get_annotation(self, image_id): loaed anotation, before return add

if len(boxes) == 0:
  return self._gen_random_background()

this should be the result

 def _get_annotation(self, image_id):
        annotation_file = self.root / f"Annotations/{image_id}.xml"
        objects = ET.parse(annotation_file).findall("object")
        if len(objects) == 0:
            return self._gen_random_background()
            raise Exception(f"xml sem box {annotation_file}")
        boxes = []
        labels = []
        is_difficult = []
        for object in objects:
            class_name = object.find("name").text.lower().strip()
            # we're only concerned with clases in our list
            if class_name in self.class_dict:
                bbox = object.find("bndbox")

                # VOC dataset format follows Matlab, in which indexes start from 0
                x1 = float(bbox.find("xmin").text) - 1
                y1 = float(bbox.find("ymin").text) - 1
                x2 = float(bbox.find("xmax").text) - 1
                y2 = float(bbox.find("ymax").text) - 1
                boxes.append([x1, y1, x2, y2])

                labels.append(self.class_dict[class_name])
                is_difficult_str = object.find("difficult").text
                is_difficult.append(int(is_difficult_str) if is_difficult_str else 0)

        if len(boxes) == 0:
            return self._gen_random_background()
        return (np.array(boxes, dtype=np.float32), np.array(labels, dtype=np.int64), np.array(is_difficult, dtype=np.uint8))

    def _gen_random_background(self):
        return (np.array([[10, 30, 40, 50]], dtype=np.float32), np.array([0], dtype=np.int64), np.array([0], dtype=np.uint8))

This works for training with background image, let me know if your model improved with this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants