Skip to content

Warning

When use PreImutils VOC segmentation it's easier to put your data in below pattern

.
└── ${YOUR_DATASET_DIR}
    ├── ImageSets
    │   └── Segmentation
    │       └── train.txt
    │       └── val.txt
    │       └── trainval.txt
    ├── JPEGImages
    ├── SegmentationClass
    ├── SegmentationClassRaw
    ├── SegmentationObject
    └── labelmap.txt

If you have custom dataset just use below method

from preimutils.segmentations.voc import utils


utils.custom_to_voc('YOUR_MASKS_DIR','YOUR_IMAGES_DIR','TARGET_TO_SAVE_VOC_DS')

preimutils.segmentation.voc.utils

Modules

preimutils.segmentations.voc.utils
custom_to_voc(masks_dir, images_dir, target_dir)

Convert your custom dataset to normal voc format

Args:

masks_dir (str): your masks path.
images_dir (str): your images path.

Returns:

(list) : unique colors from your masks
decode_segmap(label_mask, class_color, plot=False)

Decode segmentation class labels into a color image

Args:

label_mask (np.ndarray): an (M,N) array of integer values denoting
    the class label at each spatial location.
plot (bool, optional): whether to show the resulting color image
    in a figure.

Returns:

(np.ndarray, optional): the resulting decoded color image.
encode_segmap(mask, class_color)

Encode segmentation label images as pascal classes

Args:

mask (np.ndarray): raw segmentation label image of dimension
    (M, N, 3), in which the Pascal classes are encoded as colours.
class_color(list): class colors

Returns:

(np.ndarray): class map with dimensions (M,N), where the value at
a given location is the integer denoting the class index.
export_path_count_for_each_label(color_label, images_dir, masks_dir)

Get statistics of dataset with their labels with their mask and images files path

Args:

xmls_dir: all xmls file directory.
images_dir: your images directory.
color_label:[(r,g,b):object1,(r,g,b):'object2',...,(r,g,b):'objectN']

Return:

dict{   label1: {
    count:
    masks_paths:[]
    images_paths:[]
            },
            ...,
labelN: {
    count:
    masks_paths:[]
    images_paths:[]
            }
}
find_image_from_mask(mask, images_dir)

Export image path from the mask file if your image and mask names are same

Args:

mask: single mask.png file path.
images_dir: your images path.

Returns:

image path : path of the input mask  -> string.
find_maxmin_size_images(images_dir)

Export the maximum and minimum size of the dataset images

Args:

images_dir: your images path.

Returns:

image path : path of the input mask  -> string.
unique_label_from_masks(masks_dir)

get the unique colors(classes) from your masks

Args:

masks_dir:(str) your masks path.

Returns:

unique_colors : unique colors from your masks  -> list.
utils

preimutils.segmentation.voc.Dataset

Class

class preimutils.segmentations.voc.Dataset(dataset_dir)

Get dataset voc paths directly and some methods for work with it.

├── ImageSets
│   └── Segmentation
├── JPEGImages
├── SegmentationClass
├── SegmentationClassRaw
└── SegmentationObject

Attributes:

masks_dir: SegmentationClass directory path
images_dir: JPEGImages directory paths.
segmentations_object_dir : SegmentationObject directory path
label_map_path = labelmap.txt path
check_valid_dataset(self)

Check for all masks images if there isn't related mask image print the work image path If image not exist raise ValueError

Args:

None

ValueError :

if image that you want not exists

Returns:

None
seprate_dataset(self, shuffle=False, valid_persent=0.25, test_persent=None, save=True)

Seprate dataset to train.txt,trainval.txt,val.txt

Args:

valid_persentage:(float), should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the validation split.
 If None,it will be set to 0.25.
test_persentage: (float), should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split.
 If None, that means you don't have test dataset just have train validation.
save :(bool), If True dataset save train.txt, trainval.txt, val.txt , if test_present exist test.txt

Returns:

None

preimutils.segmentation.voc.LabelMap

Class

class preimutils.segmentations.voc.LabelMap(label_map_path)

A class on to work and handel labelmap.txt classes

Attributes:

None

Args:

label_map_path (:obj:str): you should have a txt file like this

    object1:0,0,0::
    object2:128,0,0::
    object3:0,128,0::
    objectN:128,128,0::

masks_dir (str): annotations files paths.
images_dir (str) :images files paths.
color_label(self)

Export color_label dict

Args:

None

Returns:

a dict of label:color
{(r,g,b):label1,(r,g,b):labelN}
color_list(self)

Export color_list in their saving order

Args:

None

Returns:

list of colors
{(r,g,b),(rN,gN,bN)}
label_color(self)

Export label_color dict

Args:

None

Returns:

a dict of label:color
{label1:(r,g,b),labelN:(r,g,b)}

preimutils.segmentation.voc.SegmentationAug

class preimutils.segmentations.voc.SegmentationAug(label_map_path, masks_dir, images_dir)

A wrapper class on albumentations package to work on voc segmentation format easily

Attributes:

mask_dir: masks files paths.
images_dir: images files paths.

Args:

label_map_path (:obj:str): you should have a txt file like this
    object1:0,0,0::
    object2:128,0,0::
    object3:0,128,0::
    object4:128,128,0::
    object5:0,0,128::
    objectN:128,0,128::
masks_dir (str): annotations files paths.
images_dir (str) :images files paths.
augment_image(self, mask_path, quantity, resize=False, width=0, height=0)

augmentation for one picture depend on quantity that you get for it if your image and mask names are same save your aug image in your dataset path with the following pattern aug_{counter}.jpg

Args:

mask_path: single mask file path.
quantity: quantity for your image to augment
resize:(bool : optional)-> defult False ... resize your augmented images
width:(int : optional) width for resized ... if resize True you should use this arg
height:(int : optional) height for resized... if resize True you should use this arg

Returns:

No return
auto_augmentation(self, count_of_each)

auto augmentation for each picture depend on statistic of the object exist in your dataset if your image and mask names are same save your aug image in your dataset path with the following pattern aug_{counter}.jpg

Args:

count_of_each(int): How much of each label you want to have !

Returns:

No return
encode_mask_dataset(self, class_color)

encode color map dataset masks to 1 channel mask used for most semantic segmentation models save your encoded mask in your YOUR_MASK_PATH/pre_encoded

Args:

class_color(int):   [(r,g,b),(r,g,b),(r,g,b),...]

Returns:

No return