ocl.preprocessing
Data preprocessing functions.
DropEntries
Drop entries from data dictionary.
Source code in ocl/preprocessing.py
CheckFormat
Check format of data.
Source code in ocl/preprocessing.py
__init__
Initialize CheckFormat.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
shape |
List[int]
|
Shape of input tensor. |
required |
one_hot |
bool
|
Check if input tensor is one hot. |
False
|
class_dim |
int
|
Axis along which tensor should be one hot. |
0
|
Source code in ocl/preprocessing.py
CompressMask
Compress masks using a binary encoding format.
This works for up to 64 objects.
Source code in ocl/preprocessing.py
CompressedMaskToTensor
Decompress a mask compressed with CompressMask.
Source code in ocl/preprocessing.py
MaskToTensor
Convert a segmentation mask numpy array to a tensor.
Source code in ocl/preprocessing.py
__call__
Apply transformation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
numpy.ndarray
|
Mask tensor of shape (..., K, H, W, 1), i.e. one-hot encoded with K classes and any number of leading dimensions. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
Tensor of shape (..., K, H, W), containing binary entries. |
Source code in ocl/preprocessing.py
DenseMaskToTensor
Convert a dense segmentation mask numpy array to a tensor.
Mask is assumed to be of shape (..., K, H, W, 1), i.e. densely encoded with K classes and any number of leading dimensions. Returned tensor is of shape (..., K, H, W).
Source code in ocl/preprocessing.py
MultiMaskToTensor
Discretize mask, where multiple objects are partially masked into an exclusive binary mask.
Source code in ocl/preprocessing.py
IntegerToOneHotMask
Convert an integer mask to a one-hot mask.
Integer masks are masks where the instance ID is written into the mask. This transform expands them to a one-hot encoding.
Source code in ocl/preprocessing.py
__init__
Initialize IntegerToOneHotMask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ignore_typical_background |
bool
|
Ignore pixels where the mask is zero or 255. This often corresponds to the background or to the segmentation boundary. |
True
|
output_axis |
int
|
Axis along which the output should be one hot. |
-4
|
max_instances |
Optional[int]
|
The maximum number of instances. |
None
|
Source code in ocl/preprocessing.py
VOCInstanceMasksToDenseMasks
Convert a segmentation mask with integer encoding into a one-hot segmentation mask.
We use this transform as Pascal VOC segmentatation and object annotations seems to not be aligned.
Source code in ocl/preprocessing.py
AddImageSize
Add height and width of image as data entry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Key of image. |
'image'
|
target_key |
str
|
Key under which to store size. |
'image_size'
|
Source code in ocl/preprocessing.py
AddEmptyMasks
Add empty masks to data if the data does not include them already.
Source code in ocl/preprocessing.py
__init__
Initialize AddEmptyMasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask_keys |
Union[str, Sequence[str]]
|
One or several keys of empty masks to be added. |
required |
take_size_from |
str
|
Key of element whose height and width is used to create mask. Element is assumed to have shape of (H, W, C). |
'image'
|
Source code in ocl/preprocessing.py
AddEmptyBboxes
Add empty bounding boxes to data if the data does not include them already.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys |
Union[str, Sequence[str]]
|
One or several keys of empty boxes to be added. |
'instance_bbox'
|
empty_value |
float
|
Value of the empty box at all coordinates. |
-1.0
|
Source code in ocl/preprocessing.py
CanonicalizeBboxes
Convert bounding boxes to canonical (x1, y1, x2, y2) format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Key of bounding box, assumed to have shape K x 4. |
'instance_bbox'
|
format |
str
|
Format of bounding boxes. Either "xywh" or "yxyx". |
'xywh'
|
Source code in ocl/preprocessing.py
RescaleBboxes
Rescale bounding boxes by size taken from data.
Bounding boxes are assumed to have format (x1, y1, x2, y2). The rescaled box is (x1 * width, y1 * height, x2 * width, y2 * height).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Key of bounding box, assumed to have shape K x 4. |
'instance_bbox'
|
take_size_from |
str
|
Key of element to take the size for rescaling from, assumed to have shape H x W x C. |
'image'
|
Source code in ocl/preprocessing.py
AddSegmentationMaskFromInstanceMask
Convert instance to segmentation masks by joining instances with the same category.
Overlaps of instances of different classes are resolved by taking the class with the higher class id.
Source code in ocl/preprocessing.py
convert
staticmethod
Convert instance to segmentation mask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance_mask |
numpy.ndarray
|
Densely encoded instance masks of shape I x H x W x 1, where I is the number of instances. |
required |
Source code in ocl/preprocessing.py
AddBBoxFromInstanceMasks
Convert instance mask to bounding box.
Source code in ocl/preprocessing.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
|
__init__
Initialize AddBBoxFromInstanceMasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance_mask_key |
str
|
mask key name. |
'mask'
|
video_id_key |
str
|
Field from which to derive the instance key. |
'__key__'
|
target_box_key |
str
|
Output field for bounding box derived from instance mask. |
'instance_bbox'
|
target_cls_key |
str
|
Output field for class derived from instance mask. |
'instance_cls'
|
target_id_key |
str
|
Output field for instance id. |
'instance_id'
|
Source code in ocl/preprocessing.py
InstanceMasksToDenseMasks
Convert binary instance masks to dense masks, i.e. where the mask value encodes the class id.
Class ids are taken from a list containing a class id per instance.
Source code in ocl/preprocessing.py
MergeCocoThingsAndStuff
Merge COCO things and stuff segmentation masks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
things_key |
str
|
Key to things instance mask. Mask is assumed to be densely encoded, i.e. the mask value encodes the class id, of shape I x H x W x 1, where I is the number of things instances. |
'instance_mask'
|
stuff_key |
str
|
Key to stuff segmentation mask. Mask is assumed to be densely encoded, i.e. the mask value encodes the class id, of shape K x H x W x 1, where K is the number stuff classes. |
'stuff_mask'
|
output_key |
str
|
Key under which the merged mask is stored. Returns mask of shape L x H x W x 1, where K <= L <= K + I. |
required |
include_crowd |
bool
|
Whether to include pixels marked as crowd with their class, or with class zero. |
False
|
Source code in ocl/preprocessing.py
FlowToTensor
Convert an optical flow numpy array to a tensor.
Flow is assumed to be of shape (..., H, W, 2), returned tensor is of shape (..., 2, H, W) to match with VideoTensor format.
Source code in ocl/preprocessing.py
ConvertCocoStuff164kMasks
Convert COCO-Stuff-164k PNG segmentation masks to our format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_key |
str
|
Key under which the output mask is stored. Returns uint8 mask of shape K x H x W x 1, where K is the number of classes in the image. Mask is densely encoded, i.e. the mask values encode the class id. |
required |
stuffthings_key |
str
|
Key to COCO-Stuff-164k PNG mask. Mask has shape H x W x 3. |
'stuffthings_mask'
|
ignore_key |
str
|
Key under which the ignore mask is stored. Returns bool mask of shape 1 x H x W x 1. Ignores pixels where PNG mask has value 255 (crowd). |
'ignore_mask'
|
drop_stuff |
bool
|
If true, remove all stuff classes (id >= 92), keeping only thing classes. |
False
|
Source code in ocl/preprocessing.py
VideoToTensor
Convert a video numpy array of shape (T, H, W, C) to a torch tensor of shape (T, C, H, W).
Source code in ocl/preprocessing.py
__call__
Convert a numpy array of a video into a torch tensor.
Assumes input is a numpy array of shape T x H x W x C (or T x H x W for monochrome videos) and convert it into torch tensor of shape T x C x H x W in order to allow application of Conv3D operations.
Source code in ocl/preprocessing.py
ToSingleFrameVideo
Convert image in tensor format to video format by adding frame dimension with single element.
Converts C x H x W tensors into tensors of shape 1 x C x H x W.
Source code in ocl/preprocessing.py
NormalizeVideo
Normalize a video tensor of shape (T, C, H, W).
Source code in ocl/preprocessing.py
Denormalize
Denormalize a tensor of shape (..., C, H, W) with any number of leading dimensions.
Source code in ocl/preprocessing.py
ResizeNearestExact
Resize a tensor using mode nearest-exact.
This mode is not available in torchvision.transforms.Resize as of v0.12. This class was adapted from torchvision.transforms.functional_tensor.resize.
Source code in ocl/preprocessing.py
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 |
|
ConvertToCocoSuperclasses
Convert segmentation mask from COCO classes (183) to COCO superclasses (27).
Source code in ocl/preprocessing.py
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
|
__call__
Convert mask to superclasses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
numpy.ndarray
|
Densely encoded segmentation mask of shape K x H x W x 1. |
required |
Returns:
Type | Description |
---|---|
numpy.ndarray
|
Segmentation mask of shape C x H x W x 1, where C is the new set of classes. |
Source code in ocl/preprocessing.py
OrigCenterCrop
Returns center crop at original image resolution.
Source code in ocl/preprocessing.py
JointRandomResizedCropwithParameters
Bases: transforms.RandomResizedCrop
Source code in ocl/preprocessing.py
forward
Returns parameters of the resize in addition to the crop.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img |
PIL Image or Tensor
|
Image to be cropped and resized. |
required |
Returns:
Type | Description |
---|---|
torch.Tensor
|
PIL Image or Tensor: Randomly cropped and resized image. |
Source code in ocl/preprocessing.py
RandomSample
SampleFramesUsingIndices
Sample frames form a tensor dependent on indices provided in the instance.
Source code in ocl/preprocessing.py
MaskInstances
Filter instances by masking non matching with NaN.
Source code in ocl/preprocessing.py
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 |
|
expand_dense_mask
Convert dense segmentation mask to one where each class occupies one dimension.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mask |
numpy.ndarray
|
Densely encoded segmentation mask of shape 1 x H x W x 1. |
required |
Densely encoded segmentation mask of shape K x H x W x 1, where K is the
Type | Description |
---|---|
numpy.ndarray
|
number of classes in the mask. Zero is taken to indicate an unoccupied pixel. |