Comments (3)
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.
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.
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)
- AttributeError: 'CopyPaste' object has no attribute '_to_dict'
- The example script is no use for my aerial image
- How do I use copy-paste-aug in instance segmentation HOT 1
- how to integrate with detectron2? HOT 1
- RandomScale of albumentations drops "bboxes" content in coco annotations HOT 1
- Torchvision Dataset Integration HOT 1
- Mosaic can't be used for Instance Segmentation?
- An error occurred while using Copy-Paste! HOT 2
- Facing issues with test dataset ../../datasets/coco/train2014/
- How to use copy-paste augmentation for YOLOX object detection
- How do I use this augment data? HOT 3
- Reproduce results in Paper HOT 3
- How to use copy-paste augmentation for YOLO5 object detection HOT 4
- copy paste to background image HOT 2
- How can I use the copy-paste augmentation without albumentation framework? HOT 1
- How To Visualize Augmented Images in As presented in Fig 2. HOT 1
- TypeError: __init__() takes from 3 to 5 positional arguments but 6 were given. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from copy-paste-aug.