OOD dataset
OODDataset
¶
Bases: object
Class for managing loading and processing of datasets that are to be used for OOD detection. The class encapsulates a dataset like object augmented with OOD related information, and then returns a dataset like object that is suited for scoring or training with the .prepare method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_id |
Union[DatasetType, tuple, dict, str]
|
The dataset to load. Can be loaded from tensorflow or torch datasets catalog when the str matches one of the datasets. Defaults to Union[DatasetType, tuple, dict, str]. |
required |
backend |
str
|
Whether the dataset is to be used for tensorflow or torch models. Defaults to "tensorflow". Alternative: "torch". |
'tensorflow'
|
keys |
list
|
keys to use for dataset elems. Default to None |
None
|
load_kwargs |
dict
|
Additional loading kwargs when loading from tensorflow_datasets catalog. Defaults to {}. |
{}
|
load_from_tensorflow_datasets |
bool
|
In the case where if the backend is torch but the user still wants to import from tensorflow_datasets catalog. In that case, tf.Tensor will not be loaded in VRAM and converted as torch.Tensors on the fly. Defaults to False. |
False
|
input_key |
str
|
The key of the element/item to consider as the model input tensor. If None, taken as the first key. Defaults to None. |
None
|
Source code in oodeel/datasets/ooddataset.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
has_ood_label: bool
property
¶
Check if the dataset has an out-of-distribution label.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if data handler has a "ood_label" feature key. |
__init__(dataset_id, backend='tensorflow', keys=None, load_kwargs={}, load_from_tensorflow_datasets=False, input_key=None)
¶
Source code in oodeel/datasets/ooddataset.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
__len__()
¶
get the length of the dataset.
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
length of the dataset |
Source code in oodeel/datasets/ooddataset.py
109 110 111 112 113 114 115 116 117 |
|
add_out_data(out_dataset, in_value=0, out_value=1, resize=False, shape=None)
¶
Concatenate two OODDatasets. Useful for scoring on multiple datasets, or training with added out-of-distribution data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
out_dataset |
Union[OODDataset, DatasetType]
|
dataset of out-of-distribution data |
required |
in_value |
int
|
ood label value for in-distribution data. Defaults to 0 |
0
|
out_value |
int
|
ood label value for out-of-distribution data. Defaults to 1 |
1
|
resize |
Optional[bool]
|
toggles if input tensors of the datasets have to be resized to have the same shape. Defaults to False. |
False
|
shape |
Optional[Tuple[int]]
|
shape to use for resizing input tensors. If None, the tensors are resized with the shape of the in_dataset input tensors. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
OODDataset |
OODDataset
|
a Dataset object with the concatenated data |
Source code in oodeel/datasets/ooddataset.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
get_ood_labels()
¶
Get ood_labels from self.data if any
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: array of labels |
Source code in oodeel/datasets/ooddataset.py
128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
prepare(batch_size=128, preprocess_fn=None, augment_fn=None, with_ood_labels=False, with_labels=True, shuffle=False, shuffle_buffer_size=None)
¶
Prepare self.data for scoring or training
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size |
int
|
Batch_size of the returned dataset like object. Defaults to 128. |
128
|
preprocess_fn |
Callable
|
Preprocessing function to apply to the dataset. Defaults to None. |
None
|
augment_fn |
Callable
|
Augment function to be used (when the returned dataset is to be used for training). Defaults to None. |
None
|
with_ood_labels |
bool
|
To return the dataset with ood_labels or not. Defaults to True. |
False
|
with_labels |
bool
|
To return the dataset with labels or not. Defaults to True. |
True
|
shuffle |
bool
|
To shuffle the returned dataset or not. Defaults to False. |
False
|
shuffle_buffer_size |
int
|
(TF only) Size of the shuffle buffer. If None, taken as the number of samples in the dataset. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
DatasetType |
DatasetType
|
prepared dataset |
Source code in oodeel/datasets/ooddataset.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
|
split_by_class(in_labels=None, out_labels=None)
¶
Filter the dataset by assigning ood labels depending on labels value (typically, class id).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
in_labels |
Optional[Union[ndarray, list]]
|
set of labels to be considered as in-distribution. Defaults to None. |
None
|
out_labels |
Optional[Union[ndarray, list]]
|
set of labels to be considered as out-of-distribution. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Optional[Tuple[OODDataset]]
|
Optional[Tuple[OODDataset]]: Tuple of in-distribution and out-of-distribution OODDatasets |
Source code in oodeel/datasets/ooddataset.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
|