Giter Site home page Giter Site logo

Comments (3)

conradry avatar conradry commented on July 19, 2024

I assume for semantic segmentation that you have a single mask with all the classes. It's not the prettiest solution, but something like this should work:

def extract_bbox(mask):
    h, w = mask.shape
    yindices = np.where(np.any(mask, axis=0))[0]
    xindices = np.where(np.any(mask, axis=1))[0]
    if yindices.shape[0]:
        y1, y2 = yindices[[0, -1]]
        x1, x2 = xindices[[0, -1]]
        y2 += 1
        x2 += 1
    else:
        y1, x1, y2, x2 = 0, 0, 0, 0

    return (y1, x1, y2, x2)

def load_example(self, index):
    image = self.load_image(index) #some function to load your image (H, W, 3)
    mask = self.load_mask(index) #some function to load your mask (H, W)

    masks = []
    bboxes = []
    #split the mask into individual binary masks for each class
    for ix, value in enumerate(np.unique(mask)[1:]):
        masks.append(mask == value)
        bboxes.append(extract_bbox(mask == value)  + (value, ix))

    #pack outputs into a dict
    output = {
        'image': image,
        'masks': masks,
        'bboxes': bboxes
    }
        
    return self.transforms(**output)

Once you have the output from copy-paste and all the other augmentations, convert back to semantic mask.

output = dataset[index]
mask_classes = [b[-2] for b in output['bboxes']]
mask_indices = [b[-1] for b in output['bboxes']]

semantic_mask = np.zeros_like(output['masks'][0]).astype(np.long) #could be uint8 if fewer than 255 classes
for class, index in  zip(mask_classes, mask_indices)
    semantic_mask += output['masks'][index] * class

del output['masks']
output['mask'] = semantic_mask

You could also further split the semantic mask by connected components (using skimage.measure.label, then you could also use skimage.measure.regionprops to extract the bounding boxes).

from copy-paste-aug.

AndyChang666 avatar AndyChang666 commented on July 19, 2024

Thanks for your quick reply. However, the semantic segmentation task does not have a bounding box for ground truth images.
And I want to apply on cityscapes instead of coco datasets. Then how should I modify these codes?

from copy-paste-aug.

conradry avatar conradry commented on July 19, 2024

Bounding boxes are easy to extract from a ground truth segmentation mask. That's what this line is for: bboxes.append(extract_bbox(mask == value) + (value, ix)).

from copy-paste-aug.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.